-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathsimulate_conversation.py
More file actions
237 lines (192 loc) · 7.21 KB
/
simulate_conversation.py
File metadata and controls
237 lines (192 loc) · 7.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
import getpass
import os
import json
import datetime
import uuid
from pathlib import Path
from dotenv import load_dotenv
from config_simulate_conversation import (
SYSTEM_PROMPT,
SIMULATED_USER_PROMPT,
SYSTEM_MODEL,
SIMULATED_USER_MODEL,
MAX_MESSAGES,
)
from utils import save_conversation_to_file
# Import LangSmith thread functionality
from langsmith import Client
import os
load_dotenv()
# Initialize LangSmith client
client = Client()
def _set_if_undefined(var: str):
if not os.environ.get(var):
os.environ[var] = getpass.getpass(f"Please provide your {var}")
_set_if_undefined("OPENAI_API_KEY")
_set_if_undefined("LANGSMITH_API_KEY")
_set_if_undefined("LANGSMITH_PROJECT")
# Ensure LangSmith tracing is enabled
if not os.environ.get("LANGSMITH_TRACING"):
os.environ["LANGSMITH_TRACING"] = "true"
from typing import List
import openai
# Agent to be tested
def my_chat_bot(messages: List[dict]) -> dict:
system_message = {
"role": "system",
"content": SYSTEM_PROMPT,
}
messages = [system_message] + messages
# Get the thread ID from environment
thread_id = os.environ.get("LANGSMITH_SESSION_ID", "")
# Create completion with metadata for LangSmith
completion = openai.chat.completions.create(
messages=messages,
model=SYSTEM_MODEL,
extra_headers={"X-LangSmith-Session-Id": thread_id} if thread_id else {}
)
return completion.choices[0].message.model_dump()
# print(my_chat_bot([{"role": "user", "content": "hi!"}]))
# Simulated user agent
from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
from langchain_openai import ChatOpenAI
system_prompt_template = """{instructions}
When you are finished with your inital objective, respond with a single word 'FINISHED' and nothing else to end the chat."""
prompt = ChatPromptTemplate.from_messages(
[
("system", system_prompt_template),
MessagesPlaceholder(variable_name="messages"),
]
)
instructions = SIMULATED_USER_PROMPT
prompt = prompt.partial(instructions=instructions)
model = ChatOpenAI(model=SIMULATED_USER_MODEL)
simulated_user = prompt | model
from langchain_core.messages import HumanMessage
messages = [HumanMessage(content="Hi! How can I help you?")]
# Agent simulation -> 2 nodes - 1 for chat chat bot and 1 for simulated user
# we will assume that HumanMessages are messages from the simulated user. This means that we need some logic in the simulated user node to swap AI and Human messages.
from langchain_community.adapters.openai import convert_message_to_dict
from langchain_core.messages import AIMessage
def chat_bot_node(state):
messages = state["messages"]
# Convert from LangChain format to the OpenAI format, which our chatbot function expects.
messages = [convert_message_to_dict(m) for m in messages]
# Call the chat bot
chat_bot_response = my_chat_bot(messages)
# Respond with an AI Message
return {"messages": [AIMessage(content=chat_bot_response["content"])]}
def _swap_roles(messages):
new_messages = []
for m in messages:
if isinstance(m, AIMessage):
new_messages.append(HumanMessage(content=m.content))
else:
new_messages.append(AIMessage(content=m.content))
return new_messages
def simulated_user_node(state):
messages = state["messages"]
# Swap roles of messages
new_messages = _swap_roles(messages)
# Call the simulated user with thread metadata
response = simulated_user.invoke(
{"messages": new_messages},
config={"metadata": {"session_id": os.environ.get("LANGSMITH_SESSION_ID")}}
)
# This response is an AI message - we need to flip this to be a human message
return {"messages": [HumanMessage(content=response.content)]}
def should_continue(state):
messages = state["messages"]
if len(messages) > MAX_MESSAGES:
return "end"
elif "FINISHED" in messages[-1].content:
return "end"
else:
return "continue"
from langgraph.graph import END, StateGraph, START
from langgraph.graph.message import add_messages
from typing import Annotated
from typing_extensions import TypedDict
class State(TypedDict):
messages: Annotated[list, add_messages]
graph_builder = StateGraph(State)
graph_builder.add_node("user", simulated_user_node)
graph_builder.add_node("chat_bot", chat_bot_node)
# Every response from your chat bot will automatically go to the
# simulated user
graph_builder.add_edge("chat_bot", "user")
graph_builder.add_conditional_edges(
"user",
should_continue,
# If the finish criteria are met, we will stop the simulation,
# otherwise, the virtual user's message will be sent to your chat bot
{
"end": END,
"continue": "chat_bot",
},
)
# The input will first go to your chat bot
graph_builder.add_edge(START, "chat_bot")
simulation = graph_builder.compile()
# Create a thread for this conversation
thread_id = str(uuid.uuid4())
print(f"Created thread with ID: {thread_id}")
# Set the thread ID in the environment for LangSmith to use
os.environ["LANGSMITH_SESSION_ID"] = thread_id
# Store the conversation for saving to file
conversation_history = []
for chunk in simulation.stream({"messages": []}):
# Print out all events aside from the final end chunk
if END not in chunk:
# Extract and print only the message content
for node, data in chunk.items():
for message in data["messages"]:
print(f"{node}: {message.content}")
# Store the message in conversation history
conversation_history.append({
"role": node,
"content": message.content
})
print("----")
else:
# Conversation has ended, save to file
config_data = {
"system_prompt": SYSTEM_PROMPT,
"simulated_user_prompt": SIMULATED_USER_PROMPT,
"system_model": SYSTEM_MODEL,
"simulated_user_model": SIMULATED_USER_MODEL,
"max_messages": MAX_MESSAGES,
"thread_id": thread_id
}
# Create the data structure to save
conversation_data = {
"config": config_data,
"conversation": conversation_history,
"thread_id": thread_id
}
# Save the conversation to a file
try:
save_conversation_to_file(conversation_data)
except Exception as e:
print(f"Error saving conversation: {str(e)}")
# Ensure conversation is saved even if END block is not executed
if conversation_history:
config_data = {
"system_prompt": SYSTEM_PROMPT,
"simulated_user_prompt": SIMULATED_USER_PROMPT,
"system_model": SYSTEM_MODEL,
"simulated_user_model": SIMULATED_USER_MODEL,
"max_messages": MAX_MESSAGES,
"thread_id": thread_id
}
# Create the data structure to save
conversation_data = {
"config": config_data,
"conversation": conversation_history,
"thread_id": thread_id
}
# Save the conversation to a file
try:
save_conversation_to_file(conversation_data)
except Exception as e:
print(f"Error saving conversation at the end of the script: {str(e)}")