# /// script # requires-python = ">=3.10" # dependencies = [ # "arize-phoenix-otel>=0.17,<1", # "openinference-instrumentation-openai>=0.1.77", # "openai>=2", # ] # /// """A small tool-using agent, traced in Arize Phoenix. GPT answers a travel question through exe.dev's LLM gateway, calling a get_weather tool as often as it needs. phoenix.otel sends one trace per question: an agent span, each model call (with tokens), and each tool call. Reads PHOENIX_COLLECTOR_ENDPOINT, PHOENIX_API_KEY, and optionally PHOENIX_CLIENT_HEADERS from the environment. """ import json import os import sys from openai import OpenAI from opentelemetry import trace from phoenix.otel import register tracer_provider = register(project_name="hello-agent", auto_instrument=True) tracer = tracer_provider.get_tracer(__name__) # exe.dev's gateway needs no key; the SDK wants a value anyway. llm = OpenAI( base_url=os.environ.get("LLM_GATEWAY", "https://llm.int.exe.xyz/v1"), api_key="exe-llm-gateway", ) MODEL = os.environ.get("MODEL", "gpt-5.4-mini") TEMPERATURES = {"lisbon": 21, "oslo": 4, "tokyo": 16, "nairobi": 24} TOOLS = [{ "type": "function", "function": { "name": "get_weather", "description": "Today's temperature in Celsius for a city.", "parameters": { "type": "object", "properties": {"city": {"type": "string"}}, "required": ["city"], }, }, }] @tracer.tool def get_weather(city: str) -> str: celsius = TEMPERATURES.get(city.strip().lower()) if celsius is None: return json.dumps({"error": f"no data for {city}"}) return json.dumps({"city": city, "celsius": celsius}) @tracer.agent def agent(question: str) -> str: span = trace.get_current_span().get_span_context() print("Trace ID:", format(span.trace_id, "032x")) messages = [ {"role": "system", "content": "You answer travel questions in one or two " "sentences. Use get_weather for temperatures; never guess them."}, {"role": "user", "content": question}, ] for _ in range(5): reply = llm.chat.completions.create(model=MODEL, messages=messages, tools=TOOLS) message = reply.choices[0].message if not message.tool_calls: return message.content messages.append(message.model_dump(exclude_none=True)) for call in message.tool_calls: result = get_weather(**json.loads(call.function.arguments)) messages.append({"role": "tool", "tool_call_id": call.id, "content": result}) return "Gave up after five steps." if __name__ == "__main__": question = " ".join(sys.argv[1:]) or "Is it warmer in Lisbon or Oslo today, and by how much?" print(agent(question)) tracer_provider.force_flush() # send the last spans before exiting