import json
import time
from openai import OpenAI
client = OpenAI(
base_url="https://api.sailresearch.com/v1",
api_key="YOUR_SAIL_API_KEY",
)
MODEL = "moonshotai/Kimi-K2.5"
TOOLS = [
{
"type": "function",
"name": "get_weather",
"description": "Get the current weather for a location.",
"parameters": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "City and state, e.g. San Francisco, CA",
}
},
"required": ["location"],
"additionalProperties": False,
},
"strict": True,
},
]
TOOL_DISPATCH = {"get_weather": lambda location: '{"temperature": "62°F", "condition": "Foggy"}'}
def poll(response, timeout=300):
start = time.time()
while response.status not in ("completed", "failed", "cancelled"):
if time.time() - start > timeout:
raise TimeoutError(f"{response.id} did not complete within {timeout}s")
time.sleep(2)
response = client.responses.retrieve(response.id)
if response.status != "completed":
raise RuntimeError(f"{response.id} status: {response.status}")
return response
def agent_turn(conversation, user_message):
"""Send a user message and loop until the model stops calling tools."""
conversation.append({"role": "user", "content": user_message})
while True:
response = client.responses.create(
model=MODEL,
input=conversation,
tools=TOOLS,
max_output_tokens=4096,
background=True,
)
response = poll(response)
tool_calls = [
item for item in (response.output or [])
if getattr(item, "type", None) == "function_call"
]
conversation.extend(response.output)
if not tool_calls:
return response
for call in tool_calls:
args = json.loads(call.arguments)
output = TOOL_DISPATCH[call.name](**args)
conversation.append(
{"type": "function_call_output", "call_id": call.call_id, "output": output}
)
conversation = []
# Turn 1: triggers a get_weather tool call, then the model summarizes the result
response = agent_turn(conversation, "What's the weather in San Francisco?")
print("Turn 1:", response.output_text)
# Turn 2: follow-up reuses conversation context
response = agent_turn(conversation, "How about New York — warmer or colder?")
print("Turn 2:", response.output_text)