Skip to content

AI.* — semantic cache and embeddings

AI.SEMANTIC_CACHE and AI.EMBED are part of Pion's supported surface. The rest of the AI.* family (AI.COMPLETE, AI.CHAT, AI.FLARE.*, AI.MEMORY) is experimental: wired and wire-tested, with no published measurement of what it buys and no user outside this project, so it may change or be removed (README).

Command Pion path GLIDE Description
FT.CREATE SLOW ❌ Create vector index (HNSW, SCHEMA, VECTOR field); indexes the hashes already in the keyspace unless SKIPINITIALSCAN; PREFIX, TAG SEPARATOR / CASESENSITIVE, NOINDEX applied; ON JSON, FILTER, TEMPORARY, AS, non-FLOAT32 TYPE and unknown options refused (vector_engine.md)
FT.SEARCH SLOW ❌ KNN vector search; PARAMS format; SORTBY, LIMIT (offset, total, default 10), RETURN, NOCONTENT, WITHSCORES; BM25 <q> [K k] [K1 f] [B f]; HYBRID (vector+text); TAG/NUMERIC prefilters and FILTER; INKEYS, GEOFILTER, WITHPAYLOADS, WITHSORTKEYS, EXPLAINSCORE and unknown options refused
FT.OPTIMIZE SLOW ❌ Build l0_compact, compact_buffer; save HNSW to disk (pion.hnsw.0)
FT.DROPINDEX SLOW ❌ Drop vector index; clears in-memory HNSW. DD deletes the documents too (-w 1). An unknown name or argument is refused, nothing dropped
FT.INFO SLOW ❌ [index_name, <name>, num_docs, <n>] (documents with a vector, deleted ones not counted); checks the name against the shared registry, same answer on every worker; unknown name → Unknown index name
FT._LIST SLOW ❌ The index this server holds, or none
FT.HYBRID SLOW ❌ FT.HYBRID <index> <text_query> <vector_blob> [K <k>] [ALPHA <a>] [K1 <f>] [B <f>] — BM25+vector fusion with Reciprocal Rank Fusion
FT.ADDTEXT SLOW ❌ Add text doc: store in keyspace + BM25 doc set (integer ids), embed via HTTP → insert into semantic HNSW
FT.SEARCHTEXT SLOW ❌ Semantic text search over docs added via FT.ADDTEXT
AI.COMPLETE SLOW ❌ Semantic cache check → LLM call on miss → cache result
AI.SEMANTIC_CACHE GET SLOW ❌ Embed query → HNSW search → return cached response if cos sim ≥ threshold
AI.SEMANTIC_CACHE SET SLOW ❌ Embed query → insert into HNSW → store response
AI.CHAT SLOW ❌ Full RAG in one command: retrieve context → build prompt → call LLM
AI.FLARE LOAD SLOW ❌ Add text to in-Mojo FLARE knowledge base
AI.FLARE RUN SLOW ❌ Mid-generation retrieval loop (FLARE algorithm in Mojo)
AI.FLARE INFO SLOW ❌ FLARE KB stats
AI.EMBED SLOW ❌ Embed text with the in-process model. -ERR AI.EMBED requires --inference or --emb-enabled when no backend is active
AI.GENERATE SLOW ❌ Generate via the sidecar; -ERR ... requires --inference otherwise
AI.LOADMODEL SLOW ❌ Load a sidecar model; -ERR ... requires --inference otherwise
AI.MEMORY SLOW ❌ AI.MEMORY ADD\|RECALL\|CONTEXT ... — agent-memory surface; a bare call returns the syntax line

The gateway commands

AI.COMPLETE

Experimental. AI.COMPLETE is wire-tested by tests/test_ai_gateway.py and tests/test_ai_chat_json.py (gate tier). No measurement of what it buys is published and nobody outside this project is known to use it, so it is outside Pion's supported surface and may change or be removed.

AI.COMPLETE <query> [TOKENS <n>] [THRESHOLD <t>]

One-command cache-augmented generation: checks the semantic cache, returns the cached response on a hit, calls the LLM and caches the result on a miss.

redis-cli AI.COMPLETE "What is the capital of France?" TOKENS 100 THRESHOLD 0.92
# Cache hit (≥ threshold cosine similarity): returns the cached response
# Cache miss: calls LLM, stores result, returns LLM response

examples/pion_vs_ollama_demo.py times a cache hit against a live Ollama call on your own machine; no result of it is published, so this page quotes no speedup.


FT.ADDTEXT

FT.ADDTEXT <index> <doc_id> <text>

Stores a text document and makes it immediately searchable:

  1. Stores doc_id + text as a HASH in the keyspace
  2. Registers doc_id in the BM25 doc set, so FT.OPTIMIZE indexes it
  3. Embeds text via EmbeddingClient (HTTP/1.0 POST to /v1/embeddings)
  4. Inserts the embedding into the per-worker semantic HNSW index (add_and_insert)
  5. Returns +OK

