RAG API with OpenAI

RAG API with OpenAI Integration Guide | Ragex

Integrate a RAG API with OpenAI to ground model answers in your documents. Upload files, search with a managed API, and feed context — working code in minutes.

OpenAI models generate fluent text but only know what is in their training data. When users ask about your refund policy or product specifics, the model hallucinates or admits it does not know. Integrating a RAG API with OpenAI bridges this gap — upload your documents, search on each query, and feed context to the model so it answers from your data.

The problem with DIY RAG for OpenAI apps

Building a retrieval pipeline for OpenAI applications means managing multiple components alongside your OpenAI integration:

  • Document parsing — extract text from PDFs, DOCX, spreadsheets, and images with separate libraries per format
  • Chunking — split documents into searchable segments with the right size and overlap
  • Embeddings — generate vectors using OpenAI's text-embedding-3 or another model ($50-150/mo in API costs)
  • Vector storage — provision and maintain Pinecone, Weaviate, or pgvector ($70-200/mo)
  • Reranking — optionally add a cross-encoder to improve result quality (another vendor, another bill)

That is 4-5 separate services before your OpenAI call even starts. When the chatbot gives a wrong answer, you debug by checking each pipeline component individually — was it a bad chunk, a weak embedding match, or a missing document? Teams building RAG-powered chatbots frequently spend more time on the retrieval pipeline than on the LLM integration itself.

How Ragex replaces the pipeline

Ragex collapses the entire retrieval stack into three endpoints. You upload documents, search them, and pass the results to OpenAI. The API handles parsing (16 file types including PDFs, images, and spreadsheets), chunking, embedding, indexing, and reranking — all behind a single API key.

Your architecture simplifies from five components to two: Ragex for retrieval and OpenAI for generation.

from ragex import RagexClient
from openai import OpenAI

rag = RagexClient(api_key="YOUR_RAGEX_API_KEY")
oai = OpenAI(api_key="YOUR_OPENAI_KEY")

# Search your knowledge base
results = rag.search(
    "kb_x1y2z3w4v5",
    query="What is the refund policy?",
    top_k=5,
)

# Build context from search results
context = "\n\n".join(r["text"] for r in results["results"])

# Generate a grounded answer
response = oai.chat.completions.create(
    model="gpt-5-mini",
    messages=[
        {"role": "system", "content": f"Answer using only this context:\n\n{context}"},
        {"role": "user", "content": "What is the refund policy?"}
    ],
)

print(response.choices[0].message.content)

That is the entire integration. Five API calls to set up the knowledge base, then two calls per user query — one to search, one to generate.

TypeScript integration

The same retrieval-then-generate pattern works in TypeScript. If you are building a Next.js application or Node.js backend, the SDK provides identical functionality with full type safety. The TypeScript SDK ships with zero external dependencies and uses native fetch under the hood:

import { RagexClient } from 'ragex';
import OpenAI from 'openai';

const rag = new RagexClient({ apiKey: 'YOUR_RAGEX_API_KEY' });
const oai = new OpenAI({ apiKey: 'YOUR_OPENAI_KEY' });

const results = await rag.search('kb_x1y2z3w4v5', {
  query: 'What is the refund policy?',
  top_k: 5,
});

const context = results.results.map(r => r.text).join('\n\n');

const completion = await oai.chat.completions.create({
  model: 'gpt-5-mini',
  messages: [
    { role: 'system', content: `Answer using only this context:\n\n${context}` },
    { role: 'user', content: 'What is the refund policy?' },
  ],
});

console.log(completion.choices[0].message.content);

Both SDKs are lightweight — the Python SDK depends only on httpx, the TypeScript SDK uses native fetch with zero dependencies.

What you eliminate vs what you keep

You eliminate:

  • Embedding model selection and API cost management (saves $50-150/mo)
  • Vector database provisioning and maintenance (saves $70-200/mo)
  • Document parsing for 16 file types (no more PyPDFLoader or Unstructured)
  • Chunking strategy configuration and tuning
  • Reranker setup — the managed API enables cross-encoder reranking by default

