mirror of
https://github.com/hwchase17/langchain.git
synced 2026-02-02 23:31:18 +00:00
958 lines
33 KiB
Plaintext
958 lines
33 KiB
Plaintext
{
|
||
"cells": [
|
||
{
|
||
"cell_type": "raw",
|
||
"metadata": {
|
||
"vscode": {
|
||
"languageId": "raw"
|
||
}
|
||
},
|
||
"source": [
|
||
"---\n",
|
||
"sidebar_position: 1\n",
|
||
"keywords: [conversationchain]\n",
|
||
"---"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"metadata": {},
|
||
"source": [
|
||
"# Build a Chatbot"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"metadata": {},
|
||
"source": [
|
||
":::note\n",
|
||
"\n",
|
||
"This tutorial previously used the [RunnableWithMessageHistory](https://python.langchain.com/api_reference/core/runnables/langchain_core.runnables.history.RunnableWithMessageHistory.html) abstraction. You can access that version of the documentation in the [v0.2 docs](https://python.langchain.com/v0.2/docs/tutorials/chatbot/).\n",
|
||
"\n",
|
||
"As of the v0.3 release of LangChain, we recommend that LangChain users take advantage of [LangGraph persistence](https://langchain-ai.github.io/langgraph/concepts/persistence/) to incorporate `memory` into new LangChain applications.\n",
|
||
"\n",
|
||
"If your code is already relying on `RunnableWithMessageHistory` or `BaseChatMessageHistory`, you do **not** need to make any changes. We do not plan on deprecating this functionality in the near future as it works for simple chat applications and any code that uses `RunnableWithMessageHistory` will continue to work as expected.\n",
|
||
"\n",
|
||
"Please see [How to migrate to LangGraph Memory](/docs/versions/migrating_memory/) for more details.\n",
|
||
":::\n",
|
||
"\n",
|
||
"## Overview\n",
|
||
"\n",
|
||
"We'll go over an example of how to design and implement an LLM-powered chatbot. \n",
|
||
"This chatbot will be able to have a conversation and remember previous interactions with a [chat model](/docs/concepts/chat_models).\n",
|
||
"\n",
|
||
"\n",
|
||
"Note that this chatbot that we build will only use the language model to have a conversation.\n",
|
||
"There are several other related concepts that you may be looking for:\n",
|
||
"\n",
|
||
"- [Conversational RAG](/docs/tutorials/qa_chat_history): Enable a chatbot experience over an external source of data\n",
|
||
"- [Agents](/docs/tutorials/agents): Build a chatbot that can take actions\n",
|
||
"\n",
|
||
"This tutorial will cover the basics which will be helpful for those two more advanced topics, but feel free to skip directly to there should you choose.\n",
|
||
"\n",
|
||
"## Setup\n",
|
||
"\n",
|
||
"### Jupyter Notebook\n",
|
||
"\n",
|
||
"This guide (and most of the other guides in the documentation) uses [Jupyter notebooks](https://jupyter.org/) and assumes the reader is as well. Jupyter notebooks are perfect for learning how to work with LLM systems because oftentimes things can go wrong (unexpected output, API down, etc) and going through guides in an interactive environment is a great way to better understand them.\n",
|
||
"\n",
|
||
"This and other tutorials are perhaps most conveniently run in a Jupyter notebook. See [here](https://jupyter.org/install) for instructions on how to install.\n",
|
||
"\n",
|
||
"### Installation\n",
|
||
"\n",
|
||
"For this tutorial we will need `langchain-core` and `langgraph`. This guide requires `langgraph >= 0.2.28`.\n",
|
||
"\n",
|
||
"import Tabs from '@theme/Tabs';\n",
|
||
"import TabItem from '@theme/TabItem';\n",
|
||
"import CodeBlock from \"@theme/CodeBlock\";\n",
|
||
"\n",
|
||
"<Tabs>\n",
|
||
" <TabItem value=\"pip\" label=\"Pip\" default>\n",
|
||
" <CodeBlock language=\"bash\">pip install langchain-core langgraph>0.2.27</CodeBlock>\n",
|
||
" </TabItem>\n",
|
||
" <TabItem value=\"conda\" label=\"Conda\">\n",
|
||
" <CodeBlock language=\"bash\">conda install langchain-core langgraph>0.2.27 -c conda-forge</CodeBlock>\n",
|
||
" </TabItem>\n",
|
||
"</Tabs>\n",
|
||
"\n",
|
||
"\n",
|
||
"\n",
|
||
"For more details, see our [Installation guide](/docs/how_to/installation).\n",
|
||
"\n",
|
||
"### LangSmith\n",
|
||
"\n",
|
||
"Many of the applications you build with LangChain will contain multiple steps with multiple invocations of LLM calls.\n",
|
||
"As these applications get more and more complex, it becomes crucial to be able to inspect what exactly is going on inside your chain or agent.\n",
|
||
"The best way to do this is with [LangSmith](https://smith.langchain.com).\n",
|
||
"\n",
|
||
"After you sign up at the link above, make sure to set your environment variables to start logging traces:\n",
|
||
"\n",
|
||
"```shell\n",
|
||
"export LANGCHAIN_TRACING_V2=\"true\"\n",
|
||
"export LANGCHAIN_API_KEY=\"...\"\n",
|
||
"```\n",
|
||
"\n",
|
||
"Or, if in a notebook, you can set them with:\n",
|
||
"\n",
|
||
"```python\n",
|
||
"import getpass\n",
|
||
"import os\n",
|
||
"\n",
|
||
"os.environ[\"LANGCHAIN_TRACING_V2\"] = \"true\"\n",
|
||
"os.environ[\"LANGCHAIN_API_KEY\"] = getpass.getpass()\n",
|
||
"```\n",
|
||
"\n",
|
||
"## Quickstart\n",
|
||
"\n",
|
||
"First up, let's learn how to use a language model by itself. LangChain supports many different language models that you can use interchangeably - select the one you want to use below!\n",
|
||
"\n",
|
||
"import ChatModelTabs from \"@theme/ChatModelTabs\";\n",
|
||
"\n",
|
||
"<ChatModelTabs openaiParams={`model=\"gpt-4o-mini\"`} />\n"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 2,
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"source": [
|
||
"# | output: false\n",
|
||
"# | echo: false\n",
|
||
"\n",
|
||
"from langchain_openai import ChatOpenAI\n",
|
||
"\n",
|
||
"model = ChatOpenAI(model=\"gpt-4o-mini\")"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"metadata": {},
|
||
"source": [
|
||
"Let's first use the model directly. `ChatModel`s are instances of LangChain \"Runnables\", which means they expose a standard interface for interacting with them. To just simply call the model, we can pass in a list of messages to the `.invoke` method."
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 3,
|
||
"metadata": {},
|
||
"outputs": [
|
||
{
|
||
"data": {
|
||
"text/plain": [
|
||
"AIMessage(content='Hi Bob! How can I assist you today?', additional_kwargs={'refusal': None}, response_metadata={'token_usage': {'completion_tokens': 10, 'prompt_tokens': 11, 'total_tokens': 21, 'completion_tokens_details': {'accepted_prediction_tokens': 0, 'audio_tokens': 0, 'reasoning_tokens': 0, 'rejected_prediction_tokens': 0}, 'prompt_tokens_details': {'audio_tokens': 0, 'cached_tokens': 0}}, 'model_name': 'gpt-4o-mini-2024-07-18', 'system_fingerprint': 'fp_0705bf87c0', 'finish_reason': 'stop', 'logprobs': None}, id='run-5211544f-da9f-4325-8b8e-b3d92b2fc71a-0', usage_metadata={'input_tokens': 11, 'output_tokens': 10, 'total_tokens': 21, 'input_token_details': {'audio': 0, 'cache_read': 0}, 'output_token_details': {'audio': 0, 'reasoning': 0}})"
|
||
]
|
||
},
|
||
"execution_count": 3,
|
||
"metadata": {},
|
||
"output_type": "execute_result"
|
||
}
|
||
],
|
||
"source": [
|
||
"from langchain_core.messages import HumanMessage\n",
|
||
"\n",
|
||
"model.invoke([HumanMessage(content=\"Hi! I'm Bob\")])"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"metadata": {},
|
||
"source": [
|
||
"The model on its own does not have any concept of state. For example, if you ask a followup question:"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 4,
|
||
"metadata": {},
|
||
"outputs": [
|
||
{
|
||
"data": {
|
||
"text/plain": [
|
||
"AIMessage(content=\"I'm sorry, but I don't have access to personal information about users unless it has been shared with me in the course of our conversation. How can I assist you today?\", additional_kwargs={'refusal': None}, response_metadata={'token_usage': {'completion_tokens': 34, 'prompt_tokens': 11, 'total_tokens': 45, 'completion_tokens_details': {'accepted_prediction_tokens': 0, 'audio_tokens': 0, 'reasoning_tokens': 0, 'rejected_prediction_tokens': 0}, 'prompt_tokens_details': {'audio_tokens': 0, 'cached_tokens': 0}}, 'model_name': 'gpt-4o-mini-2024-07-18', 'system_fingerprint': 'fp_0705bf87c0', 'finish_reason': 'stop', 'logprobs': None}, id='run-a2d13a18-7022-4784-b54f-f85c097d1075-0', usage_metadata={'input_tokens': 11, 'output_tokens': 34, 'total_tokens': 45, 'input_token_details': {'audio': 0, 'cache_read': 0}, 'output_token_details': {'audio': 0, 'reasoning': 0}})"
|
||
]
|
||
},
|
||
"execution_count": 4,
|
||
"metadata": {},
|
||
"output_type": "execute_result"
|
||
}
|
||
],
|
||
"source": [
|
||
"model.invoke([HumanMessage(content=\"What's my name?\")])"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"metadata": {},
|
||
"source": [
|
||
"Let's take a look at the example [LangSmith trace](https://smith.langchain.com/public/5c21cb92-2814-4119-bae9-d02b8db577ac/r)\n",
|
||
"\n",
|
||
"We can see that it doesn't take the previous conversation turn into context, and cannot answer the question.\n",
|
||
"This makes for a terrible chatbot experience!\n",
|
||
"\n",
|
||
"To get around this, we need to pass the entire [conversation history](/docs/concepts/chat_history) into the model. Let's see what happens when we do that:"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 5,
|
||
"metadata": {},
|
||
"outputs": [
|
||
{
|
||
"data": {
|
||
"text/plain": [
|
||
"AIMessage(content='Your name is Bob! How can I help you today, Bob?', additional_kwargs={'refusal': None}, response_metadata={'token_usage': {'completion_tokens': 14, 'prompt_tokens': 33, 'total_tokens': 47, 'completion_tokens_details': {'accepted_prediction_tokens': 0, 'audio_tokens': 0, 'reasoning_tokens': 0, 'rejected_prediction_tokens': 0}, 'prompt_tokens_details': {'audio_tokens': 0, 'cached_tokens': 0}}, 'model_name': 'gpt-4o-mini-2024-07-18', 'system_fingerprint': 'fp_0705bf87c0', 'finish_reason': 'stop', 'logprobs': None}, id='run-34bcccb3-446e-42f2-b1de-52c09936c02c-0', usage_metadata={'input_tokens': 33, 'output_tokens': 14, 'total_tokens': 47, 'input_token_details': {'audio': 0, 'cache_read': 0}, 'output_token_details': {'audio': 0, 'reasoning': 0}})"
|
||
]
|
||
},
|
||
"execution_count": 5,
|
||
"metadata": {},
|
||
"output_type": "execute_result"
|
||
}
|
||
],
|
||
"source": [
|
||
"from langchain_core.messages import AIMessage\n",
|
||
"\n",
|
||
"model.invoke(\n",
|
||
" [\n",
|
||
" HumanMessage(content=\"Hi! I'm Bob\"),\n",
|
||
" AIMessage(content=\"Hello Bob! How can I assist you today?\"),\n",
|
||
" HumanMessage(content=\"What's my name?\"),\n",
|
||
" ]\n",
|
||
")"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"metadata": {},
|
||
"source": [
|
||
"And now we can see that we get a good response!\n",
|
||
"\n",
|
||
"This is the basic idea underpinning a chatbot's ability to interact conversationally.\n",
|
||
"So how do we best implement this?"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"metadata": {},
|
||
"source": [
|
||
"## Message persistence\n",
|
||
"\n",
|
||
"[LangGraph](https://langchain-ai.github.io/langgraph/) implements a built-in persistence layer, making it ideal for chat applications that support multiple conversational turns.\n",
|
||
"\n",
|
||
"Wrapping our chat model in a minimal LangGraph application allows us to automatically persist the message history, simplifying the development of multi-turn applications.\n",
|
||
"\n",
|
||
"LangGraph comes with a simple in-memory checkpointer, which we use below. See its [documentation](https://langchain-ai.github.io/langgraph/concepts/persistence/) for more detail, including how to use different persistence backends (e.g., SQLite or Postgres)."
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 6,
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"source": [
|
||
"from langgraph.checkpoint.memory import MemorySaver\n",
|
||
"from langgraph.graph import START, MessagesState, StateGraph\n",
|
||
"\n",
|
||
"# Define a new graph\n",
|
||
"workflow = StateGraph(state_schema=MessagesState)\n",
|
||
"\n",
|
||
"\n",
|
||
"# Define the function that calls the model\n",
|
||
"def call_model(state: MessagesState):\n",
|
||
" response = model.invoke(state[\"messages\"])\n",
|
||
" return {\"messages\": response}\n",
|
||
"\n",
|
||
"\n",
|
||
"# Define the (single) node in the graph\n",
|
||
"workflow.add_edge(START, \"model\")\n",
|
||
"workflow.add_node(\"model\", call_model)\n",
|
||
"\n",
|
||
"# Add memory\n",
|
||
"memory = MemorySaver()\n",
|
||
"app = workflow.compile(checkpointer=memory)"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"metadata": {},
|
||
"source": [
|
||
"We now need to create a `config` that we pass into the runnable every time. This config contains information that is not part of the input directly, but is still useful. In this case, we want to include a `thread_id`. This should look like:"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 7,
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"source": [
|
||
"config = {\"configurable\": {\"thread_id\": \"abc123\"}}"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"metadata": {},
|
||
"source": [
|
||
"This enables us to support multiple conversation threads with a single application, a common requirement when your application has multiple users.\n",
|
||
"\n",
|
||
"We can then invoke the application:"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 8,
|
||
"metadata": {},
|
||
"outputs": [
|
||
{
|
||
"name": "stdout",
|
||
"output_type": "stream",
|
||
"text": [
|
||
"==================================\u001b[1m Ai Message \u001b[0m==================================\n",
|
||
"\n",
|
||
"Hi Bob! How can I assist you today?\n"
|
||
]
|
||
}
|
||
],
|
||
"source": [
|
||
"query = \"Hi! I'm Bob.\"\n",
|
||
"\n",
|
||
"input_messages = [HumanMessage(query)]\n",
|
||
"output = app.invoke({\"messages\": input_messages}, config)\n",
|
||
"output[\"messages\"][-1].pretty_print() # output contains all messages in state"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 9,
|
||
"metadata": {},
|
||
"outputs": [
|
||
{
|
||
"name": "stdout",
|
||
"output_type": "stream",
|
||
"text": [
|
||
"==================================\u001b[1m Ai Message \u001b[0m==================================\n",
|
||
"\n",
|
||
"Your name is Bob! How can I help you today, Bob?\n"
|
||
]
|
||
}
|
||
],
|
||
"source": [
|
||
"query = \"What's my name?\"\n",
|
||
"\n",
|
||
"input_messages = [HumanMessage(query)]\n",
|
||
"output = app.invoke({\"messages\": input_messages}, config)\n",
|
||
"output[\"messages\"][-1].pretty_print()"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"metadata": {},
|
||
"source": [
|
||
"Great! Our chatbot now remembers things about us. If we change the config to reference a different `thread_id`, we can see that it starts the conversation fresh."
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 10,
|
||
"metadata": {},
|
||
"outputs": [
|
||
{
|
||
"name": "stdout",
|
||
"output_type": "stream",
|
||
"text": [
|
||
"==================================\u001b[1m Ai Message \u001b[0m==================================\n",
|
||
"\n",
|
||
"I'm sorry, but I don't have access to personal information about you unless you've shared it in this conversation. How can I assist you today?\n"
|
||
]
|
||
}
|
||
],
|
||
"source": [
|
||
"config = {\"configurable\": {\"thread_id\": \"abc234\"}}\n",
|
||
"\n",
|
||
"input_messages = [HumanMessage(query)]\n",
|
||
"output = app.invoke({\"messages\": input_messages}, config)\n",
|
||
"output[\"messages\"][-1].pretty_print()"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"metadata": {},
|
||
"source": [
|
||
"However, we can always go back to the original conversation (since we are persisting it in a database)"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 11,
|
||
"metadata": {},
|
||
"outputs": [
|
||
{
|
||
"name": "stdout",
|
||
"output_type": "stream",
|
||
"text": [
|
||
"==================================\u001b[1m Ai Message \u001b[0m==================================\n",
|
||
"\n",
|
||
"Your name is Bob. What would you like to discuss today?\n"
|
||
]
|
||
}
|
||
],
|
||
"source": [
|
||
"config = {\"configurable\": {\"thread_id\": \"abc123\"}}\n",
|
||
"\n",
|
||
"input_messages = [HumanMessage(query)]\n",
|
||
"output = app.invoke({\"messages\": input_messages}, config)\n",
|
||
"output[\"messages\"][-1].pretty_print()"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"metadata": {},
|
||
"source": [
|
||
"This is how we can support a chatbot having conversations with many users!\n",
|
||
"\n",
|
||
":::tip\n",
|
||
"\n",
|
||
"For async support, update the `call_model` node to be an async function and use `.ainvoke` when invoking the application:\n",
|
||
"\n",
|
||
"```python\n",
|
||
"# Async function for node:\n",
|
||
"async def call_model(state: MessagesState):\n",
|
||
" response = await model.ainvoke(state[\"messages\"])\n",
|
||
" return {\"messages\": response}\n",
|
||
"\n",
|
||
"\n",
|
||
"# Define graph as before:\n",
|
||
"workflow = StateGraph(state_schema=MessagesState)\n",
|
||
"workflow.add_edge(START, \"model\")\n",
|
||
"workflow.add_node(\"model\", call_model)\n",
|
||
"app = workflow.compile(checkpointer=MemorySaver())\n",
|
||
"\n",
|
||
"# Async invocation:\n",
|
||
"output = await app.ainvoke({\"messages\": input_messages}, config)\n",
|
||
"output[\"messages\"][-1].pretty_print()\n",
|
||
"```\n",
|
||
"\n",
|
||
":::"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"metadata": {},
|
||
"source": [
|
||
"Right now, all we've done is add a simple persistence layer around the model. We can start to make the chatbot more complicated and personalized by adding in a prompt template.\n",
|
||
"\n",
|
||
"## Prompt templates\n",
|
||
"\n",
|
||
"[Prompt Templates](/docs/concepts/prompt_templates) help to turn raw user information into a format that the LLM can work with. In this case, the raw user input is just a message, which we are passing to the LLM. Let's now make that a bit more complicated. First, let's add in a system message with some custom instructions (but still taking messages as input). Next, we'll add in more input besides just the messages.\n",
|
||
"\n",
|
||
"To add in a system message, we will create a `ChatPromptTemplate`. We will utilize `MessagesPlaceholder` to pass all the messages in."
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 12,
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"source": [
|
||
"from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder\n",
|
||
"\n",
|
||
"prompt_template = ChatPromptTemplate.from_messages(\n",
|
||
" [\n",
|
||
" (\n",
|
||
" \"system\",\n",
|
||
" \"You talk like a pirate. Answer all questions to the best of your ability.\",\n",
|
||
" ),\n",
|
||
" MessagesPlaceholder(variable_name=\"messages\"),\n",
|
||
" ]\n",
|
||
")"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"metadata": {},
|
||
"source": [
|
||
"We can now update our application to incorporate this template:"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 13,
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"source": [
|
||
"workflow = StateGraph(state_schema=MessagesState)\n",
|
||
"\n",
|
||
"\n",
|
||
"def call_model(state: MessagesState):\n",
|
||
" # highlight-start\n",
|
||
" prompt = prompt_template.invoke(state)\n",
|
||
" response = model.invoke(prompt)\n",
|
||
" # highlight-end\n",
|
||
" return {\"messages\": response}\n",
|
||
"\n",
|
||
"\n",
|
||
"workflow.add_edge(START, \"model\")\n",
|
||
"workflow.add_node(\"model\", call_model)\n",
|
||
"\n",
|
||
"memory = MemorySaver()\n",
|
||
"app = workflow.compile(checkpointer=memory)"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"metadata": {},
|
||
"source": [
|
||
"We invoke the application in the same way:"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 14,
|
||
"metadata": {},
|
||
"outputs": [
|
||
{
|
||
"name": "stdout",
|
||
"output_type": "stream",
|
||
"text": [
|
||
"==================================\u001b[1m Ai Message \u001b[0m==================================\n",
|
||
"\n",
|
||
"Ahoy there, Jim! What brings ye to these waters today? Be ye seekin' treasure, knowledge, or perhaps a good tale from the high seas? Arrr!\n"
|
||
]
|
||
}
|
||
],
|
||
"source": [
|
||
"config = {\"configurable\": {\"thread_id\": \"abc345\"}}\n",
|
||
"query = \"Hi! I'm Jim.\"\n",
|
||
"\n",
|
||
"input_messages = [HumanMessage(query)]\n",
|
||
"output = app.invoke({\"messages\": input_messages}, config)\n",
|
||
"output[\"messages\"][-1].pretty_print()"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 15,
|
||
"metadata": {},
|
||
"outputs": [
|
||
{
|
||
"name": "stdout",
|
||
"output_type": "stream",
|
||
"text": [
|
||
"==================================\u001b[1m Ai Message \u001b[0m==================================\n",
|
||
"\n",
|
||
"Ye be called Jim, matey! A fine name fer a swashbuckler such as yerself! What else can I do fer ye? Arrr!\n"
|
||
]
|
||
}
|
||
],
|
||
"source": [
|
||
"query = \"What is my name?\"\n",
|
||
"\n",
|
||
"input_messages = [HumanMessage(query)]\n",
|
||
"output = app.invoke({\"messages\": input_messages}, config)\n",
|
||
"output[\"messages\"][-1].pretty_print()"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"metadata": {},
|
||
"source": [
|
||
"Awesome! Let's now make our prompt a little bit more complicated. Let's assume that the prompt template now looks something like this:"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 19,
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"source": [
|
||
"prompt_template = ChatPromptTemplate.from_messages(\n",
|
||
" [\n",
|
||
" (\n",
|
||
" \"system\",\n",
|
||
" \"You are a helpful assistant. Answer all questions to the best of your ability in {language}.\",\n",
|
||
" ),\n",
|
||
" MessagesPlaceholder(variable_name=\"messages\"),\n",
|
||
" ]\n",
|
||
")"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"metadata": {},
|
||
"source": [
|
||
"Note that we have added a new `language` input to the prompt. Our application now has two parameters-- the input `messages` and `language`. We should update our application's state to reflect this:"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 20,
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"source": [
|
||
"from typing import Sequence\n",
|
||
"\n",
|
||
"from langchain_core.messages import BaseMessage\n",
|
||
"from langgraph.graph.message import add_messages\n",
|
||
"from typing_extensions import Annotated, TypedDict\n",
|
||
"\n",
|
||
"\n",
|
||
"# highlight-next-line\n",
|
||
"class State(TypedDict):\n",
|
||
" # highlight-next-line\n",
|
||
" messages: Annotated[Sequence[BaseMessage], add_messages]\n",
|
||
" # highlight-next-line\n",
|
||
" language: str\n",
|
||
"\n",
|
||
"\n",
|
||
"workflow = StateGraph(state_schema=State)\n",
|
||
"\n",
|
||
"\n",
|
||
"def call_model(state: State):\n",
|
||
" prompt = prompt_template.invoke(state)\n",
|
||
" response = model.invoke(prompt)\n",
|
||
" return {\"messages\": [response]}\n",
|
||
"\n",
|
||
"\n",
|
||
"workflow.add_edge(START, \"model\")\n",
|
||
"workflow.add_node(\"model\", call_model)\n",
|
||
"\n",
|
||
"memory = MemorySaver()\n",
|
||
"app = workflow.compile(checkpointer=memory)"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 21,
|
||
"metadata": {},
|
||
"outputs": [
|
||
{
|
||
"name": "stdout",
|
||
"output_type": "stream",
|
||
"text": [
|
||
"==================================\u001b[1m Ai Message \u001b[0m==================================\n",
|
||
"\n",
|
||
"¡Hola, Bob! ¿Cómo puedo ayudarte hoy?\n"
|
||
]
|
||
}
|
||
],
|
||
"source": [
|
||
"config = {\"configurable\": {\"thread_id\": \"abc456\"}}\n",
|
||
"query = \"Hi! I'm Bob.\"\n",
|
||
"language = \"Spanish\"\n",
|
||
"\n",
|
||
"input_messages = [HumanMessage(query)]\n",
|
||
"output = app.invoke(\n",
|
||
" # highlight-next-line\n",
|
||
" {\"messages\": input_messages, \"language\": language},\n",
|
||
" config,\n",
|
||
")\n",
|
||
"output[\"messages\"][-1].pretty_print()"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"metadata": {},
|
||
"source": [
|
||
"Note that the entire state is persisted, so we can omit parameters like `language` if no changes are desired:"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 22,
|
||
"metadata": {},
|
||
"outputs": [
|
||
{
|
||
"name": "stdout",
|
||
"output_type": "stream",
|
||
"text": [
|
||
"==================================\u001b[1m Ai Message \u001b[0m==================================\n",
|
||
"\n",
|
||
"Tu nombre es Bob. ¿Hay algo más en lo que pueda ayudarte?\n"
|
||
]
|
||
}
|
||
],
|
||
"source": [
|
||
"query = \"What is my name?\"\n",
|
||
"\n",
|
||
"input_messages = [HumanMessage(query)]\n",
|
||
"output = app.invoke(\n",
|
||
" {\"messages\": input_messages},\n",
|
||
" config,\n",
|
||
")\n",
|
||
"output[\"messages\"][-1].pretty_print()"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"metadata": {},
|
||
"source": [
|
||
"To help you understand what's happening internally, check out [this LangSmith trace](https://smith.langchain.com/public/15bd8589-005c-4812-b9b9-23e74ba4c3c6/r)."
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"metadata": {},
|
||
"source": [
|
||
"## Managing Conversation History\n",
|
||
"\n",
|
||
"One important concept to understand when building chatbots is how to manage conversation history. If left unmanaged, the list of messages will grow unbounded and potentially overflow the context window of the LLM. Therefore, it is important to add a step that limits the size of the messages you are passing in.\n",
|
||
"\n",
|
||
"**Importantly, you will want to do this BEFORE the prompt template but AFTER you load previous messages from Message History.**\n",
|
||
"\n",
|
||
"We can do this by adding a simple step in front of the prompt that modifies the `messages` key appropriately, and then wrap that new chain in the Message History class. \n",
|
||
"\n",
|
||
"LangChain comes with a few built-in helpers for [managing a list of messages](/docs/how_to/#messages). In this case we'll use the [trim_messages](/docs/how_to/trim_messages/) helper to reduce how many messages we're sending to the model. The trimmer allows us to specify how many tokens we want to keep, along with other parameters like if we want to always keep the system message and whether to allow partial messages:"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 23,
|
||
"metadata": {},
|
||
"outputs": [
|
||
{
|
||
"data": {
|
||
"text/plain": [
|
||
"[SystemMessage(content=\"you're a good assistant\", additional_kwargs={}, response_metadata={}),\n",
|
||
" HumanMessage(content='whats 2 + 2', additional_kwargs={}, response_metadata={}),\n",
|
||
" AIMessage(content='4', additional_kwargs={}, response_metadata={}),\n",
|
||
" HumanMessage(content='thanks', additional_kwargs={}, response_metadata={}),\n",
|
||
" AIMessage(content='no problem!', additional_kwargs={}, response_metadata={}),\n",
|
||
" HumanMessage(content='having fun?', additional_kwargs={}, response_metadata={}),\n",
|
||
" AIMessage(content='yes!', additional_kwargs={}, response_metadata={})]"
|
||
]
|
||
},
|
||
"execution_count": 23,
|
||
"metadata": {},
|
||
"output_type": "execute_result"
|
||
}
|
||
],
|
||
"source": [
|
||
"from langchain_core.messages import SystemMessage, trim_messages\n",
|
||
"\n",
|
||
"trimmer = trim_messages(\n",
|
||
" max_tokens=65,\n",
|
||
" strategy=\"last\",\n",
|
||
" token_counter=model,\n",
|
||
" include_system=True,\n",
|
||
" allow_partial=False,\n",
|
||
" start_on=\"human\",\n",
|
||
")\n",
|
||
"\n",
|
||
"messages = [\n",
|
||
" SystemMessage(content=\"you're a good assistant\"),\n",
|
||
" HumanMessage(content=\"hi! I'm bob\"),\n",
|
||
" AIMessage(content=\"hi!\"),\n",
|
||
" HumanMessage(content=\"I like vanilla ice cream\"),\n",
|
||
" AIMessage(content=\"nice\"),\n",
|
||
" HumanMessage(content=\"whats 2 + 2\"),\n",
|
||
" AIMessage(content=\"4\"),\n",
|
||
" HumanMessage(content=\"thanks\"),\n",
|
||
" AIMessage(content=\"no problem!\"),\n",
|
||
" HumanMessage(content=\"having fun?\"),\n",
|
||
" AIMessage(content=\"yes!\"),\n",
|
||
"]\n",
|
||
"\n",
|
||
"trimmer.invoke(messages)"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"metadata": {},
|
||
"source": [
|
||
"To use it in our chain, we just need to run the trimmer before we pass the `messages` input to our prompt. "
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 24,
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"source": [
|
||
"workflow = StateGraph(state_schema=State)\n",
|
||
"\n",
|
||
"\n",
|
||
"def call_model(state: State):\n",
|
||
" # highlight-start\n",
|
||
" trimmed_messages = trimmer.invoke(state[\"messages\"])\n",
|
||
" prompt = prompt_template.invoke(\n",
|
||
" {\"messages\": trimmed_messages, \"language\": state[\"language\"]}\n",
|
||
" )\n",
|
||
" response = model.invoke(prompt)\n",
|
||
" # highlight-end\n",
|
||
" return {\"messages\": [response]}\n",
|
||
"\n",
|
||
"\n",
|
||
"workflow.add_edge(START, \"model\")\n",
|
||
"workflow.add_node(\"model\", call_model)\n",
|
||
"\n",
|
||
"memory = MemorySaver()\n",
|
||
"app = workflow.compile(checkpointer=memory)"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"metadata": {},
|
||
"source": [
|
||
"Now if we try asking the model our name, it won't know it since we trimmed that part of the chat history:"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 25,
|
||
"metadata": {},
|
||
"outputs": [
|
||
{
|
||
"name": "stdout",
|
||
"output_type": "stream",
|
||
"text": [
|
||
"==================================\u001b[1m Ai Message \u001b[0m==================================\n",
|
||
"\n",
|
||
"I don't know your name. You haven't told me yet!\n"
|
||
]
|
||
}
|
||
],
|
||
"source": [
|
||
"config = {\"configurable\": {\"thread_id\": \"abc567\"}}\n",
|
||
"query = \"What is my name?\"\n",
|
||
"language = \"English\"\n",
|
||
"\n",
|
||
"# highlight-next-line\n",
|
||
"input_messages = messages + [HumanMessage(query)]\n",
|
||
"output = app.invoke(\n",
|
||
" {\"messages\": input_messages, \"language\": language},\n",
|
||
" config,\n",
|
||
")\n",
|
||
"output[\"messages\"][-1].pretty_print()"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"metadata": {},
|
||
"source": [
|
||
"But if we ask about information that is within the last few messages, it remembers:"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 26,
|
||
"metadata": {},
|
||
"outputs": [
|
||
{
|
||
"name": "stdout",
|
||
"output_type": "stream",
|
||
"text": [
|
||
"==================================\u001b[1m Ai Message \u001b[0m==================================\n",
|
||
"\n",
|
||
"You asked what 2 + 2 equals.\n"
|
||
]
|
||
}
|
||
],
|
||
"source": [
|
||
"config = {\"configurable\": {\"thread_id\": \"abc678\"}}\n",
|
||
"query = \"What math problem did I ask?\"\n",
|
||
"language = \"English\"\n",
|
||
"\n",
|
||
"input_messages = messages + [HumanMessage(query)]\n",
|
||
"output = app.invoke(\n",
|
||
" {\"messages\": input_messages, \"language\": language},\n",
|
||
" config,\n",
|
||
")\n",
|
||
"output[\"messages\"][-1].pretty_print()"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"metadata": {},
|
||
"source": [
|
||
"If you take a look at LangSmith, you can see exactly what is happening under the hood in the [LangSmith trace](https://smith.langchain.com/public/04402eaa-29e6-4bb1-aa91-885b730b6c21/r)."
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"metadata": {},
|
||
"source": [
|
||
"## Streaming\n",
|
||
"\n",
|
||
"Now we've got a functioning chatbot. However, one *really* important UX consideration for chatbot applications is streaming. LLMs can sometimes take a while to respond, and so in order to improve the user experience one thing that most applications do is stream back each token as it is generated. This allows the user to see progress.\n",
|
||
"\n",
|
||
"It's actually super easy to do this!\n",
|
||
"\n",
|
||
"By default, `.stream` in our LangGraph application streams application steps-- in this case, the single step of the model response. Setting `stream_mode=\"messages\"` allows us to stream output tokens instead:"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 27,
|
||
"metadata": {},
|
||
"outputs": [
|
||
{
|
||
"name": "stdout",
|
||
"output_type": "stream",
|
||
"text": [
|
||
"|Hi| Todd|!| Here|’s| a| joke| for| you|:\n",
|
||
"\n",
|
||
"|Why| don|’t| skeleton|s| fight| each| other|?\n",
|
||
"\n",
|
||
"|Because| they| don|’t| have| the| guts|!||"
|
||
]
|
||
}
|
||
],
|
||
"source": [
|
||
"config = {\"configurable\": {\"thread_id\": \"abc789\"}}\n",
|
||
"query = \"Hi I'm Todd, please tell me a joke.\"\n",
|
||
"language = \"English\"\n",
|
||
"\n",
|
||
"input_messages = [HumanMessage(query)]\n",
|
||
"# highlight-next-line\n",
|
||
"for chunk, metadata in app.stream(\n",
|
||
" {\"messages\": input_messages, \"language\": language},\n",
|
||
" config,\n",
|
||
" # highlight-next-line\n",
|
||
" stream_mode=\"messages\",\n",
|
||
"):\n",
|
||
" if isinstance(chunk, AIMessage): # Filter to just model responses\n",
|
||
" print(chunk.content, end=\"|\")"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"metadata": {},
|
||
"source": [
|
||
"## Next Steps\n",
|
||
"\n",
|
||
"Now that you understand the basics of how to create a chatbot in LangChain, some more advanced tutorials you may be interested in are:\n",
|
||
"\n",
|
||
"- [Conversational RAG](/docs/tutorials/qa_chat_history): Enable a chatbot experience over an external source of data\n",
|
||
"- [Agents](/docs/tutorials/agents): Build a chatbot that can take actions\n",
|
||
"\n",
|
||
"If you want to dive deeper on specifics, some things worth checking out are:\n",
|
||
"\n",
|
||
"- [Streaming](/docs/how_to/streaming): streaming is *crucial* for chat applications\n",
|
||
"- [How to add message history](/docs/how_to/message_history): for a deeper dive into all things related to message history\n",
|
||
"- [How to manage large message history](/docs/how_to/trim_messages/): more techniques for managing a large chat history\n",
|
||
"- [LangGraph main docs](https://langchain-ai.github.io/langgraph/): for more detail on building with LangGraph"
|
||
]
|
||
}
|
||
],
|
||
"metadata": {
|
||
"kernelspec": {
|
||
"display_name": "Python 3 (ipykernel)",
|
||
"language": "python",
|
||
"name": "python3"
|
||
},
|
||
"language_info": {
|
||
"codemirror_mode": {
|
||
"name": "ipython",
|
||
"version": 3
|
||
},
|
||
"file_extension": ".py",
|
||
"mimetype": "text/x-python",
|
||
"name": "python",
|
||
"nbconvert_exporter": "python",
|
||
"pygments_lexer": "ipython3",
|
||
"version": "3.10.4"
|
||
}
|
||
},
|
||
"nbformat": 4,
|
||
"nbformat_minor": 4
|
||
}
|