Update tools.py
Browse files
tools.py
CHANGED
@@ -1,122 +1,20 @@
|
|
1 |
-
|
2 |
-
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
class SearchInput(TypedDict):
|
23 |
-
query: str
|
24 |
-
max_results: int
|
25 |
-
|
26 |
-
class SearchResult(TypedDict):
|
27 |
-
title: str
|
28 |
-
link: str
|
29 |
-
snippet: str
|
30 |
-
|
31 |
-
class SearchOutput(TypedDict):
|
32 |
-
results: List[SearchResult]
|
33 |
-
query: str
|
34 |
-
|
35 |
-
class SearchState(TypedDict):
|
36 |
-
input: SearchInput
|
37 |
-
output: Optional[SearchOutput]
|
38 |
-
|
39 |
-
# --- Calculator Tool Implementation ---
|
40 |
-
|
41 |
-
def create_calculator_tool() -> Graph:
|
42 |
-
"""Creates a calculator tool using LangGraph that can perform basic arithmetic operations."""
|
43 |
-
print("Creating calculator tool")
|
44 |
-
|
45 |
-
def calculator_function(state: CalculatorState) -> Dict[str, Any]:
|
46 |
-
print("Calculator function called")
|
47 |
-
input_data = state["input"]
|
48 |
-
|
49 |
-
if len(input_data["numbers"]) < 2:
|
50 |
-
raise ValueError("At least two numbers are required for calculation")
|
51 |
-
|
52 |
-
result = input_data["numbers"][0]
|
53 |
-
for num in input_data["numbers"][1:]:
|
54 |
-
if input_data["operation"] == "add":
|
55 |
-
result += num
|
56 |
-
elif input_data["operation"] == "subtract":
|
57 |
-
result -= num
|
58 |
-
elif input_data["operation"] == "multiply":
|
59 |
-
result *= num
|
60 |
-
elif input_data["operation"] == "divide":
|
61 |
-
if num == 0:
|
62 |
-
raise ValueError("Cannot divide by zero")
|
63 |
-
result /= num
|
64 |
-
else:
|
65 |
-
raise ValueError(f"Unsupported operation: {input_data['operation']}")
|
66 |
-
|
67 |
-
return {
|
68 |
-
"output": {
|
69 |
-
"result": result,
|
70 |
-
"operation": input_data["operation"]
|
71 |
-
}
|
72 |
-
}
|
73 |
-
print("Calculator function defined")
|
74 |
-
workflow = StateGraph(state_schema=CalculatorState)
|
75 |
-
print("Calculator workflow created")
|
76 |
-
workflow.add_node("calculator", ToolNode(calculator_function))
|
77 |
-
print("Calculator tool node added")
|
78 |
-
workflow.set_entry_point("calculator")
|
79 |
-
print("Calculator workflow set entry point")
|
80 |
-
workflow.set_finish_point("calculator")
|
81 |
-
print("Calculator workflow set finish point")
|
82 |
-
print("Calculator workflow created and compiled")
|
83 |
-
return workflow.compile()
|
84 |
-
|
85 |
-
# --- Search Tool Implementation ---
|
86 |
-
|
87 |
-
def create_search_tool() -> Graph:
|
88 |
-
"""Creates a search tool using DuckDuckGo that can search for information online."""
|
89 |
-
print("Creating search tool")
|
90 |
-
|
91 |
-
def search_function(state: SearchState) -> Dict[str, Any]:
|
92 |
-
print("Search function called")
|
93 |
-
query = state["input"]["query"]
|
94 |
-
max_results = state["input"].get("max_results", 3)
|
95 |
-
|
96 |
-
with DDGS() as ddgs:
|
97 |
-
raw_results = list(ddgs.text(query, max_results=max_results))
|
98 |
-
|
99 |
-
results = []
|
100 |
-
for r in raw_results:
|
101 |
-
try:
|
102 |
-
results.append({
|
103 |
-
"title": r.get("title", ""),
|
104 |
-
"link": r.get("href", r.get("link", "")),
|
105 |
-
"snippet": r.get("body", r.get("snippet", ""))
|
106 |
-
})
|
107 |
-
except Exception as e:
|
108 |
-
print("Skipping malformed search result:", r, "Error:", e)
|
109 |
-
|
110 |
-
return {
|
111 |
-
"output": {
|
112 |
-
"results": results,
|
113 |
-
"query": query
|
114 |
-
}
|
115 |
-
}
|
116 |
-
|
117 |
-
workflow = StateGraph(state_schema=SearchState)
|
118 |
-
workflow.add_node("search", ToolNode(search_function))
|
119 |
-
workflow.set_entry_point("search")
|
120 |
-
workflow.set_finish_point("search")
|
121 |
-
print("Search workflow created and compiled")
|
122 |
-
return workflow.compile()
|
|
|
1 |
+
# --- Simple Search Tool ---
|
2 |
+
|
3 |
+
def simple_search(query: str, max_results: int = 3) -> List[str]:
|
4 |
+
"""
|
5 |
+
Perform a DuckDuckGo search and return a list of strings summarizing the top results.
|
6 |
+
"""
|
7 |
+
with DDGS() as ddgs:
|
8 |
+
raw_results = list(ddgs.text(query, max_results=max_results))
|
9 |
+
|
10 |
+
results = []
|
11 |
+
for r in raw_results:
|
12 |
+
try:
|
13 |
+
title = r.get("title", "")
|
14 |
+
link = r.get("href") or r.get("link", "")
|
15 |
+
summary = f"{title} - {link}"
|
16 |
+
results.append(summary)
|
17 |
+
except Exception as e:
|
18 |
+
print("Skipping malformed search result:", r, "Error:", e)
|
19 |
+
|
20 |
+
return results
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|