Skip to content

RAG Knowledge Base

RAG (Retrieval-Augmented Generation) Knowledge Base is one of the core features of NiteMoon AI Platform. It supports dual-mode retrieval with vector search and graph search, helping users build intelligent Q&A systems based on private knowledge.

Key Features

  • 3 vector store backends: Redis, PgVector, Milvus
  • Intelligent document parsing: Supports PDF, Office, Markdown, and other formats
  • Text chunking: Configurable chunk size and overlap
  • Embedding generation: Automatic text vectorization
  • Configurable retrieval: Adjustable parameters such as topK and similarity threshold
  • Streaming conversation: Support for SSE streaming output

Workflow

mermaid
graph LR
    A[Document Upload] --> B[Document Parsing]
    B --> C[Text Chunking]
    C --> D[Embedding Generation]
    D --> E[Vector Storage]
    F[User Query] --> G[Query Embedding]
    G --> H[Vector Retrieval]
    E --> H
    H --> I[Context Injection]
    I --> J[LLM Generate Response]

Vector Store Backends

Redis

Suitable for small-scale deployment, simple and easy to use.

yaml
rag:
  vector-store:
    type: redis
    host: localhost
    port: 6379
    index: nitemoon-embeddings

PgVector

Suitable for medium-scale deployment, based on PostgreSQL.

yaml
rag:
  vector-store:
    type: pgvector
    url: jdbc:postgresql://localhost:5432/nitemoon
    table: embeddings

Milvus

Suitable for large-scale deployment, a professional vector database.

yaml
rag:
  vector-store:
    type: milvus
    host: localhost
    port: 19530
    collection: nitemoon-embeddings

Document Parsing

Supports multiple document formats:

FormatParserDescription
PDFApache TikaSupports text and image extraction
WordApache Tika.doc, .docx
ExcelApache Tika.xls, .xlsx
PowerPointApache Tika.ppt, .pptx
MarkdownBuilt-in parserNative support
TXTBuilt-in parserPlain text

Text Chunking

Configurable chunking strategy:

yaml
rag:
  chunking:
    chunk-size: 500        # Chunk size (characters)
    chunk-overlap: 50      # Overlap size
    separators:            # Separator priority
      - "\n\n"
      - "\n"
      - "。"
      - "!"
      - "?"

Retrieval Parameters

yaml
rag:
  retrieval:
    top-k: 5                    # Number of results returned
    similarity-threshold: 0.7   # Similarity threshold
    max-context-length: 4000    # Maximum context length

Use Cases

Knowledge Q&A

java
// Create knowledge base
KnowledgeBase kb = new KnowledgeBase();
kb.setName("Product Documentation");
knowledgeBaseService.save(kb);

// Upload document
documentService.upload(kb.getId(), new File("product-doc.pdf"));

// Query
RAGResponse response = ragService.query(
    kb.getId(),
    "How to configure the RAG knowledge base?"
);

Intelligent Customer Service

java
// Configure RAG parameters
RAGConfig config = RAGConfig.builder()
    .topK(3)
    .similarityThreshold(0.8)
    .build();

// Query
RAGResponse response = ragService.query(
    knowledgeBaseId,
    userQuery,
    config
);

Code Examples

Create a Knowledge Base

java
@Service
public class KnowledgeBaseService {
    
    public KnowledgeBase create(String name, String description) {
        KnowledgeBase kb = new KnowledgeBase();
        kb.setName(name);
        kb.setDescription(description);
        return repository.save(kb);
    }
}

Document Upload and Parsing

java
@Service
public class DocumentService {
    
    public void upload(Long kbId, File file) {
        // Parse document
        String content = documentParser.parse(file);
        
        // Text chunking
        List<String> chunks = textChunker.chunk(content);
        
        // Generate embeddings
        for (String chunk : chunks) {
            float[] embedding = embeddingModel.embed(chunk);
            
            // Store in vector database
            DocumentFragment fragment = new DocumentFragment();
            fragment.setKbId(kbId);
            fragment.setContent(chunk);
            fragment.setEmbedding(embedding);
            fragmentRepository.save(fragment);
        }
    }
}

RAG Query

java
@Service
public class RAGService {
    
    public RAGResponse query(Long kbId, String query) {
        // Generate query embedding
        float[] queryEmbedding = embeddingModel.embed(query);
        
        // Vector retrieval
        List<DocumentFragment> results = vectorStore.search(
            kbId,
            queryEmbedding,
            5,  // topK
            0.7 // similarityThreshold
        );
        
        // Build context
        String context = results.stream()
            .map(DocumentFragment::getContent)
            .collect(Collectors.joining("\n\n"));
        
        // LLM generate response
        String prompt = String.format(
            "Answer the question based on the following context:\n\n%s\n\nQuestion: %s",
            context, query
        );
        
        String answer = chatModel.generate(prompt);
        
        return new RAGResponse(answer, results);
    }
}

Best Practices

  1. Set appropriate chunk size: Too small loses context, too large affects retrieval precision
  2. Adjust similarity threshold: Tune based on business scenarios to avoid irrelevant results
  3. Choose the right vector store: Select based on data scale and performance requirements
  4. Update knowledge base regularly: Keep knowledge base content up to date
  5. Monitor retrieval quality: Periodically evaluate retrieval effectiveness and optimize parameters

Copyright © 2023-2026 nitemoon.cn All Rights Reserved