mirror of
https://github.com/hwchase17/langchain.git
synced 2025-06-04 22:23:50 +00:00
docs: update aws_dynamodb integration doc (#15666)
- **Description:** - Updated the docs for the memory integration module `aws_dynamodb.ipynb` - **Issue:** - #15664 - **Dependencies:** - N/A
This commit is contained in:
parent
32ec56194b
commit
167a0ac5f5
@ -9,25 +9,40 @@
|
|||||||
"\n",
|
"\n",
|
||||||
">[Amazon AWS DynamoDB](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/dynamodb/index.html) is a fully managed `NoSQL` database service that provides fast and predictable performance with seamless scalability.\n",
|
">[Amazon AWS DynamoDB](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/dynamodb/index.html) is a fully managed `NoSQL` database service that provides fast and predictable performance with seamless scalability.\n",
|
||||||
"\n",
|
"\n",
|
||||||
"This notebook goes over how to use `DynamoDB` to store chat message history."
|
"This notebook goes over how to use `DynamoDB` to store chat message history with `DynamoDBChatMessageHistory` class."
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
"id": "3f608be0",
|
"id": "9bcbd170",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"source": [
|
"source": [
|
||||||
"First make sure you have correctly configured the [AWS CLI](https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-configure.html). Then make sure you have installed `boto3`."
|
"## Setup\n",
|
||||||
|
"\n",
|
||||||
|
"First make sure you have correctly configured the [AWS CLI](https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-configure.html). Then make sure you have installed the `langchain-community` package, so we need to install that. We also need to install the `boto3` package.\n",
|
||||||
|
"\n",
|
||||||
|
"```bash\n",
|
||||||
|
"pip install -U langchain-community boto3\n",
|
||||||
|
"```"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"id": "fdec429d",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"It's also helpful (but not needed) to set up [LangSmith](https://smith.langchain.com/) for best-in-class observability"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": null,
|
"execution_count": null,
|
||||||
"id": "3a7e89c2-4c55-4a66-91ec-9bf9a37467eb",
|
"id": "47d3f725",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"!pip install boto3"
|
"# os.environ[\"LANGCHAIN_TRACING_V2\"] = \"true\"\n",
|
||||||
|
"# os.environ[\"LANGCHAIN_API_KEY\"] = getpass.getpass()"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@ -35,7 +50,9 @@
|
|||||||
"id": "030d784f",
|
"id": "030d784f",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"source": [
|
"source": [
|
||||||
"Next, create the `DynamoDB` Table where we will be storing messages:"
|
"## Create Table\n",
|
||||||
|
"\n",
|
||||||
|
"Now, create the `DynamoDB` Table where we will be storing messages:"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@ -99,20 +116,17 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 12,
|
"execution_count": 5,
|
||||||
"id": "64fc465e",
|
"id": "64fc465e",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [
|
"outputs": [
|
||||||
{
|
{
|
||||||
"data": {
|
"data": {
|
||||||
"text/plain": [
|
"text/plain": [
|
||||||
"[HumanMessage(content='hi!', additional_kwargs={}, example=False),\n",
|
"[HumanMessage(content='hi!'), AIMessage(content='whats up?')]"
|
||||||
" AIMessage(content='whats up?', additional_kwargs={}, example=False),\n",
|
|
||||||
" HumanMessage(content='hi!', additional_kwargs={}, example=False),\n",
|
|
||||||
" AIMessage(content='whats up?', additional_kwargs={}, example=False)]"
|
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
"execution_count": 12,
|
"execution_count": 5,
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"output_type": "execute_result"
|
"output_type": "execute_result"
|
||||||
}
|
}
|
||||||
@ -152,7 +166,7 @@
|
|||||||
"id": "97f8578a",
|
"id": "97f8578a",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"source": [
|
"source": [
|
||||||
"## DynamoDBChatMessageHistory With Different Keys Composite Keys\n",
|
"## DynamoDBChatMessageHistory With Composite Keys\n",
|
||||||
"The default key for DynamoDBChatMessageHistory is ```{\"SessionId\": self.session_id}```, but you can modify this to match your table design.\n",
|
"The default key for DynamoDBChatMessageHistory is ```{\"SessionId\": self.session_id}```, but you can modify this to match your table design.\n",
|
||||||
"\n",
|
"\n",
|
||||||
"### Primary Key Name\n",
|
"### Primary Key Name\n",
|
||||||
@ -181,16 +195,6 @@
|
|||||||
"text": [
|
"text": [
|
||||||
"0\n"
|
"0\n"
|
||||||
]
|
]
|
||||||
},
|
|
||||||
{
|
|
||||||
"data": {
|
|
||||||
"text/plain": [
|
|
||||||
"[HumanMessage(content='hello, composite dynamodb table!', additional_kwargs={}, example=False)]"
|
|
||||||
]
|
|
||||||
},
|
|
||||||
"execution_count": 14,
|
|
||||||
"metadata": {},
|
|
||||||
"output_type": "execute_result"
|
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"source": [
|
"source": [
|
||||||
@ -213,8 +217,27 @@
|
|||||||
"composite_table.meta.client.get_waiter(\"table_exists\").wait(TableName=\"CompositeTable\")\n",
|
"composite_table.meta.client.get_waiter(\"table_exists\").wait(TableName=\"CompositeTable\")\n",
|
||||||
"\n",
|
"\n",
|
||||||
"# Print out some data about the table.\n",
|
"# Print out some data about the table.\n",
|
||||||
"print(composite_table.item_count)\n",
|
"print(composite_table.item_count)"
|
||||||
"\n",
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 8,
|
||||||
|
"id": "f462660f",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"data": {
|
||||||
|
"text/plain": [
|
||||||
|
"[HumanMessage(content='hello, composite dynamodb table!')]"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"execution_count": 8,
|
||||||
|
"metadata": {},
|
||||||
|
"output_type": "execute_result"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
"my_key = {\n",
|
"my_key = {\n",
|
||||||
" \"PK\": \"session_id::0\",\n",
|
" \"PK\": \"session_id::0\",\n",
|
||||||
" \"SK\": \"langchain_history\",\n",
|
" \"SK\": \"langchain_history\",\n",
|
||||||
@ -234,112 +257,116 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
"id": "3b33c988",
|
"id": "131e42d2",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"source": [
|
"source": [
|
||||||
"## Agent with DynamoDB Memory"
|
"## Chaining\n",
|
||||||
|
"\n",
|
||||||
|
"We can easily combine this message history class with [LCEL Runnables](/docs/expression_language/how_to/message_history)\n",
|
||||||
|
"\n",
|
||||||
|
"To do this we will want to use OpenAI, so we need to install that"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 1,
|
"execution_count": null,
|
||||||
"id": "0264134f",
|
"id": "cbd0e91e",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"from langchain.agents import AgentType, Tool, initialize_agent\n",
|
"from typing import Optional\n",
|
||||||
"from langchain.memory import ConversationBufferMemory\n",
|
"\n",
|
||||||
"from langchain_experimental.utilities import PythonREPL\n",
|
"from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder\n",
|
||||||
|
"from langchain_core.runnables.history import RunnableWithMessageHistory\n",
|
||||||
"from langchain_openai import ChatOpenAI"
|
"from langchain_openai import ChatOpenAI"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 15,
|
"execution_count": null,
|
||||||
"id": "f92d9499",
|
"id": "d8cecb08",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"message_history = DynamoDBChatMessageHistory(table_name=\"SessionTable\", session_id=\"1\")\n",
|
"prompt = ChatPromptTemplate.from_messages(\n",
|
||||||
"memory = ConversationBufferMemory(\n",
|
" [\n",
|
||||||
" memory_key=\"chat_history\", chat_memory=message_history, return_messages=True\n",
|
" (\"system\", \"You are a helpful assistant.\"),\n",
|
||||||
")"
|
" MessagesPlaceholder(variable_name=\"history\"),\n",
|
||||||
]
|
" (\"human\", \"{question}\"),\n",
|
||||||
},
|
" ]\n",
|
||||||
{
|
")\n",
|
||||||
"cell_type": "code",
|
|
||||||
"execution_count": 16,
|
|
||||||
"id": "1167eeba",
|
|
||||||
"metadata": {},
|
|
||||||
"outputs": [],
|
|
||||||
"source": [
|
|
||||||
"python_repl = PythonREPL()\n",
|
|
||||||
"\n",
|
"\n",
|
||||||
"# You can create the tool to pass to an agent\n",
|
"chain = prompt | ChatOpenAI()"
|
||||||
"tools = [\n",
|
|
||||||
" Tool(\n",
|
|
||||||
" name=\"python_repl\",\n",
|
|
||||||
" description=\"A Python shell. Use this to execute python commands. Input should be a valid python command. If you want to see the output of a value, you should print it out with `print(...)`.\",\n",
|
|
||||||
" func=python_repl.run,\n",
|
|
||||||
" )\n",
|
|
||||||
"]"
|
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": null,
|
"execution_count": null,
|
||||||
"id": "06c6e5ba",
|
"id": "88ec540f",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"llm = ChatOpenAI(temperature=0)\n",
|
"chain_with_history = RunnableWithMessageHistory(\n",
|
||||||
"agent_chain = initialize_agent(\n",
|
" chain,\n",
|
||||||
" tools,\n",
|
" lambda session_id: DynamoDBChatMessageHistory(\n",
|
||||||
" llm,\n",
|
" table_name=\"SessionTable\", session_id=session_id\n",
|
||||||
" agent=AgentType.CHAT_CONVERSATIONAL_REACT_DESCRIPTION,\n",
|
" ),\n",
|
||||||
" verbose=True,\n",
|
" input_messages_key=\"question\",\n",
|
||||||
" memory=memory,\n",
|
" history_messages_key=\"history\",\n",
|
||||||
")"
|
")"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": null,
|
"execution_count": null,
|
||||||
"id": "952a3103",
|
"id": "9e9969fb",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"agent_chain.run(input=\"Hello!\")"
|
"# This is where we configure the session id\n",
|
||||||
|
"config = {\"configurable\": {\"session_id\": \"<SESSION_ID>\"}}"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": null,
|
"execution_count": 9,
|
||||||
"id": "54c4aaf4",
|
"id": "eb73f547",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [
|
||||||
|
{
|
||||||
|
"data": {
|
||||||
|
"text/plain": [
|
||||||
|
"AIMessage(content='Hello Bob! How can I assist you today?')"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"execution_count": 9,
|
||||||
|
"metadata": {},
|
||||||
|
"output_type": "execute_result"
|
||||||
|
}
|
||||||
|
],
|
||||||
"source": [
|
"source": [
|
||||||
"agent_chain.run(input=\"Who owns Twitter?\")"
|
"chain_with_history.invoke({\"question\": \"Hi! I'm bob\"}, config=config)"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": null,
|
"execution_count": 10,
|
||||||
"id": "f9013118",
|
"id": "7daa3508",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [
|
||||||
|
{
|
||||||
|
"data": {
|
||||||
|
"text/plain": [
|
||||||
|
"AIMessage(content='Your name is Bob! Is there anything specific you would like assistance with, Bob?')"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"execution_count": 10,
|
||||||
|
"metadata": {},
|
||||||
|
"output_type": "execute_result"
|
||||||
|
}
|
||||||
|
],
|
||||||
"source": [
|
"source": [
|
||||||
"agent_chain.run(input=\"My name is Bob.\")"
|
"chain_with_history.invoke({\"question\": \"Whats my name\"}, config=config)"
|
||||||
]
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"cell_type": "code",
|
|
||||||
"execution_count": null,
|
|
||||||
"id": "405e5315",
|
|
||||||
"metadata": {},
|
|
||||||
"outputs": [],
|
|
||||||
"source": [
|
|
||||||
"agent_chain.run(input=\"Who am I?\")"
|
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
|
Loading…
Reference in New Issue
Block a user