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

Adding Google Search support for Agent-Zero #241

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
4 changes: 3 additions & 1 deletion example.env
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ API_KEY_OPENAI=
API_KEY_ANTHROPIC=
API_KEY_GROQ=
API_KEY_PERPLEXITY=
API_KEY_GOOGLE=
API_KEY_MISTRAL=
API_KEY_OPENROUTER=
API_KEY_SAMBANOVA=
Expand All @@ -11,6 +10,9 @@ API_KEY_OPENAI_AZURE=
OPENAI_AZURE_ENDPOINT=
OPENAI_API_VERSION=

API_KEY_GOOGLE=
CX_KEY_GOOGLE=

HF_TOKEN=


Expand Down
52 changes: 52 additions & 0 deletions python/helpers/google_search.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import requests
import json
from urllib.parse import urlencode
import os
def search(query: str) -> str:
"""
Perform a search using the Google Search API.

Args:
query (str): The search query.
results (int): The maximum number of results to return.
region (str): The region for search results.
time (str): The time range for search results.

Returns:
str: The search results in a formatted string.
"""
params = {
'key': os.getenv("API_KEY_GOOGLE"),
'cx': os.getenv("CX_KEY_GOOGLE"),
'q': query
}

url = f"https://www.googleapis.com/customsearch/v1?{urlencode(params)}"
resp = requests.get(url)

if resp is None:
return ""
try:
search_results = json.loads(resp.text)
except Exception:
return ""
if search_results is None:
return ""

results = search_results.get("items", [])
search_results = []

# Normalizing results to match the format of the other search APIs
for result in results:
# skip youtube results
if "youtube.com" in result["link"]:
continue
search_result = {
"title": result["title"],
"href": result["link"],
"body": result["snippet"],
}

search_results.append(search_result)

return json.dumps(search_results, indent=2)
11 changes: 8 additions & 3 deletions python/tools/knowledge_tool.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import os
import asyncio
from python.helpers import memory, perplexity_search, duckduckgo_search
from python.helpers import memory, perplexity_search, duckduckgo_search, google_search
from python.helpers.tool import Tool, Response
from python.helpers.print_style import PrintStyle
from python.helpers.errors import handle_error
Expand All @@ -11,21 +11,23 @@ async def execute(self, question="", **kwargs):
tasks = [
self.perplexity_search(question),
self.duckduckgo_search(question),
self.google_search(question),
self.mem_search(question)
]

# Run all tasks concurrently
results = await asyncio.gather(*tasks, return_exceptions=True)

perplexity_result, duckduckgo_result, memory_result = results
perplexity_result, duckduckgo_result, google_result, memory_result = results

# Handle exceptions and format results
perplexity_result = self.format_result(perplexity_result, "Perplexity")
duckduckgo_result = self.format_result(duckduckgo_result, "DuckDuckGo")
google_result = self.format_result(google_result, "Google")
memory_result = self.format_result(memory_result, "Memory")

msg = self.agent.read_prompt("tool.knowledge.response.md",
online_sources = ((perplexity_result + "\n\n") if perplexity_result else "") + str(duckduckgo_result),
online_sources = ((perplexity_result + "\n\n") if perplexity_result else "") + str(duckduckgo_result) + str(google_result),
memory = memory_result)

await self.agent.handle_intervention(msg) # wait for intervention and handle it, if paused
Expand All @@ -42,6 +44,9 @@ async def perplexity_search(self, question):

async def duckduckgo_search(self, question):
return await asyncio.to_thread(duckduckgo_search.search, question)

async def google_search(self, question):
return await asyncio.to_thread(google_search.search, question)

async def mem_search(self, question: str):
db = await memory.Memory.get(self.agent)
Expand Down