Spaces:
Paused
Paused
File size: 4,477 Bytes
0b9dc2e 69d14a0 0b9dc2e | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 | # -*- coding: utf-8 -*-
"""The example script to start the agent service."""
import os
import uvicorn
from fastapi.middleware import Middleware
from fastapi.middleware.cors import CORSMiddleware
from agentscope.app import create_app, SubAgentTemplate
from agentscope.app.message_bus import InMemoryMessageBus
from agentscope.app.rag.knowledge_base_manager import CollectionPerKbManager
from agentscope.app.storage import RedisStorage
from agentscope.app.workspace_manager import LocalWorkspaceManager
from agentscope.mcp import MCPClient, StdioMCPConfig, HttpMCPConfig
from agentscope.permission import PermissionContext, PermissionMode
from agentscope.rag import QdrantStore
default_mcps = [
MCPClient(
name="browser-use",
mcp_config=StdioMCPConfig(
command="npx",
args=["@playwright/mcp@latest"],
),
is_stateful=True,
),
]
if os.getenv("AMAP_API_KEY"):
default_mcps.append(
MCPClient(
name="amap",
mcp_config=HttpMCPConfig(
url=f"https://mcp.amap.com/mcp?key="
f"{os.environ['AMAP_API_KEY']}",
),
is_stateful=False,
),
)
storage = RedisStorage(
host=os.getenv("REDIS_HOST", "localhost"),
port=int(os.getenv("REDIS_PORT", "6379")),
password=os.getenv("REDIS_PASSWORD", None),
)
vector_store = QdrantStore(location=":memory:")
app = create_app(
storage=storage,
message_bus=InMemoryMessageBus(),
# -- To use a Redis-backed message bus instead (recommended for
# -- multi-process / production deployments), uncomment the lines
# -- below and replace the InMemoryMessageBus() above:
#
# from agentscope.app.message_bus import RedisMessageBus
# message_bus=RedisMessageBus(
# host="localhost",
# port=6379,
# ),
workspace_manager=LocalWorkspaceManager(
basedir=os.path.join(
os.path.dirname(os.path.abspath(__file__)),
"workspaces",
),
# The default MCP servers that will be added into the workspace
default_mcps=default_mcps,
),
# Knowledge base feature — backed by an in-memory Qdrant store. The
# CollectionPerKbManager allocates one collection per knowledge base,
# so any embedding dimension is allowed.
knowledge_base_manager=CollectionPerKbManager(
storage=storage,
vector_store=vector_store,
),
# Customize your own subagent templates
custom_subagent_templates=[
SubAgentTemplate(
type="explorer",
description=(
"Read-only agents specialized in exploration tasks. It can "
"read files but cannot modify, create, or delete them. Use "
"this agent type when you need to investigate the codebase, "
"understand its structure, or gather information from files "
"to support planning—without making any changes."
),
system_prompt_template="""You are {member_name}, an explorer \
agent in team '{team_name}' led by {leader_name}.
Team purpose: {team_description}
Your role: {member_description}
## Responsibilities
- Complete the exploration tasks assigned by the team leader.
- You are read-only: you may inspect files and the codebase, but you must \
never modify, create, or delete anything.
## Reporting
- Always report the task result back to {leader_name} using the TeamSay \
tool, whether the task succeeds or fails.
- Keep your private reasoning private; only share conclusions and findings \
that the leader needs.
Note: `TeamSay` is your ONLY channel to communicate with {leader_name} and \
the other team members. Any other output you produce is invisible to them, \
so anything you want them to see MUST be sent through `TeamSay`.""",
permission_context=PermissionContext(
# Read-only
mode=PermissionMode.EXPLORE,
),
),
],
extra_middlewares=[
Middleware(
CORSMiddleware,
allow_origins=["*"],
allow_methods=["*"],
allow_headers=["*"],
),
],
)
if __name__ == "__main__":
# Start the service
uvicorn.run(
"main:app",
host="0.0.0.0",
port=8000,
reload=True,
)
|