Spaces:
Build error
Build error
Update agent.py
Browse files
agent.py
CHANGED
|
@@ -5,111 +5,99 @@ from langgraph.prebuilt import ToolNode, tools_condition
|
|
| 5 |
|
| 6 |
from langchain_google_genai import ChatGoogleGenerativeAI
|
| 7 |
from langchain_groq import ChatGroq
|
| 8 |
-
from langchain_huggingface import ChatHuggingFace, HuggingFaceEndpoint
|
| 9 |
|
| 10 |
from langchain_community.tools.tavily_search import TavilySearchResults
|
| 11 |
from langchain_community.document_loaders import WikipediaLoader, ArxivLoader
|
|
|
|
| 12 |
|
| 13 |
from langchain_core.messages import SystemMessage, HumanMessage
|
| 14 |
from langchain_core.tools import tool
|
| 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 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
])
|
| 94 |
-
return {"arvix_results": formatted_search_docs}
|
| 95 |
|
| 96 |
system_message = SystemMessage(content="""You are a helpful assistant tasked with answering questions using a set of tools.
|
| 97 |
Now, I will ask you a question. Report your thoughts, and finish your answer with the following template:
|
| 98 |
FINAL ANSWER: [YOUR FINAL ANSWER].
|
| 99 |
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.
|
| 100 |
-
Your answer should only start with
|
| 101 |
-
|
| 102 |
-
# Instantiate tools
|
| 103 |
-
multiply = MultiplyTool()
|
| 104 |
-
add = AddTool()
|
| 105 |
-
subtract = SubtractTool()
|
| 106 |
-
divide = DivideTool()
|
| 107 |
-
modulus = ModulusTool()
|
| 108 |
-
search_wikipedia = SearchWikipediaTool()
|
| 109 |
-
search_web = SearchWebTool()
|
| 110 |
-
search_arxiv = SearchArxivTool()
|
| 111 |
-
|
| 112 |
-
# Toolset
|
| 113 |
toolset = [
|
| 114 |
multiply,
|
| 115 |
add,
|
|
@@ -170,3 +158,4 @@ if __name__ == "__main__":
|
|
| 170 |
# Print the final output
|
| 171 |
for m in output_state["messages"]:
|
| 172 |
print(m.content)
|
|
|
|
|
|
| 5 |
|
| 6 |
from langchain_google_genai import ChatGoogleGenerativeAI
|
| 7 |
from langchain_groq import ChatGroq
|
| 8 |
+
from langchain_huggingface import ChatHuggingFace, HuggingFaceEndpoint, HuggingFaceEmbeddings
|
| 9 |
|
| 10 |
from langchain_community.tools.tavily_search import TavilySearchResults
|
| 11 |
from langchain_community.document_loaders import WikipediaLoader, ArxivLoader
|
| 12 |
+
from langchain_community.vectorstores import SupabaseVectorStore
|
| 13 |
|
| 14 |
from langchain_core.messages import SystemMessage, HumanMessage
|
| 15 |
from langchain_core.tools import tool
|
| 16 |
|
| 17 |
+
from supabase.client import create_client, Client
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
# Load environment variables
|
| 21 |
+
|
| 22 |
+
# ---- Basic Arithmetic Utilities ---- #
|
| 23 |
+
@tool
|
| 24 |
+
def multiply(a: int, b: int) -> int:
|
| 25 |
+
"""Returns the product of two integers."""
|
| 26 |
+
return a * b
|
| 27 |
+
|
| 28 |
+
@tool
|
| 29 |
+
def add(a: int, b: int) -> int:
|
| 30 |
+
"""Returns the sum of two integers."""
|
| 31 |
+
return a + b
|
| 32 |
+
|
| 33 |
+
@tool
|
| 34 |
+
def subtract(a: int, b: int) -> int:
|
| 35 |
+
"""Returns the difference between two integers."""
|
| 36 |
+
return a - b
|
| 37 |
+
|
| 38 |
+
@tool
|
| 39 |
+
def divide(a: int, b: int) -> float:
|
| 40 |
+
"""Performs division and handles zero division errors."""
|
| 41 |
+
if b == 0:
|
| 42 |
+
raise ValueError("Division by zero is undefined.")
|
| 43 |
+
return a / b
|
| 44 |
+
|
| 45 |
+
@tool
|
| 46 |
+
def modulus(a: int, b: int) -> int:
|
| 47 |
+
"""Returns the remainder after division."""
|
| 48 |
+
return a % b
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
# ---- Search Tools ---- #
|
| 52 |
+
@tool
|
| 53 |
+
def search_wikipedia(query: str) -> str:
|
| 54 |
+
"""Search Wikipedia for a query and return maximum 2 results.
|
| 55 |
+
|
| 56 |
+
Args:
|
| 57 |
+
query: The search query."""
|
| 58 |
+
search_docs = WikipediaLoader(query=query, load_max_docs=2).load()
|
| 59 |
+
formatted_search_docs = "\n\n---\n\n".join(
|
| 60 |
+
[
|
| 61 |
+
f'<Document source="{doc.metadata["source"]}" page="{doc.metadata.get("page", "")}"/>\n{doc.page_content}\n</Document>'
|
| 62 |
+
for doc in search_docs
|
| 63 |
+
])
|
| 64 |
+
return {"wiki_results": formatted_search_docs}
|
| 65 |
+
|
| 66 |
+
@tool
|
| 67 |
+
def search_web(query: str) -> str:
|
| 68 |
+
"""Search Tavily for a query and return maximum 3 results.
|
| 69 |
+
|
| 70 |
+
Args:
|
| 71 |
+
query: The search query."""
|
| 72 |
+
search_docs = TavilySearchResults(max_results=3).invoke(query=query)
|
| 73 |
+
formatted_search_docs = "\n\n---\n\n".join(
|
| 74 |
+
[
|
| 75 |
+
f'<Document source="{doc.metadata["source"]}" page="{doc.metadata.get("page", "")}"/>\n{doc.page_content}\n</Document>'
|
| 76 |
+
for doc in search_docs
|
| 77 |
+
])
|
| 78 |
+
return {"web_results": formatted_search_docs}
|
| 79 |
+
|
| 80 |
+
@tool
|
| 81 |
+
def search_arxiv(query: str) -> str:
|
| 82 |
+
"""Search Arxiv for a query and return maximum 3 result.
|
| 83 |
+
|
| 84 |
+
Args:
|
| 85 |
+
query: The search query."""
|
| 86 |
+
search_docs = ArxivLoader(query=query, load_max_docs=3).load()
|
| 87 |
+
formatted_search_docs = "\n\n---\n\n".join(
|
| 88 |
+
[
|
| 89 |
+
f'<Document source="{doc.metadata["source"]}" page="{doc.metadata.get("page", "")}"/>\n{doc.page_content[:1000]}\n</Document>'
|
| 90 |
+
for doc in search_docs
|
| 91 |
+
])
|
| 92 |
+
return {"arvix_results": formatted_search_docs}
|
| 93 |
+
|
|
|
|
|
|
|
| 94 |
|
| 95 |
system_message = SystemMessage(content="""You are a helpful assistant tasked with answering questions using a set of tools.
|
| 96 |
Now, I will ask you a question. Report your thoughts, and finish your answer with the following template:
|
| 97 |
FINAL ANSWER: [YOUR FINAL ANSWER].
|
| 98 |
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.
|
| 99 |
+
Your answer should only start with "FINAL ANSWER: ", then follows with the answer. """)
|
| 100 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 101 |
toolset = [
|
| 102 |
multiply,
|
| 103 |
add,
|
|
|
|
| 158 |
# Print the final output
|
| 159 |
for m in output_state["messages"]:
|
| 160 |
print(m.content)
|
| 161 |
+
|