Skip to content

fix(rag): make NaiveRAG generate from retrieved context - #7

Merged
ChinaYiqun merged 4 commits into
masterfrom
agent/fix-naive-rag-pipeline
Jul 29, 2026
Merged

ChinaYiqun merged 4 commits into
masterfrom
agent/fix-naive-rag-pipeline

Conversation

@ChinaYiqun

Copy link
Copy Markdown
Owner

What changed

  • introduces a minimal ChatService abstraction so the RAG pipeline can be tested without network calls;
  • keeps OpenAIChatService as the default OpenAI-compatible implementation;
  • changes NaiveRAG from a demo-shaped chain into an actual RAG pipeline;
  • calculates the query embedding once instead of recalculating it during sorting;
  • batches chunk embedding through EmbeddingService#getEmbeddings;
  • validates pipeline order, blank input, missing chunks, empty vectors, and dimension mismatches;
  • selects configurable Top-K chunks after ranking;
  • builds a grounded prompt containing the original question and retrieved passages;
  • instructs the model to treat retrieved text as untrusted data and to cite passages as [1], [2], etc.;
  • retains parsering() as a deprecated compatibility alias for the correctly named parsing() method;
  • replaces the commented demo test with deterministic, offline unit tests.

Root cause

The previous NaiveRAG.LLMChat() ignored both the user query and retrieved chunks. It always sent the fixed prompt 1+1 = ? to the model. The embedding phase also computed the query vector and discarded it, forcing the sorting phase to make a second provider call.

User impact

The minimal pipeline now performs the behavior its name promises:

parse -> chunk -> embed -> rank -> Top-K context -> grounded generation

The full constructor supports dependency injection for alternative splitters, embedding models, and chat providers while the original two-argument constructor remains available.

Validation

CI runs all Maven tests. The new tests verify that:

  • the query embedding is called once;
  • chunk embeddings are batched once;
  • the nearest passage becomes the selected context;
  • the original user question reaches the model;
  • the selected passage reaches the model;
  • a passage outside Top-K does not reach the model;
  • sorting before embedding fails explicitly.

@ChinaYiqun
ChinaYiqun marked this pull request as ready for review July 29, 2026 08:49
@ChinaYiqun
ChinaYiqun merged commit 5535c6e into master Jul 29, 2026
1 check passed
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.

1 participant