naman1102 commited on
Commit
b15267e
·
1 Parent(s): 687894a

Update tools.py

Browse files
Files changed (1) hide show
  1. tools.py +28 -24
tools.py CHANGED
@@ -12,6 +12,10 @@ class CalculatorOutput(BaseModel):
12
  result: float = Field(..., description="The result of the calculation")
13
  operation: str = Field(..., description="The operation that was performed")
14
 
 
 
 
 
15
  class SearchInput(BaseModel):
16
  query: str = Field(..., description="The search query to look up")
17
  max_results: int = Field(default=3, description="Maximum number of results to return")
@@ -25,40 +29,42 @@ class SearchOutput(BaseModel):
25
  results: List[SearchResult] = Field(..., description="List of search results")
26
  query: str = Field(..., description="The original search query")
27
 
 
 
 
 
28
  def create_calculator_tool() -> Graph:
29
  """Creates a calculator tool using LangGraph that can perform basic arithmetic operations."""
30
 
31
- def calculator_function(state: Dict[str, Any]) -> Dict[str, Any]:
32
- input_data = CalculatorInput(**state["input"])
33
-
34
- if len(input_data.numbers) < 2:
35
  raise ValueError("At least two numbers are required for calculation")
36
 
37
- result = input_data.numbers[0]
38
 
39
- for num in input_data.numbers[1:]:
40
- if input_data.operation == "add":
41
  result += num
42
- elif input_data.operation == "subtract":
43
  result -= num
44
- elif input_data.operation == "multiply":
45
  result *= num
46
- elif input_data.operation == "divide":
47
  if num == 0:
48
  raise ValueError("Cannot divide by zero")
49
  result /= num
50
  else:
51
- raise ValueError(f"Unsupported operation: {input_data.operation}")
52
 
53
- output = CalculatorOutput(
54
  result=result,
55
- operation=input_data.operation
56
  )
57
 
58
- return {"output": output}
59
 
60
  # Create the graph with state schema
61
- workflow = StateGraph(dict)
62
 
63
  # Add the calculator tool node
64
  workflow.add_node("calculator", ToolNode(calculator_function))
@@ -72,15 +78,13 @@ def create_calculator_tool() -> Graph:
72
  def create_search_tool() -> Graph:
73
  """Creates a search tool using DuckDuckGo that can search for information online."""
74
 
75
- def search_function(state: Dict[str, Any]) -> Dict[str, Any]:
76
- input_data = SearchInput(**state["input"])
77
-
78
  # Initialize DuckDuckGo search
79
  with DDGS() as ddgs:
80
  # Perform the search
81
  search_results = list(ddgs.text(
82
- input_data.query,
83
- max_results=input_data.max_results
84
  ))
85
 
86
  # Convert results to our model
@@ -93,15 +97,15 @@ def create_search_tool() -> Graph:
93
  for result in search_results
94
  ]
95
 
96
- output = SearchOutput(
97
  results=results,
98
- query=input_data.query
99
  )
100
 
101
- return {"output": output}
102
 
103
  # Create the graph with state schema
104
- workflow = StateGraph(dict)
105
 
106
  # Add the search tool node
107
  workflow.add_node("search", ToolNode(search_function))
 
12
  result: float = Field(..., description="The result of the calculation")
13
  operation: str = Field(..., description="The operation that was performed")
14
 
15
+ class CalculatorState(BaseModel):
16
+ input: CalculatorInput
17
+ output: CalculatorOutput = None
18
+
19
  class SearchInput(BaseModel):
20
  query: str = Field(..., description="The search query to look up")
21
  max_results: int = Field(default=3, description="Maximum number of results to return")
 
29
  results: List[SearchResult] = Field(..., description="List of search results")
30
  query: str = Field(..., description="The original search query")
31
 
32
+ class SearchState(BaseModel):
33
+ input: SearchInput
34
+ output: SearchOutput = None
35
+
36
  def create_calculator_tool() -> Graph:
37
  """Creates a calculator tool using LangGraph that can perform basic arithmetic operations."""
38
 
39
+ def calculator_function(state: CalculatorState) -> CalculatorState:
40
+ if len(state.input.numbers) < 2:
 
 
41
  raise ValueError("At least two numbers are required for calculation")
42
 
43
+ result = state.input.numbers[0]
44
 
45
+ for num in state.input.numbers[1:]:
46
+ if state.input.operation == "add":
47
  result += num
48
+ elif state.input.operation == "subtract":
49
  result -= num
50
+ elif state.input.operation == "multiply":
51
  result *= num
52
+ elif state.input.operation == "divide":
53
  if num == 0:
54
  raise ValueError("Cannot divide by zero")
55
  result /= num
56
  else:
57
+ raise ValueError(f"Unsupported operation: {state.input.operation}")
58
 
59
+ state.output = CalculatorOutput(
60
  result=result,
61
+ operation=state.input.operation
62
  )
63
 
64
+ return state
65
 
66
  # Create the graph with state schema
67
+ workflow = StateGraph(state_schema=CalculatorState)
68
 
69
  # Add the calculator tool node
70
  workflow.add_node("calculator", ToolNode(calculator_function))
 
78
  def create_search_tool() -> Graph:
79
  """Creates a search tool using DuckDuckGo that can search for information online."""
80
 
81
+ def search_function(state: SearchState) -> SearchState:
 
 
82
  # Initialize DuckDuckGo search
83
  with DDGS() as ddgs:
84
  # Perform the search
85
  search_results = list(ddgs.text(
86
+ state.input.query,
87
+ max_results=state.input.max_results
88
  ))
89
 
90
  # Convert results to our model
 
97
  for result in search_results
98
  ]
99
 
100
+ state.output = SearchOutput(
101
  results=results,
102
+ query=state.input.query
103
  )
104
 
105
+ return state
106
 
107
  # Create the graph with state schema
108
+ workflow = StateGraph(state_schema=SearchState)
109
 
110
  # Add the search tool node
111
  workflow.add_node("search", ToolNode(search_function))