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

MistralChatGenerator

此组件使用 Mistral 的文本生成模型进行聊天补全。

pipeline 中的最常见位置ChatPromptBuilder 之后
必需的初始化变量"api_key": Mistral API 密钥。可以通过MISTRAL_API_KEY 环境变量设置。
强制运行变量“messages”:一个 ChatMessage 对象列表
输出变量"replies": 一个 ChatMessage 对象列表

”meta”:一个包含与每个回复相关的元数据的字典列表,例如 token 计数、结束原因等
API 参考Mistral
GitHub 链接https://github.com/deepset-ai/haystack-core-integrations/tree/main/integrations/mistral

概述

此集成支持通过生成式终结点提供的 Mistral 模型。有关可用模型的完整列表,请查看 Mistral 文档

MistralChatGenerator 需要 Mistral API 密钥才能工作。您可以在

  • api_key 初始化参数中使用 Secret API
  • MISTRAL_API_KEY 环境变量(推荐)

当前,可用模型有

  • mistral-tiny (默认)
  • mistral-small
  • mistral-medium(即将弃用)
  • mistral-large-latest
  • codestral-latest

此组件需要一个 ChatMessage 对象列表来运行。ChatMessage 是一个数据类,其中包含消息、角色(谁生成了消息,例如user, assistant, system, function),以及可选的元数据。

有关 Mistral API 支持的参数的更多详细信息,请参阅 Mistral API 文档,您可以在运行组件时通过generation_kwargs 提供这些参数。

流式传输

此 Generator 支持将 LLM 的 token直接流式传输到输出中。要做到这一点,请将一个函数传递给streaming_callback 初始化参数。

用法

安装mistral-haystack 包来使用MistralChatGenerator:

pip install mistral-haystack

单独使用

from haystack_integrations.components.generators.mistral import MistralChatGenerator
from haystack.components.generators.utils import print_streaming_chunk
from haystack.dataclasses import ChatMessage
from haystack.utils import Secret

generator = MistralChatGenerator(api_key=Secret.from_env_var("MISTRAL_API_KEY"), streaming_callback=print_streaming_chunk)
message = ChatMessage.from_user("What's Natural Language Processing? Be brief.")
print(generator.run([message]))

在 Pipeline 中

下面是一个 RAG 管道示例,我们根据 URL 内容回答问题。我们将 URL 的内容添加到我们的messages 中,在ChatPromptBuilder 中生成答案,并使用MistralChatGenerator.

from haystack import Document
from haystack import Pipeline
from haystack.components.builders import ChatPromptBuilder
from haystack.components.generators.utils import print_streaming_chunk
from haystack.components.fetchers import LinkContentFetcher
from haystack.components.converters import HTMLToDocument
from haystack.dataclasses import ChatMessage

from haystack_integrations.components.generators.mistral import MistralChatGenerator

fetcher = LinkContentFetcher()
converter = HTMLToDocument()
prompt_builder = ChatPromptBuilder(variables=["documents"])
llm = MistralChatGenerator(streaming_callback=print_streaming_chunk, model='mistral-small')

message_template = """Answer the following question based on the contents of the article: {{query}}\n
               Article: {{documents[0].content}} \n 
           """
messages = [ChatMessage.from_user(message_template)]

rag_pipeline = Pipeline()
rag_pipeline.add_component(name="fetcher", instance=fetcher)
rag_pipeline.add_component(name="converter", instance=converter)
rag_pipeline.add_component("prompt_builder", prompt_builder)
rag_pipeline.add_component("llm", llm)

rag_pipeline.connect("fetcher.streams", "converter.sources")
rag_pipeline.connect("converter.documents", "prompt_builder.documents")
rag_pipeline.connect("prompt_builder.prompt", "llm.messages")

question = "What are the capabilities of Mixtral?"

result = rag_pipeline.run(
    {
        "fetcher": {"urls": ["https://mistral.ai/news/mixtral-of-experts"]},
        "prompt_builder": {"template_variables": {"query": question}, "template": messages},
      
        "llm": {"generation_kwargs": {"max_tokens": 165}},
    },
)

其他参考资料

🧑‍🍳 食谱: Web QA with Mixtral-8x7B-Instruct-v0.1