You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Before diving into this content, it might be helpful to read the following:
5
11
6
12
-[Add metadata and tags to traces](../tracing/add_metadata_tags)
7
-
8
-
:::
13
+
:::
9
14
10
15
Many LLM applications have a chatbot-like interface in which the user and the LLM application engage in a multi-turn conversation. In order to track these conversations, you can use the `Threads` feature in LangSmith.
11
16
@@ -22,11 +27,183 @@ The key name should be one of:
22
27
-`thread_id`
23
28
-`conversation_id`.
24
29
25
-
The value should be a UUID, such as `f47ac10b-58cc-4372-a567-0e02b2c3d479`.
30
+
The value can be any string you want, but we recommend using UUIDs, such as `f47ac10b-58cc-4372-a567-0e02b2c3d479`.
31
+
32
+
### Code example
33
+
34
+
This example demonstrates how to log and retrieve conversation history from LangSmith to maintain long-running chats.
35
+
36
+
You can [add metadata to your traces](../tracing/add_metadata_tags) in LangSmith in a variety of ways, this code will show how to do so dynamically, but read the
37
+
previously linked guide to learn about all the ways you can add thread identifier metadata to your traces.
# if an existing conversation is continued, this function looks up the current run’s metadata to get the session_id, calls get_thread_history, and appends the new user question before making a call to the chat model
74
+
75
+
@traceable(name="Chat Bot")
76
+
def chat_pipeline(question: str, get_chat_history: bool = False): # Whether to continue an existing thread or start a new one
After waiting a few seconds, you can make the following calls to contineu the conversation. By passing `getChatHistory: true`,
184
+
you can continue the conversation from where it left off. This means that the LLM will receive the entire message history and respond to it,
185
+
instead of just responding to the latest message.
186
+
187
+
<CodeTabs
188
+
tabs={[
189
+
PythonBlock(`# Continue the conversation (WAIT A FEW SECONDS BEFORE RUNNING THIS SO THE FRIST TRACE CAN BE INGESTED)
190
+
chat_pipeline("What is my name?", get_chat_history=True, langsmith_extra=langsmith_extra)
191
+
192
+
# Keep the conversation going (WAIT A FEW SECONDS BEFORE RUNNING THIS SO THE PREVIOUS TRACE CAN BE INGESTED)
193
+
194
+
chat_pipeline("What was the first message I sent you", get_chat_history=True, langsmith_extra=langsmith_extra)`),
195
+
TypeScriptBlock(`// Continue the conversation (WAIT A FEW SECONDS BEFORE RUNNING THIS SO THE FRIST TRACE CAN BE INGESTED)
196
+
await chatPipeline("What is my name?", { getChatHistory: true });
197
+
198
+
// Keep the conversation going (WAIT A FEW SECONDS BEFORE RUNNING THIS SO THE PREVIOUS TRACE CAN BE INGESTED)
199
+
await chatPipeline("What was the first message I sent you", { getChatHistory: true });`),
200
+
]}
201
+
groupId="client-language"
202
+
/>
26
203
27
204
## View threads
28
205
29
-
You can view threads by clicking on the `Threads`tad in any project details page. You will then see a list of all threads, sorted by the most recent activity.
206
+
You can view threads by clicking on the `Threads`tab in any project details page. You will then see a list of all threads, sorted by the most recent activity.
0 commit comments