RAG - Native Vector Memory

Retrieval-Augmented Generation built into the language as a first-class primitive

How RAG Works in HudHudScript

Text Input
remember / recall
Embedding
Text → Vector (1536 dimensions)
HNSW Index
Hierarchical Navigable Small World graph
Similarity Search
Top-K nearest neighbors with scores

What is RAG in HudHudScript?

HudHudScript treats vector memory as a native language primitive. Instead of integrating external vector databases or writing glue code, you declare stores, remember knowledge, and recall it , all with built-in keywords.

Native Vector Memory

Vector stores are language primitives, not external dependencies. Declare them like variables.

HNSW Index

Hierarchical Navigable Small World graphs provide logarithmic search complexity for fast similarity matching.

Zero Dependencies

No external databases, no pip install, no Docker containers. The vector store is part of the runtime.

RAG Keywords Reference

Keyword Purpose Syntax
store Declare a vector store with configuration store name { ... }
remember Embed and store text into a vector store remember "text" in store_name;
recall Search for similar text in a vector store recall "query" from store_name;
forget Delete an entry from a vector store forget "id" from store_name;
embed Explicit embedding control (aspirational) embed "text" with model;

Store Declaration

A store is a named vector index. You declare it with a backend, dimensionality, and distance metric.

store medical_knowledge {
    backend: "hnsw"
    dimensions: 1536
    distance: "cosine"
}

store patient_records {
    backend: "hnsw"
    dimensions: 1536
    distance: "cosine"
}

backend

The indexing algorithm. Currently supports hnsw (Hierarchical Navigable Small World) for logarithmic-time approximate nearest neighbor search.

dimensions

The size of the embedding vectors. Use 1536 for OpenAI embeddings, 768 for many open-source models, or match your provider.

distance

Similarity metric: cosine (angular similarity), euclidean (L2 distance), or dot_product (inner product).

Remember - Storing Knowledge

The remember keyword embeds text and stores it in a vector store. Each entry is automatically assigned a unique ID and can include optional metadata.

remember "Penicillin is a beta-lactam antibiotic used for bacterial infections" in medical_knowledge;
remember "Aspirin (acetylsalicylic acid) is used for pain relief and anti-inflammation" in medical_knowledge;
remember "Metformin is first-line treatment for type 2 diabetes mellitus" in medical_knowledge;

Automatic Embedding

You do not need to manually call an embedding API. The runtime handles text-to-vector conversion behind the scenes based on the store configuration.

Recall - Semantic Search

The recall keyword performs a similarity search against a store and returns the top 5 nearest neighbors by default.

recall "What antibiotics treat bacterial infections?" from medical_knowledge;
recall "diabetes treatment options" from medical_knowledge;

Each result contains:

Field Type Description
id String Unique identifier for the stored entry
text String The original text that was stored
score Number Similarity score (0.0 to 1.0 for cosine)
metadata Object Optional key-value metadata attached to the entry

Forget - Deleting Entries

The forget keyword removes a specific entry from a store by its unique ID.

forget "entry-uuid-here" from medical_knowledge;

Note

Deletion is permanent. The entry ID is returned when you remember or recall entries. Keep track of IDs if you need to manage store contents.

Complete Example: Medical Knowledge Base

This example shows a full RAG pipeline: declaring a provider and store, defining roles and subjects with governance, ingesting medical knowledge, and querying it with semantic search.

// Medical Knowledge Base with RAG
provider medical_llm {
    backend: "anthropic"
    model: "claude-3-sonnet"
}

store medical_knowledge {
    backend: "hnsw"
    dimensions: 1536
    distance: "cosine"
}

role Clinician {
    can diagnose
    can prescribe
    can consult
}

subject DoctorAI has Clinician {
    state speciality: "general"
    uses medical_llm via clinical_assistant
}

// Ingest medical knowledge
remember "Penicillin is a beta-lactam antibiotic" in medical_knowledge;
remember "Aspirin is used for pain relief" in medical_knowledge;
remember "Metformin treats type 2 diabetes" in medical_knowledge;

