Skip to main content

Function Calling

from openai import OpenAI
import json

client = OpenAI(api_key="your-api-key", base_url="https://aisupermarket.work/v1")

tools = [
{
"type": "function",
"function": {
"name": "get_weather",
"description": "Get the weather for a specified city",
"parameters": {
"type": "object",
"properties": {
"city": {"type": "string", "description": "City name"}
},
"required": ["city"],
},
},
}
]

response = client.chat.completions.create(
model="gemini-2.5-flash",
messages=[{"role": "user", "content": "What is the weather in Shanghai today?"}],
tools=tools,
tool_choice="auto",
)

tool_call = response.choices[0].message.tool_calls[0]
args = json.loads(tool_call.function.arguments)
print(f"Function call: {tool_call.function.name}, args: {args}")