HFswapnil commited on
Commit
383509a
·
verified ·
1 Parent(s): 5e5edc7

Update src/web_search.py

Browse files
Files changed (1) hide show
  1. src/web_search.py +78 -75
src/web_search.py CHANGED
@@ -1,76 +1,79 @@
1
- from langchain.agents import AgentExecutor, create_react_agent
2
- from langchain.agents import load_tools, Tool
3
- # from langchain.tools import DuckDuckGoSearchResults
4
- from tavily import TavilyClient
5
- from langchain.prompts import PromptTemplate
6
- from model import get_gemma
7
- from api_key import TAVILY_API_KEY
8
-
9
- gemma_model = get_gemma()
10
-
11
-
12
- # Create the ReAct template
13
- react_template = """Answer the following questions as best you can. You have access to the following tools:
14
-
15
- {tools}
16
-
17
- Use the following format:
18
-
19
- Question: the input question you must answer
20
- Thought: you should always think about what to do
21
- Action: the action to take, should be one of [{tool_names}]
22
- Action Input: the input to the action
23
- Observation: the result of the action
24
- ... (this Thought/Action/Action Input/Observation can repeat N times)
25
- Thought: I now know the final answer
26
- Final Answer: the final answer to the original input question
27
-
28
- Begin!
29
-
30
- Question: {input}
31
- Thought:{agent_scratchpad}"""
32
-
33
- prompt = PromptTemplate(
34
- template=react_template,
35
- input_variables=["tools", "tool_names", "input", "agent_scratchpad"]
36
- )
37
-
38
-
39
- tavily_client = TavilyClient(api_key=TAVILY_API_KEY)
40
-
41
- tavily_search_tool = Tool(
42
- name="tavily search",
43
- description = "A web search engine. Use this to as a search engine for general queries.",
44
- func = lambda x: tavily_client.search(x, max_results=1)
45
- )
46
-
47
- # Prepare tools
48
- tools = load_tools(["llm-math"], llm=gemma_model)
49
- tools.append(tavily_search_tool)
50
-
51
-
52
- # Construct the ReAct agent
53
- agent = create_react_agent(gemma_model, tools, prompt)
54
- agent_executor = AgentExecutor(
55
- agent=agent,
56
- tools=tools,
57
- verbose=True,
58
- handle_parsing_errors=True,
59
- return_intermediate_steps=True
60
- )
61
-
62
- def get_urls_from_response(response):
63
- urls = []
64
- for step in response["intermediate_steps"]:
65
- urls.append(step[1]["results"][0]["url"])
66
- return urls
67
-
68
-
69
- def search_web(query):
70
- response = agent_executor.invoke({"input" : query})
71
-
72
- output = response["output"]
73
- sources = get_urls_from_response(response)
74
-
75
- return output, sources
 
 
 
76
 
 
1
+ import os
2
+
3
+ from langchain.agents import AgentExecutor, create_react_agent
4
+ from langchain.agents import load_tools, Tool
5
+ from langchain.prompts import PromptTemplate
6
+ from tavily import TavilyClient
7
+
8
+ from model import get_gemma
9
+
10
+ TAVILY_API_KEY = os.environ.get("TAVILY_API_KEY")
11
+
12
+ gemma_model = get_gemma()
13
+
14
+
15
+ # Create the ReAct template
16
+ react_template = """Answer the following questions as best you can. You have access to the following tools:
17
+
18
+ {tools}
19
+
20
+ Use the following format:
21
+
22
+ Question: the input question you must answer
23
+ Thought: you should always think about what to do
24
+ Action: the action to take, should be one of [{tool_names}]
25
+ Action Input: the input to the action
26
+ Observation: the result of the action
27
+ ... (this Thought/Action/Action Input/Observation can repeat N times)
28
+ Thought: I now know the final answer
29
+ Final Answer: the final answer to the original input question
30
+
31
+ Begin!
32
+
33
+ Question: {input}
34
+ Thought:{agent_scratchpad}"""
35
+
36
+ prompt = PromptTemplate(
37
+ template=react_template,
38
+ input_variables=["tools", "tool_names", "input", "agent_scratchpad"]
39
+ )
40
+
41
+
42
+ tavily_client = TavilyClient(api_key=TAVILY_API_KEY)
43
+
44
+ tavily_search_tool = Tool(
45
+ name="tavily search",
46
+ description = "A web search engine. Use this to as a search engine for general queries.",
47
+ func = lambda x: tavily_client.search(x, max_results=1)
48
+ )
49
+
50
+ # Prepare tools
51
+ tools = load_tools(["llm-math"], llm=gemma_model)
52
+ tools.append(tavily_search_tool)
53
+
54
+
55
+ # Construct the ReAct agent
56
+ agent = create_react_agent(gemma_model, tools, prompt)
57
+ agent_executor = AgentExecutor(
58
+ agent=agent,
59
+ tools=tools,
60
+ verbose=True,
61
+ handle_parsing_errors=True,
62
+ return_intermediate_steps=True
63
+ )
64
+
65
+ def get_urls_from_response(response):
66
+ urls = []
67
+ for step in response["intermediate_steps"]:
68
+ urls.append(step[1]["results"][0]["url"])
69
+ return urls
70
+
71
+
72
+ def search_web(query):
73
+ response = agent_executor.invoke({"input" : query})
74
+
75
+ output = response["output"]
76
+ sources = get_urls_from_response(response)
77
+
78
+ return output, sources
79