Datawithsarah commited on
Commit
e0f29bb
·
1 Parent(s): 6ac8934

updated agent

Browse files
Files changed (3) hide show
  1. agent.py +193 -71
  2. requirements.txt +14 -6
  3. system_prompt.txt +5 -22
agent.py CHANGED
@@ -1,84 +1,206 @@
1
- from langgraph.graph import StateGraph, END
2
- from langchain_core.messages import HumanMessage, AIMessage, SystemMessage
 
 
 
 
 
 
 
 
 
 
 
 
3
  from langchain_core.tools import tool
4
- from typing import TypedDict, Optional
5
- import pandas as pd
6
- import string
7
 
8
- # Tools
9
 
10
  @tool
11
- def reverse_string(text: str) -> str:
12
- """Reverse a string"""
13
- return text[::-1]
 
 
 
 
14
 
15
  @tool
16
- def extract_numbers(text: str) -> str:
17
- """Extract digits from a string"""
18
- return "".join([c for c in text if c.isdigit()])
 
 
 
 
19
 
20
  @tool
21
- def strip_punctuation(text: str) -> str:
22
- """Remove all punctuation from a string"""
23
- return text.translate(str.maketrans('', '', string.punctuation))
 
 
 
 
24
 
25
  @tool
26
- def open_file_as_text(file_path: str) -> str:
27
- """Open and return the contents of a text/CSV file"""
28
- try:
29
- with open(file_path, 'r', encoding='utf-8') as f:
30
- return f.read()
31
- except Exception as e:
32
- return f"Error reading file: {e}"
 
 
33
 
34
  @tool
35
- def analyze_csv_file(file_path: str) -> str:
36
- """Read CSV file and return summary of content"""
37
- try:
38
- df = pd.read_csv(file_path)
39
- summary = f"Columns: {', '.join(df.columns)}\nRows: {len(df)}"
40
- return summary
41
- except Exception as e:
42
- return f"CSV error: {e}"
43
-
44
- # Agent State
45
-
46
- class AgentState(TypedDict):
47
- messages: list
48
- file_name: Optional[str]
49
-
50
- # Build Graph
51
-
52
- def build_graph():
53
- tools = [reverse_string, extract_numbers, strip_punctuation, open_file_as_text, analyze_csv_file]
54
-
55
- def decide_path(state: AgentState):
56
- if state.get("file_name"):
57
- msg = HumanMessage(content=f"Analyze this file: {state['file_name']}")
58
- return {"messages": state["messages"] + [msg]}
59
- else:
60
- return {"messages": state["messages"]}
61
-
62
- def agent_logic(state: AgentState):
63
- question = state["messages"][-1].content.lower()
64
- answer = ""
65
- if "reverse" in question:
66
- answer = reverse_string.invoke(question)
67
- elif "number" in question:
68
- answer = extract_numbers.invoke(question)
69
- elif "punctuation" in question:
70
- answer = strip_punctuation.invoke(question)
71
- elif state.get("file_name") and state["file_name"].endswith(".csv"):
72
- answer = analyze_csv_file.invoke(state["file_name"])
73
- else:
74
- answer = "unsupported question"
75
-
76
- return {"messages": state["messages"] + [AIMessage(content=f'final_answer("{answer}")')]}
77
-
78
- builder = StateGraph(AgentState)
79
- builder.add_node("router", decide_path)
80
- builder.add_node("agent_logic", agent_logic)
81
- builder.set_entry_point("router")
82
- builder.set_finish_point("agent_logic")
83
- builder.add_edge("router", "agent_logic")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
- huggingface-hub
9
- python-dotenv
10
- tqdm
 
 
 
 
 
 
 
 
 
 
 
 
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 highly focused AI assistant participating in the GAIA benchmark challenge.
2
-
3
- Your goal is to answer each question precisely, using tools when necessary. Follow these rules strictly:
4
-
5
- - NEVER output explanations, reasoning, or intermediate thoughts.
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.