Real-Time RAG
Inject live user data and documentation into a generative prompt using edge functions.
Document Ingestion & Embeddings
Turn raw text chunks into standardized, indexed records using built-in embedding adapters (OpenAIEmbeddings, WorkersAIEmbeddings, VoyageEmbeddings) and embedDocumentChunks(). See Embeddings & Ingestion for every provider, Python versions and how to choose IDs.
Ingestion & Embedding
import { SwarmV, OpenAIEmbeddings, embedDocumentChunks } from "@swarmv/sdk";
const memory = new SwarmV({ apiKey: process.env.SWARMV_API_KEY });
const embedder = new OpenAIEmbeddings({ model: "text-embedding-3-small", dimensions: 768 });
// Automatically embeds chunks and generates 32-bit uint IDs with standard schemas
const records = await embedDocumentChunks({
embedder,
containerId: "container:handbook",
documentId: "handbook.md",
chunks: [
"Employees are entitled to 25 days paid annual leave.",
"Remote work stipends cover home office equipment.",
],
source: "https://wiki.corp/handbook.md",
category: "company-policy",
});
await memory.insertMany(records);Retrieval & Prompt Augmentation
Retrieval adds latency before your LLM starts streaming. With SwarmV's index at the edge, next to your API routes, retrieval stays fast, and searching with the user's own token means each user only retrieves documents they're allowed to see.
Store each chunk's text in its metadata when you index it, for example
{ container_id: "container:handbook", text: chunk }. Metadata is flat (string, number or boolean values) and up to 8 KB per record, and comes back as strings. For longer documents, store a short chunk plus a reference to the full source.Next.js / Vercel AI SDK
import { SwarmV } from "@swarmv/sdk";
import { generateText } from "ai";
import { openai } from "@ai-sdk/openai";
export async function POST(req: Request) {
const user = await getSignedInUser(req); // your own auth
const { prompt, embedding } = await req.json();
// Search as this user, so they only get documents they're allowed to see.
const swarmv = new SwarmV({ token: await swarmvTokenFor(user) });
const hits = await swarmv.search(embedding, { topK: 3 });
// Each hit carries the metadata stored with it, e.g. the chunk text (values are strings).
const context = hits.map((h) => h.metadata?.text ?? "").join("\n\n");
const { text } = await generateText({
model: openai("gpt-4o"),
system: `You are a helpful assistant. Use the following context to answer:\n\n${context}`,
prompt,
});
return Response.json({ text });
}LlamaIndex Custom Retriever
SwarmV can be used as a custom retriever for LlamaIndex.
Python / LlamaIndex
from llama_index.core import Document
from swarmv import SwarmV
swarmv = SwarmV(token_provider=fetch_swarmv_token) # a token for the end user
class SwarmVRetriever:
def retrieve(self, query):
text = getattr(query, "query_str", query)
vector = embed_model.get_text_embedding(text)
hits = swarmv.search(vector, top_k=3)
# Each hit carries its stored metadata, e.g. the chunk text
return [Document(text=(h.metadata or {}).get("text", ""), id_=h.id) for h in hits]
retriever = SwarmVRetriever()
context = "\n".join(d.text for d in retriever.retrieve("How does edge RAG work?"))
response = llm.complete(f"Use this context to answer the question:\n{context}\n\nQuestion: How does edge RAG work?")
print(response)