Steps 3–4 require EmbeddingConfig.enabled = True; steps 1–2 do not, so BM25 ingest works with no embedding backend at all.

Step 2 only applies to non-negative integer doc_ids — BM25 hits are returned as integer ext_ids. A string id like doc:1 is still stored and embedded, so FT.SEARCHTEXT finds it, but it stays invisible to FT.SEARCH … BM25.

# string ids — semantic search only
redis-cli FT.ADDTEXT products doc:1 "Wireless headphones with active noise cancellation"
redis-cli FT.ADDTEXT products doc:2 "Mechanical keyboard Cherry MX switches"

# integer ids — semantic search AND BM25 (after FT.OPTIMIZE)
redis-cli FT.ADDTEXT products 1 "Wireless headphones with active noise cancellation"
redis-cli FT.OPTIMIZE products
redis-cli FT.SEARCH products BM25 "noise cancellation" K 3

FT.SEARCHTEXT

FT.SEARCHTEXT <index> <query_text> [K <k>]

Semantic search over text documents added via FT.ADDTEXT:

  1. Embeds query_text via EmbeddingClient
  2. Searches the per-worker semantic HNSW (ef=32, default k=10)
  3. Returns matching doc_id strings as a RESP array
redis-cli FT.SEARCHTEXT products "noise cancelling audio" K 3
# → 1) "doc:1"

AI.CHAT

Experimental. AI.CHAT is wire-tested by tests/test_ai_gateway.py and tests/test_ai_chat_json.py (gate tier). No measurement of what it buys is published and nobody outside this project is known to use it, so it is outside Pion's supported surface and may change or be removed.

AI.CHAT <prompt> [CONTEXT <index> <text> [K <k>]]

Full RAG pipeline in a single RESP3 command:

  1. (optional) Embeds text, searches HNSW for top-k doc_ids, retrieves stored texts
  2. Builds LLM prompt: "Context:\n<docs>\n\nUser: <prompt>" (or just <prompt> if no CONTEXT)
  3. Sends HTTP/1.0 POST to /v1/chat/completions
  4. Parses "content":"..." from JSON response, returns as RESP bulk string
# Simple chat
redis-cli AI.CHAT "What is the capital of France?"

# RAG: retrieve context then generate
redis-cli AI.CHAT "Which audio product do you recommend?" CONTEXT products "audio" K 3
# → "Based on the available products, I recommend the wireless headphones..."

AI.SEMANTIC_CACHE

Caches LLM responses keyed by semantic similarity of the input query:

AI.SEMANTIC_CACHE SET "What is the capital of France?" "+Paris\r\n"
AI.SEMANTIC_CACHE GET "capital of France?"    → +Paris
AI.SEMANTIC_CACHE GET "unrelated query"       → $-1 (cache miss)

Workspace audit trail

A cache hit tells you what the model answered, not what it had in mind while answering. WORKSPACE records the caller's snapshot — e.g. the retrieved context or top-k, base64 — at cache-WRITE time, and EXPLAIN reads it back with no inference infrastructure required. That is the point: a compliance reviewer can ask what was in the workspace without being able to run the model.

AI.SEMANTIC_CACHE SET "capital of France?" "Paris" WORKSPACE "<base64 lens top-k>"
AI.SEMANTIC_CACHE EXPLAIN "capital of France?"      → "<base64 lens top-k>"
AI.SEMANTIC_CACHE EXPLAIN "never cached"            → nil
AI.SEMANTIC_CACHE GET "capital of France?" WITHWORKSPACE
                                                     → %2 response/workspace (RESP3)
                                                     → *4 flat pairs        (RESP2)

The blob is opaque to the server — stored and returned, never interpreted — so the lens format can change without a wire change. An entry written without WORKSPACE reports nil rather than an empty string, so "no audit trail" is distinguishable from "an empty one".

WITHWORKSPACE is opt-in and the default GET reply is unchanged, on both protocols. Always returning a RESP3 map would break every RESP3 client already reading GET as a bulk string, and a wire break to add observability is the wrong trade. EXPLAIN needs no flag.

Implementation note: workspaces[] runs parallel to responses[], and other callers (AI.MEMORY ADD in ai.mojo, FT.ADDTEXT in vector.mojo) add entries without a workspace. cache_set and add_entry therefore PAD to count before appending rather than assuming alignment — if the two ever drift, EXPLAIN returns some other entry's audit trail, which is worse than returning none.

See src/network/semantic_cache.mojo and src/network/embedding_client.mojo. Test: tests/test_gh115_workspace.py. It ENV_SKIPs without an embedding backend, because every SET is a silent no-op there and the failures would all be about the missing backend.


Embedding backends and how the first request just works: Embeddings.