from openai import OpenAI
import json
client = OpenAI(base_url="https://kymaapi.com/v1", api_key="ky-your-key")
tools = [
{
"type": "function",
"function": {
"name": "search_docs",
"description": "Search documentation for a query",
"parameters": {
"type": "object",
"properties": {"query": {"type": "string"}},
"required": ["query"]
}
}
},
{
"type": "function",
"function": {
"name": "run_code",
"description": "Execute Python code and return output",
"parameters": {
"type": "object",
"properties": {"code": {"type": "string"}},
"required": ["code"]
}
}
}
]
def handle_tool_call(name, args):
if name == "search_docs":
return f"Found: documentation about {args['query']}"
if name == "run_code":
return f"Output: executed successfully"
return "Unknown tool"
def run_agent(user_message, max_steps=5):
messages = [
{"role": "system", "content": "You are a helpful agent. Use tools to answer questions."},
{"role": "user", "content": user_message}
]
for _ in range(max_steps):
response = client.chat.completions.create(
model="kimi-k2.5",
messages=messages,
tools=tools,
)
choice = response.choices[0]
messages.append(choice.message)
if choice.finish_reason == "stop":
return choice.message.content
if choice.message.tool_calls:
for call in choice.message.tool_calls:
args = json.loads(call.function.arguments)
result = handle_tool_call(call.function.name, args)
messages.append({
"role": "tool",
"tool_call_id": call.id,
"content": result
})
return messages[-1].content
print(run_agent("Search docs for authentication and write example code"))