14_semantic_rag

command
v2.26.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 20, 2026 License: MIT Imports: 15 Imported by: 0

README

14 · Semantic RAG — real embedding model + LLM

The embedder-backed counterpart to the lexical examples. It wires a real embedding model into CortexDB so retrieval works on meaning, not keyword overlap, and uses a chat LLM to answer — both via an OpenAI-compatible endpoint (the defaults target DashScope / Qwen).

docs ──InsertText (embeds via text-embedding-v4)──▶ CortexDB vector index
query ──SearchText (embeds query, cosine search)──▶ top hit by MEANING
                                                      │
                                          qwen3.7-plus answers from that context

Why it matters

The query "who deals with production outages after hours?" shares no keywords with the stored fact "When the site goes down at night, the engineer on rotation gets paged…" — lexical/FTS retrieval would miss it. With an embedder, vector search ranks it first:

=== semantic vector search (no keyword overlap): "who deals with production outages after hours?" ===
  1. [doc-oncall  score=0.545] When the site goes down at night, the engineer on rotation gets paged and must respond.
  2. [doc-payments score=0.296] ...
=== qwen3.7-plus answer ===
  The engineer on rotation gets paged and must respond when the site goes down at night.

Run it

Requires an embedding + chat endpoint (config via env or a .env loaded by godotenv):

OPENAI_API_KEY=sk-...                                   # required
OPENAI_BASE_URL=https://dashscope.aliyuncs.com/compatible-mode/v1
OPENAI_MODEL=qwen3.7-plus                                # chat
EMBED_MODEL=text-embedding-v4                            # embeddings
EMBED_DIM=1024
go run ./examples/14_semantic_rag

Any OpenAI-compatible endpoint works (OpenAI, DashScope, a local Ollama with an embedding model, etc.) — embedder.go is a ~60-line cortexdb.Embedder implementation you can copy.

How it plugs in

  • embedder.go implements cortexdb.Embedder (Embed / EmbedBatch / Dim).
  • cortexdb.Open(cfg, cortexdb.WithEmbedder(e)) with cfg.Dimensions = 1024 switches CortexDB from lexical to vector retrieval.
  • db.InsertText embeds + indexes; db.SearchText embeds the query and runs cosine similarity over the HNSW index.

Documentation

Overview

Semantic RAG — CortexDB with a real embedding model (vector search), plus an LLM answer. This is the embedder-backed counterpart to the lexical examples: it wires a DashScope (Qwen) text-embedding-v4 embedder so retrieval works on MEANING, not keyword overlap, and uses qwen for the final answer.

Reads config from the environment (or a .env via godotenv):

OPENAI_API_KEY    (required)
OPENAI_BASE_URL   default https://dashscope.aliyuncs.com/compatible-mode/v1
OPENAI_MODEL      chat model, default qwen3.7-plus
EMBED_MODEL       embedding model, default text-embedding-v4
EMBED_DIM         embedding dimension, default 1024

go run ./examples/14_semantic_rag

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL