Commit
·
3b21447
1
Parent(s):
5a0e318
- __pycache__/agent.cpython-310.pyc +0 -0
- agent.py +29 -26
- langgraph_state_diagram.png +0 -0
- system_prompt.txt +1 -5
__pycache__/agent.cpython-310.pyc
CHANGED
Binary files a/__pycache__/agent.cpython-310.pyc and b/__pycache__/agent.cpython-310.pyc differ
|
|
agent.py
CHANGED
@@ -96,38 +96,41 @@ def wiki_search(query: str) -> str:
|
|
96 |
# for doc in search_docs
|
97 |
# ])
|
98 |
# return {"web_results": formatted_search_docs}
|
|
|
|
|
99 |
@tool
|
100 |
def web_search(query: str) -> str:
|
101 |
"""Search Tavily for a query and return maximum 3 results.
|
102 |
-
|
103 |
Args:
|
104 |
query: The search query."""
|
105 |
try:
|
106 |
-
# Initialize the tool
|
107 |
tavily_tool = TavilySearchResults(max_results=3)
|
108 |
-
#
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
|
|
|
|
|
|
|
|
128 |
formatted_search_docs = search_results
|
129 |
else:
|
130 |
-
# Fallback or handle unexpected format
|
131 |
print(f"Unexpected Tavily search result format: {type(search_results)}")
|
132 |
formatted_search_docs = str(search_results)
|
133 |
|
@@ -248,14 +251,14 @@ def build_graph(provider: str = "huggingface"):
|
|
248 |
example_msg = HumanMessage(
|
249 |
content=f"Here I provide a similar question and answer for reference: \n\n{similar_question[0].page_content}",
|
250 |
)
|
251 |
-
print(
|
252 |
return {"messages": [sys_msg] + state["messages"] + [example_msg]}
|
253 |
|
254 |
builder = StateGraph(MessagesState)
|
255 |
builder.add_node("retriever", retriever)
|
256 |
builder.add_node("assistant", assistant)
|
257 |
builder.add_node("tools", ToolNode(tools))
|
258 |
-
builder.add_edge(START, "
|
259 |
builder.add_edge("retriever", "assistant")
|
260 |
builder.add_conditional_edges(
|
261 |
"assistant",
|
@@ -288,7 +291,7 @@ def build_graph(provider: str = "huggingface"):
|
|
288 |
# --- END: Visualization code ---
|
289 |
|
290 |
return compiled_graph # This should be the last line of the function
|
291 |
-
|
292 |
# test
|
293 |
if __name__ == "__main__":
|
294 |
question = "When was a picture of St. Thomas Aquinas first added to the Wikipedia page on the Principle of double effect?"
|
|
|
96 |
# for doc in search_docs
|
97 |
# ])
|
98 |
# return {"web_results": formatted_search_docs}
|
99 |
+
|
100 |
+
|
101 |
@tool
|
102 |
def web_search(query: str) -> str:
|
103 |
"""Search Tavily for a query and return maximum 3 results.
|
|
|
104 |
Args:
|
105 |
query: The search query."""
|
106 |
try:
|
|
|
107 |
tavily_tool = TavilySearchResults(max_results=3)
|
108 |
+
search_results = tavily_tool.invoke(input=query) # Correct parameter is 'input'
|
109 |
+
|
110 |
+
formatted_docs = []
|
111 |
+
if isinstance(search_results, list) and search_results:
|
112 |
+
if all(isinstance(doc, dict) for doc in search_results): # Check for list of dicts
|
113 |
+
for doc_dict in search_results:
|
114 |
+
title = doc_dict.get("title", "N/A")
|
115 |
+
source_url = doc_dict.get("url", "N/A")
|
116 |
+
content = doc_dict.get("content", "")
|
117 |
+
# Adjust formatting to be most useful for the LLM
|
118 |
+
formatted_docs.append(
|
119 |
+
f'<Document source="{source_url}" title="{title}">\n{content}\n</Document>'
|
120 |
+
)
|
121 |
+
formatted_search_docs = "\n\n---\n\n".join(formatted_docs)
|
122 |
+
elif all(hasattr(doc, 'metadata') and hasattr(doc, 'page_content') for doc in search_results): # Original check for Document objects
|
123 |
+
formatted_search_docs = "\n\n---\n\n".join(
|
124 |
+
[
|
125 |
+
f'<Document source="{doc.metadata.get("source", "N/A")}" page="{doc.metadata.get("page", "")}"/>\n{doc.page_content}\n</Document>'
|
126 |
+
for doc in search_results
|
127 |
+
])
|
128 |
+
else: # Fallback for other list types
|
129 |
+
print(f"Tavily search returned a list of unexpected item types: {type(search_results[0])}")
|
130 |
+
formatted_search_docs = str(search_results)
|
131 |
+
elif isinstance(search_results, str): # Single string result
|
132 |
formatted_search_docs = search_results
|
133 |
else:
|
|
|
134 |
print(f"Unexpected Tavily search result format: {type(search_results)}")
|
135 |
formatted_search_docs = str(search_results)
|
136 |
|
|
|
251 |
example_msg = HumanMessage(
|
252 |
content=f"Here I provide a similar question and answer for reference: \n\n{similar_question[0].page_content}",
|
253 |
)
|
254 |
+
print(example_msg)
|
255 |
return {"messages": [sys_msg] + state["messages"] + [example_msg]}
|
256 |
|
257 |
builder = StateGraph(MessagesState)
|
258 |
builder.add_node("retriever", retriever)
|
259 |
builder.add_node("assistant", assistant)
|
260 |
builder.add_node("tools", ToolNode(tools))
|
261 |
+
builder.add_edge(START, "retriever")
|
262 |
builder.add_edge("retriever", "assistant")
|
263 |
builder.add_conditional_edges(
|
264 |
"assistant",
|
|
|
291 |
# --- END: Visualization code ---
|
292 |
|
293 |
return compiled_graph # This should be the last line of the function
|
294 |
+
|
295 |
# test
|
296 |
if __name__ == "__main__":
|
297 |
question = "When was a picture of St. Thomas Aquinas first added to the Wikipedia page on the Principle of double effect?"
|
langgraph_state_diagram.png
ADDED
![]() |
system_prompt.txt
CHANGED
@@ -1,5 +1 @@
|
|
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.
|
|
|
1 |
+
You are a helpful assistant tasked with answering questions using a set of tools. Now, I will ask you a question. Report your thoughts, and finish your answer with the following template: FINAL ANSWER: [YOUR FINAL ANSWER]. 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.Your answer should only start with "FINAL ANSWER: ", then follows with the answer.
|
|
|
|
|
|
|
|