keenthinker commited on
Commit
75454b9
·
verified ·
1 Parent(s): 3d5be75

Create WebSearch.py

Browse files
Files changed (1) hide show
  1. WebSearch.py +54 -0
WebSearch.py ADDED
@@ -0,0 +1,54 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # from langchain_community.tools import DuckDuckGoSearchRun
2
+
3
+ # search_tool = DuckDuckGoSearchRun()
4
+ # results = search_tool.invoke("Who's the current President of France?")
5
+ # print(results)
6
+
7
+ from langchain.tools import Tool
8
+ import random
9
+ import requests
10
+ import os
11
+ from dotenv import load_dotenv
12
+ load_dotenv()
13
+ weather_API_key = os.getenv("OPENWEATHER_KEY")
14
+ #print(f'weather_API_key: {weather_API_key}')
15
+ def get_weather_info(location: str) -> str:
16
+ """Fetches weather information for a given location."""
17
+ try:
18
+ base_url = "https://api.openweathermap.org/data/2.5/weather"
19
+ params = {
20
+ "q": location,
21
+ "appid": weather_API_key,
22
+ "units": "metric"
23
+ }
24
+ response = requests.get(base_url, params=params)
25
+ if response.status_code == 200:
26
+ data = response.json()
27
+ weather = data["weather"][0]["description"]
28
+ temp = data["main"]["temp"]
29
+ feels_like = data["main"]["feels_like"]
30
+ humidity = data["main"]["humidity"]
31
+ print(f"\nWeather in {location.title()}:")
32
+ print(f"- Condition: {weather}")
33
+ print(f"- Temperature: {temp}°C (feels like {feels_like}°C)")
34
+ print(f"- Humidity: {humidity}%")
35
+ data= {"condition": weather, "temp_c": temp,"humidity": humidity}
36
+ except requests.exceptions.RequestException as e:
37
+ print(f"Error: {e}")
38
+ # If the API call fails, we can use dummy data
39
+ # Dummy weather data
40
+ weather_conditions = [
41
+ {"condition": "Rainy", "temp_c": 15,"humidity": 30},
42
+ {"condition": "Clear", "temp_c": 25,"humidity": 50},
43
+ {"condition": "Windy", "temp_c": 20,"humidity": 80}
44
+ ]
45
+ # Randomly select a weather condition
46
+ data = random.choice(weather_conditions)
47
+ return f"Weather in {location}: {data['condition']}, {data['temp_c']}°C"
48
+
49
+ # Initialize the tool
50
+ weather_info_tool = Tool(
51
+ name="get_weather_info",
52
+ func=get_weather_info,
53
+ description="Fetches dummy weather information for a given location."
54
+ )