# See docs for more details on token counts and usd cost in Langfuse # https://langfuse.com/docs/model-usage-and-cost langfuse_context.update_current_observation( usage_details={ "input": response.usage.input_tokens, "output": response.usage.output_tokens } ) return response.content[0].text
# Configure logfire instrumentation. logfire.configure( service_name='my_agent_service', send_to_logfire=False ) # This method automatically patches the OpenAI Agents SDK to send logs via OTLP to Langfuse. logfire.instrument_openai_agents()
asyncdefmain(): agent = Agent( name="Assistant", instructions="You are a helpful assistant.", ) result = await Runner.run(agent, "What is the captial of France?") print(result.final_output)
if __name__ == "__main__": asyncio.run(main())
输出结果为:
1 2 3 4
13:34:39.454 OpenAI Agents trace: Agent workflow 13:34:39.455 Agent run: 'Assistant' 13:34:39.460 Responses API with 'gpt-4o' The capital of France is Paris.
# Configure logfire instrumentation. logfire.configure( service_name='my_agent_service', send_to_logfire=False ) # This method automatically patches the OpenAI Agents SDK to send logs via OTLP to Langfuse. logfire.instrument_openai_agents()
zh2en_agent = Agent( name="Chinese to English agent", instructions="You are a translator from Chinese to English.", )
en2zh_agent = Agent( name="English to Chinese agent", instructions="You are a translator from English to Chinese.", )
translation_agent = Agent( name="Translation agent", instructions="You are a translation agent. If the input is in Chinese, translate it to English." " If the input is in English, translate it to Chinese.", handoffs=[zh2en_agent, en2zh_agent], )
asyncdefmain(): result = await Runner.run(translation_agent, input="The Shawshank Redemption") print(result.final_output)
if __name__ == "__main__": asyncio.run(main())
输出结果如下:
1 2 3 4 5 6 7
13:38:56.614 OpenAI Agents trace: Agent workflow 13:38:56.615 Agent run: 'Translation agent' 13:38:56.620 Responses API with'gpt-4o' 13:38:58.060 Handoff: Translation agent -> None 13:38:58.061 Agent run: 'English to Chinese agent' 13:38:58.061 Responses API with'gpt-4o' 《肖申克的救赎》
# Configure logfire instrumentation. logfire.configure( service_name='my_agent_service', send_to_logfire=False ) # This method automatically patches the OpenAI Agents SDK to send logs via OTLP to Langfuse. logfire.instrument_openai_agents()
@function_tool defget_weather(city: str) -> str: returnf"The weather in {city} is sunny."
@function_tool defget_now_time() -> str: now_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S") returnf"The current time is {now_time}."
weather_agent = Agent( name="Weather agent", instructions="You are a weather agent.", tools=[get_weather], )
time_agent = Agent( name="Time agent", instructions="You are a time agent.", tools=[get_now_time], )
agent = Agent( name="Agent", instructions="You are an helpful agent.", handoffs=[weather_agent, time_agent], )
asyncdefmain(): result1 = await Runner.run(agent, input="What's the weather in Tokyo?") print(result1.final_output) # The weather in Tokyo is sunny. result2 = await Runner.run(agent, input="What's the time now?") print(result2.final_output)