jtan4albany commited on
Commit
b3a8254
·
verified ·
1 Parent(s): 9bba869

Update agent.py

Browse files
Files changed (1) hide show
  1. agent.py +82 -93
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
- # ---- Basic Arithmetic Utilities as Classes ---- #
17
-
18
- class MultiplyTool:
19
- name = "multiply"
20
- description = "Returns the product of two integers."
21
-
22
- def __call__(self, a: int, b: int) -> int:
23
- return a * b
24
-
25
- class AddTool:
26
- name = "add"
27
- description = "Returns the sum of two integers."
28
-
29
- def __call__(self, a: int, b: int) -> int:
30
- return a + b
31
-
32
- class SubtractTool:
33
- name = "subtract"
34
- description = "Returns the difference between two integers."
35
-
36
- def __call__(self, a: int, b: int) -> int:
37
- return a - b
38
-
39
- class DivideTool:
40
- name = "divide"
41
- description = "Performs division and handles zero division errors."
42
-
43
- def __call__(self, a: int, b: int) -> float:
44
- if b == 0:
45
- raise ValueError("Division by zero is undefined.")
46
- return a / b
47
-
48
- class ModulusTool:
49
- name = "modulus"
50
- description = "Returns the remainder after division."
51
-
52
- def __call__(self, a: int, b: int) -> int:
53
- return a % b
54
-
55
- # ---- Search Tools as Classes ---- #
56
-
57
- class SearchWikipediaTool:
58
- name = "search_wikipedia"
59
- description = "Search Wikipedia for a query and return maximum 2 results."
60
-
61
- def __call__(self, query: str) -> str:
62
- search_docs = WikipediaLoader(query=query, load_max_docs=2).load()
63
- formatted_search_docs = "\n\n---\n\n".join(
64
- [
65
- f'<Document source="{doc.metadata["source"]}" page="{doc.metadata.get("page", "")}"/>\n{doc.page_content}\n</Document>'
66
- for doc in search_docs
67
- ])
68
- return {"wiki_results": formatted_search_docs}
69
-
70
- class SearchWebTool:
71
- name = "search_web"
72
- description = "Search Tavily for a query and return maximum 3 results."
73
-
74
- def __call__(self, query: str) -> str:
75
- search_docs = TavilySearchResults(max_results=3).invoke(query=query)
76
- formatted_search_docs = "\n\n---\n\n".join(
77
- [
78
- f'<Document source="{doc.metadata["source"]}" page="{doc.metadata.get("page", "")}"/>\n{doc.page_content}\n</Document>'
79
- for doc in search_docs
80
- ])
81
- return {"web_results": formatted_search_docs}
82
-
83
- class SearchArxivTool:
84
- name = "search_arxiv"
85
- description = "Search Arxiv for a query and return maximum 3 results."
86
-
87
- def __call__(self, query: str) -> str:
88
- search_docs = ArxivLoader(query=query, load_max_docs=3).load()
89
- formatted_search_docs = "\n\n---\n\n".join(
90
- [
91
- f'<Document source="{doc.metadata["source"]}" page="{doc.metadata.get("page", "")}"/>\n{doc.page_content[:1000]}\n</Document>'
92
- for doc in search_docs
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 \"FINAL ANSWER: \" then follows with the answer. """)
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
+