mirror of
https://github.com/hwchase17/langchain
synced 2024-11-06 03:20:49 +00:00
add docs for save/load messages (#1697)
This commit is contained in:
parent
ced412e1c1
commit
3ea6d9c4d2
@ -30,36 +30,12 @@
|
|||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"from langchain.memory import ChatMessageHistory"
|
"from langchain.memory import ChatMessageHistory\n",
|
||||||
]
|
"\n",
|
||||||
},
|
"history = ChatMessageHistory()\n",
|
||||||
{
|
"\n",
|
||||||
"cell_type": "code",
|
"history.add_user_message(\"hi!\")\n",
|
||||||
"execution_count": 2,
|
"\n",
|
||||||
"id": "4404d509",
|
|
||||||
"metadata": {},
|
|
||||||
"outputs": [],
|
|
||||||
"source": [
|
|
||||||
"history = ChatMessageHistory()"
|
|
||||||
]
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"cell_type": "code",
|
|
||||||
"execution_count": 3,
|
|
||||||
"id": "78c1a67b",
|
|
||||||
"metadata": {},
|
|
||||||
"outputs": [],
|
|
||||||
"source": [
|
|
||||||
"history.add_user_message(\"hi!\")"
|
|
||||||
]
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"cell_type": "code",
|
|
||||||
"execution_count": 4,
|
|
||||||
"id": "525ce606",
|
|
||||||
"metadata": {},
|
|
||||||
"outputs": [],
|
|
||||||
"source": [
|
|
||||||
"history.add_ai_message(\"whats up?\")"
|
"history.add_ai_message(\"whats up?\")"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
@ -331,6 +307,99 @@
|
|||||||
"conversation.predict(input=\"Tell me about yourself.\")"
|
"conversation.predict(input=\"Tell me about yourself.\")"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"id": "fb68bb9e",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## Saving Message History\n",
|
||||||
|
"\n",
|
||||||
|
"You may often to save messages, and then load them to use again. This can be done easily by first converting the messages to normal python dictionaries, saving those (as json or something) and then loading those. Here is an example of doing that."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 1,
|
||||||
|
"id": "b5acbc4b",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"import json\n",
|
||||||
|
"\n",
|
||||||
|
"from langchain.memory import ChatMessageHistory\n",
|
||||||
|
"from langchain.schema import messages_from_dict, messages_to_dict\n",
|
||||||
|
"\n",
|
||||||
|
"history = ChatMessageHistory()\n",
|
||||||
|
"\n",
|
||||||
|
"history.add_user_message(\"hi!\")\n",
|
||||||
|
"\n",
|
||||||
|
"history.add_ai_message(\"whats up?\")"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 2,
|
||||||
|
"id": "7812ee21",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"dicts = messages_to_dict(history.messages)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 3,
|
||||||
|
"id": "3ed6e6a0",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"data": {
|
||||||
|
"text/plain": [
|
||||||
|
"[{'type': 'human', 'data': {'content': 'hi!', 'additional_kwargs': {}}},\n",
|
||||||
|
" {'type': 'ai', 'data': {'content': 'whats up?', 'additional_kwargs': {}}}]"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"execution_count": 3,
|
||||||
|
"metadata": {},
|
||||||
|
"output_type": "execute_result"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"dicts"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 4,
|
||||||
|
"id": "cdf4ebd2",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"new_messages = messages_from_dict(dicts)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 5,
|
||||||
|
"id": "9724e24b",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"data": {
|
||||||
|
"text/plain": [
|
||||||
|
"[HumanMessage(content='hi!', additional_kwargs={}),\n",
|
||||||
|
" AIMessage(content='whats up?', additional_kwargs={})]"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"execution_count": 5,
|
||||||
|
"metadata": {},
|
||||||
|
"output_type": "execute_result"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"new_messages"
|
||||||
|
]
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
"id": "7826c210",
|
"id": "7826c210",
|
||||||
|
@ -84,15 +84,15 @@ class ChatMessage(BaseMessage):
|
|||||||
return "chat"
|
return "chat"
|
||||||
|
|
||||||
|
|
||||||
def _message_to_json(message: BaseMessage) -> dict:
|
def _message_to_dict(message: BaseMessage) -> dict:
|
||||||
return {"type": message.type, "data": message.dict()}
|
return {"type": message.type, "data": message.dict()}
|
||||||
|
|
||||||
|
|
||||||
def messages_to_json(messages: List[BaseMessage]) -> List[dict]:
|
def messages_to_dict(messages: List[BaseMessage]) -> List[dict]:
|
||||||
return [_message_to_json(m) for m in messages]
|
return [_message_to_dict(m) for m in messages]
|
||||||
|
|
||||||
|
|
||||||
def _message_from_json(message: dict) -> BaseMessage:
|
def _message_from_dict(message: dict) -> BaseMessage:
|
||||||
_type = message["type"]
|
_type = message["type"]
|
||||||
if _type == "human":
|
if _type == "human":
|
||||||
return HumanMessage(**message["data"])
|
return HumanMessage(**message["data"])
|
||||||
@ -106,8 +106,8 @@ def _message_from_json(message: dict) -> BaseMessage:
|
|||||||
raise ValueError(f"Got unexpected type: {_type}")
|
raise ValueError(f"Got unexpected type: {_type}")
|
||||||
|
|
||||||
|
|
||||||
def messages_from_json(messages: List[dict]) -> List[BaseMessage]:
|
def messages_from_dict(messages: List[dict]) -> List[BaseMessage]:
|
||||||
return [_message_from_json(m) for m in messages]
|
return [_message_from_dict(m) for m in messages]
|
||||||
|
|
||||||
|
|
||||||
class ChatGeneration(Generation):
|
class ChatGeneration(Generation):
|
||||||
|
Loading…
Reference in New Issue
Block a user