|
18 | 18 | "source": [ |
19 | 19 | "# Use the ⚡ vLLM inference engine with Haystack\n", |
20 | 20 | "\n", |
21 | | - "<img src=\"https://haystack.deepset.ai/images/haystack-ogimage.png\" width=\"430\" style=\"display:inline;\"> <img src=\"https://docs.vllm.ai/en/stable/assets/logos/vllm-logo-text-light.png\" width=\"500\" style=\"display:inline;\">\n", |
22 | | - "\n", |
23 | | - "[vLLM](https://docs.vllm.ai/en/stable/) is a high-throughput and memory-efficient inference and serving engine for LLMs.\n", |
24 | | - "\n", |
25 | | - "This notebook shows how to use it with Haystack." |
| 21 | + "This notebook demonstrates how to integrate vLLM, a high-throughput and memory-efficient inference engine for Large Language Models (LLMs), with the Haystack framework." |
26 | 22 | ] |
27 | 23 | }, |
28 | 24 | { |
|
33 | 29 | "source": [ |
34 | 30 | "## Install vLLM + Haystack integration\n", |
35 | 31 | "\n", |
36 | | - "- we install vLLM using uv ([installation docs](https://docs.vllm.ai/en/stable/getting_started/installation.html)). For production use cases, there are also other options, including Docker ([docs](https://docs.vllm.ai/en/stable/deployment/docker)).\n", |
37 | | - "- we also install `vllm-haystack`, the vLLM/Haystack integration.\n" |
| 32 | + "This section covers the installation of vLLM using `uv` and the `vllm-haystack` integration package. For production environments, alternative installation methods like Docker are available." |
38 | 33 | ] |
39 | 34 | }, |
40 | 35 | { |
|
56 | 51 | "source": [ |
57 | 52 | "## Serving and using Generative Language Models\n", |
58 | 53 | "\n", |
59 | | - "vLLM primarily supports most open-weights Generative Language Models.\n", |
60 | | - "\n", |
61 | | - "`vllm serve` launches an OpenAI-compatible server.\n", |
62 | | - "\n", |
63 | | - "In the following cell we spin up a server running on `localhost:8000`.\n", |
64 | | - "\n", |
65 | | - "We serve `Qwen/Qwen3-0.6B`, a very small but good Language Model, with reasoning and tool calling capabilities. Some parameters like `reasoning-parser` and `tool-call-parser` follow the values indicated in [Qwen documentation](https://qwen.readthedocs.io/en/latest/deployment/vllm.html).\n", |
66 | | - "\n", |
67 | | - "We also set some parameters specific for our Colab environment:\n", |
68 | | - "- `--enforce-eager`: disables the construction of CUDA graph. This negatively impacts performance but reduces memory requirements and server start time.\n", |
69 | | - "- `--gpu-memory-utilization 0.5`: limits GPU utilization. This is needed since later we'll run two other servers in this notebook for embedding and ranking models.\n" |
| 54 | + "vLLM supports a wide range of open-weight Generative Language Models. This section details how to launch an OpenAI-compatible vLLM server to serve models like `Qwen/Qwen3-0.6B`. Specific parameters are used to optimize for a Colab environment, such as `--enforce-eager` and `--gpu-memory-utilization`." |
70 | 55 | ] |
71 | 56 | }, |
72 | 57 | { |
|
108 | 93 | "source": [ |
109 | 94 | "### Chat with the model\n", |
110 | 95 | "\n", |
111 | | - "Once we have launched the vLLM server,\n", |
112 | | - "we can simply initialize a `VLLMChatGenerator` ([docs](https://docs.haystack.deepset.ai/docs/vllmchatgenerator)) pointing to the vLLM server URL and start chatting!\n", |
113 | | - "\n", |
114 | | - "Here we disable reasoning ([Qwen docs](https://qwen.readthedocs.io/en/latest/deployment/vllm.html#thinking-non-thinking-modes)) and set a `streaming_callback` to allow streaming.\n", |
115 | | - "\n" |
| 96 | + "After setting up the vLLM server, a `VLLMChatGenerator` can be initialized to interact with the served model. This example shows how to configure the generator, including disabling reasoning and enabling streaming output via a `streaming_callback`." |
116 | 97 | ] |
117 | 98 | }, |
118 | 99 | { |
|
177 | 158 | { |
178 | 159 | "cell_type": "markdown", |
179 | 160 | "source": [ |
180 | | - "Not bad but, given \"hello\" as input, this would print \"o l l e h\".\n", |
181 | | - "Let's see if reasoning helps." |
| 161 | + "The previous response was functional but could be improved. Let's see if enabling reasoning yields a better result." |
182 | 162 | ], |
183 | 163 | "metadata": { |
184 | 164 | "id": "Fl-ElT77rK1L" |
|
189 | 169 | "source": [ |
190 | 170 | "### With reasoning\n", |
191 | 171 | "\n", |
192 | | - "We simply enable reasoning ([Qwen docs](https://qwen.readthedocs.io/en/latest/deployment/vllm.html#thinking-non-thinking-modes))." |
| 172 | + "This section demonstrates the model's ability to engage in a 'thinking' mode before generating a response by enabling reasoning in the `VLLMChatGenerator` configuration." |
193 | 173 | ], |
194 | 174 | "metadata": { |
195 | 175 | "id": "fV2CFCr8rR23" |
|
287 | 267 | { |
288 | 268 | "cell_type": "markdown", |
289 | 269 | "source": [ |
290 | | - "Better!\n", |
291 | | - "\n", |
292 | | - "We can also easily extract reasoning as follows:" |
| 270 | + "The response with reasoning provided a more accurate and robust solution. The model's reasoning process can also be extracted for analysis." |
293 | 271 | ], |
294 | 272 | "metadata": { |
295 | 273 | "id": "z4OciCMar4V1" |
|
326 | 304 | "source": [ |
327 | 305 | "### Structured outputs\n", |
328 | 306 | "\n", |
329 | | - "This model also supports structured outputs.\n", |
330 | | - "\n", |
331 | | - "Let's try this, with reasoning and 0 temperature. These settings should help produce a more reliable output." |
| 307 | + "This model also supports generating structured outputs, such as JSON. By defining a `response_format` and setting `temperature` to 0.0, the model can produce more reliable and structured data." |
332 | 308 | ], |
333 | 309 | "metadata": { |
334 | 310 | "id": "MJ-YlCdmvy8s" |
|
398 | 374 | "source": [ |
399 | 375 | "### Tool-calling Agent\n", |
400 | 376 | "\n", |
401 | | - "Now let's build a simple agent with a weather tool. The example below requires handling parallel tool calls, which our model supports." |
| 377 | + "This section illustrates how to build a simple agent using the `Agent` component from Haystack, integrated with a custom `get_weather` tool. The example demonstrates the model's capability to handle parallel tool calls." |
402 | 378 | ], |
403 | 379 | "metadata": { |
404 | 380 | "id": "4T9AhOvcrxXe" |
|
532 | 508 | { |
533 | 509 | "cell_type": "markdown", |
534 | 510 | "source": [ |
535 | | - "Well done!" |
| 511 | + "The agent successfully used the tools to answer the user's query, demonstrating effective tool-calling capabilities." |
536 | 512 | ], |
537 | 513 | "metadata": { |
538 | 514 | "id": "b7pCJc-3tP_T" |
|
543 | 519 | "source": [ |
544 | 520 | "## Serving and using Embedding Models\n", |
545 | 521 | "\n", |
546 | | - "vLLM also supports [embedding models](https://docs.vllm.ai/en/latest/models/pooling_models/embed/), used for computing semantic vectors from text.\n", |
547 | | - "\n", |
548 | | - "We serve the classic `sentence-transformers/all-MiniLM-L6-v2` embedding model, on port `8001`.\n", |
549 | | - "\n", |
550 | | - "`--enforce-eager` and `--gpu-memory-utilization` are only used to limit memory utilization in Colab." |
| 522 | + "vLLM supports serving embedding models, which are used to compute semantic vectors from text. This section shows how to serve `sentence-transformers/all-MiniLM-L6-v2` on a specific port, with memory utilization adjusted for Colab." |
551 | 523 | ], |
552 | 524 | "metadata": { |
553 | 525 | "id": "nrdR0j3fO3V_" |
|
582 | 554 | { |
583 | 555 | "cell_type": "markdown", |
584 | 556 | "source": [ |
585 | | - "Let's try both [VLLMTextEmbedder (docs)](https://docs.haystack.deepset.ai/docs/vllmtextembedder) and [VLLMDocumentEmbedder (docs)](https://docs.haystack.deepset.ai/docs/vllmdocumentembedder) and create a simple retrieval pipeline." |
| 557 | + "This section demonstrates the use of both `VLLMTextEmbedder` and `VLLMDocumentEmbedder` to create a basic retrieval pipeline for semantic search." |
586 | 558 | ], |
587 | 559 | "metadata": { |
588 | 560 | "id": "4ugjx_wtvYkz" |
|
699 | 671 | { |
700 | 672 | "cell_type": "markdown", |
701 | 673 | "source": [ |
702 | | - "vLLM also supports [ranking models](https://docs.vllm.ai/en/latest/models/pooling_models/scoring/), generally used after keyword or embedding retrieval to reorder the retrieved documents by relevance to the query. vLLM supports different ranking models, including cross-encoders and late interaction models.\n", |
703 | | - "\n", |
704 | | - "We serve the `BAAI/bge-reranker-base` cross-encoder model, on port `8002`.\n", |
705 | | - "\n", |
706 | | - "`--enforce-eager` and `--gpu-memory-utilization` are only used to limit memory utilization in Colab." |
| 674 | + "vLLM also supports ranking models, which are crucial for reordering retrieved documents based on relevance. This example sets up a `BAAI/bge-reranker-base` cross-encoder model, with Colab-specific memory settings." |
707 | 675 | ], |
708 | 676 | "metadata": { |
709 | 677 | "id": "XVJaI07JyTJQ" |
|
738 | 706 | { |
739 | 707 | "cell_type": "markdown", |
740 | 708 | "source": [ |
741 | | - "Let's build a two-stage retrieval pipeline:\n", |
742 | | - "- [`InMemoryBM25Retriever`](https://docs.haystack.deepset.ai/docs/inmemorybm25retriever) for fast keyword retrieval\n", |
743 | | - "- [`VLLMRanker`](https://docs.haystack.deepset.ai/docs/vllmranker) to select only the relevant documents from the initial set" |
| 709 | + "This section constructs a two-stage retrieval pipeline, combining `InMemoryBM25Retriever` for initial keyword retrieval with `VLLMRanker` for reranking and selecting the most relevant documents." |
744 | 710 | ], |
745 | 711 | "metadata": { |
746 | 712 | "id": "bxlDHYE_zN1G" |
|
833 | 799 | { |
834 | 800 | "cell_type": "markdown", |
835 | 801 | "source": [ |
836 | | - "Nice work! 🇫🇷" |
| 802 | + "The ranking pipeline effectively reordered the documents, placing the most relevant results at the top." |
837 | 803 | ], |
838 | 804 | "metadata": { |
839 | 805 | "id": "hli_bzo10Qth" |
|
842 | 808 | { |
843 | 809 | "cell_type": "markdown", |
844 | 810 | "source": [ |
845 | | - "In this notebook, we explored serving Generative Language Models, Embedding Models and Ranking Models via vLLM and performing inference through Haystack.\n", |
846 | | - "\n", |
847 | | - "For more information, check the documentation:\n", |
848 | | - "- [`VLLMChatGenerator`](https://docs.haystack.deepset.ai/docs/vllmchatgenerator)\n", |
849 | | - "- [`VLLMTextEmbedder`](https://docs.haystack.deepset.ai/docs/vllmtextembedder)\n", |
850 | | - "- [`VLLMDocumentEmbedder`](https://docs.haystack.deepset.ai/docs/vllmdocumentembedder)\n", |
851 | | - "- [`VLLMRanker`](https://docs.haystack.deepset.ai/docs/vllmranker)\n", |
852 | | - "\n", |
853 | | - "*Notebook by [Stefano Fiorucci](https://github.com/anakin87)*" |
| 811 | + "In this notebook, we explored serving Generative Language Models, Embedding Models, and Ranking Models using vLLM and integrating them with Haystack for various inference tasks. For more detailed information, refer to the official documentation for `VLLMChatGenerator`, `VLLMTextEmbedder`, `VLLMDocumentEmbedder`, and `VLLMRanker`." |
854 | 812 | ], |
855 | 813 | "metadata": { |
856 | 814 | "id": "U_y3uAyEbnZx" |
|
862 | 820 | "colab": { |
863 | 821 | "gpuType": "T4", |
864 | 822 | "provenance": [], |
865 | | - "toc_visible": true, |
866 | 823 | "include_colab_link": true |
867 | 824 | }, |
868 | 825 | "kernelspec": { |
|
0 commit comments