用 Haystack 接入 vLLM 服务
Haystack 是端到端 LLM 框架,可将 vLLM 作为后端暴露 OpenAI 兼容端点。文中给出安装依赖、启动 vLLM 服务并用 OpenAIChatGenerator 发起对话的完整流程。
## 海丝达克[¶](<https://docs.vllm.ai/en/latest/deployment/frameworks/haystack/#haystack>) [海丝达克](<https://github.com/deepset-ai/haystack>) 是一个 end-to-end LLM 框架,它允许您构建由 LLMS,变形器模型,向量搜索等功能驱动的应用程序。无论您想要进行 Retrieved-augment 生成(RAG),文档搜索,问答或解答生成,Haystack可以将 state-of-the-art 嵌入模型和 LLMs 安装到管道中,以构建 end-to-end NLP 应用程序并解决您的使用大小写. 它允许您部署一个以vLLM为后端的大型语言模型(LLM)服务器,这暴露了OpenAI兼容的端点. ## 先决条件[¶](<https://docs.vllm.ai/en/latest/deployment/frameworks/haystack/#prerequisites>) 设置 vLLM 和 Haystack 环境 : ``` pip install vllm haystack-ai ``` ## 部署[¶](<https://docs.vllm.ai/en/latest/deployment/frameworks/haystack/#deploy>) 1. 启动 vLLM 服务器使用支持的聊天补全模式,例如. ``` vllm serve mistralai/Mistral-7B-Instruct-v0.1 ``` 2. 使用该 `OpenAIGenerator` 财务报告和财务报告 `OpenAIChatGenerator` 用于查询 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}})]} ------------------------------ ``` 详情请见教程 [在海斯塔克使用 vLLM](<https://github.com/deepset-ai/haystack-integrations/blob/main/integrations/vllm.md>). 10月 14, 2025 (英语).
查看原始来源