Skip to content

feat: Redis vector storage for PDF-based RAG#5

Draft
Copilot wants to merge 3 commits intomainfrom
copilot/add-redis-vector-storage
Draft

feat: Redis vector storage for PDF-based RAG#5
Copilot wants to merge 3 commits intomainfrom
copilot/add-redis-vector-storage

Conversation

Copy link
Copy Markdown

Copilot AI commented Apr 26, 2026

Adds infrastructure for ingesting PDF documents into Redis vector indexes and surfacing retrieved context to agents via MCP tools. Agents like HeatingAgent and AppliancesAgent can now search ingested manuals (heatpump setup guides, Miele appliance PDFs) for relevant context when answering questions.

Vector store (raw Redis Search)

Microsoft.Extensions.VectorData.Redis doesn't exist on NuGet yet, so the implementation uses FT.CREATE/FT.SEARCH commands directly via StackExchange.Redis. HNSW indexing with configurable distance metric (default: cosine).

// Key schema
rag:{collection}:{chunkId}     // Hash — chunk content + embedding vector
rag:{collection}:idx           // FT.INDEX — vector similarity index
rag:meta:{collection}:{docId}  // Hash — document metadata

PDF ingestion pipeline

PdfDocumentIngestionService: PdfPig extraction → token-approximate chunking (configurable size/overlap) → batch embedding via IEmbeddingGenerator<string, Embedding<float>> → upsert to Redis.

Embedding generator factory

EmbeddingGeneratorFactory — mirrors the existing AgentExtensions.CreateAgent pattern for IChatClient. Supports Ollama (OllamaApiClient directly implements IEmbeddingGenerator), OpenAI, and AzureOpenAI. Local helper until CasCap.Common.AI adds a shared version.

MCP tools (RagMcpQueryService)

  • SearchDocuments — KNN vector similarity search
  • ListDocuments / GetDocumentInfo — collection metadata
  • IngestDocument / RemoveDocument — on-demand management

Infrastructure

  • docker-compose: redisredis/redis-stack-server (enables Search module); ollama-init pulls nomic-embed-text (768d)
  • RagIngestionBgService: auto-ingests PDFs from configured directory on startup
  • Feature-gated: FeatureNames.Rag — opt-in, zero impact on existing features

Configuration

"RagConfig": {
  "EmbeddingProvider": "EdgeEmbedding",
  "IndexName": "rag-documents",
  "Dimension": 768,
  "ChunkSizeTokens": 512,
  "ChunkOverlapTokens": 50,
  "TopK": 5,
  "DocumentsPath": "/data/rag-documents",
  "AgentSources": {
    "HeatingAgent": [{ "CollectionName": "rag-documents", "TopK": 5 }],
    "AppliancesAgent": [{ "CollectionName": "rag-documents", "TopK": 3 }]
  }
}

Notes for follow-up

  • EmbeddingGeneratorFactory should migrate to CasCap.Common.AI as a shared AgentExtensions.CreateEmbeddingGenerator
  • RAG context middleware (IChatClient decorator that auto-injects retrieved chunks into agent prompts) is a natural next step
  • When Microsoft.Extensions.VectorData.Redis ships on NuGet, RedisDocumentVectorStore can be simplified to use the official abstraction

Copilot AI and others added 3 commits April 26, 2026 02:18
- Add NuGet packages: Microsoft.Extensions.VectorData.Redis, UglyToad.PdfPig
- Create RagConfig, DocumentChunk, DocumentInfo, DocumentSearchResult models
- Create IDocumentVectorStore and IDocumentIngestionService abstractions
- Create RedisDocumentVectorStore (Redis vector search implementation)
- Create PdfDocumentIngestionService (PDF extraction, chunking, embedding)
- Create RagMcpQueryService with MCP tools (search, list, ingest, remove)
- Create RagIngestionBgService for auto-ingestion on startup
- Create EmbeddingGeneratorFactory for Ollama/OpenAI/AzureOpenAI
- Add FeatureNames.Rag constant and wire up in Program.cs
- Update docker-compose to use redis/redis-stack-server image
- Add nomic-embed-text embedding model pull to ollama-init
- Add EdgeEmbedding provider and RagConfig to appsettings
- Update HeatingAgent and AppliancesAgent instructions with RAG guidance

Agent-Logs-Url: https://github.com/f2calv/SmartHaus/sessions/7df8a854-c8fb-4d26-81aa-074fe0d0135c

Co-authored-by: f2calv <16097639+f2calv@users.noreply.github.com>
…ctory

- Use PdfPig 0.1.14 (correct version on NuGet)
- Remove Microsoft.Extensions.VectorData.Redis (not available on NuGet)
- Implement Redis vector search using raw FT.CREATE/FT.SEARCH commands
- Fix RagIngestionBgService to use IBgFeature.ExecuteAsync pattern
- Fix OllamaApiClient embedding (directly implements IEmbeddingGenerator)
- Use System.ClientModel.ApiKeyCredential for OpenAI client

Agent-Logs-Url: https://github.com/f2calv/SmartHaus/sessions/7df8a854-c8fb-4d26-81aa-074fe0d0135c

Co-authored-by: f2calv <16097639+f2calv@users.noreply.github.com>
- Extract Server property with null-safe FirstOrDefault in RedisDocumentVectorStore
- Extract CharsPerTokenApprox constant in PdfDocumentIngestionService
- Add ArgumentException guard for overlap >= chunk size (prevents infinite loop)
- Rename Create to CreateEmbeddingGenerator in EmbeddingGeneratorFactory

Agent-Logs-Url: https://github.com/f2calv/SmartHaus/sessions/7df8a854-c8fb-4d26-81aa-074fe0d0135c

Co-authored-by: f2calv <16097639+f2calv@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants