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
import boto3
import os
from langchain_aws.chat_models.bedrock import ChatBedrock
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.output_parsers import StrOutputParser
from langchain_core.runnables.history import RunnableWithMessageHistory
from langchain_community.chat_message_histories import DynamoDBChatMessageHistory,
from langchain_core.runnables import ConfigurableFieldSpec
region = "us-east-1"
session = boto3.session.Session()
bedrock_client = session.client("bedrock-runtime", region_name=region)
model_id = "anthropic.<model-id>"
user_id = "user_id1"
conversation_id = "conversation_id1"
def get_session_history(user_id: str, conversation_id: str):
return DynamoDBChatMessageHistory(
user_id=user_id,
conversation_id=conversation_id,
table_name="<test-table-name>"
)
llm = ChatBedrock(**{"client": bedrock_client, "model_id": model_id, "streaming": False})
system_prompt = """
Human: You are a friendly AI assistant. Try to be brief in your response.
Here is the current chat history:
{history}
Question: {input}
Assistant:
"""
prompt = ChatPromptTemplate.from_messages(
[
("system", system_prompt),
("placeholder", "{history}"),
("human", "{input}"),
]
)
chain = prompt | llm | StrOutputParser()
with_message_history = RunnableWithMessageHistory(
chain,
get_session_history=get_session_history,
input_messages_key="input",
history_messages_key="history",
history_factory_config=[
ConfigurableFieldSpec(
id="user_id",
annotation=str,
name="User ID",
description="Unique identifier for the user.",
default="",
is_shared=True,
),
ConfigurableFieldSpec(
id="conversation_id",
annotation=str,
name="Conversation ID",
description="Unique identifier for the conversation.",
default="",
is_shared=True,
),
],
)
response1 = with_message_history.invoke(
{
"input": "what is the highest peak in the world?",
},
config={
"configurable": {"user_id": user_id, "conversation_id": conversation_id}
},
)
print(response1) # The highest peak in the world is Mount Everest.
response2 = with_message_history.invoke(
{
"input": "how can I climb it?",
},
config={
"configurable": {"user_id": user_id, "conversation_id": conversation_id}
},
)
print(response2) # I do not have enough context to provide a specific recommendation for how to climb something. Could you provide more details about what you are trying to climb? Providing more information would help me give a more helpful response.
I assumed that the point of using RunnableWithMessageHistory is that it takes care of memory management. But the DynamoDB memory/messages is not updated like it should be and the next conversation has no idea of the history. The history is basically empty for the second round of conversation. How do I manage memory? Do I manually add messages to the memory store? The point of RunnableWithMessageHistory is just to retrieve and replace memory, but not update it?
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
I have the following code:
I assumed that the point of using
RunnableWithMessageHistory
is that it takes care of memory management. But the DynamoDB memory/messages is not updated like it should be and the next conversation has no idea of the history. The history is basically empty for the second round of conversation. How do I manage memory? Do I manually add messages to the memory store? The point ofRunnableWithMessageHistory
is just to retrieve and replace memory, but not update it?Beta Was this translation helpful? Give feedback.
All reactions