(20-2-02)基于《哈利·波特》系列图书内容的问答系统:语言模型的集成与优化(2)

8.4.3  pipeline处理

Hugging Face中的 pipeline 是一个强大的工具,它允许研究人员和开发人员快速、轻松地使用预训练模型来执行各种自然语言处理(NLP)任务。pipeline 的设计目的是简化模型的调用过程,使得NLP应用的开发变得更加直观和高效。

(1)使用Hugging Face中的函数pipeline创建一个实现文本生成任务的管道,并将该管道封装为langchain库中的HuggingFacePipeline对象。通过这种方式,可以将Hugging Face大模型集成到我们的NLP项目中,同时利用 langchain 提供的额外功能和结构。

pipe = pipeline(
    task = "text-generation",
    model = model,
    tokenizer = tokenizer,
    pad_token_id = tokenizer.eos_token_id,
#     do_sample = True,
    max_length = max_len,
    temperature = CFG.temperature,
    top_p = CFG.top_p,
    repetition_penalty = CFG.repetition_penalty
)

#langchain pipeline
llm = HuggingFacePipeline(pipeline = pipe)
llm

在上述代码中,最后的 llm 是创建的 HuggingFacePipeline 实例,可以用于生成文本或集成到更大的NLP工作流程中。执行后会输出:

HuggingFacePipeline(pipeline=<transformers.pipelines.text_generation.TextGenerationPipeline object at 0x795f44be5c60>)

(2)使用langchain库中的HuggingFacePipeline对象(在这个上下文中是llm)调用一个预定义的NLP任务,具体操作是使用之前配置的文本生成模型来生成文本。

%%time
query = "Give me 5 examples of cool potions and explain what they do"
llm.invoke(query)

执行上述代码后,模型将基于它的训练数据和配置的参数(如温度、最大长度等)生成文本。由于现在查询与《哈利·波特》系列无关,所以生成的答案将基于模型的通用知识,而不是特定于《哈利·波特》书籍的内容。执行后会输出:

CPU times: user 50.8 s, sys: 234 ms, total: 51 s
Wall time: 43 s
'.\n\nSure thing! Here are five examples of cool potions that you might find in a fantasy world, along with their effects:\n\n1. Potion of Healing: This potion restores health to the drinker, healing wounds and injuries. It might also grant temporary immunity to future damage or disease.\n2. Potion of Strength: This potion grants the drinker increased physical strength and endurance for a short period of time, allowing them to lift heavier objects, run faster, and fight longer.\n3. Potion of Speed: This potion allows the drinker to move at incredible speeds for a short period of time, making it easier to escape danger or chase down enemies.\n4. Potion of Invisibility: This potion makes the drinker temporarily invisible, allowing them to sneak past guards, avoid detection by monsters, or steal valuable items without being caught.\n5. Potion of Flight: This potion gives the drinker the ability to fly for a short period of time, allowing them to traverse long distances quickly, reach high places, or escape from dangerous situations.\n\nThese are just a few examples of the many cool potions that you might find in a fantasy world. Depending on the setting and the story, there could be countless other types of potions with different effects, such as potions of teleportation, potions of illusion, or potions of transformation. The possibilities are endless!'

未完待续

  • 8
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

码农三叔

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值