import cycls
import json
import os
from openai import OpenAI
agent = cycls.Agent(
pip=["openai", "python-dotenv"],
copy=[".env"]
)
# 1. Define the tool logic
def get_weather(location):
return json.dumps({"temp": "24", "unit": "celsius"})
@agent("tools-agent", title="Tools Agent")
async def tool_agent(context):
from dotenv import load_dotenv
load_dotenv()
client = OpenAI()
# 2. Define the tool schema
tools = [{
"type": "function",
"name": "get_weather",
"description": "Get current temperature",
"parameters": {
"type": "object",
"properties": {
"location": {"type": "string"}
},
"required": ["location"]
}
}]
# 3. First Call
response = client.responses.create(
model="gpt-4o",
input=context.messages,
tools=tools
)
# Update history
context.messages.extend(response.output)
# 4. Handle Tool Execution
tool_called = False
for item in response.output:
if item.type == "function_call":
tool_called = True
if item.name == "get_weather":
args = json.loads(item.arguments)
result = get_weather(args["location"])
context.messages.append({
"type": "function_call_output",
"call_id": item.call_id,
"output": result
})
# Final Call
if tool_called:
final = client.responses.create(
model="gpt-4o",
input=context.messages
)
yield final.output_text
else:
yield response.output_text
agent.deploy(prod=False)