Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions index.toml
Original file line number Diff line number Diff line change
Expand Up @@ -368,3 +368,8 @@ title = "LinkedIn, Company Intelligence & Lead Enrichment with Haystack, MongoDB
notebook = "ai_sales_research_assistant.ipynb"
new = true
topics = ["RAG", "Web-QA"]

[[cookbook]]
title = "RAG Pipeline with MiniMax via OpenAI-Compatible API"
notebook = "rag_with_minimax.ipynb"
topics = ["RAG"]
354 changes: 354 additions & 0 deletions notebooks/rag_with_minimax.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,354 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# RAG Pipeline with MiniMax via OpenAI-Compatible API\n",
"\n",
"In this notebook, you'll learn how to use **[MiniMax](https://www.minimax.io/)** large language models with **Haystack** through the OpenAI-compatible API.\n",
"\n",
"MiniMax provides models like **MiniMax-M2.7** (204K context window) and **MiniMax-M2.7-highspeed**, which are accessible via an OpenAI-compatible endpoint. This means you can use Haystack's built-in `OpenAIChatGenerator` to work with MiniMax models — no extra integration packages required.\n",
"\n",
"We'll build:\n",
"1. A **basic chat** example to verify the connection\n",
"2. A full **RAG pipeline** using MiniMax as the generator"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Setup\n",
"\n",
"Install the required packages:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"!pip install -q -U haystack-ai datasets sentence-transformers"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Set your MiniMax API key. You can get one from the [MiniMax Platform](https://platform.minimax.io/)."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"from getpass import getpass\n",
"\n",
"if \"MINIMAX_API_KEY\" not in os.environ:\n",
" os.environ[\"MINIMAX_API_KEY\"] = getpass(\"Enter your MiniMax API key: \")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Basic Chat with MiniMax\n",
"\n",
"Since MiniMax provides an OpenAI-compatible API at `https://api.minimax.io/v1`, you can use\n",
"Haystack's `OpenAIChatGenerator` directly by setting `api_base_url` and passing the API key."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from haystack.components.generators.chat import OpenAIChatGenerator\n",
"from haystack.dataclasses import ChatMessage\n",
"from haystack.utils import Secret\n",
"\n",
"minimax_chat = OpenAIChatGenerator(\n",
" api_key=Secret.from_env_var(\"MINIMAX_API_KEY\"),\n",
" model=\"MiniMax-M2.7\",\n",
" api_base_url=\"https://api.minimax.io/v1\",\n",
" generation_kwargs={\"temperature\": 0.7, \"max_tokens\": 512},\n",
")\n",
"\n",
"messages = [ChatMessage.from_user(\"What are the Seven Wonders of the Ancient World? List them briefly.\")]\n",
"result = minimax_chat.run(messages=messages)\n",
"\n",
"print(result[\"replies\"][0].text)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Build a RAG Pipeline\n",
"\n",
"Now let's build a full Retrieval-Augmented Generation (RAG) pipeline using MiniMax as the LLM.\n",
"We'll use:\n",
"- **InMemoryDocumentStore** for storing documents\n",
"- **SentenceTransformersDocumentEmbedder** for creating document embeddings\n",
"- **InMemoryEmbeddingRetriever** for retrieval\n",
"- **OpenAIChatGenerator** (pointing to MiniMax) for generation"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Load and Index Documents\n",
"\n",
"We'll use the [Seven Wonders](https://huggingface.co/datasets/bilgeyucel/seven-wonders) dataset."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from datasets import load_dataset\n",
"from haystack import Document\n",
"\n",
"dataset = load_dataset(\"bilgeyucel/seven-wonders\", split=\"train\")\n",
"docs = [Document(content=doc[\"content\"], meta=doc[\"meta\"]) for doc in dataset]\n",
"\n",
"print(f\"Loaded {len(docs)} documents\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from haystack.document_stores.in_memory import InMemoryDocumentStore\n",
"from haystack.components.embedders import SentenceTransformersDocumentEmbedder\n",
"\n",
"document_store = InMemoryDocumentStore()\n",
"\n",
"doc_embedder = SentenceTransformersDocumentEmbedder(\n",
" model=\"sentence-transformers/all-MiniLM-L6-v2\"\n",
")\n",
"\n",
"docs_with_embeddings = doc_embedder.run(docs)\n",
"document_store.write_documents(docs_with_embeddings[\"documents\"])\n",
"\n",
"print(f\"Indexed {document_store.count_documents()} documents\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Define the Prompt Template\n",
"\n",
"Create a prompt that instructs the model to answer based on the retrieved documents."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from haystack.components.builders import ChatPromptBuilder\n",
"from haystack.dataclasses import ChatMessage\n",
"\n",
"system_message = ChatMessage.from_system(\n",
" \"You are a helpful assistant that answers questions based on the provided documents. \"\n",
" \"If the documents don't contain the answer, say so.\"\n",
")\n",
"\n",
"user_message_template = \"\"\"\\\n",
"Answer the question based on these documents:\n",
"\n",
"{% for document in documents %}\n",
"Document {{ loop.index }}:\n",
"{{ document.content }}\n",
"---\n",
"{% endfor %}\n",
"\n",
"Question: {{ question }}\n",
"Answer:\"\"\"\n",
"\n",
"prompt_builder = ChatPromptBuilder(\n",
" template=[system_message, ChatMessage.from_user(user_message_template)]\n",
")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Assemble the RAG Pipeline"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from haystack import Pipeline\n",
"from haystack.components.embedders import SentenceTransformersTextEmbedder\n",
"from haystack.components.retrievers.in_memory import InMemoryEmbeddingRetriever\n",
"\n",
"text_embedder = SentenceTransformersTextEmbedder(\n",
" model=\"sentence-transformers/all-MiniLM-L6-v2\"\n",
")\n",
"retriever = InMemoryEmbeddingRetriever(document_store)\n",
"\n",
"llm = OpenAIChatGenerator(\n",
" api_key=Secret.from_env_var(\"MINIMAX_API_KEY\"),\n",
" model=\"MiniMax-M2.7\",\n",
" api_base_url=\"https://api.minimax.io/v1\",\n",
" generation_kwargs={\"temperature\": 0.7, \"max_tokens\": 1024},\n",
")\n",
"\n",
"rag_pipeline = Pipeline()\n",
"rag_pipeline.add_component(\"text_embedder\", text_embedder)\n",
"rag_pipeline.add_component(\"retriever\", retriever)\n",
"rag_pipeline.add_component(\"prompt_builder\", prompt_builder)\n",
"rag_pipeline.add_component(\"llm\", llm)\n",
"\n",
"rag_pipeline.connect(\"text_embedder.embedding\", \"retriever.query_embedding\")\n",
"rag_pipeline.connect(\"retriever\", \"prompt_builder.documents\")\n",
"rag_pipeline.connect(\"prompt_builder\", \"llm\")\n",
"\n",
"rag_pipeline.draw(\"rag_pipeline_minimax.png\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Ask Questions"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"question = \"Why were people visiting the Temple of Artemis?\"\n",
"\n",
"result = rag_pipeline.run(\n",
" data={\n",
" \"text_embedder\": {\"text\": question},\n",
" \"retriever\": {\"top_k\": 3},\n",
" \"prompt_builder\": {\"question\": question},\n",
" }\n",
")\n",
"\n",
"print(result[\"llm\"][\"replies\"][0].text)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"question = \"How did the Colossus of Rhodes collapse?\"\n",
"\n",
"result = rag_pipeline.run(\n",
" data={\n",
" \"text_embedder\": {\"text\": question},\n",
" \"retriever\": {\"top_k\": 3},\n",
" \"prompt_builder\": {\"question\": question},\n",
" }\n",
")\n",
"\n",
"print(result[\"llm\"][\"replies\"][0].text)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Using MiniMax-M2.7-highspeed\n",
"\n",
"For faster inference, you can switch to the `MiniMax-M2.7-highspeed` model.\n",
"It has the same 204K context window but optimized for speed."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"fast_llm = OpenAIChatGenerator(\n",
" api_key=Secret.from_env_var(\"MINIMAX_API_KEY\"),\n",
" model=\"MiniMax-M2.7-highspeed\",\n",
" api_base_url=\"https://api.minimax.io/v1\",\n",
" generation_kwargs={\"temperature\": 0.7, \"max_tokens\": 512},\n",
")\n",
"\n",
"messages = [ChatMessage.from_user(\"Explain what RAG (Retrieval-Augmented Generation) is in 3 sentences.\")]\n",
"result = fast_llm.run(messages=messages)\n",
"\n",
"print(result[\"replies\"][0].text)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Summary\n",
"\n",
"In this notebook, you learned how to:\n",
"- Use **MiniMax** models with Haystack's `OpenAIChatGenerator` via the OpenAI-compatible API\n",
"- Build a complete **RAG pipeline** with MiniMax as the generator\n",
"- Switch between **MiniMax-M2.7** (high quality) and **MiniMax-M2.7-highspeed** (optimized for speed)\n",
"\n",
"### Available MiniMax Models\n",
"\n",
"| Model | Context Window | Description |\n",
"|-------|---------------|-------------|\n",
"| `MiniMax-M2.7` | 204K tokens | Latest flagship model |\n",
"| `MiniMax-M2.7-highspeed` | 204K tokens | Speed-optimized variant |\n",
"\n",
"### Key Configuration\n",
"\n",
"To use MiniMax with any Haystack component that supports `OpenAIChatGenerator`:\n",
"\n",
"```python\n",
"from haystack.components.generators.chat import OpenAIChatGenerator\n",
"from haystack.utils import Secret\n",
"\n",
"generator = OpenAIChatGenerator(\n",
" api_key=Secret.from_env_var(\"MINIMAX_API_KEY\"),\n",
" model=\"MiniMax-M2.7\",\n",
" api_base_url=\"https://api.minimax.io/v1\",\n",
")\n",
"```\n",
"\n",
"For more details, visit the [MiniMax API documentation](https://platform.minimax.io/)."
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"name": "python",
"version": "3.10.0"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
Loading