naman1102 commited on
Commit
4beca24
·
1 Parent(s): a22806f
Files changed (2) hide show
  1. app.py +7 -7
  2. tools.py +12 -4
app.py CHANGED
@@ -50,7 +50,7 @@ class BasicAgent:
50
  """Create the agent workflow using LangGraph."""
51
  # Create the workflow with state schema
52
  print("Creating Stategraph : error happens here?")
53
- workflow = StateGraph(state_schema=Dict[str, Any])
54
  print("Stategraph created")
55
  # Add nodes
56
  workflow.add_node("analyze", self._analyze_question)
@@ -66,7 +66,7 @@ class BasicAgent:
66
  workflow.add_edge("search", "final_answer")
67
 
68
  # Define conditional edges
69
- def router(state: Dict[str, Any]) -> str:
70
  if state["current_step"] == 'calculator':
71
  return 'calculator'
72
  elif state["current_step"] == 'search':
@@ -105,7 +105,7 @@ class BasicAgent:
105
  print(f"Error calling LLM API: {e}")
106
  return f"Error getting response from LLM: {str(e)}"
107
 
108
- def _analyze_question(self, state: Dict[str, Any]) -> Dict[str, Any]:
109
  """Analyze the question and determine the next step."""
110
  prompt = f"""Analyze this question and determine what needs to be done: {state['question']}
111
  Return your analysis in this format:
@@ -134,7 +134,7 @@ class BasicAgent:
134
 
135
  return state
136
 
137
- def _use_calculator(self, state: Dict[str, Any]) -> Dict[str, Any]:
138
  """Use the calculator tool."""
139
  try:
140
  # Create calculator state with input from tool_output
@@ -155,7 +155,7 @@ class BasicAgent:
155
  state["current_step"] = 'final_answer'
156
  return state
157
 
158
- def _use_search(self, state: Dict[str, Any]) -> Dict[str, Any]:
159
  """Use the search tool."""
160
  try:
161
  # Create search state with input from search_query
@@ -180,7 +180,7 @@ class BasicAgent:
180
  state["current_step"] = 'final_answer'
181
  return state
182
 
183
- def _generate_final_answer(self, state: Dict[str, Any]) -> Dict[str, Any]:
184
  """Generate the final answer based on all gathered information."""
185
  history_str = "\n".join([f"{h['step']}: {h.get('output', h.get('results', h.get('error', '')))}"
186
  for h in state["history"]])
@@ -202,7 +202,7 @@ class BasicAgent:
202
 
203
  try:
204
  # Initialize the state
205
- initial_state = {
206
  "question": question,
207
  "current_step": "analyze",
208
  "tool_output": "",
 
50
  """Create the agent workflow using LangGraph."""
51
  # Create the workflow with state schema
52
  print("Creating Stategraph : error happens here?")
53
+ workflow = StateGraph(state_schema=AgentState)
54
  print("Stategraph created")
55
  # Add nodes
56
  workflow.add_node("analyze", self._analyze_question)
 
66
  workflow.add_edge("search", "final_answer")
67
 
68
  # Define conditional edges
69
+ def router(state: AgentState) -> str:
70
  if state["current_step"] == 'calculator':
71
  return 'calculator'
72
  elif state["current_step"] == 'search':
 
105
  print(f"Error calling LLM API: {e}")
106
  return f"Error getting response from LLM: {str(e)}"
107
 
108
+ def _analyze_question(self, state: AgentState) -> AgentState:
109
  """Analyze the question and determine the next step."""
110
  prompt = f"""Analyze this question and determine what needs to be done: {state['question']}
111
  Return your analysis in this format:
 
134
 
135
  return state
136
 
137
+ def _use_calculator(self, state: AgentState) -> AgentState:
138
  """Use the calculator tool."""
139
  try:
140
  # Create calculator state with input from tool_output
 
155
  state["current_step"] = 'final_answer'
156
  return state
157
 
158
+ def _use_search(self, state: AgentState) -> AgentState:
159
  """Use the search tool."""
160
  try:
161
  # Create search state with input from search_query
 
180
  state["current_step"] = 'final_answer'
