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 ans