docs: update aws_dynamodb integration doc (#15666)

- **Description:** 
- Updated the docs for the memory integration module
`aws_dynamodb.ipynb`
  - **Issue:** 
    - #15664 
  - **Dependencies:** 
    - N/A
pull/15723/head
Kane Sweet 6 months ago committed by GitHub
parent 32ec56194b
commit 167a0ac5f5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -9,25 +9,40 @@
"\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",
"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",
"id": "3f608be0",
"id": "9bcbd170",
"metadata": {},
"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",
"execution_count": null,
"id": "3a7e89c2-4c55-4a66-91ec-9bf9a37467eb",
"id": "47d3f725",
"metadata": {},
"outputs": [],
"source": [
"!pip install boto3"
"# os.environ[\"LANGCHAIN_TRACING_V2\"] = \"true\"\n",
"# os.environ[\"LANGCHAIN_API_KEY\"] = getpass.getpass()"
]
},
{
@ -35,7 +50,9 @@
"id": "030d784f",
"metadata": {},
"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",
"execution_count": 12,
"execution_count": 5,
"id": "64fc465e",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[HumanMessage(content='hi!', additional_kwargs={}, example=False),\n",
" 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)]"
"[HumanMessage(content='hi!'), AIMessage(content='whats up?')]"
]
},
"execution_count": 12,
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
@ -152,7 +166,7 @@
"id": "97f8578a",
"metadata": {},
"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",
"\n",
"### Primary Key Name\n",
@ -181,16 +195,6 @@
"text": [
"0\n"
]
},
{
"data": {
"text/plain": [
"[HumanMessage(content='hello, composite dynamodb table!', additional_kwargs={}, example=False)]"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
@ -213,8 +217,27 @@
"composite_table.meta.client.get_waiter(\"table_exists\").wait(TableName=\"CompositeTable\")\n",
"\n",
"# Print out some data about the table.\n",
"print(composite_table.item_count)\n",
"\n",
"print(composite_table.item_count)"
]
},
{
"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",
" \"PK\": \"session_id::0\",\n",
" \"SK\": \"langchain_history\",\n",
@ -234,112 +257,116 @@
},
{
"cell_type": "markdown",
"id": "3b33c988",
"id": "131e42d2",
"metadata": {},
"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",
"execution_count": 1,
"id": "0264134f",
"execution_count": null,
"id": "cbd0e91e",
"metadata": {},
"outputs": [],
"source": [
"from langchain.agents import AgentType, Tool, initialize_agent\n",
"from langchain.memory import ConversationBufferMemory\n",
"from langchain_experimental.utilities import PythonREPL\n",
"from typing import Optional\n",
"\n",
"from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder\n",
"from langchain_core.runnables.history import RunnableWithMessageHistory\n",
"from langchain_openai import ChatOpenAI"
]
},
{
"cell_type": "code",
"execution_count": 15,
"id": "f92d9499",
"metadata": {},
"outputs": [],
"source": [
"message_history = DynamoDBChatMessageHistory(table_name=\"SessionTable\", session_id=\"1\")\n",
"memory = ConversationBufferMemory(\n",
" memory_key=\"chat_history\", chat_memory=message_history, return_messages=True\n",
")"
]
},
{
"cell_type": "code",
"execution_count": 16,
"id": "1167eeba",
"execution_count": null,
"id": "d8cecb08",
"metadata": {},
"outputs": [],
"source": [
"python_repl = PythonREPL()\n",
"prompt = ChatPromptTemplate.from_messages(\n",
" [\n",
" (\"system\", \"You are a helpful assistant.\"),\n",
" MessagesPlaceholder(variable_name=\"history\"),\n",
" (\"human\", \"{question}\"),\n",
" ]\n",
")\n",
"\n",
"# You can create the tool to pass to an agent\n",
"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",
"]"
"chain = prompt | ChatOpenAI()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "06c6e5ba",
"id": "88ec540f",
"metadata": {},
"outputs": [],
"source": [
"llm = ChatOpenAI(temperature=0)\n",
"agent_chain = initialize_agent(\n",
" tools,\n",
" llm,\n",
" agent=AgentType.CHAT_CONVERSATIONAL_REACT_DESCRIPTION,\n",
" verbose=True,\n",
" memory=memory,\n",
"chain_with_history = RunnableWithMessageHistory(\n",
" chain,\n",
" lambda session_id: DynamoDBChatMessageHistory(\n",
" table_name=\"SessionTable\", session_id=session_id\n",
" ),\n",
" input_messages_key=\"question\",\n",
" history_messages_key=\"history\",\n",
")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "952a3103",
"metadata": {},
"outputs": [],
"source": [
"agent_chain.run(input=\"Hello!\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "54c4aaf4",
"id": "9e9969fb",
"metadata": {},
"outputs": [],
"source": [
"agent_chain.run(input=\"Who owns Twitter?\")"
"# This is where we configure the session id\n",
"config = {\"configurable\": {\"session_id\": \"<SESSION_ID>\"}}"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f9013118",
"execution_count": 9,
"id": "eb73f547",
"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": [
"agent_chain.run(input=\"My name is Bob.\")"
"chain_with_history.invoke({\"question\": \"Hi! I'm bob\"}, config=config)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "405e5315",
"execution_count": 10,
"id": "7daa3508",
"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": [
"agent_chain.run(input=\"Who am I?\")"
"chain_with_history.invoke({\"question\": \"Whats my name\"}, config=config)"
]
}
],

Loading…
Cancel
Save