|
| 1 | +# RAG Pipeline with Distill |
| 2 | + |
| 3 | +Use Distill's dedup + memory pipeline to build a retrieval-augmented generation system that avoids redundant context and remembers past interactions. |
| 4 | + |
| 5 | +## Architecture |
| 6 | + |
| 7 | +``` |
| 8 | +Documents → Distill (dedup + embed) → Memory Store |
| 9 | + ↓ |
| 10 | +User Query → Distill (recall) → Ranked Context → LLM → Response |
| 11 | + ↓ |
| 12 | + Distill (store answer) |
| 13 | +``` |
| 14 | + |
| 15 | +## Ingest documents |
| 16 | + |
| 17 | +Deduplicate and store document chunks: |
| 18 | + |
| 19 | +```python |
| 20 | +import requests |
| 21 | + |
| 22 | +DISTILL = "http://localhost:8080" |
| 23 | + |
| 24 | +def ingest(chunks: list[str], source: str): |
| 25 | + """Dedup chunks, then store unique ones as memories.""" |
| 26 | + # Deduplicate |
| 27 | + resp = requests.post(f"{DISTILL}/v1/dedupe", json={ |
| 28 | + "chunks": chunks, |
| 29 | + }) |
| 30 | + resp.raise_for_status() |
| 31 | + unique = resp.json()["unique_chunks"] |
| 32 | + |
| 33 | + # Store each unique chunk |
| 34 | + for chunk in unique: |
| 35 | + requests.post(f"{DISTILL}/v1/memory/store", json={ |
| 36 | + "content": chunk, |
| 37 | + "agent_id": "rag-pipeline", |
| 38 | + "tags": ["document", source], |
| 39 | + "auto_classify": True, |
| 40 | + }).raise_for_status() |
| 41 | + |
| 42 | + print(f"Stored {len(unique)}/{len(chunks)} chunks from {source}") |
| 43 | +``` |
| 44 | + |
| 45 | +## Query with context |
| 46 | + |
| 47 | +```python |
| 48 | +def query(question: str, llm_fn) -> str: |
| 49 | + """Recall relevant context and generate an answer.""" |
| 50 | + # Recall from memory |
| 51 | + resp = requests.post(f"{DISTILL}/v1/memory/recall", json={ |
| 52 | + "query": question, |
| 53 | + "agent_id": "rag-pipeline", |
| 54 | + "top_k": 10, |
| 55 | + "min_relevance": 0.3, |
| 56 | + "boost_tags": ["document"], |
| 57 | + }) |
| 58 | + resp.raise_for_status() |
| 59 | + memories = resp.json()["memories"] |
| 60 | + |
| 61 | + # Build prompt |
| 62 | + context = "\n---\n".join(m["content"] for m in memories) |
| 63 | + prompt = f"Context:\n{context}\n\nQuestion: {question}\nAnswer:" |
| 64 | + |
| 65 | + answer = llm_fn(prompt) |
| 66 | + |
| 67 | + # Store the Q&A pair for future recall |
| 68 | + requests.post(f"{DISTILL}/v1/memory/store", json={ |
| 69 | + "content": f"Q: {question}\nA: {answer}", |
| 70 | + "agent_id": "rag-pipeline", |
| 71 | + "tags": ["qa"], |
| 72 | + }).raise_for_status() |
| 73 | + |
| 74 | + return answer |
| 75 | +``` |
| 76 | + |
| 77 | +## Handle stale knowledge |
| 78 | + |
| 79 | +When documents are updated, supersede old memories: |
| 80 | + |
| 81 | +```python |
| 82 | +def update_document(old_memory_id: str, new_content: str): |
| 83 | + """Replace outdated memory with new version.""" |
| 84 | + # Store new version |
| 85 | + resp = requests.post(f"{DISTILL}/v1/memory/store", json={ |
| 86 | + "content": new_content, |
| 87 | + "agent_id": "rag-pipeline", |
| 88 | + "tags": ["document"], |
| 89 | + }) |
| 90 | + resp.raise_for_status() |
| 91 | + new_id = resp.json()["id"] |
| 92 | + |
| 93 | + # Supersede old version |
| 94 | + requests.post(f"{DISTILL}/v1/memory/supersede", json={ |
| 95 | + "id": old_memory_id, |
| 96 | + "new_id": new_id, |
| 97 | + }).raise_for_status() |
| 98 | +``` |
| 99 | + |
| 100 | +## Session-based context window |
| 101 | + |
| 102 | +For multi-turn conversations, use sessions to manage token budgets: |
| 103 | + |
| 104 | +```python |
| 105 | +def create_session(): |
| 106 | + resp = requests.post(f"{DISTILL}/v1/session/create", json={ |
| 107 | + "max_tokens": 4000, |
| 108 | + "strategy": "sliding_window", |
| 109 | + }) |
| 110 | + return resp.json()["session_id"] |
| 111 | + |
| 112 | +def add_to_session(session_id: str, role: str, content: str): |
| 113 | + requests.post(f"{DISTILL}/v1/session/push", json={ |
| 114 | + "session_id": session_id, |
| 115 | + "entries": [{"role": role, "content": content}], |
| 116 | + }).raise_for_status() |
| 117 | + |
| 118 | +def get_context(session_id: str) -> list[dict]: |
| 119 | + resp = requests.post(f"{DISTILL}/v1/session/context", json={ |
| 120 | + "session_id": session_id, |
| 121 | + }) |
| 122 | + return resp.json()["entries"] |
| 123 | +``` |
| 124 | + |
| 125 | +## Full example |
| 126 | + |
| 127 | +```python |
| 128 | +from openai import OpenAI |
| 129 | + |
| 130 | +client = OpenAI() |
| 131 | + |
| 132 | +def llm(prompt: str) -> str: |
| 133 | + resp = client.chat.completions.create( |
| 134 | + model="gpt-4o", |
| 135 | + messages=[{"role": "user", "content": prompt}], |
| 136 | + ) |
| 137 | + return resp.choices[0].message.content |
| 138 | + |
| 139 | +# Ingest |
| 140 | +chunks = [ |
| 141 | + "Distill deduplicates context before sending to LLMs.", |
| 142 | + "Memory entries decay over time based on access patterns.", |
| 143 | + "Sensitivity classification detects PII automatically.", |
| 144 | +] |
| 145 | +ingest(chunks, source="distill-docs") |
| 146 | + |
| 147 | +# Query |
| 148 | +answer = query("How does Distill handle sensitive data?", llm) |
| 149 | +print(answer) |
| 150 | +``` |
0 commit comments