LangGraph Agent Simulation

Open in ColabOpen in GitHub

Overview

In this section, We'll simulate the user interactions with customer service scenario.

Before simulate, we have to define state, role, and simulation with LangGraph.

At last, we'll define graphs and visualize the structure we've made.

Table of Contents

References


Environment Setup

Setting up your environment is the first step. See the Environment Setup guide for more details.

[Note]

The langchain-opentutorial is a package of easy-to-use environment setup guidance, useful functions and utilities for tutorials. Check out the langchain-opentutorial for more details.

You can set API keys in a .env file or set them manually.

[Note] If you’re not using the .env file, no worries! Just enter the keys directly in the cell below, and you’re good to go.

Agent Conversation Simulation (Customer Support Scenario)

When building a chatbot, such as a customer support assistant, evaluating its performance can be challenging. Manually interacting with the chatbot for each code change is time-consuming.

One way to make the evaluation process easier and more reproducible is simulating user interactions.

Using LangGraph, setting this up is straightforward.

Below is an example of creating a "Simulated User" to simulate conversations.

Define State

Define Roles: Agent and Customer

Define Agent Role

Define the chatbot role acting as the agent in the simulation.

[ Note ]

  • The implementation inside call_chatbot is configurable, and the internal model used can be replaced with an Agent.

  • call_chatbot takes user messages as input and generates responses as a customer support agent.

It can be used to generate conversational responses in customer support scenarios.

call_chatbot processes user inputs and generates chatbot responses.

Define Simulated User Role

Define the role of the simulated customer. This simulates conversations in a customer support scenario.

The system prompt sets up the interaction between the customer and the support agent, with detailed user instructions defining the scenario.

This configuration is used to simulate the model's response to a specific user request, such as a refund request.

Create a hypothetical scenario. This hypothetical scenario is from the customer's perspective.

In here, we'll create a scenario for requesting a refund.

Use the generated simulated_user to send messages to the simulated user.

Define Agent Simulation

Now let's write the code to create a LangGraph workflow to run the simulation.

Here are the main components:

  1. Two nodes for the simulated user and the chatbot.

  2. A graph with conditional stop criteria.

Define Nodes

First, define nodes in the graph. These nodes take a list of messages as input and return a list of additional messages to be added to the state. They are wrappers that wrap around the chatbot and the simulated user.

[ Note ] The tricky part here is distinguishing which message is from which entity.

Since both the chatbot and the simulated user are implemented with LLMs, both will respond with AI messages. Our state will be a list of alternating human and AI messages, which means it requires logic in one of the nodes to swap roles between AI and human.

In this example, HumanMessages are assumed to come from the simulated user. This means the simulated user node needs logic to exchange AI and human messages.

Call the Agent Node.

Next, define a node for the user we simulated.

[ Note ]

  • This process includes some minor logic to swap message roles.

Define Edges

Now we need to define the logic for edges. The primary logic happens after the simulated user has completed their task, leading to one of two outcomes:

  • Continue by calling the support bot ( "continue").

  • End the conversation ( "end" ).

When does the conversation end?

It ends when the simulated user responds with FINISHED (as specified in the system prompt) or conversation exceeds six messages (an arbitrary limit to keep this example brief).

The should_continue function takes a list of messages as argument and returns "end" if the list exceeds six messages or the last message content is FINISHED . Otherwise, it returns "continue" to proceed.

Define graphs

Now define the graph for setting up the simulation.

The MessageGraph class is used to structure and simulate interactions between the chatbot and the simulated user.

png

Start the Simulation

Now, we can evaluate our chatbot! You can call it with empty messages to simulate the chatbot initiating the conversation.

We'll iterate over the data chunks streaming from the simulation, outputting all events except the final ending chunk (END).

Last updated