admin管理员组文章数量:1130349
1. 学习背景
在LangChain for LLM应用程序开发中课程中,学习了LangChain框架扩展应用程序开发中语言模型的用例和功能的基本技能,遂做整理为后面的应用做准备。视频地址:基于LangChain的大语言模型应用开发+构建和评估。
2. 四种memory模式
本实验基于jupyternotebook进行。
2.1 ConversationBufferMemory
import warnings
warnings.filterwarnings('ignore')
from langchain.chat_models import ChatOpenAI
from langchain.chains import ConversationChain
from langchain.memory import ConversationBufferMemory
llm = ChatOpenAI(
api_key = "XXXX",
base_url = "XXXX",
temperature=0.0
)
memory = ConversationBufferMemory()
conversation = ConversationChain(
llm=llm,
memory = memory,
verbose=True #设置为True可以看到模型的具体思考过程
)
conversation.predict(input="Hi, my name is Andrew")
输出如下:
> Entering new ConversationChain chain...
Prompt after formatting:
The 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, my name is Andrew
AI:
> Finished chain.
"Hello Andrew! It's nice to meet you. My name is AI and I'm here to assist you with any questions or tasks you have. How can I help you today?"
继续测试
conversation.predict(input="What is 1+1?")
输出如下:
> Entering new ConversationChain chain...
Prompt after formatting:
The 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, my name is Andrew
AI: Hello Andrew! It's nice to meet you. My name is AI and I'm here to assist you with any questions or tasks you have. How can I help you today?
Human: What is 1+1?
AI:
> Finished chain.
'1+1 equals 2. This is a basic mathematical operation that can be solved by adding the two numbers together. Is there anything else I can assist you with?'
再次输入之前的问题
conversation.predict(input="What is my name?")
输出如下:
> Entering new ConversationChain chain...
Prompt after formatting:
The 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 ans1. 学习背景
在LangChain for LLM应用程序开发中课程中,学习了LangChain框架扩展应用程序开发中语言模型的用例和功能的基本技能,遂做整理为后面的应用做准备。视频地址:基于LangChain的大语言模型应用开发+构建和评估。
2. 四种memory模式
本实验基于jupyternotebook进行。
2.1 ConversationBufferMemory
import warnings
warnings.filterwarnings('ignore')
from langchain.chat_models import ChatOpenAI
from langchain.chains import ConversationChain
from langchain.memory import ConversationBufferMemory
llm = ChatOpenAI(
api_key = "XXXX",
base_url = "XXXX",
temperature=0.0
)
memory = ConversationBufferMemory()
conversation = ConversationChain(
llm=llm,
memory = memory,
verbose=True #设置为True可以看到模型的具体思考过程
)
conversation.predict(input="Hi, my name is Andrew")
输出如下:
> Entering new ConversationChain chain...
Prompt after formatting:
The 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, my name is Andrew
AI:
> Finished chain.
"Hello Andrew! It's nice to meet you. My name is AI and I'm here to assist you with any questions or tasks you have. How can I help you today?"
继续测试
conversation.predict(input="What is 1+1?")
输出如下:
> Entering new ConversationChain chain...
Prompt after formatting:
The 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, my name is Andrew
AI: Hello Andrew! It's nice to meet you. My name is AI and I'm here to assist you with any questions or tasks you have. How can I help you today?
Human: What is 1+1?
AI:
> Finished chain.
'1+1 equals 2. This is a basic mathematical operation that can be solved by adding the two numbers together. Is there anything else I can assist you with?'
再次输入之前的问题
conversation.predict(input="What is my name?")
输出如下:
> Entering new ConversationChain chain...
Prompt after formatting:
The 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 ans版权声明:本文标题:LangChain学习之四种Memory模式使用 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/jiaocheng/1754605377a2707714.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


发表评论