Open In Colab Open on GitHub The Perplexity AI search integration allows users to perform real-time web searches within the AG2 framework. Follow these steps to integrate PerplexitySearchTool with AG2 Agents.

Configuring Your Perplexity API Key

  1. Create a Perplexity Account:
    • Visit Perplexity AI
    • Click Sign Up and create an account
    • Select Free Tier during registration
  2. Get API Key:
    • Navigate to API Settings
    • Add payment method (required)
    • Generate API key under API Keys
  3. Set Environment Variable:
    export PERPLEXITY_API_KEY="your_api_key_here"
    

Package Installation

To get started with the Perplexity Search integration in AG2, follow these steps: Install AG2 with openai extra since we use openai in our examples:
pip install -U "ag2[openai]"
Note: If you have been using autogen or ag2, all you need to do is upgrade it using:
pip install -U "autogen[openai]"
or
pip install -U "ag2[openai]"
as autogen, and ag2 are aliases for the same PyPI package.
You’re all set! Now you can start using Perplexity AI Search with AG2.

Implementation

The PerplexitySearchTool enables agents to perform real time AI Powered web search.

Imports

import os

from autogen import AssistantAgent, UserProxyAgent
from autogen.tools.experimental import PerplexitySearchTool

Agent Configuration

Configure an assistant agent and user proxy to be used for LLM recommendation and execution respectively.
os.environ["AUTOGEN_USE_DOCKER"] = "False"

from autogen import LLMConfig

config_list = LLMConfig(api_type="openai", model="gpt-4o-mini")

assistant = AssistantAgent(
    name="assistant",
    llm_config=config_list,
)

user_proxy = UserProxyAgent(name="user_proxy", human_input_mode="NEVER")

Tool Setup

perplexity_search_tool = PerplexitySearchTool(api_key=os.getenv("PERPLEXITY_API_KEY"), max_tokens=1000)

# Register the tool for LLM recommendation and execution.
perplexity_search_tool.register_for_llm(assistant)
perplexity_search_tool.register_for_execution(user_proxy)

Start the Conversation

With the setup complete, you can now use the assistant to fetch live web search results.
response = user_proxy.initiate_chat(
    recipient=assistant,
    message="What happened with stock prices after deepseek was launched? please search the web.",
    max_turns=2,
)
print(f"Final Answer: {response.summary}")