# 用 OpenAI Agents SDK 重写第二章的计算 Agent，模型换成本机 Ollama（或任何 OpenAI 兼容接口）。
import os
from agents import Agent, Runner, AsyncOpenAI, OpenAIChatCompletionsModel, set_tracing_disabled
from agents.decorators import tool

set_tracing_disabled(disabled=True)            # 没有 OpenAI 平台的 key 时要关掉 tracing
client = AsyncOpenAI(base_url=os.environ.get("AGENT_BASE_URL", "http://localhost:11434/v1"),
                     api_key=os.environ.get("AGENT_API_KEY", "ollama"))
model = OpenAIChatCompletionsModel(model=os.environ.get("AGENT_MODEL", "qwen3:8b"), openai_client=client)

@tool
def add(a: float, b: float) -> float:
    """计算两个数的和"""
    return a + b

@tool
def multiply(a: float, b: float) -> float:
    """计算两个数的积"""
    return a * b

agent = Agent(name="计算助手", instructions="用工具做计算，最后用中文回答。", tools=[add, multiply], model=model)
result = Runner.run_sync(agent, "一箱苹果 24 个，我买了 17 箱，又多拿了 9 个。一共多少个？请用工具计算。")
print(result.final_output)
