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!'