|
8 | 8 | from agents import Agent, Runner, gen_trace_id, trace
|
9 | 9 | from agents.mcp import MCPServer, MCPServerSse
|
10 | 10 | from agents.model_settings import ModelSettings
|
| 11 | +from examples.models import get_agent_chat_model |
11 | 12 |
|
12 | 13 |
|
13 | 14 | async def run(mcp_server: MCPServer):
|
| 15 | + """运行代理示例""" |
| 16 | + deepseek = get_agent_chat_model("deepseek-v3") |
14 | 17 | agent = Agent(
|
15 |
| - name="Assistant", |
16 |
| - instructions="Use the tools to answer the questions.", |
| 18 | + name="助手", |
| 19 | + instructions="使用工具回答问题", |
17 | 20 | mcp_servers=[mcp_server],
|
18 | 21 | model_settings=ModelSettings(tool_choice="required"),
|
| 22 | + model=deepseek, |
19 | 23 | )
|
20 | 24 |
|
21 |
| - # Use the `add` tool to add two numbers |
22 |
| - message = "Add these numbers: 7 and 22." |
23 |
| - print(f"Running: {message}") |
| 25 | + # 使用`add`工具相加两个数字 |
| 26 | + message = "计算这两个数的和:7和22" |
| 27 | + print(f"执行: {message}") |
24 | 28 | result = await Runner.run(starting_agent=agent, input=message)
|
25 | 29 | print(result.final_output)
|
26 | 30 |
|
27 |
| - # Run the `get_weather` tool |
28 |
| - message = "What's the weather in Tokyo?" |
29 |
| - print(f"\n\nRunning: {message}") |
| 31 | + # 运行`get_weather`工具 |
| 32 | + message = "东京的天气如何?" |
| 33 | + print(f"\n\n执行: {message}") |
30 | 34 | result = await Runner.run(starting_agent=agent, input=message)
|
31 | 35 | print(result.final_output)
|
32 | 36 |
|
33 |
| - # Run the `get_secret_word` tool |
34 |
| - message = "What's the secret word?" |
35 |
| - print(f"\n\nRunning: {message}") |
| 37 | + # 运行`get_secret_word`工具 |
| 38 | + message = "秘密单词是什么?" |
| 39 | + print(f"\n\n执行: {message}") |
36 | 40 | result = await Runner.run(starting_agent=agent, input=message)
|
37 | 41 | print(result.final_output)
|
38 | 42 |
|
39 | 43 |
|
40 | 44 | async def main():
|
| 45 | + """主函数""" |
41 | 46 | async with MCPServerSse(
|
42 |
| - name="SSE Python Server", |
| 47 | + name="SSE Python服务器", |
43 | 48 | params={
|
44 | 49 | "url": "http://localhost:8000/sse",
|
45 | 50 | },
|
46 | 51 | ) as server:
|
47 | 52 | trace_id = gen_trace_id()
|
48 |
| - with trace(workflow_name="SSE Example", trace_id=trace_id): |
49 |
| - print(f"View trace: https://platform.openai.com/traces/trace?trace_id={trace_id}\n") |
| 53 | + with trace(workflow_name="SSE示例", trace_id=trace_id): |
| 54 | + print(f"查看追踪: https://platform.openai.com/traces/trace?trace_id={trace_id}\n") |
50 | 55 | await run(server)
|
51 | 56 |
|
52 | 57 |
|
53 | 58 | if __name__ == "__main__":
|
54 |
| - # Let's make sure the user has uv installed |
| 59 | + # 检查用户是否安装了uv |
55 | 60 | if not shutil.which("uv"):
|
56 | 61 | raise RuntimeError(
|
57 |
| - "uv is not installed. Please install it: https://docs.astral.sh/uv/getting-started/installation/" |
| 62 | + "未安装uv。请安装: https://docs.astral.sh/uv/getting-started/installation/" |
58 | 63 | )
|
59 | 64 |
|
60 |
| - # We'll run the SSE server in a subprocess. Usually this would be a remote server, but for this |
61 |
| - # demo, we'll run it locally at http://localhost:8000/sse |
| 65 | + # 在子进程中运行SSE服务器。通常这会是一个远程服务器, |
| 66 | + # 但在这个演示中,我们将在本地运行它:http://localhost:8000/sse |
62 | 67 | process: subprocess.Popen[Any] | None = None
|
63 | 68 | try:
|
64 | 69 | this_dir = os.path.dirname(os.path.abspath(__file__))
|
65 | 70 | server_file = os.path.join(this_dir, "server.py")
|
66 | 71 |
|
67 |
| - print("Starting SSE server at http://localhost:8000/sse ...") |
| 72 | + print("正在启动SSE服务器: http://localhost:8000/sse ...") |
68 | 73 |
|
69 |
| - # Run `uv run server.py` to start the SSE server |
| 74 | + # 运行`uv run server.py`启动SSE服务器 |
70 | 75 | process = subprocess.Popen(["uv", "run", server_file])
|
71 |
| - # Give it 3 seconds to start |
| 76 | + # 等待3秒让它启动 |
72 | 77 | time.sleep(3)
|
73 | 78 |
|
74 |
| - print("SSE server started. Running example...\n\n") |
| 79 | + print("SSE服务器已启动。正在运行示例...\n\n") |
75 | 80 | except Exception as e:
|
76 |
| - print(f"Error starting SSE server: {e}") |
| 81 | + print(f"启动SSE服务器时出错: {e}") |
77 | 82 | exit(1)
|
78 | 83 |
|
79 | 84 | try:
|
|
0 commit comments