Update tools.py
Browse files
tools.py
CHANGED
@@ -80,30 +80,30 @@ def create_search_tool() -> Graph:
|
|
80 |
"""Creates a search tool using DuckDuckGo that can search for information online."""
|
81 |
|
82 |
def search_function(state: SearchState) -> dict:
|
83 |
-
# Initialize DuckDuckGo search
|
84 |
with DDGS() as ddgs:
|
85 |
-
#
|
86 |
-
|
87 |
state.input.query,
|
88 |
max_results=state.input.max_results
|
89 |
))
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
|
|
107 |
|
108 |
# Create the graph with state schema
|
109 |
workflow = StateGraph(state_schema=SearchState)
|
|
|
80 |
"""Creates a search tool using DuckDuckGo that can search for information online."""
|
81 |
|
82 |
def search_function(state: SearchState) -> dict:
|
|
|
83 |
with DDGS() as ddgs:
|
84 |
+
# Run search
|
85 |
+
raw_results = list(ddgs.text(
|
86 |
state.input.query,
|
87 |
max_results=state.input.max_results
|
88 |
))
|
89 |
+
|
90 |
+
results = []
|
91 |
+
for r in raw_results:
|
92 |
+
try:
|
93 |
+
results.append(SearchResult(
|
94 |
+
title=r.get("title", ""),
|
95 |
+
link=r.get("href", r.get("link", "")), # DuckDuckGo sometimes uses "href"
|
96 |
+
snippet=r.get("body", r.get("snippet", ""))
|
97 |
+
))
|
98 |
+
except Exception as e:
|
99 |
+
print("Skipping malformed search result:", r, "Error:", e)
|
100 |
+
|
101 |
+
return {
|
102 |
+
"output": SearchOutput(
|
103 |
+
results=results,
|
104 |
+
query=state.input.query
|
105 |
+
)
|
106 |
+
}
|
107 |
|
108 |
# Create the graph with state schema
|
109 |
workflow = StateGraph(state_schema=SearchState)
|