rsynak commited on
Commit
f58647f
·
verified ·
1 Parent(s): 82ff939

update app.py

Browse files
Files changed (1) hide show
  1. app.py +10 -6
app.py CHANGED
@@ -3,13 +3,14 @@ import datetime
3
  import requests
4
  import pytz
5
  import yaml
 
6
  from tools.final_answer import FinalAnswerTool
7
 
8
  from Gradio_UI import GradioUI
9
 
10
  @tool
11
  def fetch_state_capital_and_altitude(state: str) -> str:
12
- """A tool that fetches the capital and altitude of a US state using DuckDuckGo search.
13
  Args:
14
  state: A US state name.
15
  """
@@ -18,14 +19,17 @@ def fetch_state_capital_and_altitude(state: str) -> str:
18
  capital_query = f"What is the capital of {state}?"
19
  capital_result = duckduckgo_tool(capital_query)
20
 
21
- # Extract a simple capital name from the result
22
- # In a real-world case, you'd want better parsing here
23
- capital = capital_result[:100] # crude truncation for demonstration
24
 
25
  # Query altitude
26
  altitude_query = f"What is the altitude of {capital}?"
27
  altitude_result = duckduckgo_tool(altitude_query)
28
- altitude = altitude_result[:100] # crude again
 
 
 
29
 
30
  return f"The capital of {state} is {capital}. Its altitude is {altitude}."
31
  except Exception as e:
@@ -68,7 +72,7 @@ with open("prompts.yaml", 'r') as stream:
68
 
69
  agent = CodeAgent(
70
  model=model,
71
- tools=[final_answer, fetch_state_capital_and_altitude], ## add your tools here (don't remove final answer)
72
  max_steps=6,
73
  verbosity_level=1,
74
  grammar=None,
 
3
  import requests
4
  import pytz
5
  import yaml
6
+ import re
7
  from tools.final_answer import FinalAnswerTool
8
 
9
  from Gradio_UI import GradioUI
10
 
11
  @tool
12
  def fetch_state_capital_and_altitude(state: str) -> str:
13
+ """A tool that fetches the capital and altitude of a US state using DuckDuckGo search and regex.
14
  Args:
15
  state: A US state name.
16
  """
 
19
  capital_query = f"What is the capital of {state}?"
20
  capital_result = duckduckgo_tool(capital_query)
21
 
22
+ # Attempt to extract capital using a regex pattern
23
+ capital_match = re.search(r"The capital of [\w\s]+ is ([A-Z][a-zA-Z\s]+)", capital_result)
24
+ capital = capital_match.group(1).strip() if capital_match else "Unknown"
25
 
26
  # Query altitude
27
  altitude_query = f"What is the altitude of {capital}?"
28
  altitude_result = duckduckgo_tool(altitude_query)
29
+
30
+ # Attempt to extract altitude (look for digits followed by meters or feet)
31
+ altitude_match = re.search(r"(\d{2,5})\s?(meters|feet|m|ft)", altitude_result, re.IGNORECASE)
32
+ altitude = f"{altitude_match.group(1)} {altitude_match.group(2)}" if altitude_match else "Unknown"
33
 
34
  return f"The capital of {state} is {capital}. Its altitude is {altitude}."
35
  except Exception as e:
 
72
 
73
  agent = CodeAgent(
74
  model=model,
75
+ tools=[final_answer, fetch_state_capital_and_altitude, duckduckgo_tool], ## add your tools here (don't remove final answer)
76
  max_steps=6,
77
  verbosity_level=1,
78
  grammar=None,