01. Agent conversation simulation (customer response scenario)

Chatbot conversation simulation

When building a chatbot, for example, a customer support assistant, it can be difficult to properly evaluate the performance of the chatbot. Interacting manually intensively for each code change is time consuming.

One way to make the evaluation process easier and reproducible Simulating user interaction is.

With LangGraph, it is easy to set this up.

Below is an example of how to create a "Simulated User" to simulate a conversation.

Preferences

State definition

Counselor defines customer roles

Define the role of the counselor

In simulation Counselor Define a chatbot that plays a role.

Reference

  • call_chatbot My implementation is setable, and it is also possible to change the model used inside to Agent.

  • call_chatbot We will receive a message from the user as input and give the role of consulting the customer.

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

call_chatbot Take the user's input and process the chatbot's response.

Define customer roles (Simulated User)

Now define the role of the simulated customer. Simulate conversations in customer support scenarios.

The system prompt establishes the interaction between the customer and the customer support representative, and provides details of the scenario through user directives.

This configuration is used to simulate the model's response to specific user needs (e.g. refund requests).

Generate a hypothetical scenario. This fictitious scenario is from the customer's point of view.

Here we define the scenario for requesting a refund.

Created simulated_user Call to forward the message to the simulated user.

Define agent simulation

The code below creates a LangGraph workflow to run the simulation.

The main components are:

  1. These are two nodes above the simulated user and the chatbot.

  2. The graph itself with conditional stop criteria.

Call the node of the counselor role.

In this example, we will assume that HumanMessages are messages from simulated users. This means that the simulated user node needs logic to exchange AI and Human messages.

Since both chatbots and simulated users are LLMs, both will respond with AI messages. Our state will be a list of alternating human and AI messages. This means that one of the nodes needs logic to change AI and human roles.

Note: The tricky thing here is to distinguish which message is what.

First, define the node in the graph. They need to receive a list of messages as input and return a list of messages to add to the status. These are the chatbots on top and the rappers around the simulated user.

Note: The tricky thing here is to distinguish which message is what.

Since both chatbots and simulated users are LLMs, both will respond with AI messages. Our state will be a list of alternating human and AI messages. This means that one of the nodes needs logic to change AI and human roles.

In this example, we will assume that HumanMessages are messages from simulated users. This means that the simulated user node needs logic to exchange AI and Human messages.

Call the node of the counselor role.

Next, let's define a node for our simulated user.

Reference

  • This process will include a small logic that replaces the role of the message.

Edge definition

Now you need to define a logic for the edge. The main logic occurs after the simulated user has finished working, and should lead to one of two results:

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

  • End the conversation ("end")

So what is the logic that ends the conversation? We have this human chatbot FINISHED We will either respond with (see system prompt) or define the conversation as if it exceeds 6 messages (this is a random number to keep this example short). should_continue The function takes the message list as an factor and returns'end' if the length of the list exceeds 6 or the content of the last message is'FINISHED'.

Otherwise, return'continue' to continue processing.

Graph definition

Now define the graph that sets the simulation.

MessageGraph Classes are used to construct and simulate interactions between chatbots and simulated users.

Last updated