Commit
·
4e1775f
1
Parent(s):
3b21447
ert
Browse files- __pycache__/agent.cpython-310.pyc +0 -0
- agent.py +26 -29
- langgraph_state_diagram.png +0 -0
__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,41 +96,38 @@ def wiki_search(query: str) -> str:
|
|
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 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
)
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
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,14 +248,14 @@ def build_graph(provider: str = "huggingface"):
|
|
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, "
|
262 |
builder.add_edge("retriever", "assistant")
|
263 |
builder.add_conditional_edges(
|
264 |
"assistant",
|
@@ -291,7 +288,7 @@ def build_graph(provider: str = "huggingface"):
|
|
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?"
|
|
|
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 |
+
# Invoke it correctly
|
109 |
+
search_results = tavily_tool.invoke(input=query) # <--- CORRECTED LINE
|
110 |
+
|
111 |
+
# The result of TavilySearchResults.invoke is usually a list of strings or a single string.
|
112 |
+
# Let's check its type and format accordingly.
|
113 |
+
# Typically, TavilySearchResults directly returns a list of Document objects
|
114 |
+
# or a list of dictionaries if you've configured it differently.
|
115 |
+
# For the default, it's often a list of strings or a single concatenated string.
|
116 |
+
# If it returns a list of Document objects (which is common for loaders/retrievers):
|
117 |
+
if isinstance(search_results, list) and all(hasattr(doc, 'metadata') and hasattr(doc, 'page_content') for doc in search_results):
|
118 |
+
formatted_search_docs = "\n\n---\n\n".join(
|
119 |
+
[
|
120 |
+
f'<Document source="{doc.metadata.get("source", "N/A")}" page="{doc.metadata.get("page", "")}"/>\n{doc.page_content}\n</Document>'
|
121 |
+
for doc in search_results
|
122 |
+
])
|
123 |
+
# If it returns a list of strings (less likely for Tavily in recent versions, but good to check)
|
124 |
+
elif isinstance(search_results, list) and all(isinstance(item, str) for item in search_results):
|
125 |
+
formatted_search_docs = "\n\n---\n\n".join(search_results)
|
126 |
+
# If it returns a single string
|
127 |
+
elif isinstance(search_results, str):
|
|
|
|
|
|
|
|
|
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 |
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("ex msgs"+[sys_msg] + state["messages"] + [example_msg])
|
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, "assistant")
|
259 |
builder.add_edge("retriever", "assistant")
|
260 |
builder.add_conditional_edges(
|
261 |
"assistant",
|
|
|
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?"
|
langgraph_state_diagram.png
CHANGED
![]() |
![]() |