# pip install openai
# Function calling: the model asks to call get_weather, you run it and send the result back.
import json
import os

from openai import OpenAI

client = OpenAI(base_url="https://api.klinch.cloud/v1", api_key=os.environ["KLINCH_API_KEY"])
tools = [{
    "type": "function",
    "function": {
        "name": "get_weather",
        "description": "Current weather in a city",
        "parameters": {"type": "object", "properties": {"city": {"type": "string"}}, "required": ["city"]},
    },
}]
messages = [{"role": "user", "content": "What is the weather in Almaty?"}]

r = client.chat.completions.create(model="claude-sonnet-5-5", messages=messages, tools=tools)
call = r.choices[0].message.tool_calls[0]
messages.append(r.choices[0].message)
messages.append({"role": "tool", "tool_call_id": call.id, "content": json.dumps({"city": json.loads(call.function.arguments)["city"], "temp_c": 4})})

r = client.chat.completions.create(model="claude-sonnet-5-5", messages=messages, tools=tools)
print(r.choices[0].message.content)
