import json
from openai import OpenAI

client = OpenAI(api_key="sk-tMsrBRocOuY3j8UCFcCb1cF74f294f369f0a26Bf1e5b7f6a", base_url="https://api.apiyi.com/v1")

tools = [{"type": "function", "function": {
    "name": "get_weather",
    "description": "获取指定城市的天气",
    "parameters": {"type": "object", "properties": {
        "city": {"type": "string", "description": "城市名"}
    }, "required": ["city"]}
}}]

resp = client.chat.completions.create(
    model="gpt-4o-mini",
    messages=[{"role": "user", "content": "北京天气怎么样？"}],
    tools=tools,
)

msg = resp.choices[0].message
if msg.tool_calls:
    tc = msg.tool_calls[0]
    print(f"模型调用工具: {tc.function.name}({tc.function.arguments})")
else:
    print(f"模型直接回复: {msg.content}")
