补充:qwen3-coder-plus 的免费额度很快就用完了(一上午的时间),背刺了我 50 元人民币。如果使用 deepseek reasoner,根本不需要这么多。因此我建议大家还是使用 deepseek API。
我也是服啦。
2025-07-31
有结构的 agent 框架要比没有的好用。为了测试 qwen3-coder-plus 是否能够良好的与 pydantic-ai 结合,我编写了下列代码。
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
| """测试 qwen api 在 openai sdk 下,能否与 pydantic 进行协作"""
import os
from openai import AsyncOpenAI
from pydantic_ai import Agent, RunContext
from pydantic_ai.models.openai import OpenAIModel
from pydantic_ai.providers.openai import OpenAIProvider
def get_agent():
"""get custom client"""
client = AsyncOpenAI(max_retries=3, base_url=os.getenv("OPENAPI_BASE_URL"), api_key=os.getenv("OPENAI_API_KEY"))
model = OpenAIModel("qwen3-coder-plus", provider=OpenAIProvider(openai_client=client))
agent = Agent(
model,
deps_type=int,
output_type=bool,
system_prompt=(
"Use the `roulette_wheel` function to see if the customer has won based on the number they provide."
),
)
return agent
agent = get_agent()
@agent.tool
async def roulette_wheel(ctx: RunContext[int], square: int) -> str:
"""check if the square is a winner"""
return "winner" if square == ctx.deps else "loser"
def main():
success_number = 18
result = agent.run_sync("Put my money on square eighteen", deps=success_number)
print(result.output)
if __name__ == "__main__":
main()
|
执行:
1
| uv run test_qwen_agent.py
|
输出结果:
1
2
| -> % uv run ./tests/test_qwen_agent.py
True
|
这证明 pydantic-ai 可以和 qwen3-coder-plus 良好协作。
又打通了一环!