T-K-O-H commited on
Commit
8130aa2
·
1 Parent(s): abd0d1f

Rename search_web to weather_tool and make it weather-specific

Browse files
Files changed (1) hide show
  1. app.py +20 -10
app.py CHANGED
@@ -48,17 +48,27 @@ app = FastAPI()
48
 
49
  # Define tools
50
  @tool
51
- def search_web(query: str) -> str:
52
- """Search the web for information on a given query."""
53
- logger.info(f"Searching web for query: {query}")
54
  try:
55
  search = DuckDuckGoSearchAPIWrapper()
56
- results = search.run(query)
57
- logger.info(f"Search results: {results}")
58
- return results
 
 
 
 
 
 
 
 
 
 
59
  except Exception as e:
60
- logger.error(f"Error in web search: {str(e)}")
61
- return f"Error searching the web: {str(e)}"
62
 
63
 
64
  @tool
@@ -96,7 +106,7 @@ def generate_image_prompt(description: str) -> str:
96
 
97
  # Create tools list
98
  logger.info("Creating tools list...")
99
- tools = [search_web, calculate, generate_image_prompt]
100
 
101
  # Set up the language model
102
  logger.info("Initializing ChatOpenAI model...")
@@ -145,7 +155,7 @@ def create_agent_node():
145
  # System prompt for the agent
146
  system_prompt = """You are a helpful AI assistant with access to the following tools:
147
 
148
- 1. search_web: Search the web for information
149
  2. calculate: Calculate the result of mathematical expressions
150
  3. generate_image_prompt: Generate prompts for image generation
151
 
 
48
 
49
  # Define tools
50
  @tool
51
+ def weather_tool(query: str) -> str:
52
+ """Get weather information for a specific location or query."""
53
+ logger.info(f"Getting weather information for: {query}")
54
  try:
55
  search = DuckDuckGoSearchAPIWrapper()
56
+ # Focus the search on weather information
57
+ weather_query = f"current weather {query} site:weather.com OR site:accuweather.com"
58
+ results = search.run(weather_query)
59
+
60
+ if not results:
61
+ # If no specific weather results, try a more general weather search
62
+ results = search.run(f"weather {query}")
63
+
64
+ if not results:
65
+ return f"I couldn't find weather information for '{query}'. Please specify a location or try rephrasing your question."
66
+
67
+ logger.info(f"Weather results: {results}")
68
+ return f"Here's the weather information for {query}:\n\n{results}"
69
  except Exception as e:
70
+ logger.error(f"Error getting weather information: {str(e)}")
71
+ return f"Error getting weather information: {str(e)}"
72
 
73
 
74
  @tool
 
106
 
107
  # Create tools list
108
  logger.info("Creating tools list...")
109
+ tools = [weather_tool, calculate, generate_image_prompt]
110
 
111
  # Set up the language model
112
  logger.info("Initializing ChatOpenAI model...")
 
155
  # System prompt for the agent
156
  system_prompt = """You are a helpful AI assistant with access to the following tools:
157
 
158
+ 1. weather_tool: Get weather information
159
  2. calculate: Calculate the result of mathematical expressions
160
  3. generate_image_prompt: Generate prompts for image generation
161