Update src/web_search.py
Browse files- src/web_search.py +78 -75
src/web_search.py
CHANGED
|
@@ -1,76 +1,79 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
from
|
| 5 |
-
from langchain.prompts import PromptTemplate
|
| 6 |
-
from
|
| 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 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
|
|
|
|
|
|
|
|
|
| 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 |
|