naman1102 commited on
Commit
2683234
·
1 Parent(s): e038027

Update tools.py

Browse files
Files changed (1) hide show
  1. tools.py +20 -20
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
- # Perform the search
86
- search_results = list(ddgs.text(
87
  state.input.query,
88
  max_results=state.input.max_results
89
  ))
90
-
91
- # Convert results to our model
92
- results = [
93
- SearchResult(
94
- title=result["title"],
95
- link=result["link"],
96
- snippet=result["body"]
97
- )
98
- for result in search_results
99
- ]
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)
 
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)