-
Notifications
You must be signed in to change notification settings - Fork 0
/
googleSearch.py
46 lines (37 loc) · 1.34 KB
/
googleSearch.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
from dotenv import load_dotenv
import os
from langchain.llms import OpenAI
from langchain.utilities import GoogleSearchAPIWrapper
from langchain.agents import initialize_agent, Tool
from langchain.agents import AgentType
load_dotenv()
os.environ['OPENAI_API_KEY'] = os.environ.get('OPENAI_API_KEY')
os.environ['GOOGLE_API_KEY'] = os.environ.get('GOOGLE_API_KEY')
os.environ['GOOGLE_CSE_ID'] = os.environ.get('GOOGLE_CSE_ID')
def create_tool(name, func, description):
return Tool(
name=name,
func=func.run,
description=description
)
def google_search(tools):
llm = OpenAI(model="gpt-3.5-turbo-instruct", temperature=0)
return initialize_agent(tools,
llm,
agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION,
verbose=True,
max_iterations=6)
if __name__ == "__main__":
# Google Search via API.
search = GoogleSearchAPIWrapper()
search_agent = google_search(
tools=[
create_tool(
name="google-search",
func=search,
description="useful for when you need to search google to answer questions about current events"
)
]
)
response = search_agent("which films nominated for oscar 2024?")
print(response['output'])