Skip to content

Commit

Permalink
added support for google search api, corrected set_gemini_api_key
Browse files Browse the repository at this point in the history
  • Loading branch information
nalaso committed Mar 24, 2024
1 parent 31969e8 commit 9125761
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 5 deletions.
3 changes: 3 additions & 0 deletions config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,13 @@ REPOS_DIR = "repos"

[API_KEYS]
BING = "<YOUR_BING_API_KEY>"
GOOGLE_SEARCH = "<YOUR_GOOGLE_SEARCH_API_KEY>"
GOOGLE_SEARCH_ENGINE_ID = "<YOUR_GOOGLE_SEARCH_ENGINE_ID>"
CLAUDE = "<YOUR_CLAUDE_API_KEY>"
NETLIFY = "<YOUR_NETLIFY_API_KEY>"
OPENAI = "<YOUR_OPENAI_API_KEY>"
GEMINI = "<YOUR_GEMINI_API_KEY>"

[API_ENDPOINTS]
BING = "https://api.bing.microsoft.com/v7.0/search"
GOOGLE_SEARCH = "https://www.googleapis.com/customsearch/v1"
5 changes: 3 additions & 2 deletions src/agents/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

from src.bert.sentence import SentenceBert
from src.memory import KnowledgeBase
from src.browser.search import BingSearch,DuckDuckGoSearch
from src.browser.search import BingSearch,DuckDuckGoSearch,GoogleSearch
from src.browser import Browser
from src.browser import start_interaction
from src.filesystem import ReadCode
Expand Down Expand Up @@ -64,7 +64,8 @@ def search_queries(self, queries: list, project_name: str) -> dict:

knowledge_base = KnowledgeBase()
# web_search = BingSearch()
web_search = DuckDuckGoSearch()
# web_search = DuckDuckGoSearch()
web_search = GoogleSearch()
browser = Browser()

for query in queries:
Expand Down
29 changes: 27 additions & 2 deletions src/browser/search.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import requests
from src.config import Config
from duckduckgo_search import DDGS

class BingSearch:
def __init__(self):
self.config = Config()
Expand Down Expand Up @@ -33,4 +32,30 @@ def search(self, query):
return err

def get_first_link(self):
return self.query_result[0]["href"]
return self.query_result[0]["href"]

class GoogleSearch:
def __init__(self):
self.config = Config()
self.google_search_api_key = self.config.get_google_search_api_key()
self.google_search_engine_ID = self.config.get_google_search_engine_id()
self.google_search_api_endpoint = self.config.get_google_search_api_endpoint()
self.query_result = None

def search(self, query):
try:
params = {
'q': query,
'key': self.google_search_api_key,
'cx': self.google_search_engine_ID
}
response = requests.get(self.google_search_api_endpoint, params=params)
self.query_result = response.json()
except Exception as err:
return err

def get_first_link(self):
item = ""
if 'items' in self.query_result:
item = self.query_result['items'][0]['link']
return item
23 changes: 22 additions & 1 deletion src/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,15 @@ def get_bing_api_key(self):
def get_bing_api_endpoint(self):
return self.config["API_ENDPOINTS"]["BING"]

def get_google_search_api_key(self):
return self.config["API_KEYS"]["GOOGLE_SEARCH"]

def get_google_search_engine_id(self):
return self.config["API_KEYS"]["GOOGLE_SEARCH_ENGINE_ID"]

def get_google_search_api_endpoint(self):
return self.config["API_ENDPOINTS"]["GOOGLE_SEARCH"]

def get_claude_api_key(self):
return self.config["API_KEYS"]["CLAUDE"]

Expand Down Expand Up @@ -50,6 +59,18 @@ def set_bing_api_key(self, key):
def set_bing_api_endpoint(self, endpoint):
self.config["API_ENDPOINTS"]["BING"] = endpoint
self.save_config()

def set_google_search_api_key(self, key):
self.config["API_KEYS"]["GOOGLE_SEARCH"] = key
self.save_config()

def set_google_search_engine_id(self, key):
self.config["API_KEYS"]["GOOGLE_SEARCH_ENGINE_ID"] = key
self.save_config()

def set_google_search_api_endpoint(self, endpoint):
self.config["API_ENDPOINTS"]["GOOGLE_SEARCH"] = endpoint
self.save_config()

def set_claude_api_key(self, key):
self.config["API_KEYS"]["CLAUDE"] = key
Expand All @@ -59,7 +80,7 @@ def set_openai_api_key(self, key):
self.config["API_KEYS"]["OPENAI"] = key
self.save_config()

def set_openai_api_key(self, key):
def set_gemini_api_key(self, key):
self.config["API_KEYS"]["GEMINI"] = key
self.save_config()

Expand Down

0 comments on commit 9125761

Please sign in to comment.