文档API 参考📓 教程🧑‍🍳 食谱🤝 集成💜 Discord🎨 Studio
文档

AzureOpenAIChatGenerator

该组件支持通过 Azure 服务使用 OpenAI 的大型语言模型 (LLM) 进行聊天补全。

pipeline 中的最常见位置使用 ChatPromptBuilder 后
必需的初始化变量"api_key": Azure OpenAI API 密钥。可以通过以下方式设置环境变量 AZURE_OPENAI_API_KEY

"azure_ad_token": Microsoft Entra ID token。可以通过AZURE_OPENAI_AD_TOKEN 环境变量设置。
强制运行变量“messages”:一个 ChatMessage 对象列表,代表聊天记录
输出变量“replies”: LLM 对输入聊天的备选回复列表
API 参考Generators (生成器)
GitHub 链接https://github.com/deepset-ai/haystack/blob/main/haystack/components/generators/chat/azure.py

概述

AzureOpenAIChatGenerator 支持通过 Azure 服务部署的 OpenAI 模型。要查看支持的模型列表,请参阅 Azure 文档。该组件默认使用的模型是gpt-4o-mini.

要使用 Azure 组件,您将需要一个 Azure OpenAI API 密钥以及一个 Azure OpenAI 端点。您可以在 Azure 文档 中了解有关它们的更多信息。

该组件使用AZURE_OPENAI_API_KEY 和 AZURE_OPENAI_AD_TOKEN 环境变量。否则,您可以在初始化时传递api_key 和 azure_ad_token

client = AzureOpenAIChatGenerator(azure_endpoint="<Your Azure endpoint e.g. `https://your-company.azure.openai.com/>",
                        api_key=Secret.from_token("<your-api-key>"),
                        azure_deployment="<a model name>")

📘

我们建议使用环境变量而不是初始化参数。

然后,组件需要一个列表,其中包含ChatMessage 对象列表才能运行。ChatMessage 是一个数据类,其中包含消息、角色(谁生成了消息,例如user, assistant, system, function),以及可选的元数据。有关示例,请参阅 用法 部分。

您可以将适用于openai.ChatCompletion.create 方法的任何聊天补全参数直接传递给AzureOpenAIChatGenerator,通过generation_kwargs 参数,无论是初始化时还是run() 方法。有关支持的参数的更多详细信息,请参阅 Azure 文档

您还可以通过azure_deployment 初始化参数为此组件指定模型。

结构化输出

AzureOpenAIChatGenerator 支持结构化输出生成,允许您以可预测的格式接收响应。您可以通过response_format 参数来使用 Pydantic 模型或 JSON 模式定义输出的结构。generation_kwargs.

当您需要从文本中提取结构化数据或生成符合特定格式的响应时,这非常有用。

from pydantic import BaseModel
from haystack.components.generators.chat import AzureOpenAIChatGenerator
from haystack.dataclasses import ChatMessage

class NobelPrizeInfo(BaseModel):
    recipient_name: str
    award_year: int
    category: str
    achievement_description: str
    nationality: str

client = AzureOpenAIChatGenerator(
    azure_endpoint="<Your Azure endpoint>",
    azure_deployment="gpt-4o",
    generation_kwargs={"response_format": NobelPrizeInfo}
)

response = client.run(messages=[
    ChatMessage.from_user(
        "In 2021, American scientist David Julius received the Nobel Prize in"
        " Physiology or Medicine for his groundbreaking discoveries on how the human body"
        " senses temperature and touch."
    )
])
print(response["replies"][0].text)

>> {"recipient_name":"David Julius","award_year":2021,"category":"Physiology or Medicine",
>> "achievement_description":"David Julius was awarded for his transformative findings 
>> regarding the molecular mechanisms underlying the human body's sense of temperature 
>> and touch. Through innovative experiments, he identified specific receptors responsible 
>> for detecting heat and mechanical stimuli, ranging from gentle touch to pain-inducing 
>> pressure.","nationality":"American"}

📘

模型兼容性和限制

  • 从 GPT-4o 开始的最新模型支持 Pydantic 模型和 JSON 模式。
  • 旧模型仅支持通过{"type": "json_object"} 进行基本的 JSON 模式。有关详细信息,请参阅 OpenAI JSON 模式文档
  • 流式传输限制:在使用流式传输处理结构化输出时,您必须为response_format.
  • 提供 JSON 模式而不是 Pydantic 模型。有关完整信息,请查看 Azure OpenAI 结构化输出文档

流式传输

您可以随着输出的生成而流式传输。将回调函数传递给streaming_callback。使用内置的print_streaming_chunk 来打印文本 token 和工具事件(工具调用和工具结果)。

from haystack.components.generators.utils import print_streaming_chunk

# Configure any `Generator` or `ChatGenerator` with a streaming callback
component = SomeGeneratorOrChatGenerator(streaming_callback=print_streaming_chunk)

# If this is a `ChatGenerator`, pass a list of messages:
# from haystack.dataclasses import ChatMessage
# component.run([ChatMessage.from_user("Your question here")])

# If this is a (non-chat) `Generator`, pass a prompt:
# component.run({"prompt": "Your prompt here"})

📘

流式输出仅适用于单个响应。如果提供程序支持多个候选,请将n=1.

设置为 1。有关流式输出如何工作以及如何编写自定义回调函数,请参阅我们的流式输出支持文档。StreamingChunk 工作原理以及如何编写自定义回调。

默认首选print_streaming_chunk。仅当您需要特定的传输(例如 SSE/WebSocket)或自定义 UI 格式时,才编写自定义回调。

用法

单独使用

基本用法

from haystack.dataclasses import ChatMessage
from haystack.components.generators.chat import AzureOpenAIChatGenerator
client = AzureOpenAIChatGenerator()
response = client.run(
	  [ChatMessage.from_user("What's Natural Language Processing? Be brief.")]
)
print(response)

使用流式传输

from haystack.dataclasses import ChatMessage
from haystack.components.generators.chat import AzureOpenAIChatGenerator
client = AzureOpenAIChatGenerator(streaming_callback=lambda chunk: print(chunk.content, end="", flush=True))
response = client.run(
	  [ChatMessage.from_user("What's Natural Language Processing? Be brief.")]
)
print(response)

在 pipeline 中

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

# no parameter init, we don't use any runtime template variables
prompt_builder = ChatPromptBuilder()
llm = AzureOpenAIChatGenerator()

pipe = Pipeline()
pipe.add_component("prompt_builder", prompt_builder)
pipe.add_component("llm", llm)
pipe.connect("prompt_builder.prompt", "llm.messages")
location = "Berlin"
messages = [ChatMessage.from_system("Always respond in German even if some input data is in other languages."),
            ChatMessage.from_user("Tell me about {{location}}")]
pipe.run(data={"prompt_builder": {"template_variables":{"location": location}, "template": messages}})

相关链接

在我们的 API 参考中查看参数详情