This repository implements a weather query example application of an agent, including function calling based on the IBM Developer Blog Post Create a LangChain AI Agent in Python using watsonx. Therefore, the endpoint https://wttr.in/ is used as the resource for the weather information.
Related blog post: Implementing LangChain AI Agent with WatsonxLLM for a Weather Queries application
git clone https://github.com/thomassuedbroecker/temp-agent-tools-langchain-watsonx.git
cd agent-tools-langchain-watsonx
cd code
python3 -m venv --upgrade-deps venv
source venv/bin/activate
python3 -m pip install -qU langchain-ibm
python3 -m pip install -qU langchain
python3 -m pip install langchain_core
# show the agent chain as graph
python3 -m pip install grandalf
cat env_example_template > .env
Insert the values for the two environment variables:
WATSONX_PROJECT_ID=YOUR_WATSONX_PROJECT_ID
IBMCLOUD_APIKEY=YOUR_KEY
Content of the environment file.
export IBMCLOUD_APIKEY=YOUR_KEY
export IBMCLOUD_URL="https://iam.cloud.ibm.com/identity/token"
# Watsonx
export WATSONX_URL="https://eu-de.ml.cloud.ibm.com"
export WATSONX_VERSION=2023-05-29
export WATSONX_PROJECT_ID=YOUR_PROJECT_ID
export WATSONX_MIN_NEW_TOKENS=1
export WATSONX_MAX_NEW_TOKENS=300
export WATSONX_LLM_NAME=ibm/granite-13b-chat-v2
export WATSONX_INSTANCE_ID=YOUR_WATSONX_INSTANCE_ID
export WATSONX_USERNAME="exampe_user@exampl.net"
cd code
bash example_agent_invocation.sh
- Output
> Entering new AgentExecutor chain...
Action:
...json
{
"action": "weather_service",
"action_input": {
"cities": ["LA", "NY"]
}
}
...
Weather service response:
...json
{
"result": "LA",
"cities": ["LA", "NY"]
}
...
[{'city': 'NY', 'temperature': '14'}, {'city': 'LA', 'temperature': '12'}]
The hottest city is NY with a temperature or 14 degrees Celsius. In the city LA the temperature is 12 degrees Celsius.
Action:
...json
{
"action": "Final Answer",
"action_input": "The hottest city is NY with a temperature of 14 degrees Celsius."
}
...
> Finished chain.
16.a Response of the agent executor
{'input': 'Which city is hotter today: LA or NY?', 'history': '', 'output': 'The hottest city is NY with a temperature of 14 degrees Celsius.'}
> Entering new AgentExecutor chain...
{
"action": "Final Answer",
"action_input": "The temperature in Berlin is 10 degrees Celsius."
}
> Finished chain.
16.b Response of the agent executor
{'input': 'How hot is it today in Berlin?', 'history': 'Human: Which city is hotter today: LA or NY?\nAI: The hottest city is NY with a temperature of 14 degrees Celsius.', 'output': 'The temperature in Berlin is 10 degrees Celsius.'}
> Entering new AgentExecutor chain...
{
"action": "Final Answer",
"action_input": "Weather is a phenomenon that occurs in the atmosphere, primarily near the Earth's surface, and is characterized by a combination of temperature, humidity, precipitation, and wind."
}
> Finished chain.
16.c Response of the agent executor
{'input': 'What is the offical definition of the term weather?', 'history': 'Human: Which city is hotter today: LA or NY?\nAI: The hottest city is NY with a temperature of 14 degrees Celsius.\nHuman: How hot is it today in Berlin?\nAI: The temperature in Berlin is 10 degrees Celsius.', 'output': "Weather is a phenomenon that occurs in the atmosphere, primarily near the Earth's surface, and is characterized by a combination of temperature, humidity, precipitation, and wind."}
> Entering new AgentExecutor chain...
{
"action": "Final Answer",
"action_input": "The only way to win a soccer game is to score more goals than the opposing team."
}
> Finished chain.
16.d Response of the agent executor
{'input': 'How to win a soccer game?', 'history': "Human: Which city is hotter today: LA or NY?\nAI: The hottest city is NY with a temperature of 14 degrees Celsius.\nHuman: How hot is it today in Berlin?\nAI: The temperature in Berlin is 10 degrees Celsius.\nHuman: What is the offical definition of the term weather?\nAI: Weather is a phenomenon that occurs in the atmosphere, primarily near the Earth's surface, and is characterized by a combination of temperature, humidity, precipitation, and wind.", 'output': 'The only way to win a soccer game is to score more goals than the opposing team.'}
cd code
bash example_agent_graph_invocation.sh
- Output
...
6. Show the 'simple chain' dependencies graph
+-------------+
| PromptInput |
+-------------+
*
*
*
+----------------+
| PromptTemplate |
+----------------+
*
*
*
+------------+
| WatsonxLLM |
+------------+
*
*
*
+------------------+
| WatsonxLLMOutput |
+------------------+
7. Invoke the simple chain by asking a question.
....
16. Show the 'agent chain' dependencies graph
+----------------------------------------------+
| Parallel<agent_scratchpad,chat_history>Input |
+----------------------------------------------+
*** * ***
**** * ****
** * **
+--------+ +--------+ +-------------+
| Lambda |* | Lambda | | Passthrough |
+--------+ **** +--------+ *+-------------+
*** * ***
**** * ****
** * **
+-----------------------------------------------+
| Parallel<agent_scratchpad,chat_history>Output |
+-----------------------------------------------+
*
*
*
+--------------------+
| ChatPromptTemplate |
+--------------------+
*
*
*
+------------+
| WatsonxLLM |
+------------+
*
*
*
+-----------------------+
| JSONAgentOutputParser |
+-----------------------+
*
*
*
+-----------------------------+
| JSONAgentOutputParserOutput |
+-----------------------------+
17. Create an agent executor
...