# 第一步：让模型“调用”一个函数。模型自己不执行任何东西，它只回一段 JSON：想调哪个函数、传什么参数。
# 用任何 OpenAI 兼容的接口都行：本地 Ollama、DeepSeek、通义千问、智谱……改下面三个环境变量即可。
import json, os
from openai import OpenAI

client = OpenAI(base_url=os.environ.get("AGENT_BASE_URL", "http://localhost:11434/v1"),
                api_key=os.environ.get("AGENT_API_KEY", "ollama"))
MODEL = os.environ.get("AGENT_MODEL", "qwen3:8b")

tools = [{
    "type": "function",
    "function": {
        "name": "add",
        "description": "计算两个数的和",
        "parameters": {
            "type": "object",
            "properties": {"a": {"type": "number"}, "b": {"type": "number"}},
            "required": ["a", "b"],
        },
    },
}]

resp = client.chat.completions.create(
    model=MODEL,
    messages=[{"role": "user", "content": "3829 加 9471 等于多少？"}],
    tools=tools,
)
msg = resp.choices[0].message
print("finish_reason:", resp.choices[0].finish_reason)
print("content:", msg.content)
for call in msg.tool_calls or []:
    print("tool_call:", call.function.name, call.function.arguments)
