刚接触Langchain,整体感觉下来就是langchain中的事件顺序都是按照一定的套路来实现,例如常见的链LLMChain,需要使用到的大模型LLM和Langchain自带的PromptTemplate两块来实现,要求PromptTemplate有预留好下一步插入的内容。比如下面这个例子。
import os
os.environ["OPENAI_API_KEY"]="..."
from langchain.prompts import PromptTemplate
from langchain.llms import OpenAI
from langchain.chains import LLMChain
llm = OpenAI(temperature=0.9)
prompt = PromptTemplate(
input_variables=["product"],
template="What is a good name for a company that make {product}"
)
chain = LLMChain(llm=llm,prompt=prompt)
llama_full_prompt = PromptTemplate.from_template(
template="<s>[INST]<<SYS>>{sys_msg}<</SYS>>\n\nContext:\n{history}\n\nHuman: {input}\n[/INST] {primer}",
)
chain.run("colorful socks")
print(llama_full_prompt)
除此之外呢,像ConversationChain是让模型记住历史信息,
from langchain import OpenAI,ConversationChain
llm = OpenAI(temperature=0)
conversation = ConversationChain(llm=llm,verbose=True)
output = conversation.predict(input="Hi I'm barry")
print(output)
output2 = conversation.predict(input="Hello, What is my name?")
print(output2)
这里模型就会记住第一部输入的内容
但是有可能考虑到使用ConversationChain时会导致输入的内容超过模型输入的上下文,所以适用了ConversationSummaryMemory,就是让模型自己对过去的对话进行总结,但是这种方式可能会丢失一定的关键信息。
from langchain.chains import ConversationChain
from langchain.memory import ConversationSummaryMemory
from langchain.prompts import PromptTemplate
# from langchain import LLM, DefaultChainConfig
# 假设你已经有了一个可用的语言模型实例 llm
# llm = LLM(...)
# # 创建一个ConversationSummaryMemory实例
memory = ConversationSummaryMemory(llm=llm, temperature=0, verbose=True)
# 定义对话提示模板,包含input和history变量
conversation_prompt = PromptTemplate.from_template("""
You are a helpful and knowledgeable assistant.
{history}
Human: {input}
Assistant:
""")
# 定义摘要提示模板
summary_template = """Summary:
{summary}
Latest Chat:
{new_lines}
New Summary:
"""
print(summary_template)
# 从模板创建PromptTemplate实例
summary_prompt = PromptTemplate.from_template(template=summary_template)
print(summary_prompt)
# 使用更新后的摘要提示模板实例化ConversationSummaryMemory
memory = ConversationSummaryMemory(llm=llm, temperature=0, prompt=summary_prompt, verbose=True)
print(conversation_prompt)
print("#$@#$")
# 创建ConversationChain实例,使用提示模板和memory
conv_chain = ConversationChain(
llm=llm,
prompt=conversation_prompt, # 使用包含history变量的对话提示模板
memory=memory,
verbose=True
)
# 运行对话链,并传入用户的输入
print(conv_chain.run(input="Hello World! My name is John Doe"))
print(conv_chain.run(input="Who are you anyways?"))
print(conv_chain.run(input="What was the first thing I asked you?"))
print(conv_chain.run(input="What is my name?"))
这里主要涉及到两个部分,一个是使用conversation_prompt
,在这个prompt里面要包含**{history}和{input}**,另一个部分就是要配置好ConversationSummaryMemory,这个要是依赖于一个prompt,这个prompt必须包含{summary},{new_lines}这两个。
# # 创建一个ConversationSummaryMemory实例
memory = ConversationSummaryMemory(llm=llm, temperature=0, verbose=True)
# 定义摘要提示模板
summary_template = """Summary:
{summary}
Latest Chat:
{new_lines}
New Summary:
"""
# print(summary_template)
# 从模板创建PromptTemplate实例
summary_prompt = PromptTemplate.from_template(template=summary_template)
# print(summary_prompt)
# 使用更新后的摘要提示模板实例化ConversationSummaryMemory
memory = ConversationSummaryMemory(llm=llm, temperature=0, prompt=summary_prompt, verbose=True)