LangChain Chains and Agents for Budilding LLM Applications

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 for Building Applications

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.


Understanding Chains

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.

Sequential Chain Example

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 for selecting the geographic region to get the famous dish in that location
  • Chain 2 for providing the recipe
  • Chain 3 for estimating the cooking time

Building Chain 1

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.

Implementation Steps

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)

Building Chain 2

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.

Implementation Steps

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)

Building Chain 3

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.

Implementation Steps

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)

Combining Chains

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.


Memory Storage in LangChain

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.

Memory Read and Write Process

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.

Chat Message 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.

Implementation Example

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

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.

Agent Capabilities

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.

Pandas DataFrame Agent Example

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.


Conclusion

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.


FAQs

LangChain is a platform embedded with APIs to develop applications, empowering them to infuse language processing capabilities.

Chains in LangChain are a sequence of calls where each step takes one input to generate one output, creating a seamless flow of information. The output from one step becomes the input for the next step.

LangChain uses documents, chains, and agents for building applications.

The three steps are:

  • Define a template string for the prompt
  • Create a prompt template using the defined template
  • Create an LLM chain object

The sequential chain example aims to identify the recipe and the estimated cooking time for a famous dish available in a specified location by chaining three individual steps together.

Chain 1 selects the geographic region to get a famous dish in that location. For example, if the location is China, the output should be a famous dish like Peking Duck.

  1. Memory stores only the final output of chains
  2. Memory reads historical data to enhance user inputs and writes current runs back for continuity
  3. Memory replaces the need for prompt templates
  4. Memory is only used for error handling
(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.

The ChatMessageHistory class in LangChain is designed to manage and store conversation histories effectively, including human messages and AI messages.

  1. The chain will execute faster
  2. A clear and detailed view of how each input is transformed through the chain will be provided
  3. The memory will be cleared automatically
  4. The chain will skip intermediate steps
(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.

Agents in LangChain are dynamic systems where a language model determines and sequences actions. 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.

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.

The create_pandas_dataframe_agent allows users to query and visualize data with natural language by transforming queries into Python code that is executed in the background.

  1. Sequential chains only
  2. ChatMessageHistory class for memory storage
  3. Prompt templates only
  4. Output parsers
(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.

ComponentDescription
A. Chain 11. Estimates cooking time based on recipe
B. Chain 22. Selects famous dish based on location
C. Chain 33. Creates unified process from individual chains
D. Sequential Chain4. Provides recipe for the dish
A-2, B-4, C-1, D-3.

  1. Agents are dynamic systems
  2. Language models determine and sequence actions
  3. Agents directly execute all actions without external tools
  4. Agents integrate with search engines and databases
(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.

  1. Memory is only read at the beginning of execution
  2. Context preservation across interactions enables conversational capabilities
  3. Memory slows down chain execution significantly
  4. Memory stores only error messages
(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.

  1. The color scheme of the output
  2. The LLM chat model and data frame are passed to create_pandas_dataframe_agent
  3. The number of chains in the sequence
  4. The memory storage capacity
(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.

  1. Define a template string for the prompt
  2. Create a prompt template using the defined template
  3. Manually execute Python code for each step
  4. Create an LLM chain object
(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.