Multi-Turn RAG

A conversational RAG system where users can ask follow-up questions. The system retrieves new context based on the evolving conversation.

Why This Example?

This showcases hypergraph's key strength: a DAG (retrieval) nested inside a cycle (conversation).

Pure DAG frameworks can't do this — they can't loop back for follow-up questions. Hypergraph handles it naturally.

The Architecture

┌────────────────────────────────────────────────────────────┐
│                   CONVERSATION LOOP                        │
│                                                            │
│  user_input → RAG_PIPELINE → accumulate → should_continue  │
│       ↑           │                            │           │
│       │           ▼                            │           │
│       │    ┌─────────────┐                     │           │
│       │    │ embed       │                     │           │
│       │    │     ↓       │                     │           │
│       │    │ retrieve    │ (DAG)               │           │
│       │    │     ↓       │                     │           │
│       │    │ generate    │                     │           │
│       │    └─────────────┘                     │           │
│       │                                        │           │
│       └────────────────────────────────────────┘           │
│                                        │                   │
│                                        ▼                   │
│                                       END                  │
└────────────────────────────────────────────────────────────┘

Complete Implementation

Key Design Decisions

1. Context-Aware Embedding

The query embedding includes recent conversation history:

This helps retrieval understand follow-up questions like "Tell me more about that" or "What about the second point?"

2. Adaptive Retrieval

Adjust retrieval strategy based on conversation state:

Initial questions get fewer documents (broad context). Follow-ups get more (specific details).

3. Clean History Management

The accumulate_history node handles state updates:

No mutation. No side effects. Just pure data transformation.

4. Flexible Termination

The routing logic can terminate based on multiple conditions:

Testing the System

Test the RAG pipeline independently:

Test the full conversation:

Extending the Pattern

Add Streaming

Add Memory Summarization

For long conversations, summarize older history:

Add Citation Tracking

What's Next?

Last updated