181
  return state
182
 
183
+ def _generate_final_answer(self, state: AgentState) -> AgentState:
184
  """Generate the final answer based on all gathered information."""
185
  history_str = "\n".join([f"{h['step']}: {h.get('output', h.get('results', h.get('error', '')))}"
186
  for h in state["history"]])
 
202
 
203
  try:
204
  # Initialize the state
205
+ initial_state: AgentState = {
206
  "question": question,
207
  "current_step": "analyze",
208
  "tool_output": "",
tools.py CHANGED
@@ -11,6 +11,10 @@ class CalculatorOutput(TypedDict):
11
  result: float
12
  operation: str
13
 
 
 
 
 
14
  class SearchResult(TypedDict):
15
  title: str
16
  link: str
@@ -20,11 +24,15 @@ class SearchOutput(TypedDict):
20
  results: List[SearchResult]
21
  query: str
22
 
 
 
 
 
23
  def create_calculator_tool() -> Graph:
24
  """Creates a calculator tool using LangGraph that can perform basic arithmetic operations."""
25
  print("Creating calculator tool")
26
 
27
- def calculator_function(state: Dict[str, Any]) -> dict:
28
  print("Calculator function called")
29
  input_data = state["input"]
30
  if len(input_data["numbers"]) < 2:
@@ -54,7 +62,7 @@ def create_calculator_tool() -> Graph:
54
  }
55
 
56
  # Create the graph with state schema
57
- workflow = StateGraph(state_schema=Dict[str, Any])
58
  print("Calculator graph for workflow created")
59
  # Add the calculator tool node
60
  workflow.add_node("calculator", ToolNode(calculator_function))
@@ -68,7 +76,7 @@ def create_calculator_tool() -> Graph:
68
  def create_search_tool() -> Graph:
69
  """Creates a search tool using DuckDuckGo that can search for information online."""
70
 
71
- def search_function(state: Dict[str, Any]) -> dict:
72
  with DDGS() as ddgs:
73
  raw_results = list(ddgs.text(
74
  state["input"]["query"],
@@ -94,7 +102,7 @@ def create_search_tool() -> Graph:
94
  }
95
 
96
  # Create the graph with state schema
97
- workflow = StateGraph(state_schema=Dict[str, Any])
98
 
99
  # Add the search tool node
100
  workflow.add_node("search", ToolNode(search_function))
 
11
  result: float
12
  operation: str
13
 
14
+ class CalculatorState(TypedDict):
15
+ input: CalculatorInput
16
+ output: Optional[CalculatorOutput]
17
+
18
  class SearchResult(TypedDict):
19
  title: str
20
  link: str
 
24
  results: List[SearchResult]
25
  query: str
26
 
27
+ class SearchState(TypedDict):
28
+ input: Dict[str, Any] # Contains query and max_results
29
+ output: Optional[SearchOutput]
30
+
31
  def create_calculator_tool() -> Graph:
32
  """Creates a calculator tool using LangGraph that can perform basic arithmetic operations."""
33
  print("Creating calculator tool")
34
 
35
+ def calculator_function(state: CalculatorState) -> Dict[str, Any]:
36
  print("Calculator function called")
37
  input_data = state["input"]
38
  if len(input_data["numbers"]) < 2:
 
62
  }
63
 
64
  # Create the graph with state schema
65
+ workflow = StateGraph(state_schema=CalculatorState)
66
  print("Calculator graph for workflow created")
67
  # Add the calculator tool node
68
  workflow.add_node("calculator", ToolNode(calculator_function))
 
76
  def create_search_tool() -> Graph:
77
  """Creates a search tool using DuckDuckGo that can search for information online."""
78
 
79
+ def search_function(state: SearchState) -> Dict[str, Any]:
80
  with DDGS() as ddgs:
81
  raw_results = list(ddgs.text(
82
  state["input"]["query"],
 
102
  }
103
 
104
  # Create the graph with state schema
105
+ workflow = StateGraph(state_schema=SearchState)
106
 
107
  # Add the search tool node
108
  workflow.add_node("search", ToolNode(search_function))