Skip to content

Commit feecb72

Browse files
committed
Add basic and weather agent implementations with ReAct pattern and API integration
1 parent eec1d5e commit feecb72

File tree

2 files changed

+84
-0
lines changed

2 files changed

+84
-0
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
from langchain.chat_models import ChatOpenAI
2+
from langchain_core.tools import tool
3+
from langchain_community.tools import DuckDuckGoSearchRun
4+
from dotenv import load_dotenv
5+
from langchain.agents import create_react_agent, AgentExecutor
6+
from langchain import hub
7+
import os, warnings, requests
8+
warnings.filterwarnings('ignore')
9+
10+
load_dotenv()
11+
openai_api_key = os.getenv('OPENAI_API_KEY')
12+
13+
llm = ChatOpenAI(api_key=openai_api_key)
14+
15+
search_tool = DuckDuckGoSearchRun()
16+
17+
results = search_tool.invoke("top news in football today")
18+
# print(results)
19+
20+
# Pull the ReAct prompt from the LangChain Hub
21+
prompt = hub.pull("hwchase17/react") # pulls the standard react agent prompt
22+
23+
# ReAct = Reasoning + Acting It's a design pattern
24+
# Create a ReAct agent with the pulled prompt
25+
agent = create_react_agent(
26+
llm=llm,
27+
tools=[search_tool],
28+
prompt=prompt
29+
)
30+
31+
# Wrap the agent with an executor
32+
agent_executor = AgentExecutor(
33+
agent=agent,
34+
tools=[search_tool],
35+
verbose=True
36+
)
37+
38+
query = "Give a detailed overview of the results of today's IPL match between MI and RR"
39+
result = agent_executor.invoke({"input": query})
40+
print(result['output'])
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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}&query={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

Comments
 (0)