Update agent.py
Browse files
agent.py
CHANGED
|
@@ -11,38 +11,17 @@ from langchain_huggingface import HuggingFaceEndpoint, ChatHuggingFace
|
|
| 11 |
from langchain_core.tools import tool
|
| 12 |
from langchain_community.document_loaders import WikipediaLoader
|
| 13 |
from langchain_google_genai import ChatGoogleGenerativeAI
|
| 14 |
-
from
|
| 15 |
|
| 16 |
load_dotenv()
|
| 17 |
|
| 18 |
# ReAct System Prompt
|
| 19 |
-
REACT_SYSTEM_PROMPT = """You are a helpful assistant tasked with answering questions using a set of tools.
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
2. **Action**: Use the appropriate tool(s) to gather information or perform calculations
|
| 26 |
-
3. **Observation**: Analyze the results from your actions
|
| 27 |
-
4. Repeat the Thought-Action-Observation cycle as needed until you have enough information to provide a complete answer
|
| 28 |
-
|
| 29 |
-
When you need to use tools:
|
| 30 |
-
- Think about which tool is most appropriate for the task
|
| 31 |
-
- Use tools to gather information or perform calculations
|
| 32 |
-
- Analyze the results and determine if you need additional information
|
| 33 |
-
- Continue until you can provide a comprehensive answer
|
| 34 |
-
|
| 35 |
-
Available tools:
|
| 36 |
-
- multiply, add, subtract, divide: For mathematical calculations
|
| 37 |
-
- wikidata_search: Search Wikipedia for factual information
|
| 38 |
-
- duckduckgo_search: Search the web for current information
|
| 39 |
-
|
| 40 |
-
Now, I will ask you a question. Report your thoughts, and finish your answer with the following template:
|
| 41 |
-
FINAL ANSWER: [YOUR FINAL ANSWER].
|
| 42 |
-
YOUR FINAL ANSWER should be a number OR as few words as possible OR a comma separated list of numbers and/or strings. If you are asked for a number, don't use comma to write your number neither use units such as $ or percent sign unless specified otherwise. If you are asked for a string, don't use articles, neither abbreviations (e.g. for cities), and write the digits in plain text unless specified otherwise. If you are asked for a comma separated list, apply the above rules depending of whether the element to be put in the list is a number or a string.
|
| 43 |
-
Your answer should only start with "FINAL ANSWER: ", then follows with the answer.
|
| 44 |
-
|
| 45 |
-
Always explain your reasoning process and show your work step by step."""
|
| 46 |
|
| 47 |
@tool
|
| 48 |
def multiply(a:int, b:int) -> int:
|
|
@@ -89,21 +68,13 @@ def wikidata_search(query: str) -> str:
|
|
| 89 |
])
|
| 90 |
return {"wiki_results": formatted_search_docs}
|
| 91 |
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
Args:
|
| 98 |
-
query: The search query.
|
| 99 |
-
"""
|
| 100 |
-
search = DuckDuckGoSearchRun()
|
| 101 |
-
# DuckDuckGo returns a string, so we need to format it differently
|
| 102 |
-
results = search.run(query)
|
| 103 |
-
formatted_search_docs = f'<Document source="DuckDuckGo Search" page=""/>\n{results}\n</Document>'
|
| 104 |
-
return {"web_results": formatted_search_docs}
|
| 105 |
|
| 106 |
-
tools = [multiply, add, subtract, divide, wikidata_search,
|
| 107 |
|
| 108 |
def build_graph():
|
| 109 |
llm = ChatGoogleGenerativeAI(model="gemini-2.0-flash", api_key=os.getenv("GOOGLE_API_KEY"))
|
|
|
|
| 11 |
from langchain_core.tools import tool
|
| 12 |
from langchain_community.document_loaders import WikipediaLoader
|
| 13 |
from langchain_google_genai import ChatGoogleGenerativeAI
|
| 14 |
+
from langchain_tavily import TavilySearch
|
| 15 |
|
| 16 |
load_dotenv()
|
| 17 |
|
| 18 |
# ReAct System Prompt
|
| 19 |
+
REACT_SYSTEM_PROMPT = """You are a helpful assistant tasked with answering questions using a set of tools.
|
| 20 |
+
Now, I will ask you a question. Report your thoughts, and finish your answer with the following template:
|
| 21 |
+
FINAL ANSWER: [YOUR FINAL ANSWER].
|
| 22 |
+
YOUR FINAL ANSWER should be a number OR as few words as possible OR a comma separated list of numbers and/or strings. If you are asked for a number, don't use comma to write your number neither use units such as $ or percent sign unless specified otherwise. If you are asked for a string, don't use articles, neither abbreviations (e.g. for cities), and write the digits in plain text unless specified otherwise. If you are asked for a comma separated list, Apply the rules above for each element (number or string), ensure there is exactly one space after each comma.
|
| 23 |
+
Your answer should only start with "FINAL ANSWER: ", then follows with the answer.
|
| 24 |
+
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
@tool
|
| 27 |
def multiply(a:int, b:int) -> int:
|
|
|
|
| 68 |
])
|
| 69 |
return {"wiki_results": formatted_search_docs}
|
| 70 |
|
| 71 |
+
# Initialize Tavily Search Tool
|
| 72 |
+
tavily_search_tool = TavilySearch(
|
| 73 |
+
max_results=3,
|
| 74 |
+
topic="general",
|
| 75 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
|
| 77 |
+
tools = [multiply, add, subtract, divide, wikidata_search, tavily_search_tool]
|
| 78 |
|
| 79 |
def build_graph():
|
| 80 |
llm = ChatGoogleGenerativeAI(model="gemini-2.0-flash", api_key=os.getenv("GOOGLE_API_KEY"))
|