1+ import  os , warnings , requests 
2+ warnings .filterwarnings ('ignore' )
3+ from  dotenv  import  load_dotenv 
4+ from  langchain_core .tools  import  tool 
5+ from  langchain .chat_models  import  ChatOpenAI 
6+ from  langchain .agents  import  create_react_agent , AgentExecutor 
7+ from  langchain_community .tools  import  DuckDuckGoSearchRun 
8+ from  langchain  import  hub 
9+ 
10+ load_dotenv ()
11+ openai_api_key  =  os .getenv ('OPENAI_API_KEY' )
12+ weatherstack_api_key  =  os .getenv ('WEATHERSTACK_API_KEY' )
13+ 
14+ llm  =  ChatOpenAI (api_key = openai_api_key )
15+ search_tool  =  DuckDuckGoSearchRun ()
16+ 
17+ @tool  
18+ def  get_weather_data (city : str ) ->  str :
19+  """ 
20+  This function fetches the current weather data for a given city. 
21+  """ 
22+  response  =  requests .get (f"https://api.weatherstack.com/current?access_key={ weatherstack_api_key } { city }  )
23+  return  response .json ()
24+ 
25+ # Pull the ReAct prompt from LangChain Hub 
26+ prompt  =  hub .pull ("hwchase17/react" )
27+ 
28+ # Create a ReAct agent with the pulled prompt 
29+ agent  =  create_react_agent (
30+  llm = llm ,
31+  tools = [search_tool , get_weather_data ],
32+  prompt = prompt 
33+ )
34+ 
35+ # Wrap it with AgentExecutor 
36+ agent_executor  =  AgentExecutor .from_agent_and_tools (
37+  agent = agent ,
38+  tools = [search_tool , get_weather_data ],
39+  verbose = True 
40+ )
41+ 
42+ query  =  "Find the capital of Scotland and find its current weather condition." 
43+ response  =  agent_executor .invoke ({"input" : query })
44+ print (response ['output' ])
0 commit comments