Integration Patterns
AI Agent Loop
Build a simple ReAct-style agent that calls tools and feeds results back into the conversation until a final answer is reached.
Python
python
import requests, json
BASE_URL = "https://api.oneinfer.ai"
token = requests.post(
f"{BASE_URL}/v1/ula/oauth-authentication?api_key=YOUR_API_KEY"
).json()["access_token"]
headers = {"Authorization": f"Bearer {token}", "Content-Type": "application/json"}
TOOLS = {
"get_weather": lambda city: f"The weather in {city} is 22°C and sunny.",
"get_time": lambda tz: f"The current time in {tz} is 14:32.",
}
SYSTEM = """You are an assistant that can use tools.
When you need a tool, output JSON: {"tool": "<name>", "args": {"<key>": "<val>"}}
When you have a final answer, output: {"answer": "<text>"}"""
messages = [{"role": "system", "content": SYSTEM}]
def agent_step(user_input: str) -> str:
messages.append({"role": "user", "content": user_input})
for _ in range(5): # max iterations
resp = requests.post(
f"{BASE_URL}/v1/ula/chat/completions",
headers=headers,
json={"provider": "openai", "model": "gpt-4o-mini", "messages": messages, "max_tokens": 256},
).json()["data"]["text"]
messages.append({"role": "assistant", "content": resp})
try:
parsed = json.loads(resp)
except json.JSONDecodeError:
return resp
if "answer" in parsed:
return parsed["answer"]
if "tool" in parsed:
fn = TOOLS.get(parsed["tool"])
result = fn(**parsed["args"]) if fn else "Tool not found."
messages.append({"role": "user", "content": f"Tool result: {result}"})
return "Max iterations reached."
print(agent_step("What's the weather in London and what time is it in UTC?"))