// Query knowledge base
recall "antibiotic for infections" from medical_knowledge;
recall "diabetes medication" from medical_knowledge;

Governance + RAG

Notice how the DoctorAI subject has a Clinician role with specific permissions. RAG stores can be combined with HudHudScript governance to control who can access what knowledge.

Multi-Agent Research Pipeline

RAG becomes even more powerful with multiple stores and agents. This example shows a three-stage research pipeline: ingest, analyze, and write.

store research_papers {
    backend: "hnsw"
    dimensions: 1536
    distance: "cosine"
}

store analysis_notes {
    backend: "hnsw"
    dimensions: 1536
    distance: "cosine"
}

store draft_sections {
    backend: "hnsw"
    dimensions: 1536
    distance: "cosine"
}

// Stage 1: Ingest papers
remember "RAG combines retrieval with generation for grounded responses" in research_papers;
remember "HNSW provides logarithmic search complexity" in research_papers;

// Stage 2: Analyze
recall "vector search algorithms" from research_papers;

// Stage 3: Write
remember "Section 1: Introduction to RAG systems" in draft_sections;
Stage 1
Ingest source papers into research_papers store
Stage 2
Recall and analyze findings, store insights in analysis_notes
Stage 3
Compose draft sections into draft_sections store

Multilingual RAG

HudHudScript supports RAG keywords in multiple languages. The same vector memory system works with localized keywords.

Turkish

// Vektor Bellek Sistemi
depo bilgi_tabani {
    backend: "hnsw"
    dimensions: 1536
    distance: "cosine"
}

hatirla "Penisilin bakteriyel enfeksiyonlar icin kullanilir" in bilgi_tabani;
animsa "antibiyotik tedavisi" from bilgi_tabani;
unut "kayit-id" from bilgi_tabani;
English Turkish
store depo
remember hatirla
recall animsa
forget unut

Arabic

// نظام الذاكرة المتجهة
مخزن قاعدة_المعرفة {
    backend: "hnsw"
    dimensions: 1536
    distance: "cosine"
}

تذكر "البنسلين يستخدم للعدوى البكتيرية" in قاعدة_المعرفة;
استرجع "علاج المضادات الحيوية" from قاعدة_المعرفة;
انسى "معرف-السجل" from قاعدة_المعرفة;
English Arabic
store مخزن
remember تذكر
recall استرجع
forget انسى

Japanese

// ベクトルメモリシステム
ストア 知識ベース {
    backend: "hnsw"
    dimensions: 1536
    distance: "cosine"
}

記憶 "ペニシリンは細菌感染症に使用される" in 知識ベース;
想起 "抗生物質治療" from 知識ベース;
忘却 "レコードID" from 知識ベース;
English Japanese
store ストア
remember 記憶
recall 想起
forget 忘却

RAG Architecture

  +------------------+     +------------------+     +------------------+
  |   remember       |     |   Embedding      |     |   HNSW Index     |
  |   "Penicillin    | --> |   Model          | --> |                  |
  |    is a ..."     |     |   text -> vec    |     |   [0.12, 0.87,   |
  |                  |     |   (1536 dims)    |     |    0.34, ...]    |
  +------------------+     +------------------+     +--------+---------+
                                                             |
                                                             | stored
                                                             v
  +------------------+     +------------------+     +------------------+
  |   Results        |     |   Similarity     |     |   Vector Store   |
  |                  | <-- |   Search         | <-- |                  |
  |   id: "abc-123"  |     |   cosine / L2 /  |     |   medical_       |
  |   score: 0.94    |     |   dot_product    |     |   knowledge      |
  |   text: "..."    |     |                  |     |                  |
  +------------------+     +------------------+     +------------------+
                                  ^
                                  |
                           +------+-------+
                           |   recall     |
                           |   "antibiotic|
                           |    for ..."  |
                           +--------------+