Commit
·
e0f29bb
1
Parent(s):
6ac8934
updated agent
Browse files- agent.py +193 -71
- requirements.txt +14 -6
- system_prompt.txt +5 -22
agent.py
CHANGED
@@ -1,84 +1,206 @@
|
|
1 |
-
|
2 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
3 |
from langchain_core.tools import tool
|
4 |
-
from
|
5 |
-
import
|
6 |
-
import string
|
7 |
|
8 |
-
|
9 |
|
10 |
@tool
|
11 |
-
def
|
12 |
-
"""
|
13 |
-
|
|
|
|
|
|
|
|
|
14 |
|
15 |
@tool
|
16 |
-
def
|
17 |
-
"""
|
18 |
-
|
|
|
|
|
|
|
|
|
19 |
|
20 |
@tool
|
21 |
-
def
|
22 |
-
"""
|
23 |
-
|
|
|
|
|
|
|
|
|
24 |
|
25 |
@tool
|
26 |
-
def
|
27 |
-
"""
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
|
|
|
|
33 |
|
34 |
@tool
|
35 |
-
def
|
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 |
return builder.compile()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""LangGraph Agent"""
|
2 |
+
import os
|
3 |
+
from dotenv import load_dotenv
|
4 |
+
from langgraph.graph import START, StateGraph, MessagesState
|
5 |
+
from langgraph.prebuilt import tools_condition
|
6 |
+
from langgraph.prebuilt import ToolNode
|
7 |
+
from langchain_google_genai import ChatGoogleGenerativeAI
|
8 |
+
from langchain_groq import ChatGroq
|
9 |
+
from langchain_huggingface import ChatHuggingFace, HuggingFaceEndpoint, HuggingFaceEmbeddings
|
10 |
+
from langchain_community.tools.tavily_search import TavilySearchResults
|
11 |
+
from langchain_community.document_loaders import WikipediaLoader
|
12 |
+
from langchain_community.document_loaders import ArxivLoader
|
13 |
+
from langchain_community.vectorstores import SupabaseVectorStore
|
14 |
+
from langchain_core.messages import SystemMessage, HumanMessage
|
15 |
from langchain_core.tools import tool
|
16 |
+
from langchain.tools.retriever import create_retriever_tool
|
17 |
+
from supabase.client import Client, create_client
|
|
|
18 |
|
19 |
+
load_dotenv()
|
20 |
|
21 |
@tool
|
22 |
+
def multiply(a: int, b: int) -> int:
|
23 |
+
"""Multiply two numbers.
|
24 |
+
Args:
|
25 |
+
a: first int
|
26 |
+
b: second int
|
27 |
+
"""
|
28 |
+
return a * b
|
29 |
|
30 |
@tool
|
31 |
+
def add(a: int, b: int) -> int:
|
32 |
+
"""Add two numbers.
|
33 |
+
Args:
|
34 |
+
a: first int
|
35 |
+
b: second int
|
36 |
+
"""
|
37 |
+
return a + b
|
38 |
|
39 |
@tool
|
40 |
+
def subtract(a: int, b: int) -> int:
|
41 |
+
"""Subtract two numbers.
|
42 |
+
Args:
|
43 |
+
a: first int
|
44 |
+
b: second int
|
45 |
+
"""
|
46 |
+
return a - b
|
47 |
|
48 |
@tool
|
49 |
+
def divide(a: int, b: int) -> int:
|
50 |
+
"""Divide two numbers.
|
51 |
+
Args:
|
52 |
+
a: first int
|
53 |
+
b: second int
|
54 |
+
"""
|
55 |
+
if b == 0:
|
56 |
+
raise ValueError("Cannot divide by zero.")
|
57 |
+
return a / b
|
58 |
|
59 |
@tool
|
60 |
+
def modulus(a: int, b: int) -> int:
|
61 |
+
"""Get the modulus of two numbers.
|
62 |
+
Args:
|
63 |
+
a: first int
|
64 |
+
b: second int
|
65 |
+
"""
|
66 |
+
return a % b
|
67 |
+
|
68 |
+
@tool
|
69 |
+
def wiki_search(query: str) -> str:
|
70 |
+
"""Search Wikipedia for a query and return maximum 2 results.
|
71 |
+
Args:
|
72 |
+
query: The search query."""
|
73 |
+
search_docs = WikipediaLoader(query=query, load_max_docs=2).load()
|
74 |
+
formatted_search_docs = "\n\n---\n\n".join(
|
75 |
+
[
|
76 |
+
f'<Document source="{doc.metadata["source"]}" page="{doc.metadata.get("page", "")}"/>\n{doc.page_content}\n</Document>'
|
77 |
+
for doc in search_docs
|
78 |
+
])
|
79 |
+
return {"wiki_results": formatted_search_docs}
|
80 |
+
|
81 |
+
@tool
|
82 |
+
def web_search(query: str) -> str:
|
83 |
+
"""Search Tavily for a query and return maximum 3 results.
|
84 |
+
Args:
|
85 |
+
query: The search query."""
|
86 |
+
search_docs = TavilySearchResults(max_results=3).invoke(query=query)
|
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}\n</Document>'
|
90 |
+
for doc in search_docs
|
91 |
+
])
|
92 |
+
return {"web_results": formatted_search_docs}
|
93 |
+
|
94 |
+
@tool
|
95 |
+
def arvix_search(query: str) -> str:
|
96 |
+
"""Search Arxiv for a query and return maximum 3 result.
|
97 |
+
Args:
|
98 |
+
query: The search query."""
|
99 |
+
search_docs = ArxivLoader(query=query, load_max_docs=3).load()
|
100 |
+
formatted_search_docs = "\n\n---\n\n".join(
|
101 |
+
[
|
102 |
+
f'<Document source="{doc.metadata["source"]}" page="{doc.metadata.get("page", "")}"/>\n{doc.page_content[:1000]}\n</Document>'
|
103 |
+
for doc in search_docs
|
104 |
+
])
|
105 |
+
return {"arvix_results": formatted_search_docs}
|
106 |
+
|
107 |
+
|
108 |
+
|
109 |
+
# load the system prompt from the file
|
110 |
+
with open("system_prompt.txt", "r", encoding="utf-8") as f:
|
111 |
+
system_prompt = f.read()
|
112 |
+
|
113 |
+
# System message
|
114 |
+
sys_msg = SystemMessage(content=system_prompt)
|
115 |
+
|
116 |
+
# build a retriever
|
117 |
+
embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-mpnet-base-v2") # dim=768
|
118 |
+
supabase: Client = create_client(
|
119 |
+
os.environ.get("SUPABASE_URL"),
|
120 |
+
os.environ.get("SUPABASE_SERVICE_KEY"))
|
121 |
+
vector_store = SupabaseVectorStore(
|
122 |
+
client=supabase,
|
123 |
+
embedding= embeddings,
|
124 |
+
table_name="Vector_Test",
|
125 |
+
query_name="match_documents_langchain",
|
126 |
+
)
|
127 |
+
create_retriever_tool = create_retriever_tool(
|
128 |
+
retriever=vector_store.as_retriever(),
|
129 |
+
name="Question Search",
|
130 |
+
description="A tool to retrieve similar questions from a vector store.",
|
131 |
+
)
|
132 |
+
|
133 |
+
|
134 |
+
|
135 |
+
tools = [
|
136 |
+
multiply,
|
137 |
+
add,
|
138 |
+
subtract,
|
139 |
+
divide,
|
140 |
+
modulus,
|
141 |
+
wiki_search,
|
142 |
+
web_search,
|
143 |
+
arvix_search,
|
144 |
+
]
|
145 |
+
|
146 |
+
# Build graph function
|
147 |
+
def build_graph(provider: str = "groq"):
|
148 |
+
"""Build the graph"""
|
149 |
+
# Load environment variables from .env file
|
150 |
+
if provider == "google":
|
151 |
+
# Google Gemini
|
152 |
+
llm = ChatGoogleGenerativeAI(model="gemini-2.0-flash", temperature=0)
|
153 |
+
elif provider == "groq":
|
154 |
+
# Groq https://console.groq.com/docs/models
|
155 |
+
llm = ChatGroq(model="qwen-qwq-32b", temperature=0) # optional : qwen-qwq-32b gemma2-9b-it
|
156 |
+
elif provider == "huggingface":
|
157 |
+
# TODO: Add huggingface endpoint
|
158 |
+
llm = ChatHuggingFace(
|
159 |
+
llm=HuggingFaceEndpoint(
|
160 |
+
url="https://api-inference.huggingface.co/models/Meta-DeepLearning/llama-2-7b-chat-hf",
|
161 |
+
temperature=0,
|
162 |
+
),
|
163 |
+
)
|
164 |
+
else:
|
165 |
+
raise ValueError("Invalid provider. Choose 'google', 'groq' or 'huggingface'.")
|
166 |
+
# Bind tools to LLM
|
167 |
+
llm_with_tools = llm.bind_tools(tools)
|
168 |
+
|
169 |
+
# Node
|
170 |
+
def assistant(state: MessagesState):
|
171 |
+
"""Assistant node"""
|
172 |
+
return {"messages": [llm_with_tools.invoke(state["messages"])]}
|
173 |
+
|
174 |
+
def retriever(state: MessagesState):
|
175 |
+
"""Retriever node"""
|
176 |
+
similar_question = vector_store.similarity_search(state["messages"][0].content)
|
177 |
+
example_msg = HumanMessage(
|
178 |
+
content=f"Here I provide a similar question and answer for reference: \n\n{similar_question[0].page_content}",
|
179 |
+
)
|
180 |
+
return {"messages": [sys_msg] + state["messages"] + [example_msg]}
|
181 |
+
|
182 |
+
builder = StateGraph(MessagesState)
|
183 |
+
builder.add_node("retriever", retriever)
|
184 |
+
builder.add_node("assistant", assistant)
|
185 |
+
builder.add_node("tools", ToolNode(tools))
|
186 |
+
builder.add_edge(START, "retriever")
|
187 |
+
builder.add_edge("retriever", "assistant")
|
188 |
+
builder.add_conditional_edges(
|
189 |
+
"assistant",
|
190 |
+
tools_condition,
|
191 |
+
)
|
192 |
+
builder.add_edge("tools", "assistant")
|
193 |
+
|
194 |
+
# Compile graph
|
195 |
return builder.compile()
|
196 |
+
|
197 |
+
# test
|
198 |
+
if __name__ == "__main__":
|
199 |
+
question = "When was a picture of St. Thomas Aquinas first added to the Wikipedia page on the Principle of double effect?"
|
200 |
+
# Build the graph
|
201 |
+
graph = build_graph(provider="groq")
|
202 |
+
# Run the graph
|
203 |
+
messages = [HumanMessage(content=question)]
|
204 |
+
messages = graph.invoke({"messages": messages})
|
205 |
+
for m in messages["messages"]:
|
206 |
+
m.pretty_print()
|
requirements.txt
CHANGED
@@ -1,10 +1,18 @@
|
|
1 |
gradio
|
2 |
-
pandas
|
3 |
requests
|
4 |
-
langchain-core
|
5 |
-
langgraph
|
6 |
langchain
|
7 |
langchain-community
|
8 |
-
|
9 |
-
|
10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
gradio
|
|
|
2 |
requests
|
|
|
|
|
3 |
langchain
|
4 |
langchain-community
|
5 |
+
langchain-core
|
6 |
+
langchain-google-genai
|
7 |
+
langchain-huggingface
|
8 |
+
langchain-groq
|
9 |
+
langchain-tavily
|
10 |
+
langchain-chroma
|
11 |
+
langgraph
|
12 |
+
huggingface_hub
|
13 |
+
supabase
|
14 |
+
arxiv
|
15 |
+
pymupdf
|
16 |
+
wikipedia
|
17 |
+
pgvector
|
18 |
+
python-dotenv
|
system_prompt.txt
CHANGED
@@ -1,22 +1,5 @@
|
|
1 |
-
You are a
|
2 |
-
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
- NEVER output more than one answer.
|
7 |
-
- When you reach the final answer, return it as:
|
8 |
-
final_answer("...your answer...")
|
9 |
-
- Use tools like `open_file_as_text`, `extract_number`, or `reverse_text` where applicable.
|
10 |
-
- If the task involves a file, use the given file_name — not one mentioned in the question.
|
11 |
-
- Format numbers without commas or symbols (e.g., 1739, not $1,739).
|
12 |
-
- Format comma-separated lists with a single space after each comma.
|
13 |
-
- When extracting or reversing text, always preserve punctuation unless instructed otherwise.
|
14 |
-
|
15 |
-
Example:
|
16 |
-
Q: What is the reverse of "good job"?
|
17 |
-
A: final_answer("boj doog")
|
18 |
-
|
19 |
-
Q: What is the third number listed in the attached file?
|
20 |
-
A: final_answer("42")
|
21 |
-
|
22 |
-
Be brief. Be exact. Use tools. Output only the final answer in the correct format.
|
|
|
1 |
+
You are a helpful assistant tasked with answering questions using a set of tools.
|
2 |
+
Now, I will ask you a question. Report your thoughts, and finish your answer with the following template:
|
3 |
+
FINAL ANSWER: [YOUR FINAL ANSWER].
|
4 |
+
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.
|
5 |
+
Your answer should only start with "FINAL ANSWER: ", then follows with the answer.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|