:::info
The nest-asyncio package is required for running async code in environments like Jupyter notebooks that already have an event loop running.
:::
# Required: Your Perplexity API keyexport EXAMPLE_API_KEY="your-perplexity-api-key"# Optional: Customize the API endpoint (defaults to official endpoint)export EXAMPLE_BASE_URL="https://api.perplexity.ai"# Optional: Choose your model (defaults to sonar-pro)export EXAMPLE_MODEL_NAME="sonar-pro"
Hereβs the full implementation with detailed explanations:
Copy
Ask AI
# Import necessary standard librariesimport asyncio # For running asynchronous codeimport os # To access environment variables# Import AsyncOpenAI for creating an async clientfrom openai import AsyncOpenAI# Import custom classes and functions from the agents package.# These handle agent creation, model interfacing, running agents, and more.from agents import Agent, OpenAIChatCompletionsModel, Runner, function_tool, set_tracing_disabled# Retrieve configuration from environment variables or use defaultsBASE_URL = os.getenv("EXAMPLE_BASE_URL") or "https://api.perplexity.ai"API_KEY = os.getenv("EXAMPLE_API_KEY") MODEL_NAME = os.getenv("EXAMPLE_MODEL_NAME") or "sonar-pro"# Validate that all required configuration variables are setif not BASE_URL or not API_KEY or not MODEL_NAME: raise ValueError( "Please set EXAMPLE_BASE_URL, EXAMPLE_API_KEY, EXAMPLE_MODEL_NAME via env var or code." )# Initialize the custom OpenAI async client with the specified BASE_URL and API_KEY.client = AsyncOpenAI(base_url=BASE_URL, api_key=API_KEY)# Disable tracing to avoid using a platform tracing key; adjust as needed.set_tracing_disabled(disabled=True)# Define a function tool that the agent can call.# The decorator registers this function as a tool in the agents framework.@function_tooldef get_weather(city: str): """ Simulate fetching weather data for a given city. Args: city (str): The name of the city to retrieve weather for. Returns: str: A message with weather information. """ print(f"[debug] getting weather for {city}") return f"The weather in {city} is sunny."# Import nest_asyncio to support nested event loopsimport nest_asyncio# Apply the nest_asyncio patch to enable running asyncio.run() # even if an event loop is already running.nest_asyncio.apply()async def main(): """ Main asynchronous function to set up and run the agent. This function creates an Agent with a custom model and function tools, then runs a query to get the weather in Tokyo. """ # Create an Agent instance with: # - A name ("Assistant") # - Custom instructions ("Be precise and concise.") # - A model built from OpenAIChatCompletionsModel using our client and model name. # - A list of tools; here, only get_weather is provided. agent = Agent( name="Assistant", instructions="Be precise and concise.", model=OpenAIChatCompletionsModel(model=MODEL_NAME, openai_client=client), tools=[get_weather], ) # Execute the agent with the sample query. result = await Runner.run(agent, "What's the weather in Tokyo?") # Print the final output from the agent. print(result.final_output)# Standard boilerplate to run the async main() function.if __name__ == "__main__": asyncio.run(main())
This creates an async OpenAI client pointed at Perplexityβs Sonar API. The client handles all HTTP communication and maintains compatibility with OpenAIβs interface.
# For quick, lightweight queriesMODEL_NAME = "sonar"# For complex research and analysis (default)MODEL_NAME = "sonar-pro"# For deep reasoning tasksMODEL_NAME = "sonar-reasoning-pro"
agent = Agent( name="Research Assistant", instructions=""" You are a research assistant specializing in academic literature. Always provide citations and verify information through multiple sources. Be thorough but concise in your responses. """, model=OpenAIChatCompletionsModel(model=MODEL_NAME, openai_client=client), tools=[search_papers, get_citations],)
@function_tooldef search_web(query: str): """Search the web for current information.""" # Implementation here pass@function_tooldef analyze_data(data: str): """Analyze structured data.""" # Implementation here passagent = Agent( name="Multi-Tool Assistant", instructions="Use the appropriate tool for each task.", model=OpenAIChatCompletionsModel(model=MODEL_NAME, openai_client=client), tools=[get_weather, search_web, analyze_data],)
Ready to build? This integration opens up powerful possibilities for creating intelligent, grounded agents. Start with the basic example and gradually add more sophisticated tools and capabilities! π