LangChain简介

LangChain是什么

LangChain是基于大语言模型,提供很多工具组件,来构建语言模型应用的开发框架。我们可以用来开发聊天机器人、生成式问题回答(GQA)、摘要等应用。

框架的核心思想是,基于大语言模型,将不同的组件链接在一起,创建高级的应用。组件链由来自不同模块的组件构成,通常有下列的组件:

  • Prompt模板:提示模板是不同类型提示的模板。例如“聊天机器人”风格的模板,问答等。
  • LLMs:像GPT-3,BLOOM等大型语言模型。
  • Agents:代理使用LLMs来决定应该采取哪些操作。可以使用像网页搜索或计算器等工具,所有的工具都被打包成一个操作的逻辑循环。
  • Memory:短期记忆,长期记忆。

Prompt模版组件

输入到LLMs的Prompt通常会以不同的方式结构化,以便我们能够获得不同的结果。对于问答,我们可以将用户的问题重新格式化,以适应不同的问答风格,例如传统的问答形式、答案的项目列表,甚至是与给定问题相关的问题摘要。

使用LangChain创建Prompts

首先安装 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:

Hugging Face Hub LLM

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方法逐一回答所有问题。
  • 将所有问题放入单个Prompt,这个需要看所使用的LLM的支持情况确定。

首先,让我们看看如何使用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 LLMs

使用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

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值