This document describes chains in LangChain for generating responses, memory storage mechanisms, and agents for dynamic action sequencing to build sophisticated LLM applications.
This document explores LangChain chains as sequences of calls that create seamless information flows, demonstrates sequential chain implementation through practical examples, explains memory storage mechanisms for preserving conversation context, and introduces agents as dynamic systems that leverage language models with external tools to autonomously fulfill complex user requests.
LangChain is a platform embedded with APIs to develop applications, empowering them to infuse language processing capabilities. Developers find LangChain suitable for building applications due to its comprehensive toolset and integration capabilities.
LangChain uses certain tools, including documents, chains, and agents for building applications. These components work together to create powerful and flexible AI-powered solutions that can handle complex tasks and workflows.
In LangChain, chains are a sequence of calls that enable structured processing of information. A sequential chain consists of basic steps where each step takes one input to generate one output to create one seamless flow of information. The output from Step 1 becomes the input for Step 2, creating a logical progression through the workflow.
The following example demonstrates the creation of a sequential chain of three individual chains. The aim of this chain is to identify the recipe and the estimated cooking time for the famous dish available in the inserted location.
The implementation uses three chains working together:
Chain 1 in the sequence uses a user’s prompt as input for a specific dish based on the user-specified location. For example, if the location is China, the output for Chain 1 should be a famous dish in China, such as Peking Duck.
The code to create this chain follows these steps:
First, define a template string for the prompt asking for a specific dish from a specified location. Create a prompt template object using the defined template, specifying the input variable as location. Then create an LLM chain object named location_chain using an LLM-based language model, such as Mixtral LLM. Before running this code, the chat model object must be available. The output will be stored under the key meal.
1# Define template for Chain 1
2template = "What is a famous dish from {location}?"
3prompt_template = PromptTemplate(template=template, input_variables=["location"])
4
5# Create LLM chain
6location_chain = LLMChain(
7 llm=mixtral_llm,
8 prompt=prompt_template,
9 output_key="meal"
10)
In the second chain of the sequential setup, the output from the first chain (the name of the dish) becomes the input. The output from this chain will be the recipe itself.
First, define the template asking for a simple recipe for a given meal. Next, create a prompt template with meal as the input variable. Finally, create an LLM chain named dish_chain using Mixtral LLM and prompt template with the output key recipe.
1# Define template for Chain 2
2template = "Provide a simple recipe for {meal}"
3prompt_template = PromptTemplate(template=template, input_variables=["meal"])
4
5# Create LLM chain
6dish_chain = LLMChain(
7 llm=mixtral_llm,
8 prompt=prompt_template,
9 output_key="recipe"
10)
Chain 3 takes the recipe obtained from the second chain as input. This chain is designed to estimate the cooking time for the meal based on the recipe.
Like Chain 2, define a template to estimate the cooking time for a given recipe. Next, create a prompt template with recipe as the input variable. Lastly, create an LLM chain named recipe_chain using Mixtral LLM and prompt template with the output_key time.
1# Define template for Chain 3
2template = "Estimate the cooking time for this recipe: {recipe}"
3prompt_template = PromptTemplate(template=template, input_variables=["recipe"])
4
5# Create LLM chain
6recipe_chain = LLMChain(
7 llm=mixtral_llm,
8 prompt=prompt_template,
9 output_key="time"
10)
Using the three individual chains, the overall setup is a sequential chain that wraps all the individual chains together, creating a unified process. By invoking the query through this combined chain, the flow of information can be traced from start to end.
The verbose option can be set to true to view the overall output. This provides a clear and detailed view of how each input is transformed through the chain into the final output. The example demonstrates how chains can be composed to create complex multi-step workflows.
In LangChain, memory storage is important for reading and writing historical data. Each chain relies on specific input, such as user input and memory. Understanding how memory works is crucial for building conversational applications.
Chain reads from memory to enhance user inputs before executing its core logic. After execution, the chain writes the current run’s inputs and outputs back to the memory. This ensures continuity and context preservation across interactions, enabling the application to maintain conversation history.
The ChatMessageHistory class in LangChain is designed to manage and store conversation histories effectively, including human messages and AI messages. This allows adding messages from the AI and users to the history.
The following example demonstrates memory usage:
1# Call ChatMessageHistory class
2history = ChatMessageHistory()
3
4# Add AI message into memory
5history.add_ai_message("Hi")
6
7# Add user's message
8history.add_user_message("What is the capital of France?")
The memory will append the AI message as input, then append the user’s message as human message input. Responses are generated based on the stored memory, maintaining conversation context.
Agents in LangChain are dynamic systems where a language model determines and sequences actions such as pre-defined chains. The model generates text outputs to guide actions, but does not execute them directly.
Agents integrate with tools such as search engines, databases, and websites to fulfill user requests. For example, if a user asks for the population of Italy, the agent uses the language model to find options, query a database for details, and return a curated list. This shows the agent’s ability to autonomously leverage LLM reasoning with external tools.
The following example creates a Pandas DataFrame agent using LangChain. This agent allows users to query and visualize data with natural language.
1# Import necessary class
2from langchain.agents import create_pandas_dataframe_agent
3
4# Instantiate the agent
5agent = create_pandas_dataframe_agent(
6 llm=chat_model,
7 df=dataframe,
8 verbose=True
9)
10
11# Execute query
12response = agent.invoke("How many rows in the data frame?")
To set it up, instantiate the create_pandas_dataframe_agent class. In this class, pass the LLM chat model and the data frame. Set verbose to true to see how the LLM thinks. Use the invoke method to execute the query.
The LLM transforms queries into Python code, executed in the background, enabling precise answers to questions about the DataFrame. For example, the response might show there are 139 rows in the DataFrame.
LangChain is a platform that embeds APIs for developing applications with language processing capabilities. Chains represent sequences of calls where the output from one step becomes the input for the next step, creating seamless workflows. In LangChain, chains are built by defining template strings for prompts, creating prompt templates using the defined templates, and creating LLM chain objects. Memory storage is important for reading and writing historical data, with the ChatMessageHistory class managing conversation histories effectively. Agents in LangChain are dynamic systems where a language model determines and sequences actions, integrating with tools such as search engines, databases, and websites to autonomously fulfill user requests.
The three steps are:
(2) Memory storage is important for reading and writing historical data. Chains read from memory to enhance user inputs before executing core logic and write current inputs and outputs back to ensure continuity and context preservation across interactions.
(2) Setting verbose to true provides a clear and detailed view of how each input is transformed through the chain into the final output, helping developers understand the flow of information.
In a sequential chain, the output from one step becomes the input for the next step.
True. Sequential chains consist of basic steps where each step takes one input to generate one output, and the output from Step 1 becomes the input for Step 2, creating a seamless flow of information.
(2) The ChatMessageHistory class should be implemented to manage and store conversation histories effectively, allowing the application to track both human messages and AI messages across interactions.
| Component | Description |
|---|---|
| A. Chain 1 | 1. Estimates cooking time based on recipe |
| B. Chain 2 | 2. Selects famous dish based on location |
| C. Chain 3 | 3. Creates unified process from individual chains |
| D. Sequential Chain | 4. Provides recipe for the dish |
A-2, B-4, C-1, D-3.
(3) This is incorrect. Agents generate text outputs to guide actions but do not execute them directly. Instead, they integrate with external tools such as search engines, databases, and websites to fulfill user requests.
(2) The process of reading from memory to enhance inputs and writing back current runs ensures continuity and context preservation across interactions, which is essential for building conversational applications.
(2) When setting up a Pandas DataFrame agent, the first priority is to ensure that the LLM chat model and the data frame are properly passed to the create_pandas_dataframe_agent class for instantiation.
The ChatMessageHistory class can add both AI messages and user messages to the conversation history.
True. The ChatMessageHistory class is designed to manage and store conversation histories effectively by allowing the addition of messages from both the AI and users to the history.
(3) Manually executing Python code for each step is not part of the standard chain building process. The three steps are: defining a template string, creating a prompt template, and creating an LLM chain object.