📘 **TELUS Agriculture & Consumer Goods** 如何通过 **Haystack Agents** 转变促销交易

教程:序列化 LLM 管道


概述

📚 相关文档: 序列化

序列化意味着将管道转换为您可以保存在磁盘上并在以后加载的格式。这尤其有用,因为序列化管道可以保存在磁盘或数据库中,通过网络发送等等。

虽然也可以序列化为其他格式,但 Haystack 开箱即用地支持 YAML,以便人类可以轻松地进行更改,而无需在 Python 代码之间来回操作。在本教程中,我们将用 Python 代码创建一个非常简单的管道,将其序列化为 YAML,对其进行修改,然后将其反序列化回 Haystack Pipeline

准备 Colab 环境

安装 Haystack

使用 pip 安装 Haystack

%%bash

pip install haystack-ai

创建简单的管道

首先,让我们创建一个非常简单的管道,该管道期望用户输入一个 topic,然后使用 Qwen/Qwen2.5-1.5B-Instruct 为该主题生成摘要。您可以随意修改管道。请注意,在此管道中,我们使用的是一个本地模型,该模型来自 Hugging Face。我们使用的是一个相对较小、开源的 LLM。

from haystack import Pipeline
from haystack.components.builders import ChatPromptBuilder
from haystack.dataclasses import ChatMessage
from haystack.components.generators.chat import HuggingFaceLocalChatGenerator

template = [
    ChatMessage.from_user(
        """
Please create a summary about the following topic:
{{ topic }}
"""
    )
]

builder = ChatPromptBuilder(template=template)
llm = HuggingFaceLocalChatGenerator(model="Qwen/Qwen2.5-1.5B-Instruct", generation_kwargs={"max_new_tokens": 150})

pipeline = Pipeline()
pipeline.add_component(name="builder", instance=builder)
pipeline.add_component(name="llm", instance=llm)

pipeline.connect("builder.prompt", "llm.messages")
topic = "Climate change"
result = pipeline.run(data={"builder": {"topic": topic}})
print(result["llm"]["replies"][0].text)

将管道序列化为 YAML

开箱即用地,Haystack 支持 YAML。使用 dumps() 将管道转换为 YAML

yaml_pipeline = pipeline.dumps()

print(yaml_pipeline)

您应该会得到一个看起来像下面的管道 YAML

components:
  builder:
    init_parameters:
      required_variables: null
      template:
      - _content:
        - text: '

            Please create a summary about the following topic:

            {{ topic }}

            '
        _meta: {}
        _name: null
        _role: user
      variables: null
    type: haystack.components.builders.chat_prompt_builder.ChatPromptBuilder
  llm:
    init_parameters:
      init_parameters:
      generation_kwargs:
        max_new_tokens: 150
        stop_sequences: []
      huggingface_pipeline_kwargs:
        device: cpu
        model: Qwen/Qwen2.5-1.5B-Instruct
        task: text-generation
      streaming_callback: null
      token:
        env_vars:
        - HF_API_TOKEN
        - HF_TOKEN
        strict: false
        type: env_var
    type: haystack.components.generators.chat.hugging_face_local.HuggingFaceLocalChatGenerator
connections:
- receiver: llm.messages
  sender: builder.prompt
max_runs_per_component: 100
metadata: {}

在 YAML 中编辑管道

让我们看看如何更改序列化管道。例如,下面,我们将修改 ChatPromptBuilder 的模板,将提供的 sentence 翻译成法语

yaml_pipeline = """
components:
  builder:
    init_parameters:
      template:
      - _content:
        - text: 'Please translate the following to French: \n{{ sentence }}\n'
        _meta: {}
        _name: null
        _role: user
      variables: null
    type: haystack.components.builders.chat_prompt_builder.ChatPromptBuilder
  llm:
    init_parameters:
      generation_kwargs:
        max_new_tokens: 150
        stop_sequences: []
      huggingface_pipeline_kwargs:
        device: cpu
        model: Qwen/Qwen2.5-1.5B-Instruct
        task: text-generation
      streaming_callback: null
      chat_template : "{% for message in messages %}{% if message['role'] == 'user' %}{{ ' ' }}{% endif %}{{ message['content'] }}{% if not loop.last %}{{ '  ' }}{% endif %}{% endfor %}{{ eos_token }}"
      token:
        env_vars:
        - HF_API_TOKEN
        - HF_TOKEN
        strict: false
        type: env_var
    type: haystack.components.generators.chat.hugging_face_local.HuggingFaceLocalChatGenerator
connections:
- receiver: llm.messages
  sender: builder.prompt
max_runs_per_component: 100
metadata: {}
"""

将 YAML 管道反序列化回 Python

您可以通过调用 loads() 来反序列化管道。下面,我们正在反序列化我们编辑过的 yaml_pipeline

from haystack import Pipeline

new_pipeline = Pipeline.loads(yaml_pipeline)

现在我们可以运行我们在 YAML 中定义的新管道。我们已经将其修改为使 ChatPromptBuilder 期望一个 sentence 并将句子翻译成法语

new_pipeline.run(data={"builder": {"sentence": "I love capybaras"}})

下一步

🎉 恭喜!您已将管道序列化为 YAML,对其进行了编辑并再次运行!

如果您喜欢这个教程,您可能还会喜欢

要及时了解最新的 Haystack 开发,您可以注册我们的新闻通讯。感谢您的阅读!