mirror of
https://github.com/langchain-ai/langchain.git
synced 2026-10-05 17:35:28 +03:00
Simply fixing a small typo in the memory page. Also removed an extra code block at the end of the file. Along the way, the current outputs seem to have changed in a few places so left that for posterity, and updated the number of runs which seems harmless, though I can clean that up if preferred.
12 KiB
12 KiB
In [2]:
from langchain.memory import ChatMessageHistory
history = ChatMessageHistory()
history.add_user_message("hi!")
history.add_ai_message("whats up?")In [3]:
history.messagesOut [3]:
[HumanMessage(content='hi!', additional_kwargs={}, example=False),
AIMessage(content='whats up?', additional_kwargs={}, example=False)]In [4]:
from langchain.memory import ConversationBufferMemoryIn [5]:
memory = ConversationBufferMemory()
memory.chat_memory.add_user_message("hi!")
memory.chat_memory.add_ai_message("whats up?")In [7]:
memory.load_memory_variables({})Out [7]:
{'history': 'Human: hi!\nAI: whats up?'}In [8]:
memory = ConversationBufferMemory(return_messages=True)
memory.chat_memory.add_user_message("hi!")
memory.chat_memory.add_ai_message("whats up?")In [9]:
memory.load_memory_variables({})Out [9]:
{'history': [HumanMessage(content='hi!', additional_kwargs={}, example=False),
AIMessage(content='whats up?', additional_kwargs={}, example=False)]}In [10]:
from langchain.llms import OpenAI
from langchain.chains import ConversationChain
llm = OpenAI(temperature=0)
conversation = ConversationChain(
llm=llm,
verbose=True,
memory=ConversationBufferMemory()
)In [11]:
conversation.predict(input="Hi there!")Out [11]:
[1m> Entering new ConversationChain chain...[0m Prompt after formatting: [32;1m[1;3mThe following is a friendly conversation between a human and an AI. The AI is talkative and provides lots of specific details from its context. If the AI does not know the answer to a question, it truthfully says it does not know. Current conversation: Human: Hi there! AI:[0m [1m> Finished chain.[0m
" Hi there! It's nice to meet you. How can I help you today?"
In [12]:
conversation.predict(input="I'm doing well! Just having a conversation with an AI.")Out [12]:
[1m> Entering new ConversationChain chain...[0m Prompt after formatting: [32;1m[1;3mThe following is a friendly conversation between a human and an AI. The AI is talkative and provides lots of specific details from its context. If the AI does not know the answer to a question, it truthfully says it does not know. Current conversation: Human: Hi there! AI: Hi there! It's nice to meet you. How can I help you today? Human: I'm doing well! Just having a conversation with an AI. AI:[0m [1m> Finished chain.[0m
" That's great! It's always nice to have a conversation with someone new. What would you like to talk about?"
In [13]:
conversation.predict(input="Tell me about yourself.")Out [13]:
[1m> Entering new ConversationChain chain...[0m Prompt after formatting: [32;1m[1;3mThe following is a friendly conversation between a human and an AI. The AI is talkative and provides lots of specific details from its context. If the AI does not know the answer to a question, it truthfully says it does not know. Current conversation: Human: Hi there! AI: Hi there! It's nice to meet you. How can I help you today? Human: I'm doing well! Just having a conversation with an AI. AI: That's great! It's always nice to have a conversation with someone new. What would you like to talk about? Human: Tell me about yourself. AI:[0m [1m> Finished chain.[0m
" Sure! I'm an AI created to help people with their everyday tasks. I'm programmed to understand natural language and provide helpful information. I'm also constantly learning and updating my knowledge base so I can provide more accurate and helpful answers."
In [14]:
import json
from langchain.memory import ChatMessageHistory
from langchain.schema import messages_from_dict, messages_to_dict
history = ChatMessageHistory()
history.add_user_message("hi!")
history.add_ai_message("whats up?")In [15]:
dicts = messages_to_dict(history.messages)In [16]:
dictsOut [16]:
[{'type': 'human',
'data': {'content': 'hi!', 'additional_kwargs': {}, 'example': False}},
{'type': 'ai',
'data': {'content': 'whats up?', 'additional_kwargs': {}, 'example': False}}]In [17]:
new_messages = messages_from_dict(dicts)In [18]:
new_messagesOut [18]:
[HumanMessage(content='hi!', additional_kwargs={}, example=False),
AIMessage(content='whats up?', additional_kwargs={}, example=False)]