Spaces:
Sleeping
Sleeping
Upload 3 files
Browse files
examples/cmd/agent_deploy/playwright_aworld_agent/agent.py
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import logging
|
| 2 |
+
import os
|
| 3 |
+
import json
|
| 4 |
+
from typing import AsyncGenerator
|
| 5 |
+
|
| 6 |
+
from aworld.cmd import BaseAWorldAgent, ChatCompletionRequest
|
| 7 |
+
from aworld.config.conf import AgentConfig, TaskConfig
|
| 8 |
+
from aworld.agents.llm_agent import Agent
|
| 9 |
+
from aworld.core.task import Task
|
| 10 |
+
from aworld.output.ui.base import AworldUI
|
| 11 |
+
from aworld.output.ui.markdown_aworld_ui import MarkdownAworldUI
|
| 12 |
+
from aworld.runner import Runners
|
| 13 |
+
|
| 14 |
+
logger = logging.getLogger(__name__)
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
class AWorldAgent(BaseAWorldAgent):
|
| 18 |
+
def __init__(self, *args, **kwargs):
|
| 19 |
+
super().__init__(*args, **kwargs)
|
| 20 |
+
|
| 21 |
+
def name(self):
|
| 22 |
+
return "Powerful playwright agent"
|
| 23 |
+
|
| 24 |
+
def description(self):
|
| 25 |
+
return "Powerful playwright agent docs"
|
| 26 |
+
|
| 27 |
+
async def run(self, prompt: str = None, request: ChatCompletionRequest = None):
|
| 28 |
+
llm_provider = os.getenv("LLM_PROVIDER_PLAYWRIGHT", "openai")
|
| 29 |
+
llm_model_name = os.getenv("LLM_MODEL_NAME_PLAYWRIGHT")
|
| 30 |
+
llm_api_key = os.getenv("LLM_API_KEY_PLAYWRIGHT")
|
| 31 |
+
llm_base_url = os.getenv("LLM_BASE_URL_PLAYWRIGHT")
|
| 32 |
+
llm_temperature = os.getenv("LLM_TEMPERATURE_WEATHER", 0.0)
|
| 33 |
+
|
| 34 |
+
if not llm_model_name or not llm_api_key or not llm_base_url:
|
| 35 |
+
raise ValueError(
|
| 36 |
+
"LLM_MODEL_NAME, LLM_API_KEY, LLM_BASE_URL must be set in your envrionment variables"
|
| 37 |
+
)
|
| 38 |
+
|
| 39 |
+
agent_config = AgentConfig(
|
| 40 |
+
llm_provider=llm_provider,
|
| 41 |
+
llm_model_name=llm_model_name,
|
| 42 |
+
llm_api_key=llm_api_key,
|
| 43 |
+
llm_base_url=llm_base_url,
|
| 44 |
+
llm_temperature=llm_temperature,
|
| 45 |
+
)
|
| 46 |
+
|
| 47 |
+
path_cwd = os.path.dirname(os.path.abspath(__file__))
|
| 48 |
+
mcp_path = os.path.join(path_cwd, "mcp.json")
|
| 49 |
+
with open(mcp_path, "r") as f:
|
| 50 |
+
mcp_config = json.load(f)
|
| 51 |
+
|
| 52 |
+
super_agent = Agent(
|
| 53 |
+
conf=agent_config,
|
| 54 |
+
name="powerful_playwright_agent",
|
| 55 |
+
system_prompt="You are a powerful weather agent, you can use playwright to do anything you want",
|
| 56 |
+
mcp_config=mcp_config,
|
| 57 |
+
mcp_servers=mcp_config.get("mcpServers", {}).keys(),
|
| 58 |
+
)
|
| 59 |
+
|
| 60 |
+
if prompt is None and request is not None:
|
| 61 |
+
prompt = request.messages[-1].content
|
| 62 |
+
|
| 63 |
+
task = Task(
|
| 64 |
+
input=prompt,
|
| 65 |
+
agent=super_agent,
|
| 66 |
+
conf=TaskConfig(max_steps=20),
|
| 67 |
+
)
|
| 68 |
+
|
| 69 |
+
rich_ui = MarkdownAworldUI()
|
| 70 |
+
async for output in Runners.streamed_run_task(task).stream_events():
|
| 71 |
+
logger.info(f"Agent Output: {output}")
|
| 72 |
+
res = await AworldUI.parse_output(output, rich_ui)
|
| 73 |
+
for item in res if isinstance(res, list) else [res]:
|
| 74 |
+
if isinstance(item, AsyncGenerator):
|
| 75 |
+
async for sub_item in item:
|
| 76 |
+
yield sub_item
|
| 77 |
+
else:
|
| 78 |
+
yield item
|
examples/cmd/agent_deploy/playwright_aworld_agent/mcp.json
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"mcpServers": {
|
| 3 |
+
"aworld-playwright": {
|
| 4 |
+
"command": "npx",
|
| 5 |
+
"args": [
|
| 6 |
+
"playwright-mcp-aworld",
|
| 7 |
+
"--isolated"
|
| 8 |
+
],
|
| 9 |
+
"env": {
|
| 10 |
+
"OSS_ENDPOINT": "${OSS_ENDPOINT}",
|
| 11 |
+
"OSS_ACCESS_KEY_ID": "${OSS_ACCESS_KEY_ID}",
|
| 12 |
+
"OSS_ACCESS_KEY_SECRET": "${OSS_ACCESS_KEY_SECRET}",
|
| 13 |
+
"OSS_BUCKET": "${OSS_BUCKET}",
|
| 14 |
+
"PLAYWRIGHT_TIMEOUT": "120000",
|
| 15 |
+
"SESSION_REQUEST_CONNECT_TIMEOUT": "120"
|
| 16 |
+
}
|
| 17 |
+
}
|
| 18 |
+
}
|
| 19 |
+
}
|
examples/cmd/agent_deploy/playwright_aworld_agent/requirements.txt
ADDED
|
File without changes
|