{
  "schema_version": 2,
  "id": "integrate/fireworks-ai",
  "title": "Redis with Fireworks AI",
  "url": "https://redis.io/docs/latest/integrate/fireworks-ai/",
  "summary": "Build a retrieval-augmented generation application with Fireworks AI and Redis.",
  "tags": [
    "docs",
    "integrate",
    "oss",
    "rs",
    "rc"
  ],
  "last_updated": "2026-10-28T08:49:15-07:00",
  "children": [],
  "page_type": "content",
  "content_hash": "e1cd9b3faa733056ade8ff487aef89a1da7c5ebb3b889f5f56e13b6f1bd37c92",
  "sections": [
    {
      "id": "overview",
      "title": "Overview",
      "role": "overview",
      "text": "Use Redis as the retrieval layer for retrieval-augmented generation (RAG) applications powered by [Fireworks AI](https://fireworks.ai/).\n\nFireworks AI provides OpenAI-compatible application programming interfaces (APIs) for embeddings and text generation. Redis stores document chunks, vector embeddings, and metadata in the same database and retrieves relevant context with vector search. You can also rerank the Redis results with a Fireworks reranking model before sending them to the generative model.\n\nIn this architecture:\n\n1. Fireworks converts document chunks into vector embeddings.\n2. Redis stores and indexes the chunks, embeddings, and metadata.\n3. At query time, Fireworks embeds the user's question.\n4. Redis retrieves the most relevant chunks with vector search and optional metadata filters.\n5. Fireworks optionally reranks the candidates and generates an answer from the retrieved context."
    },
    {
      "id": "prerequisites",
      "title": "Prerequisites",
      "role": "content",
      "text": "You need:\n\n- A Redis database with search capabilities. You can create a free database with [Redis Cloud](https://redis.io/try-free/).\n- A [Fireworks AI account and API key](https://docs.fireworks.ai/getting-started/quickstart).\n- Python 3.10 or later.\n\nInstall the required packages:\n\n[code example]\n\nSet the connection details in environment variables:\n\n[code example]\n\nFor Redis Cloud, copy the connection URL from the database's **Connect** dialog. Use a `rediss://` URL when Transport Layer Security (TLS) is enabled."
    },
    {
      "id": "create-the-clients",
      "title": "Create the clients",
      "role": "content",
      "text": "Fireworks exposes an OpenAI-compatible endpoint, so you can use the OpenAI Python client for both embeddings and chat completions.\n\n[code example]\n\nThe Qwen3 embedding model supports shortened vectors through the `dimensions` parameter. The vector field in Redis must use the same dimension as the vectors returned by Fireworks."
    },
    {
      "id": "create-a-vector-index",
      "title": "Create a vector index",
      "role": "content",
      "text": "The following schema stores documents as JavaScript Object Notation (JSON). It indexes the document text for lexical search, metadata for filtering, and a Hierarchical Navigable Small World (HNSW) vector field for approximate nearest-neighbor search.\n\n[code example]\n\nHNSW is a good default for production collections where low-latency approximate search is preferred. For a small collection where exact results matter more than search latency, use `flat` instead."
    },
    {
      "id": "embed-and-load-documents",
      "title": "Embed and load documents",
      "role": "content",
      "text": "Split source documents into chunks before indexing them. A chunk size of roughly 200 to 500 tokens is a useful starting point, but you should evaluate chunking against your own documents and queries.\n\nThis example uses three short chunks:\n\n[code example]\n\nIn a production ingestion job, batch embedding and Redis write operations rather than processing one chunk at a time. Store enough metadata with each chunk to support access controls, filters, and source attribution."
    },
    {
      "id": "retrieve-context-and-generate-an-answer",
      "title": "Retrieve context and generate an answer",
      "role": "content",
      "text": "Embed the question with the same model and dimensions used for the indexed documents. Then use a Redis vector query to retrieve candidate chunks.\n\n[code example]\n\nThe Redis query returns only the fields needed to build the prompt. Keeping the return set small reduces network traffic and prompt-building overhead."
    },
    {
      "id": "optional-rerank-the-retrieved-chunks",
      "title": "Optional: rerank the retrieved chunks",
      "role": "parameters",
      "text": "Vector search is an efficient first-stage retriever. For queries where precision matters, retrieve a wider candidate set from Redis and use a Fireworks reranker to select the best passages before generation.\n\n[code example]\n\nUse the reranked results in place of `matches` when constructing the model context. A common pattern is to retrieve 20 to 50 candidates from Redis and rerank the best 5 to 10, but the right values depend on your corpus, latency target, and model context window."
    },
    {
      "id": "add-metadata-filters",
      "title": "Add metadata filters",
      "role": "content",
      "text": "For multi-tenant or permission-aware applications, add fields such as `tenant_id`, `document_type`, or `created_at` to the index. Apply these constraints as Redis query filters so that ineligible documents are excluded before vector ranking. Avoid retrieving a broad candidate set and filtering it in application code.\n\nFor workloads that need both keyword relevance and semantic similarity, use [`FT.HYBRID`](https://redis.io/docs/latest/commands/ft.hybrid). Redis 8.4 and later supports `FT.HYBRID` for combining lexical and vector ranking in one query."
    },
    {
      "id": "production-considerations",
      "title": "Production considerations",
      "role": "content",
      "text": "- Keep the embedding model and vector dimensions consistent for document and query embeddings. Reindex the corpus when changing embedding models.\n- Use metadata filters to enforce tenant and document-level boundaries during retrieval.\n- Preserve source identifiers and URLs with every chunk so generated answers can include citations.\n- Evaluate retrieval recall, reranking quality, answer faithfulness, latency, and cost with representative queries.\n- Use a versioned index and an alias when changing the schema or embedding model so applications can switch indexes without downtime.\n- Add retries with exponential backoff for transient Fireworks API errors. Dedicated Fireworks deployments can return a temporary `503` while scaling from zero."
    },
    {
      "id": "more-information",
      "title": "More information",
      "role": "content",
      "text": "- [RAG with Redis](https://redis.io/docs/latest/develop/get-started/rag)\n- [Redis vector search](https://redis.io/docs/latest/develop/ai/search-and-query/vectors)\n- [RedisVL](https://redis.io/docs/latest/develop/ai/redisvl)\n- [Fireworks embeddings and reranking](https://docs.fireworks.ai/guides/querying-embeddings-models)\n- [Fireworks text models](https://docs.fireworks.ai/guides/querying-text-models)"
    }
  ],
  "examples": [
    {
      "id": "prerequisites-ex0",
      "language": "bash",
      "code": "pip install \"redisvl>=0.18\" \"openai>=1.0\" requests",
      "section_id": "prerequisites"
    },
    {
      "id": "prerequisites-ex1",
      "language": "bash",
      "code": "export REDIS_URL=\"redis://localhost:6379\"\nexport FIREWORKS_API_KEY=\"<your-fireworks-api-key>\"",
      "section_id": "prerequisites"
    },
    {
      "id": "create-the-clients-ex0",
      "language": "python",
      "code": "import os\n\nfrom openai import OpenAI\nfrom redisvl.index import SearchIndex\n\nfireworks = OpenAI(\n    api_key=os.environ[\"FIREWORKS_API_KEY\"],\n    base_url=\"https://api.fireworks.ai/inference/v1\",\n)\n\nREDIS_URL = os.environ[\"REDIS_URL\"]\nEMBEDDING_MODEL = \"fireworks/qwen3-embedding-8b\"\nEMBEDDING_DIM = 768\nCHAT_MODEL = os.getenv(\n    \"FIREWORKS_CHAT_MODEL\",\n    \"accounts/fireworks/models/glm-5p3-flash\",\n)",
      "section_id": "create-the-clients"
    },
    {
      "id": "create-a-vector-index-ex0",
      "language": "python",
      "code": "schema = {\n    \"index\": {\n        \"name\": \"idx:fireworks-rag\",\n        \"prefix\": \"fireworks-rag:doc:\",\n        \"storage_type\": \"json\",\n    },\n    \"fields\": [\n        {\"name\": \"id\", \"type\": \"tag\"},\n        {\"name\": \"title\", \"type\": \"text\"},\n        {\"name\": \"content\", \"type\": \"text\"},\n        {\"name\": \"source\", \"type\": \"tag\"},\n        {\n            \"name\": \"embedding\",\n            \"type\": \"vector\",\n            \"attrs\": {\n                \"algorithm\": \"hnsw\",\n                \"datatype\": \"float32\",\n                \"dims\": EMBEDDING_DIM,\n                \"distance_metric\": \"cosine\",\n            },\n        },\n    ],\n}\n\nindex = SearchIndex.from_dict(schema, redis_url=REDIS_URL)\nif not index.exists():\n    index.create()",
      "section_id": "create-a-vector-index"
    },
    {
      "id": "embed-and-load-documents-ex0",
      "language": "python",
      "code": "documents = [\n    {\n        \"id\": \"vector-search\",\n        \"title\": \"Vector search\",\n        \"source\": \"https://redis.io/docs/latest/develop/ai/search-and-query/vectors/\",\n        \"content\": (\n            \"Redis supports vector similarity search with FLAT and HNSW indexes. \"\n            \"Vector queries can be combined with filters over document metadata.\"\n        ),\n    },\n    {\n        \"id\": \"redisvl\",\n        \"title\": \"RedisVL\",\n        \"source\": \"https://redis.io/docs/latest/develop/ai/redisvl/\",\n        \"content\": (\n            \"RedisVL is a Python client library for building AI applications \"\n            \"with Redis, including vector search and retrieval workflows.\"\n        ),\n    },\n    {\n        \"id\": \"rag\",\n        \"title\": \"RAG with Redis\",\n        \"source\": \"https://redis.io/docs/latest/develop/get-started/rag/\",\n        \"content\": (\n            \"A RAG pipeline retrieves relevant information from Redis, adds it \"\n            \"to the model prompt, and generates an answer grounded in that context.\"\n        ),\n    },\n]\n\nembedding_response = fireworks.embeddings.create(\n    model=EMBEDDING_MODEL,\n    input=[document[\"content\"] for document in documents],\n    dimensions=EMBEDDING_DIM,\n)\n\nembeddings = sorted(embedding_response.data, key=lambda item: item.index)\nfor document, embedding in zip(documents, embeddings):\n    document[\"embedding\"] = embedding.embedding\n\nindex.load(documents, id_field=\"id\")",
      "section_id": "embed-and-load-documents"
    },
    {
      "id": "retrieve-context-and-generate-an-answer-ex0",
      "language": "python",
      "code": "from redisvl.query import VectorQuery\n\n\ndef embed_query(question: str) -> list[float]:\n    response = fireworks.embeddings.create(\n        model=EMBEDDING_MODEL,\n        input=question,\n        dimensions=EMBEDDING_DIM,\n    )\n    return response.data[0].embedding\n\n\ndef retrieve(question: str, limit: int = 8) -> list[dict]:\n    query = VectorQuery(\n        vector=embed_query(question),\n        vector_field_name=\"embedding\",\n        return_fields=[\"id\", \"title\", \"content\", \"source\", \"vector_distance\"],\n        num_results=limit,\n    )\n    return index.query(query)\n\n\ndef answer(question: str) -> str:\n    matches = retrieve(question)\n    context = \"\\n\\n\".join(\n        f\"[{position}] {item['title']}\\n\"\n        f\"Source: {item['source']}\\n\"\n        f\"{item['content']}\"\n        for position, item in enumerate(matches, start=1)\n    )\n\n    response = fireworks.chat.completions.create(\n        model=CHAT_MODEL,\n        messages=[\n            {\n                \"role\": \"system\",\n                \"content\": (\n                    \"Answer using only the supplied context. \"\n                    \"If the context is insufficient, say so. \"\n                    \"Cite supporting passages with bracketed numbers such as [1].\"\n                ),\n            },\n            {\n                \"role\": \"user\",\n                \"content\": f\"Question: {question}\\n\\nContext:\\n{context}\",\n            },\n        ],\n        temperature=0,\n    )\n    return response.choices[0].message.content\n\n\nprint(answer(\"How does Redis support a RAG application?\"))",
      "section_id": "retrieve-context-and-generate-an-answer"
    },
    {
      "id": "optional-rerank-the-retrieved-chunks-ex0",
      "language": "python",
      "code": "import requests\n\n\ndef rerank(question: str, matches: list[dict], limit: int = 5) -> list[dict]:\n    response = requests.post(\n        \"https://api.fireworks.ai/inference/v1/rerank\",\n        headers={\n            \"Authorization\": f\"Bearer {os.environ['FIREWORKS_API_KEY']}\",\n            \"Content-Type\": \"application/json\",\n        },\n        json={\n            \"model\": \"fireworks/qwen3-reranker-8b\",\n            \"query\": question,\n            \"documents\": [item[\"content\"] for item in matches],\n            \"top_n\": limit,\n            \"return_documents\": False,\n        },\n        timeout=30,\n    )\n    response.raise_for_status()\n    ranking = response.json()[\"data\"]\n    return [matches[item[\"index\"]] for item in ranking]\n\n\nquestion = \"How does Redis support a RAG application?\"\ncandidates = retrieve(question, limit=20)\nbest_matches = rerank(question, candidates, limit=5)",
      "section_id": "optional-rerank-the-retrieved-chunks"
    }
  ]
}
