跳到内容

Haystack

Haystack 是一个端到端的 LLM 框架,它使您能够构建由 LLMs、Transformer 模型、向量搜索等驱动的应用程序。无论您是想执行检索增强生成 (RAG)、文档搜索、问答还是答案生成,Haystack 都可以将最先进的嵌入模型和 LLMs 编排到管道中,以构建端到端 NLP 应用程序并解决您的用例。

它允许您使用 vLLM 作为后端部署大型语言模型 (LLM) 服务器,该服务器暴露了 OpenAI 兼容的端点。

前提条件

  • 设置 vLLM 和 Haystack 环境
pip install vllm haystack-ai

部署

  • 启动 vLLM 服务器并使用支持的聊天完成模型,例如
vllm serve mistralai/Mistral-7B-Instruct-v0.1
  • 在 Haystack 中使用 OpenAIGeneratorOpenAIChatGenerator 组件来查询 vLLM 服务器。
from haystack.components.generators.chat import OpenAIChatGenerator
from haystack.dataclasses import ChatMessage
from haystack.utils import Secret

generator = OpenAIChatGenerator(
    # for compatibility with the OpenAI API, a placeholder api_key is needed
    api_key=Secret.from_token("VLLM-PLACEHOLDER-API-KEY"),
    model="mistralai/Mistral-7B-Instruct-v0.1",
    api_base_url="http://{your-vLLM-host-ip}:{your-vLLM-host-port}/v1",
    generation_kwargs = {"max_tokens": 512}
)

response = generator.run(
  messages=[ChatMessage.from_user("Hi. Can you help me plan my next trip to Italy?")]
)

print("-"*30)
print(response)
print("-"*30)

输出,例如

------------------------------
{'replies': [ChatMessage(_role=<ChatRole.ASSISTANT: 'assistant'>, _content=[TextContent(text=' Of course! Where in Italy would you like to go and what type of trip are you looking to plan?')], _name=None, _meta={'model': 'mistralai/Mistral-7B-Instruct-v0.1', 'index': 0, 'finish_reason': 'stop', 'usage': {'completion_tokens': 23, 'prompt_tokens': 21, 'total_tokens': 44, 'completion_tokens_details': None, 'prompt_tokens_details': None}})]}
------------------------------

有关详细信息,请参阅教程 在 Haystack 中使用 vLLM