You keep:

  • OpenAI's chat completion API for generation
  • Your choice of GPT-5-mini, GPT-5.4, or any OpenAI model
  • Prompt engineering and system messages
  • Streaming responses and function calling
  • Your existing OpenAI billing and rate limits

This separation means you can swap OpenAI for another LLM provider (Anthropic, open-source models) without touching your retrieval code. Teams building internal knowledge bases often start with one model and later test alternatives — Ragex stays the same.

The managed API also handles document lifecycle. When a source document changes — a product guide gets a new section, a policy is updated — re-upload the file and the API re-parses and re-indexes it automatically. No manual reprocessing, no stale search results. This matters in regulated industries like healthcare where accuracy depends on the knowledge base staying current.

Streaming responses with retrieval context

Most production OpenAI integrations use streaming to display tokens as they generate. Ragex returns search results synchronously in milliseconds, so you call search first, then stream the generation:

results = rag.search("kb_x1y2z3w4v5", query=user_question, top_k=5)
context = "\n\n".join(r["text"] for r in results["results"])

stream = oai.chat.completions.create(
    model="gpt-5-mini",
    messages=[
        {"role": "system", "content": f"Answer using only this context:\n\n{context}"},
        {"role": "user", "content": user_question}
    ],
    stream=True,
)

for chunk in stream:
    if chunk.choices[0].delta.content:
        print(chunk.choices[0].delta.content, end="")

Retrieval adds minimal latency — search results typically return in under 200ms, and the managed API's reranking runs server-side. Users see the first token from OpenAI almost immediately after submitting their query. For a full streaming integration with React, see the Vercel AI SDK guide.

Reducing hallucinations with retrieval context

The most common reason to add RAG to an OpenAI app is reducing hallucinations. Without retrieval, the model guesses when it does not know something. With retrieval, the model sees the actual source text and can answer accurately.

Two techniques help:

  1. Explicit system instructions — tell the model to only answer from the provided context and say "I don't know" when the context does not cover the question
  2. Score thresholds — Ragex returns a relevance score with each result. Filter out low-scoring results before they reach the LLM, so the model only sees high-confidence matches

Teams building customer support systems typically set top_k=5 and score_threshold=0.3 to balance coverage with accuracy. The managed API's default reranking improves these scores compared to raw vector similarity.

Scaling from prototype to production

The same code that runs in your prototype runs in production. Upload more documents and the knowledge base grows automatically. The managed API handles indexing, storage, and query load internally — no database resizing or index tuning.

Plans start at $29/mo (Starter), with Pro at $79/mo and Scale at $199/mo. Compare that to the combined cost of a DIY RAG pipeline — a vector database plus embedding API plus parsing service typically runs $200-500/mo.

For teams already using LangChain or LlamaIndex with OpenAI, Ragex can replace the retrieval components while keeping the existing LLM chain logic. Migration is incremental — swap the retriever, keep everything else.

FAQ

Can I use OpenAI's embedding model with this API?

You do not need to. Ragex handles embedding automatically with a built-in model optimized for retrieval. This saves on OpenAI embedding API costs and eliminates the need to manage vector dimensions or model versions. When better embedding models become available, the API upgrades automatically.

Does this work with OpenAI's streaming completions?

Yes. Ragex returns search results in a single JSON response in milliseconds. You then pass those results as context to OpenAI's streaming chat completion endpoint. Retrieval is fast and synchronous; streaming applies to the generation step only. There is no conflict between the two.

How do I handle documents that update frequently?

Re-upload the updated document to the same knowledge base. The API re-parses, re-chunks, and re-embeds it automatically. Delete the old version if needed. Changes are reflected in search results as soon as the new document reaches ready status — no manual re-indexing.

Can I use function calling with RAG results?

Yes. Include the search results in the system message or as tool output, then let the model use function calling as normal. The RAG context does not interfere with function calling — it is just additional context in the conversation. Teams building AI assistants commonly combine retrieval with function calling for multi-capability agents.


Last updated: 2026-03-09

Try it yourself

First query in under 5 minutes. No credit card required.