mirror of
https://github.com/langchain-ai/langchain.git
synced 2026-10-05 17:35:28 +03:00
12 KiB
12 KiB
In [1]:
from langchain.memory import ChatMessageHistory
history = ChatMessageHistory()
history.add_user_message("hi!")
history.add_ai_message("whats up?")In [5]:
history.messagesOut [5]:
[HumanMessage(content='hi!', additional_kwargs={}),
AIMessage(content='whats up?', additional_kwargs={})]In [7]:
from langchain.memory import ConversationBufferMemoryIn [10]:
memory = ConversationBufferMemory()
memory.chat_memory.add_user_message("hi!")
memory.chat_memory.add_ai_message("whats up?")In [12]:
memory.load_memory_variables({})Out [12]:
{'history': 'Human: hi!\nAI: whats up?'}In [13]:
memory = ConversationBufferMemory(return_messages=True)
memory.chat_memory.add_user_message("hi!")
memory.chat_memory.add_ai_message("whats up?")In [14]:
memory.load_memory_variables({})Out [14]:
{'history': [HumanMessage(content='hi!', additional_kwargs={}),
AIMessage(content='whats up?', additional_kwargs={})]}In [15]:
from langchain.llms import OpenAI
from langchain.chains import ConversationChain
llm = OpenAI(temperature=0)
conversation = ConversationChain(
llm=llm,
verbose=True,
memory=ConversationBufferMemory()
)In [16]:
conversation.predict(input="Hi there!")Out [16]:
[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 [17]:
conversation.predict(input="I'm doing well! Just having a conversation with an AI.")Out [17]:
[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 [18]:
conversation.predict(input="Tell me about yourself.")Out [18]:
[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 [1]:
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 [2]:
dicts = messages_to_dict(history.messages)In [3]:
dictsOut [3]:
[{'type': 'human', 'data': {'content': 'hi!', 'additional_kwargs': {}}},
{'type': 'ai', 'data': {'content': 'whats up?', 'additional_kwargs': {}}}]In [4]:
new_messages = messages_from_dict(dicts)In [5]:
new_messagesOut [5]:
[HumanMessage(content='hi!', additional_kwargs={}),
AIMessage(content='whats up?', additional_kwargs={})]In [ ]: