|
| 1 | +# Import necessary standard libraries |
| 2 | +import asyncio # For running asynchronous code |
| 3 | +import os # To access environment variables |
| 4 | + |
| 5 | +# Import AsyncOpenAI for creating an async client |
| 6 | +from openai import AsyncOpenAI |
| 7 | + |
| 8 | +# Import custom classes and functions from the agents package. |
| 9 | +# These handle agent creation, model interfacing, running agents, and more. |
| 10 | +from agents import Agent, OpenAIChatCompletionsModel, Runner, function_tool, set_tracing_disabled |
| 11 | + |
| 12 | +# Retrieve configuration from environment variables or use defaults |
| 13 | +BASE_URL = os.getenv("EXAMPLE_BASE_URL") or "https://api.perplexity.ai" |
| 14 | +API_KEY = os.getenv("EXAMPLE_API_KEY") |
| 15 | +MODEL_NAME = os.getenv("EXAMPLE_MODEL_NAME") or "sonar-pro" |
| 16 | + |
| 17 | +# Validate that all required configuration variables are set |
| 18 | +if not BASE_URL or not API_KEY or not MODEL_NAME: |
| 19 | + raise ValueError( |
| 20 | + "Please set EXAMPLE_BASE_URL, EXAMPLE_API_KEY, EXAMPLE_MODEL_NAME via env var or code." |
| 21 | + ) |
| 22 | + |
| 23 | +""" |
| 24 | +This example illustrates how to use a custom provider with a specific agent: |
| 25 | +1. We create an asynchronous OpenAI client configured to interact with the Perplexity Sonar API. |
| 26 | +2. We define a custom model using this client. |
| 27 | +3. We set up an Agent with our custom model and attach function tools. |
| 28 | +Note: Tracing is disabled in this example. If you have an OpenAI platform API key, |
| 29 | +you can enable tracing by setting the environment variable OPENAI_API_KEY or using set_tracing_export_api_key(). |
| 30 | +""" |
| 31 | + |
| 32 | +# Initialize the custom OpenAI async client with the specified BASE_URL and API_KEY. |
| 33 | +client = AsyncOpenAI(base_url=BASE_URL, api_key=API_KEY) |
| 34 | + |
| 35 | +# Disable tracing to avoid using a platform tracing key; adjust as needed. |
| 36 | +set_tracing_disabled(disabled=True) |
| 37 | + |
| 38 | +# (Alternate approach example, commented out) |
| 39 | +# PROVIDER = OpenAIProvider(openai_client=client) |
| 40 | +# agent = Agent(..., model="some-custom-model") |
| 41 | +# Runner.run(agent, ..., run_config=RunConfig(model_provider=PROVIDER)) |
| 42 | + |
| 43 | +# Define a function tool that the agent can call. |
| 44 | +# The decorator registers this function as a tool in the agents framework. |
| 45 | +@function_tool |
| 46 | +def get_weather(city: str): |
| 47 | + """ |
| 48 | + Simulate fetching weather data for a given city. |
| 49 | + |
| 50 | + Args: |
| 51 | + city (str): The name of the city to retrieve weather for. |
| 52 | + |
| 53 | + Returns: |
| 54 | + str: A message with weather information. |
| 55 | + """ |
| 56 | + print(f"[debug] getting weather for {city}") |
| 57 | + return f"The weather in {city} is sunny." |
| 58 | + |
| 59 | +# Import nest_asyncio to support nested event loops (helpful in interactive environments like Jupyter) |
| 60 | +import nest_asyncio |
| 61 | + |
| 62 | +# Apply the nest_asyncio patch to enable running asyncio.run() even if an event loop is already running. |
| 63 | +nest_asyncio.apply() |
| 64 | + |
| 65 | +async def main(): |
| 66 | + """ |
| 67 | + Main asynchronous function to set up and run the agent. |
| 68 | + |
| 69 | + This function creates an Agent with a custom model and function tools, |
| 70 | + then runs a query to get the weather in Tokyo. |
| 71 | + """ |
| 72 | + # Create an Agent instance with: |
| 73 | + # - A name ("Assistant") |
| 74 | + # - Custom instructions ("Be precise and concise.") |
| 75 | + # - A model built from OpenAIChatCompletionsModel using our client and model name. |
| 76 | + # - A list of tools; here, only get_weather is provided. |
| 77 | + agent = Agent( |
| 78 | + name="Assistant", |
| 79 | + instructions="Be precise and concise.", |
| 80 | + model=OpenAIChatCompletionsModel(model=MODEL_NAME, openai_client=client), |
| 81 | + tools=[get_weather], |
| 82 | + ) |
| 83 | + |
| 84 | + # Execute the agent with the sample query. |
| 85 | + result = await Runner.run(agent, "What's the weather in Tokyo?") |
| 86 | + |
| 87 | + # Print the final output from the agent. |
| 88 | + print(result.final_output) |
| 89 | + |
| 90 | +# Standard boilerplate to run the async main() function. |
| 91 | +if __name__ == "__main__": |
| 92 | + asyncio.run(main()) |
0 commit comments