In this blog post, I will show you how to modify the example of my last blog post Implementing LangChain AI Agent with WatsonxLLM for a Weather Queries application to create a dependency graph from the runnable chains to get a better understanding of each chain.
Table of content
- Objective
- Introduction
- Steps to customize the existing example source code
- Add the graph to the simple chain
- Add the graph to the agent chain
- Run the example
- Additional Resources
- Summary
1. Objective
Add a dependency graph to our runnable agent chain to get a better understanding of the dependencies of the components used inside the chain.
The source code is available in this GitHub repository: https://github.com/thomassuedbroecker/agent-tools-langchain-watsonx
2. Introduction
In the last blog post, we did the example implementation of a runnable agent chain with a LangChain AI Agent, WatsonxLLM, for a Weather Queries application. Now, we can use the graph functionality to display the differences between the simple chain for question answering and the runnable agent chain for the Weather Query Application. You can find the details in the section Inspect your runnables from LangChain.
chain.get_graph().print_ascii()
LangChain provides a function to show this chain as a graph to understand the dependencies of chains better. To use this functionality, you must add the Graph and drawing algorithms framework library to the Python application.
Example graph:
+----------------------------------------------+
| Parallel<agent_scratchpad,chat_history>Input |
+----------------------------------------------+
*** * ***
**** * ****
** * **
+--------+ +--------+ +-------------+
| Lambda |* | Lambda | | Passthrough |
+--------+ **** +--------+ *+-------------+
*** * ***
**** * ****
** * **
+-----------------------------------------------+
| Parallel<agent_scratchpad,chat_history>Output |
+-----------------------------------------------+
*
*
*
+--------------------+
| ChatPromptTemplate |
+--------------------+
*
*
*
+------------+
| WatsonxLLM |
+------------+
*
*
*
+-----------------------+
| JSONAgentOutputParser |
+-----------------------+
*
*
*
+-----------------------------+
| JSONAgentOutputParserOutput |
+-----------------------------+
3. Steps to customize the existing example source code
# show the agent chain as graph
python3 -m pip install grandalf
Step 1: Add the graph to the simple chain
We use the terminal output functionality for the graph creation.
print(f"5. Create a simple chain with the created prompt and the create watsonx_client.\n")
simple_chain = prompt | watsonx_client
print(f"6. Show the 'simple chain' graph dependencies\n")
simple_chain.get_graph().print_ascii()
Step 2: Add the graph to the agent chain
We use the terminal output functionality for the graph creation.
agent_chain = ( RunnablePassthrough.assign(
agent_scratchpad=lambda x: format_log_to_str(x["intermediate_steps"]),
chat_history=lambda x: memory.chat_memory.messages,
)
| agent_chat_incl_tools_prompt |watsonx_client | JSONAgentOutputParser()
)
print(f"15. Define an agent chain, including a runnable passthrough\n{agent_chain}\n")
print(f"16. Show the 'agent chain' graph dependencies\n")
agent_chain.get_graph().print_ascii()
4. Run the example
Follow the steps in the GitHub repository: https://github.com/thomassuedbroecker/agent-tools-langchain-watsonx
Here is the example invocation and output:
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
...
5. Additional Resources
- LangChain Runnable interface
- LangChain Inspect your runnables
6. Summary
The graph creation is a fantastic out-of-the-box functionality that helps us better understand the dependencies of chains.
I hope this was useful to you and let’s see what’s next?
Greetings,
Thomas
#llm, #langchain, #ai, #opensource, #aiagent, #python, #graph, #watsonxllm

Leave a comment