| 1 | # Haystack |
| 2 | |
| 3 | [Haystack](https://github.com/deepset-ai/haystack) is an end-to-end LLM framework that allows you to build applications powered by LLMs, Transformer models, vector search and more. Whether you want to perform retrieval-augmented generation (RAG), document search, question answering or answer generation, Haystack can orchestrate state-of-the-art embedding models and LLMs into pipelines to build end-to-end NLP applications and solve your use case. |
| 4 | |
| 5 | It allows you to deploy a large language model (LLM) server with vLLM as the backend, which exposes OpenAI-compatible endpoints. |
| 6 | |
| 7 | ## Prerequisites |
| 8 | |
| 9 | Set up the vLLM and Haystack environment: |
| 10 | |
| 11 | ```bash |
| 12 | pip install vllm haystack-ai |
| 13 | ``` |
| 14 | |
| 15 | ## Deploy |
| 16 | |
| 17 | 1. Start the vLLM server with the supported chat completion model, e.g. |
| 18 | |
| 19 | ```bash |
| 20 | vllm serve mistralai/Mistral-7B-Instruct-v0.1 |
| 21 | ``` |
| 22 | |
| 23 | 1. Use the `OpenAIGenerator` and `OpenAIChatGenerator` components in Haystack to query the vLLM server. |
| 24 | |
| 25 | ??? code |
| 26 | |
| 27 | ```python |
| 28 | from haystack.components.generators.chat import OpenAIChatGenerator |
| 29 | from haystack.dataclasses import ChatMessage |
| 30 | from haystack.utils import Secret |
| 31 | |
| 32 | generator = OpenAIChatGenerator( |
| 33 | # for compatibility with the OpenAI API, a placeholder api_key is needed |
| 34 | api_key=Secret.from_token("VLLM-PLACEHOLDER-API-KEY"), |
| 35 | model="mistralai/Mistral-7B-Instruct-v0.1", |
| 36 | api_base_url="http://{your-vLLM-host-ip}:{your-vLLM-host-port}/v1", |
| 37 | generation_kwargs={"max_tokens": 512}, |
| 38 | ) |
| 39 | |
| 40 | response = generator.run( |
| 41 | messages=[ChatMessage.from_user("Hi. Can you help me plan my next trip to Italy?")] |
| 42 | ) |
| 43 | |
| 44 | print("-"*30) |
| 45 | print(response) |
| 46 | print("-"*30) |
| 47 | ``` |
| 48 | |
| 49 | ```console |
| 50 | ------------------------------ |
| 51 | {'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}})]} |
| 52 | ------------------------------ |
| 53 | ``` |
| 54 | |
| 55 | For details, see the tutorial [Using vLLM in Haystack](https://github.com/deepset-ai/haystack-integrations/blob/main/integrations/vllm.md). |