Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Langchain external search tool examples for PraisonAI agents #407

Merged
merged 1 commit into from
Mar 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions examples/tools/langchain/brave-search.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# pip install langchain-community
# export BRAVE_SEARCH_API=your_api_key_here
# export OPENAI_API_KEY=your_api_key_here

from praisonaiagents import Agent, PraisonAIAgents
from langchain_community.tools import BraveSearch
import os

def search_brave(query: str):
"""Searches using BraveSearch and returns results."""
api_key = os.environ['BRAVE_SEARCH_API']
tool = BraveSearch.from_api_key(api_key=api_key, search_kwargs={"count": 3})
return tool.run(query)
Comment on lines +11 to +13

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

Consider adding error handling to check if the BRAVE_SEARCH_API environment variable is set. If it's not set, the program will crash. You can use os.getenv with a default value or raise an exception with a helpful message.

Suggested change
api_key = os.environ['BRAVE_SEARCH_API']
tool = BraveSearch.from_api_key(api_key=api_key, search_kwargs={"count": 3})
return tool.run(query)
api_key = os.getenv('BRAVE_SEARCH_API')
if not api_key:
raise ValueError("BRAVE_SEARCH_API environment variable not set.")
tool = BraveSearch.from_api_key(api_key=api_key, search_kwargs={"count": 3})


data_agent = Agent(instructions="Search about AI job trends in 2025", tools=[search_brave])
editor_agent = Agent(instructions="Write a blog article")
agents = PraisonAIAgents(agents=[data_agent, editor_agent])
agents.start()
18 changes: 18 additions & 0 deletions examples/tools/langchain/google-trends.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# pip install langchain-community google-search-results
# export SERPAPI_API_KEY=your_api_key_here
# export OPENAI_API_KEY=your_api_key_here
Comment on lines +1 to +3

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

It's good to include the installation and environment variable setup instructions, but it's missing instructions on how to obtain the SERPAPI_API_KEY.

Suggested change
# pip install langchain-community google-search-results
# export SERPAPI_API_KEY=your_api_key_here
# export OPENAI_API_KEY=your_api_key_here
# pip install langchain-community google-search-results
# export SERPAPI_API_KEY=your_api_key_here # Get your API key from https://serpapi.com/
# export OPENAI_API_KEY=your_api_key_here


from langchain_community.utilities.google_trends import GoogleTrendsAPIWrapper
from praisonaiagents import Agent, PraisonAIAgents

research_agent = Agent(
instructions="Research trending topics related to AI",
tools=[GoogleTrendsAPIWrapper]
Comment on lines +8 to +10

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

For consistency with the other examples (brave-search.py and serp-api.py), consider wrapping GoogleTrendsAPIWrapper in a function.

Suggested change
research_agent = Agent(
instructions="Research trending topics related to AI",
tools=[GoogleTrendsAPIWrapper]
def google_trends_search(query: str):
"""Searches Google Trends and returns results."""
tool = GoogleTrendsAPIWrapper()
return tool.run(query)
research_agent = Agent(
instructions="Research trending topics related to AI",
tools=[google_trends_search]
)

)

summarise_agent = Agent(
instructions="Summarise findings from the research agent",
)

agents = PraisonAIAgents(agents=[research_agent, summarise_agent])
agents.start()
12 changes: 12 additions & 0 deletions examples/tools/langchain/serp-api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# pip install langchain-community google-search-results
# export SERPAPI_API_KEY=your_api_key_here
# export OPENAI_API_KEY=your_api_key_here

from praisonaiagents import Agent, PraisonAIAgents
from langchain_community.utilities import SerpAPIWrapper

data_agent = Agent(instructions="Search about AI job trends in 2025", tools=[SerpAPIWrapper])
editor_agent = Agent(instructions="Write a blog article")

agents = PraisonAIAgents(agents=[data_agent, editor_agent])
agents.start()
Loading