LangChain是基于大语言模型,提供很多工具组件,来构建语言模型应用的开发框架。我们可以用来开发聊天机器人、生成式问题回答(GQA)、摘要等应用。
框架的核心思想是,基于大语言模型,将不同的组件链接在一起,创建高级的应用。组件链由来自不同模块的组件构成,通常有下列的组件:
输入到LLMs的Prompt通常会以不同的方式结构化,以便我们能够获得不同的结果。对于问答,我们可以将用户的问题重新格式化,以适应不同的问答风格,例如传统的问答形式、答案的项目列表,甚至是与给定问题相关的问题摘要。
首先安装 langchain库
pip install langchain
导入PromptTemplate模块,然后创建模版
from langchain import PromptTemplate
template = """Question: {question}
Answer: """
prompt = PromptTemplate(
template=template,
input_variables=['question']
)
# user question
question = "Which NFL team won the Super Bowl in the 2010 season?"
最终基于模版和问题创建得到的Prompt如下:
Question: Which NFL team won the Super Bowl in the 2010 season? Answer:
huggging face类似github,上面提供了大量的免费预训练模型,LangChain也提供了对应的组件,可以访问huggging face上模型接口来进行问答。
在LangChain中,提供了Hugging Face模型访问组件,首先我们需要一个Hugging Face账户和API密钥[1]。
一旦你拥有API密钥,我们可以将其添加到HUGGINGFACEHUB_API_TOKEN环境变量中。我们可以使用Python完成此操作,代码如下
import os
os.environ['HUGGINGFACEHUB_API_TOKEN'] = 'HF_API_KEY'
接着安装hugging face库
pip install huggingface_hub
接下来使用模型google/flan-t5-x1进行文本生成,最终完整的代码如下:
from langchain import HuggingFaceHub, LLMChain
# initialize Hub LLM
hub_llm = HuggingFaceHub(
repo_id='google/flan-t5-xl',
model_kwargs={'temperature':1e-10}
)
# create prompt template > LLM chain
llm_chain = LLMChain(
prompt=prompt,
llm=hub_llm
)
# ask the user question about NFL 2010
print(llm_chain.run(question))
使用huggingface api[2]最终得到一个输出答案。
如果我们想提问多个问题,可以尝试两种方法:
首先,让我们看看如何使用generate方法:
qs = [
{'question': "Which NFL team won the Super Bowl in the 2010 season?"},
{'question': "If I am 6 ft 4 inches, how tall am I in centimeters?"},
{'question': "Who was the 12th person on the moon?"},
{'question': "How many eyes does a blade of grass have?"}
]
res = llm_chain.generate(qs)
除了第一个问题的答案,其余的问题结果外都不太好。如果模型无法准确回答单个问题,将所有查询都放在一个Prompt中也不太可能起作用。作为实验,我们可以试一试。
multi_template = """Answer the following questions one at a time.
Questions:
{questions}
Answers:
"""
long_prompt = PromptTemplate(template=multi_template, input_variables=["questions"])
llm_chain = LLMChain(
prompt=long_prompt,
llm=flan_t5
)
qs_str = (
"Which NFL team won the Super Bowl in the 2010 season?\n" +
"If I am 6 ft 4 inches, how tall am I in centimeters?\n" +
"Who was the 12th person on the moon?" +
"How many eyes does a blade of grass have?"
)
print(llm_chain.run(qs_str))
使用OpenAI接口,提前注册好账户,获得key
import os
os.environ['OPENAI_API_TOKEN'] = 'OPENAI_API_KEY'
接着安装OpenAI的接口库
pip install openai
现在我们可以使用OpenAI的接口来生成文本。例子中使用text-davinci-003模型。
from langchain.llms import OpenAI
davinci = OpenAI(model_name='text-davinci-003')
此外还可以通过Azure来使用
from langchain.llms import AzureOpenAI
llm = AzureOpenAI(
deployment_name="your-azure-deployment",
model_name="text-davinci-003"
)
我们将使用与之前Hugging Face示例相同的简单问题-答案Prompt模板。唯一的变化是使用的模型。
llm_chain = LLMChain(
prompt=prompt,
llm=davinci
)
print(llm_chain.run(question))
同样,我们可以使用generate方法发起多个提问
qs = [
{'question': "Which NFL team won the Super Bowl in the 2010 season?"},
{'question': "If I am 6 ft 4 inches, how tall am I in centimeters?"},
{'question': "Who was the 12th person on the moon?"},
{'question': "How many eyes does a blade of grass have?"}
]
llm_chain.generate(qs)
我们的大多数结果都是正确的或者有一定的真实性。这个模型无疑比google/flan-t5-xl模型更好地运行。像之前一样,让我们尝试一次把所有的问题都输入到模型中。
llm_chain = LLMChain(
prompt=long_prompt,
llm=davinci
)
qs_str = (
"Which NFL team won the Super Bowl in the 2010 season?\n" +
"If I am 6 ft 4 inches, how tall am I in centimeters?\n" +
"Who was the 12th person on the moon?" +
"How many eyes does a blade of grass have?"
)
print(llm_chain.run(qs_str))
这就是我们对LangChain的简单介绍——一个允许我们基于LLMs构建更高级应用程序的库,如OpenAI的GPT-3模型或通过Hugging Face提供的模型。
[1].Hugging Face API key
,https://zhuanlan.zhihu.com/p/671128034
[2].hugging face api quicktour rhttps://huggingface.co/docs/api-inference/quicktour