import json
from openai import OpenAI
client = OpenAI(base_url="http://localhost:8000/v1", api_key="EMPTY")
tools = [
{
"type": "function",
"function": {
"name": "get_stock_price",
"description": "获取指定股票代码的当前股价",
"parameters": {
"type": "object",
"required": ["ticker"],
"properties": {
"ticker": {"type": "string", "description": "股票代码(例如:AAPL)"}
}
}
}
},
{
"type": "function",
"function": {
"name": "calculate_portfolio_value",
"description": "根据持仓计算投资组合总价值",
"parameters": {
"type": "object",
"required": ["holdings"],
"properties": {
"holdings": {
"type": "array",
"items": {
"type": "object",
"properties": {
"ticker": {"type": "string"},
"shares": {"type": "number"}
}
}
}
}
}
}
}
]
response = client.chat.completions.create(
model="mistralai/Mistral-Small-3.1-24B-Instruct-2503",
messages=[{"role": "user", "content": "当前 AAPL 和 MSFT 的价格是多少?"}],
tools=tools,
tool_choice="auto",
temperature=0.15
)
for tool_call in response.choices[0].message.tool_calls:
print(f"Call: {tool_call.function.name}({tool_call.function.arguments})")