mirror of
https://github.com/langchain-ai/langchain.git
synced 2026-10-06 09:55:21 +03:00
Co-authored-by: Ankush Gola <9536492+agola11@users.noreply.github.com>
24 KiB
24 KiB
In [1]:
from langchain.llms import OpenAI
from langchain.chains import ConversationChain
from langchain.chains.conversation.memory import ConversationBufferMemory
llm = OpenAI(temperature=0)
conversation = ConversationChain(
llm=llm,
verbose=True,
memory=ConversationBufferMemory()
)In [2]:
conversation.predict(input="Hi there!")Out [2]:
[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 [3]:
conversation.predict(input="I'm doing well! Just having a conversation with an AI.")Out [3]:
[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 [4]:
conversation.predict(input="Tell me about yourself.")Out [4]:
[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 ConversationChain 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 [5]:
from langchain.chains.conversation.memory import ConversationSummaryMemoryIn [6]:
conversation_with_summary = ConversationChain(
llm=llm,
memory=ConversationSummaryMemory(llm=OpenAI()),
verbose=True
)
conversation_with_summary.predict(input="Hi, what's up?")Out [6]:
[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, what's up? AI:[0m [1m> Finished ConversationChain chain.[0m
" Hi there! I'm doing great. I'm currently helping a customer with a technical issue. How about you?"
In [7]:
conversation_with_summary.predict(input="Tell me more about it!")Out [7]:
[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: The human greets the AI and the AI responds, saying it is doing well and is currently helping a customer with a technical issue. Human: Tell me more about it! AI:[0m [1m> Finished ConversationChain chain.[0m
" Sure! The customer is having trouble with their computer not connecting to the internet. I'm helping them troubleshoot the issue and figure out what the problem is. So far, we've tried resetting the router and checking the network settings, but the issue still persists. We're currently looking into other possible causes."
In [8]:
conversation_with_summary.predict(input="Very cool -- what is the scope of the project?")Out [8]:
[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: The human greets the AI and the AI responds, saying it is doing well and is currently helping a customer with a technical issue. The customer is having trouble with their computer not connecting to the internet, and the AI is helping them troubleshoot the issue by resetting the router and checking the network settings. They are still looking into other possible causes. Human: Very cool -- what is the scope of the project? AI:[0m [1m> Finished ConversationChain chain.[0m
' The scope of the project is to help the customer troubleshoot the issue with their computer not connecting to the internet. We are currently resetting the router and checking the network settings, and we are looking into other possible causes.'
In [9]:
from langchain.chains.conversation.memory import ConversationBufferWindowMemoryIn [10]:
conversation_with_summary = ConversationChain(
llm=llm,
# We set a low k=2, to only keep the last 2 interactions in memory
memory=ConversationBufferWindowMemory(k=2),
verbose=True
)
conversation_with_summary.predict(input="Hi, what's up?")Out [10]:
[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, what's up? AI:[0m [1m> Finished ConversationChain chain.[0m
" Hi there! I'm doing great. I'm currently helping a customer with a technical issue. How about you?"
In [11]:
conversation_with_summary.predict(input="What's their issues?")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, what's up? AI: Hi there! I'm doing great. I'm currently helping a customer with a technical issue. How about you? Human: What's their issues? AI:[0m [1m> Finished ConversationChain chain.[0m
" The customer is having trouble connecting to their Wi-Fi network. I'm helping them troubleshoot the issue and get them connected."
In [12]:
conversation_with_summary.predict(input="Is it going well?")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, what's up? AI: Hi there! I'm doing great. I'm currently helping a customer with a technical issue. How about you? Human: What's their issues? AI: The customer is having trouble connecting to their Wi-Fi network. I'm helping them troubleshoot the issue and get them connected. Human: Is it going well? AI:[0m [1m> Finished ConversationChain chain.[0m
" Yes, it's going well so far. We've already identified the problem and are now working on a solution."
In [13]:
# Notice here that the first interaction does not appear.
conversation_with_summary.predict(input="What's the solution?")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: What's their issues? AI: The customer is having trouble connecting to their Wi-Fi network. I'm helping them troubleshoot the issue and get them connected. Human: Is it going well? AI: Yes, it's going well so far. We've already identified the problem and are now working on a solution. Human: What's the solution? AI:[0m [1m> Finished ConversationChain chain.[0m
" The solution is to reset the router and reconfigure the settings. We're currently in the process of doing that."
In [14]:
from langchain.chains.conversation.memory import ConversationSummaryBufferMemoryIn [15]:
conversation_with_summary = ConversationChain(
llm=llm,
# We set a very low max_token_limit for the purposes of testing.
memory=ConversationSummaryBufferMemory(llm=OpenAI(), max_token_limit=40),
verbose=True
)
conversation_with_summary.predict(input="Hi, what's up?")Out [15]:
[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, what's up? AI:[0m [1m> Finished ConversationChain chain.[0m
" Hi there! I'm doing great. I'm currently helping a customer with a technical issue. How about you?"
In [16]:
conversation_with_summary.predict(input="Just working on writing some documentation!")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, what's up? AI: Hi there! I'm doing great. I'm currently helping a customer with a technical issue. How about you? Human: Just working on writing some documentation! AI:[0m [1m> Finished ConversationChain chain.[0m
' That sounds like a lot of work. What kind of documentation are you writing?'
In [17]:
# We can see here that there is a summary of the conversation and then some previous interactions
conversation_with_summary.predict(input="For LangChain! Have you heard of it?")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: The human asked the AI what it was up to, and the AI responded that it was helping a customer with a technical issue. Human: Just working on writing some documentation! AI: That sounds like a lot of work. What kind of documentation are you writing? Human: For LangChain! Have you heard of it? AI:[0m [1m> Finished ConversationChain chain.[0m
' Yes, I have heard of LangChain. It is a blockchain-based language learning platform. Can you tell me more about the documentation you are writing?'
In [18]:
# We can see here that the summary and the buffer are updated
conversation_with_summary.predict(input="Haha nope, although a lot of people confuse it for that")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: The human asked the AI what it was up to, and the AI responded that it was helping a customer with a technical issue. The human then mentioned they were writing documentation for LangChain, a blockchain-based language learning platform, and the AI revealed they had heard of it and asked the human to tell them more about the documentation they were writing. Human: Haha nope, although a lot of people confuse it for that AI:[0m [1m> Finished ConversationChain chain.[0m
' Oh, I see. So, what kind of documentation are you writing for LangChain?'
In [ ]: