# shaynep/livekit-rag-thinking architecture

> Architecture diagram of the GitHub repository shaynep/livekit-rag-thinking, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-10-13). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/shaynep/livekit-rag-thinking
- Repository: https://github.com/shaynep/livekit-rag-thinking

## Overview

This repository is a LiveKit voice assistant demo that answers LiveKit questions using retrieval-augmented generation. A user speaks through LiveKit; the voice pipeline transcribes input and invokes the registered RAG function, which announces a lookup, embeds the question, searches the Annoy index, enriches chat context, and speaks the generated answer. The separate data-building stage embeds paragraphs from raw_data.txt and saves the index and paragraph lookup. README documents alternative delay messages; source excerpts show static text enabled and audio/LLM-generated alternatives commented out.

Main entry points: [`agent.py`](https://github.com/shaynep/livekit-rag-thinking/blob/main/agent.py) (RAG function), [`build_data.py`](https://github.com/shaynep/livekit-rag-thinking/blob/main/build_data.py) (Paragraph tokenizing), [`raw_data.txt`](https://github.com/shaynep/livekit-rag-thinking/blob/main/raw_data.txt) (Knowledge text), [`let_me_check_that.wav`](https://github.com/shaynep/livekit-rag-thinking/blob/main/let_me_check_that.wav) (Recorded prompt)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_prep["Knowledge preparation"]
  node_source_text["Knowledge text<br/>[raw_data.txt]"]
  node_paragraphs["Paragraph tokenizing<br/>[build_data.py]"]
  node_embed_build["Embedding creation<br/>[build_data.py]"]
  node_index_build["Index construction<br/>[build_data.py]"]
  node_index_store[("Annoy index<br/>[build_data.py]")]
  node_paragraph_store[("Paragraph lookup<br/>[build_data.py]")]
end

subgraph group_runtime["Voice assistant"]
  node_agent_entry["Agent entrypoint<br/>[agent.py]"]
  node_voice_pipeline["Voice pipeline<br/>[agent.py]"]
  node_speech_recognition["Speech recognition<br/>[agent.py]"]
  node_speech_synthesis["Speech synthesis<br/>[agent.py]"]
end

subgraph group_rag["Retrieval and response"]
  node_rag_tool["RAG function<br/>[agent.py]"]
  node_question_embedding["Question embedding<br/>[agent.py]"]
  node_annoy_search["Similarity search<br/>[agent.py]"]
  node_context_enrichment["Context enrichment<br/>[agent.py]"]
  node_llm_response["LLM response<br/>[agent.py]"]
end

subgraph group_delay["Delay handling"]
  node_static_thinking["Static messages<br/>[agent.py]"]
  node_dynamic_thinking["Generated messages<br/>[agent.py]"]
  node_audio_playback["Audio playback<br/>[agent.py]"]
  node_audio_file["Recorded prompt"]
end

node_user(("Voice user"))
node_livekit["LiveKit room"]

node_source_text -->|"tokenizes"| node_paragraphs
node_paragraphs -->|"embeds paragraphs"| node_embed_build
node_embed_build -->|"adds vectors"| node_index_build
node_index_build -->|"saves index"| node_index_store
node_paragraphs -->|"saves lookup"| node_paragraph_store
node_user -->|"speaks through"| node_livekit
node_livekit -->|"connects"| node_agent_entry
node_agent_entry -->|"starts"| node_voice_pipeline
node_voice_pipeline -->|"transcribes"| node_speech_recognition
node_voice_pipeline -->|"speaks responses"| node_speech_synthesis
node_voice_pipeline -->|"invokes"| node_rag_tool
node_rag_tool -->|"announces lookup"| node_static_thinking
node_rag_tool -->|"runs lookup"| node_context_enrichment
node_context_enrichment -->|"embeds question"| node_question_embedding
node_context_enrichment -->|"queries"| node_annoy_search
node_annoy_search -->|"searches"| node_index_store
node_annoy_search -->|"looks up result"| node_paragraph_store
node_context_enrichment -->|"requests answer"| node_llm_response
node_llm_response -->|"returns speech stream"| node_voice_pipeline
node_rag_tool -.->|"may generate"| node_dynamic_thinking
node_rag_tool -.->|"may play"| node_audio_playback
node_audio_playback -->|"plays"| node_audio_file

click node_source_text "https://github.com/shaynep/livekit-rag-thinking/blob/main/raw_data.txt"
click node_paragraphs "https://github.com/shaynep/livekit-rag-thinking/blob/main/build_data.py"
click node_embed_build "https://github.com/shaynep/livekit-rag-thinking/blob/main/build_data.py"
click node_index_build "https://github.com/shaynep/livekit-rag-thinking/blob/main/build_data.py"
click node_index_store "https://github.com/shaynep/livekit-rag-thinking/blob/main/build_data.py"
click node_paragraph_store "https://github.com/shaynep/livekit-rag-thinking/blob/main/build_data.py"
click node_agent_entry "https://github.com/shaynep/livekit-rag-thinking/blob/main/agent.py"
click node_voice_pipeline "https://github.com/shaynep/livekit-rag-thinking/blob/main/agent.py"
click node_speech_recognition "https://github.com/shaynep/livekit-rag-thinking/blob/main/agent.py"
click node_speech_synthesis "https://github.com/shaynep/livekit-rag-thinking/blob/main/agent.py"
click node_rag_tool "https://github.com/shaynep/livekit-rag-thinking/blob/main/agent.py"
click node_question_embedding "https://github.com/shaynep/livekit-rag-thinking/blob/main/agent.py"
click node_annoy_search "https://github.com/shaynep/livekit-rag-thinking/blob/main/agent.py"
click node_context_enrichment "https://github.com/shaynep/livekit-rag-thinking/blob/main/agent.py"
click node_llm_response "https://github.com/shaynep/livekit-rag-thinking/blob/main/agent.py"
click node_static_thinking "https://github.com/shaynep/livekit-rag-thinking/blob/main/agent.py"
click node_dynamic_thinking "https://github.com/shaynep/livekit-rag-thinking/blob/main/agent.py"
click node_audio_playback "https://github.com/shaynep/livekit-rag-thinking/blob/main/agent.py"
click node_audio_file "https://github.com/shaynep/livekit-rag-thinking/blob/main/let_me_check_that.wav"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_source_text,node_paragraphs,node_embed_build,node_index_build,node_index_store,node_paragraph_store,node_user toneBlue
class node_agent_entry,node_voice_pipeline,node_speech_recognition,node_speech_synthesis toneAmber
class node_rag_tool,node_question_embedding,node_annoy_search,node_context_enrichment,node_llm_response toneMint
class node_static_thinking,node_dynamic_thinking,node_audio_playback,node_audio_file toneRose
class node_livekit toneIndigo
```

## Components

### Knowledge preparation

- [Knowledge text](https://github.com/shaynep/livekit-rag-thinking/blob/main/raw_data.txt): `raw_data.txt`
- [Paragraph tokenizing](https://github.com/shaynep/livekit-rag-thinking/blob/main/build_data.py): `build_data.py`
- [Embedding creation](https://github.com/shaynep/livekit-rag-thinking/blob/main/build_data.py): `build_data.py`
- [Index construction](https://github.com/shaynep/livekit-rag-thinking/blob/main/build_data.py): `build_data.py`
- [Annoy index](https://github.com/shaynep/livekit-rag-thinking/blob/main/build_data.py): `build_data.py`
- [Paragraph lookup](https://github.com/shaynep/livekit-rag-thinking/blob/main/build_data.py): `build_data.py`

### Voice assistant

- [Agent entrypoint](https://github.com/shaynep/livekit-rag-thinking/blob/main/agent.py): `agent.py`
- [Voice pipeline](https://github.com/shaynep/livekit-rag-thinking/blob/main/agent.py): `agent.py`
- [Speech recognition](https://github.com/shaynep/livekit-rag-thinking/blob/main/agent.py): `agent.py`
- [Speech synthesis](https://github.com/shaynep/livekit-rag-thinking/blob/main/agent.py): `agent.py`

### Retrieval and response

- [RAG function](https://github.com/shaynep/livekit-rag-thinking/blob/main/agent.py): `agent.py`
- [Question embedding](https://github.com/shaynep/livekit-rag-thinking/blob/main/agent.py): `agent.py`
- [Similarity search](https://github.com/shaynep/livekit-rag-thinking/blob/main/agent.py): `agent.py`
- [Context enrichment](https://github.com/shaynep/livekit-rag-thinking/blob/main/agent.py): `agent.py`
- [LLM response](https://github.com/shaynep/livekit-rag-thinking/blob/main/agent.py): `agent.py`

### Delay handling

- [Static messages](https://github.com/shaynep/livekit-rag-thinking/blob/main/agent.py): `agent.py`
- [Generated messages](https://github.com/shaynep/livekit-rag-thinking/blob/main/agent.py): `agent.py`
- [Audio playback](https://github.com/shaynep/livekit-rag-thinking/blob/main/agent.py): `agent.py`
- [Recorded prompt](https://github.com/shaynep/livekit-rag-thinking/blob/main/let_me_check_that.wav): `let_me_check_that.wav`

### Other components

- **Voice user**
- **LiveKit room**

## Connections

- Knowledge text → Paragraph tokenizing: tokenizes (evidence: `build_data.py`)
- Paragraph tokenizing → Embedding creation: embeds paragraphs (evidence: `build_data.py`)
- Embedding creation → Index construction: adds vectors (evidence: `build_data.py`)
- Index construction → Annoy index: saves index (evidence: `build_data.py`)
- Paragraph tokenizing → Paragraph lookup: saves lookup (evidence: `build_data.py`)
- Voice user → LiveKit room: speaks through (evidence: `README.md`)
- LiveKit room → Agent entrypoint: connects (evidence: `agent.py`)
- Agent entrypoint → Voice pipeline: starts (evidence: `agent.py`)
- Voice pipeline → Speech recognition: transcribes (evidence: `README.md`)
- Voice pipeline → Speech synthesis: speaks responses (evidence: `README.md`)
- Voice pipeline → RAG function: invokes (evidence: `agent.py`)
- RAG function → Static messages: announces lookup (evidence: `agent.py`)
- RAG function → Context enrichment: runs lookup (evidence: `agent.py`)
- Context enrichment → Question embedding: embeds question (evidence: `agent.py`)
- Context enrichment → Similarity search: queries (evidence: `agent.py`)
- Similarity search → Annoy index: searches (evidence: `agent.py`)
- Similarity search → Paragraph lookup: looks up result (evidence: `agent.py`)
- Context enrichment → LLM response: requests answer (evidence: `agent.py`)
- LLM response → Voice pipeline: returns speech stream (evidence: `agent.py`)
- RAG function → Generated messages: may generate (evidence: `README.md`)
- RAG function → Audio playback: may play (evidence: `README.md`)
- Audio playback → Recorded prompt: plays (evidence: `agent.py`)
