发布时间:2023-04-20 文章分类:电脑百科 投稿人:李佳 字号: 默认 | | 超大 打印

大语言模型集成工具 LangChain

LangChain 介绍

安装介绍

pip install langchain
pip install openai

LangChain 应用(基于0.0.64 版本测试)

获取 LLM 的预测 (QA 任务)

text = "What would be a good company name a company that makes colorful socks?"
print(llm(text)) # 返回 Socktastic!

简单数学问题:

from langchain.llms import OpenAI # 导入 LLM wrapper
llm = OpenAI(temperature=0.9) # 大的 temperature 会让输出有更多的随机性
text = "what is the results of 5+6?"
print(llm(text)) # 返回 11
text = "what is the results of 55+66?"
print(llm(text)) # 返回 121
text = "what is the results of 55555+66666?"
print(llm(text)) # 返回 122221
text = "what is the results of 512311+89749878?"
print(llm(text)) # 返回 89,876,189,终于错了...

另一个例子,这里返回的是同义词,如果要返回同音词则需要修改输入的 prompt(另外一个解决方式是基于以下章节中的 Memory 模式):

text = "what word is similar to good?"
print(llm(text)) # 返回 Excellent
text = "what word is homophone of good?"
print(llm(text)) # 返回 Goo

输入 prompts 模板设置

from langchain.prompts import PromptTemplate
prompt = PromptTemplate(
    input_variables=["product"],
    template="What is a good name for a company that makes {product}?",
)
print(prompt.format(product="colorful socks")) # 返回 What is a good name for a company that makes colorful socks?
text = prompt.format(product="colorful socks")
print(llm(text)) # 返回 Socktastic!
text = prompt.format(product="chocolates")
print(llm(text)) # 返回 ChocoDelightz!

Memory 功能: 在 LLM 交互中记录交互的历史状态,并基于历史状态修正模型预测

from langchain import OpenAI, ConversationChain
llm = OpenAI(temperature=0)
conversation = ConversationChain(llm=llm, verbose=True)
conversation.predict(input="Hi there!") # 返回如下
#> 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 there!
#AI:
#> Finished chain.
# Out[53]: " Hi there! It's nice to meet you. How can I help you today?"
conversation.predict(input="I'm doing well! Just having a conversation with an AI.") # 返回如下
#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 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:
#> Finished chain.
#Out[54]: " That's great! It's always nice to have a conversation with someone new. What would you like to talk about?"
conversation.predict(input="what word is similar to good?") # 返回 ' Synonyms for "good" include excellent, great, fine, and superb.'
conversation.predict(input="similar to means with similar pronunciation") # 返回 ' Ah, I see. Synonyms for "good" with similar pronunciation include wood, hood, and should.'

这里的实现看起来和 memprompt 非常类似,每个问题不会直接回答答案,而是回答 understating+answer,从而让用户可以基于对 understating 的理解来判断模型反馈是否符合用户的预期,而不用直接判断 answer 的正确性

总结