Files
langgraph/examples/chatbot-simulation-evaluation/_testing/simulation.py
T
2024-03-06 13:07:33 -08:00

50 lines
1.6 KiB
Python

import openai
from langchain_openai import ChatOpenAI
from simulation_utils import (
create_chat_simulator,
create_simulated_user,
langchain_to_openai_messages,
)
openai_client = openai.Client()
def my_chat_bot(messages: list) -> str:
oai_messages = langchain_to_openai_messages(messages)
system_message = {
"role": "system",
"content": "You are a customer support agent for an airline.",
}
messages = [system_message] + oai_messages
completion = openai_client.chat.completions.create(
messages=messages, model="gpt-3.5-turbo"
)
return completion.choices[0].message.content
my_chat_bot([{"role": "user", "content": "hi!"}])
system_prompt_template = """You are role playing as a customer of an airline company.
You are interacting with the customer support agent.
Instructions for this conversation: {instructions}
You will start the conversation, and respond with your next message as the customer.
When you are finished with the conversation, respond with a single word 'FINISHED'."""
simulated_user = create_simulated_user(
system_prompt_template, llm=ChatOpenAI(model="gpt-3.5-turbo")
)
# my chat bot accepts a list of LangChain mesages
# Simulated user accepts a list of LangChain messages
# TODO: Pass additional arguments to the simulated user
simulator = create_chat_simulator(my_chat_bot, simulated_user, input_key="input")
simulator.invoke(
{
"input": "I need a discount.",
"instructions": "You are extremely disgruntled and will cusss and swear to get your way. Try to get a discount by any means necessary."
}
)