Skip to content

RAG: Embedding + Rerank

RAG (Retrieval-Augmented Generation) is how you get more accurate answers by having the model "read" your own documents. In this guide we'll build a simple RAG pipeline using Qevron's embedding, rerank and chat models together.

The idea

User question

   ├─▶ 1. Embedding: question + documents are turned into vectors
   ├─▶ 2. Search: nearest documents found (vector similarity)
   ├─▶ 3. Rerank: candidates sorted by relevance
   └─▶ 4. Chat: best documents + question given to the model → answer

1. Embed the documents

python
import os, numpy as np
from openai import OpenAI

client = OpenAI(api_key=os.environ["QEVRON_API_KEY"], base_url="https://app.qevron.ai/v1")

docs = [
    "Qevron authenticates requests with the Authorization: Bearer header.",
    "For streaming, add stream: true to the request.",
    "The spook-background model removes an image's background.",
    "Embedding turns text into a vector; used for search.",
]

emb = client.embeddings.create(model="veriEmbedding", input=docs)
doc_vectors = np.array([d.embedding for d in emb.data])

2. Embed the question and find the nearest

python
def cosine(a, b):
    return (a @ b) / (np.linalg.norm(a) * np.linalg.norm(b) + 1e-9)

question = "How does authentication work in Qevron?"
q_vec = np.array(client.embeddings.create(model="veriEmbedding", input=question).data[0].embedding)

scores = [cosine(q_vec, dv) for dv in doc_vectors]
top_idx = sorted(range(len(docs)), key=lambda i: scores[i], reverse=True)[:3]
candidates = [docs[i] for i in top_idx]

3. Pick the most relevant with rerank

Vector search is fast but coarse. A rerank model sorts the candidates far more accurately:

python
import httpx

rr = httpx.post(
    "https://app.qevron.ai/v1/rerank",
    headers={"Authorization": f"Bearer {os.environ['QEVRON_API_KEY']}"},
    json={
        "model": "verirerag",
        "query": question,
        "documents": candidates,
        "top_n": 2,
        "return_documents": True,
    },
).json()

best_docs = [r["document"]["text"] for r in rr["results"]]

4. Give the context to the model and get an answer

python
context = "\n".join(f"- {d}" for d in best_docs)
resp = client.chat.completions.create(
    model="verinova",
    messages=[
        {"role": "system", "content": "Answer only using the given context. If it's not there, say 'I don't know'."},
        {"role": "user", "content": f"Context:\n{context}\n\nQuestion: {question}"},
    ],
)
print(resp.choices[0].message.content)

Production tips

  • Use a vector database (FAISS, Qdrant, pgvector) — the plain NumPy search above is just an example.
  • Chunk your documents; split long texts before embedding.
  • Limit context: only give the model the top N documents (save tokens).
  • Related APIs: Embeddings · Rerank · Chat.

Qevron — AI gateway. Arpanet / OpenAI / Anthropic / Gemini compatible.