// tools.py
def calculator(expression):
return eval(expression)
def get_weather(city):
weather = {
"台南": 32,
"台北": 29,
"高雄": 33,
}
return weather.get(city, "unknown")
// agent.py
def run_agent(user_input):
state = [
{
"role": "user",
"content": user_input
}
]
while True:
response = fake_llm(state)
if response["type"] == "final":
return response["content"]
if response["type"] == "tool_call":
tool_name = response["tool"]
arguments = response["arguments"]
tool = tools[tool_name]
result = tool(**arguments)
state.append({
"role": "assistant",
"content": response
})
state.append({
"role": "tool",
"content": result
})