SagemakerGenerator
此组件支持使用部署在 Amazon Sagemaker 上的 LLM 进行文本生成。
| pipeline 中的最常见位置 | 在 PromptBuilder 之后 |
| 必需的初始化变量 | "model": 要使用的模型 "aws_access_key_id": AWS 访问密钥 ID。可以通过以下方式设置: AWS_ACCESS_KEY_ID 环境变量。"aws_secret_access_key": AWS 密钥访问密钥。可以通过以下方式设置: AWS_SECRET_ACCESS_KEY 环境变量。 |
| 强制运行变量 | “prompt”:一个包含 LLM 提示的字符串 |
| 输出变量 | “replies”:一个包含 LLM 生成的所有回复的字符串列表 ”meta”:一个包含与每个回复相关的元数据的字典列表,例如 token 计数、结束原因等 |
| API 参考 | Amazon Sagemaker |
| GitHub 链接 | https://github.com/deepset-ai/haystack-core-integrations/tree/main/integrations/amazon_sagemaker |
SagemakerGenerator 允许您使用部署在 AWS SageMaker 上的模型。
参数概述
SagemakerGenerator 需要 AWS 凭证才能工作。请设置AWS_ACCESS_KEY_ID 和AWS_SECRET_ACCESS_KEY 环境变量。
您还需要在组件初始化时指定您的 Sagemaker 端点,以便组件正常工作。请将端点名称传递给model 参数,如下所示:
generator = SagemakerGenerator(model="jumpstart-dft-hf-llm-falcon-7b-instruct-bf16")
此外,您还可以通过使用SagemakerGenerator 的generation_kwargs 参数,无论是初始化时还是run() 方法,将适用于您特定模型的任何文本生成参数直接传递给
如果您的模型还需要自定义属性,请在初始化时通过设置aws_custom_attributes 参数以字典形式传递。
Llama2 是一个需要这些自定义参数的著名模型系列,它需要用{"accept_eula": True} :
generator = SagemakerGenerator(
model="jumpstart-dft-meta-textgenerationneuron-llama-2-7b",
aws_custom_attributes={"accept_eula": True}
)
用法
您需要首先安装来初始化。请安装SagemakerGenerator:
pip install amazon-sagemaker-haystack
单独使用
基本用法
from haystack_integrations.components.generators.amazon_sagemaker import SagemakerGenerator
client = SagemakerGenerator(model="jumpstart-dft-hf-llm-falcon-7b-instruct-bf16")
client.warm_up()
response = client.run("Briefly explain what NLP is in one sentence.")
print(response)
>>> {'replies': ["Natural Language Processing (NLP) is a subfield of artificial intelligence and computational linguistics that focuses on the interaction between computers and human languages..."],
'metadata': [{}]}
在 pipeline 中
在 RAG 管道中
from haystack_integrations.components.generators.amazon_sagemaker import SagemakerGenerator
from haystack import Pipeline
from haystack.components.retrievers.in_memory import InMemoryBM25Retriever
from haystack.components.builders import PromptBuilder
template = """
Given the following information, answer the question.
Context:
{% for document in documents %}
{{ document.content }}
{% endfor %}
Question: What's the official language of {{ country }}?
"""
pipe = Pipeline()
pipe.add_component("retriever", InMemoryBM25Retriever(document_store=docstore))
pipe.add_component("prompt_builder", PromptBuilder(template=template))
pipe.add_component("llm", SagemakerGenerator(model="jumpstart-dft-hf-llm-falcon-7b-instruct-bf16"))
pipe.connect("retriever", "prompt_builder.documents")
pipe.connect("prompt_builder", "llm")
pipe.run({
"prompt_builder": {
"country": "France"
}
})
更新于 大约 1 年前
