Mini Context Graph

Mini Context Graph

Builds a persistent knowledge base from ingested documents: the agent writes wiki pages and extracts entities and relations into a graph linked back to source text.

Skill files14 files

references/

scripts/

scripts/tools/

SKILL.md197 lines
1
---
2
name: mini-context-graph
3
description: |
4
A persistent, compounding knowledge base combining Karpathy's LLM Wiki pattern
5
with a structured knowledge graph. Ingest documents once — the LLM writes wiki
6
pages, extracts entities/relations into the graph, and stores raw content for
7
evidence retrieval. Knowledge accumulates and cross-references; it is never
8
re-derived from scratch.
9
---
10
11
# Mini Context Graph Skill
12
13
## The Core Idea
14
15
Standard RAG re-discovers knowledge from scratch on every query. This skill is different:
16
17
1. **Wiki layer** — The LLM writes and maintains persistent markdown pages (summaries, entity pages, topic syntheses). Cross-references are already there. The wiki gets richer with every ingest.
18
2. **Graph layer** — Entities and relations are extracted once and stored as a navigable knowledge graph. BFS traversal answers structural queries without re-reading sources.
19
3. **Raw source layer** — Original documents are stored immutably with chunks. Provenance links tie every graph node and edge back to the exact text that supports it.
20
21
> The LLM writes; the Python tools handle all bookkeeping.
22
23
---
24
25
## Three Layers
26
27
| Layer | Where | What the LLM does | What Python does |
28
|-------|-------|-------------------|-----------------|
29
| **Raw Sources** | `data/documents.json` | Reads (never modifies) | Stores chunks + metadata |
30
| **Wiki** | `wiki/` (markdown) | Writes/updates pages | Manages index.md + log.md |
31
| **Graph** | `data/graph.json` | Extracts entities + relations | Persists, deduplicates, traverses |
32
33
---
34
35
## ⚡ Quick Start for Agents
36
37
A complete runnable version of this workflow is in `scripts/template_agent_workflow.py` — copy and adapt it.
38
39
```python
40
from scripts.contextgraph import ContextGraphSkill
41
from scripts.tools import wiki_store
42
43
skill = ContextGraphSkill()
44
45
# ===== INGEST WITH FULL RAG + WIKI =====
46
# 1. Read references/ingestion.md and references/ontology.md first
47
# 2. Extract entities and relations (LLM reasoning step)
48
entities = [
49
{"name": "memory leak", "type": "issue", "supporting_text": "memory leaks cause crashes"},
50
{"name": "system crash", "type": "issue", "supporting_text": "system crashes due to memory leaks"},
51
]
52
relations = [
53
{"source": "memory leak", "target": "system crash", "type": "causes",
54
"confidence": 1.0, "supporting_text": "System crashes due to memory leaks."},
55
]
56
57
result = skill.ingest_with_content(
58
doc_id="doc_001",
59
title="System Crash Analysis",
60
source="/docs/incident_report.pdf",
61
raw_content="System crashes due to memory leaks. Memory leaks occur when objects are not released.",
62
entities=entities,
63
relations=relations,
64
)
65
# result = {"doc_id": "doc_001", "chunk_count": 1, "nodes_added": 2, "edges_added": 1}
66
67
# 3. Write a wiki summary page for this document
68
wiki_store.write_page(
69
category="summary",
70
title="System Crash Analysis Summary",
71
content="""---
72
title: System Crash Analysis
73
source_document: doc_001
74
tags: [summary, incident]
75
---
76
77
# System Crash Analysis
78
79
**Source:** incident_report.pdf
80
81
## Key Claims
82
83
- [[memory-leak]] causes [[system-crash]] (confidence: 1.0)
84
85
## Entities
86
87
- [[memory-leak]] (issue)
88
- [[system-crash]] (issue)
89
""",
90
summary="Incident report: memory leaks cause system crashes.",
91
)
92
93
# ===== QUERY WITH EVIDENCE =====
94
result = skill.query_with_evidence("Why does the system crash?")
95
# Returns: {"query": ..., "subgraph": ..., "supporting_documents": [...], "evidence_chain": ...}
96
97
# ===== WIKI SEARCH (read wiki before answering) =====
98
pages = wiki_store.search_wiki("memory leak")
99
# Returns: [{slug, category, path, snippet}, ...]
100
```
101
102
---
103
104
## Operations
105
106
### Ingest
107
108
When a user provides a new document:
109
110
1. Read `references/ingestion.md` — entity/relation extraction rules.
111
2. Read `references/ontology.md` — type normalization rules.
112
3. Extract entities and relations using your LLM reasoning.
113
4. Call `skill.ingest_with_content(...)` — stores raw content + chunks + graph nodes + provenance.
114
5. **Write a wiki summary page** using `wiki_store.write_page(category="summary", ...)`.
115
6. **Update entity pages** — for each new/updated entity, write or update `wiki_store.write_page(category="entity", ...)`.
116
7. **Update topic pages** if the document touches an existing synthesis topic.
117
8. A single document ingest will typically touch 3–10 wiki pages.
118
119
### Query
120
121
When a user asks a question:
122
123
1. **Check the wiki first** — `wiki_store.search_wiki(query)` to find relevant pages. Read them.
124
2. If the wiki has a good answer, synthesize from wiki pages (fast path).
125
3. If deeper graph traversal is needed, call `skill.query_with_evidence(query)`.
126
4. Return the answer with evidence citations from `supporting_documents`.
127
5. If the answer is valuable, file it back as a new wiki topic page.
128
129
### Lint
130
131
Periodically health-check the wiki:
132
133
```python
134
from scripts.tools import wiki_store
135
issues = wiki_store.lint_wiki()
136
# Returns: {orphan_pages, missing_pages, broken_wikilinks, isolated_pages}
137
```
138
139
Ask the LLM to review and fix: broken links, orphan pages, stale claims, missing cross-references. See `references/lint.md` for full lint workflow.
140
141
---
142
143
## Ingestion Constraints
144
145
- ❌ Do NOT hallucinate entities not present in the text
146
- ❌ Do NOT add relations without explicit textual evidence
147
- ❌ Do NOT add edges with confidence < 0.6
148
- ✅ Provide `supporting_text` for every entity and relation — this enables provenance
149
- ✅ Write a wiki summary page for every ingested document
150
- ✅ Update existing entity pages when new information arrives
151
- ✅ Flag contradictions in wiki pages when new data conflicts with old claims
152
153
---
154
155
## Retrieval Constraints
156
157
- 🔒 Traversal depth MUST NOT exceed 2 (config: MAX_GRAPH_DEPTH)
158
- 🔒 Only edges with confidence ≥ 0.6 (config: MIN_CONFIDENCE)
159
- 🔒 Maximum 50 nodes returned (config: MAX_NODES)
160
- ❌ Do NOT fabricate nodes or edges not in the graph
161
162
---
163
164
## Full Python API Reference
165
166
| Method | Purpose | When to Use |
167
|--------|---------|-------------|
168
| `skill.ingest_with_content(doc_id, title, source, raw_content, entities, relations)` | Full RAG ingest: raw docs + graph + provenance | Every new document |
169
| `skill.add_node(name, node_type)` | Add single entity (no provenance) | Quick additions without a source doc |
170
| `skill.add_edge(source_name, target_name, relation, confidence)` | Add single relation | Quick additions without a source doc |
171
| `skill.query(query)` | Graph-only retrieval → subgraph | Structural queries |
172
| `skill.query_with_evidence(query)` | Graph + provenance → subgraph + source chunks | Queries requiring citations |
173
| `wiki_store.write_page(category, title, content, summary)` | Write/update a wiki page | After every ingest; after answering queries |
174
| `wiki_store.read_page(category, title)` | Read a wiki page | Before answering; for cross-referencing |
175
| `wiki_store.search_wiki(query)` | Keyword search across wiki | Fast path before graph traversal |
176
| `wiki_store.list_pages(category)` | List all wiki pages | Getting an overview |
177
| `wiki_store.get_log(last_n)` | Read recent operations | Understanding wiki history |
178
| `wiki_store.lint_wiki()` | Health check | Periodic maintenance |
179
| `documents_store.list_documents()` | List all ingested raw sources | Audit / provenance checking |
180
| `documents_store.search_chunks(query)` | Chunk-level search | Finding specific evidence |
181
182
---
183
184
## Design Philosophy
185
186
> "The wiki is a persistent, compounding artifact. The cross-references are already there. The synthesis already reflects everything you've read." — Karpathy
187
188
| Layer | What Happens | Who Owns It |
189
|-------|-----------|-------------|
190
| **LLM Reasoning** | Extraction, synthesis, writing wiki pages | Agent (.md guidance files) |
191
| **Wiki Persistence** | Index, log, file I/O | `wiki_store.py` |
192
| **Graph Persistence** | Dedup, index, BFS traverse | `graph_store.py`, `retrieval_engine.py` |
193
| **Raw Source Storage** | Immutable docs + chunks + provenance | `documents_store.py` |
194
195
The human curates sources and asks questions. The LLM writes the wiki, extracts the graph, and answers with citations. Python handles all bookkeeping.
196
197

© GitHub, Inc. · MIT · source · posted by @OpenSourceBuffGuy on promptparty.com

@OpenSourceBuffGuy

I find openly licensed prompts and post them here.

0 followers · 14,621 prompts

More by @OpenSourceBuffGuy

See all
Sea turtle carrying floating film studio island prompt image

Sea turtle carrying floating film studio island

Draws a giant sea turtle carrying a floating island film studio through a starry sky, where fairies polish rough film strips into golden film.

image-prompt, nano-banana, 3d-render, cartoon, fantasy-world, creatures, animals, whimsical, dreamy, cinematic, dramatic-lighting, vibrant-colors, bokeh, miniature, widescreen, night-scene, illustration, concept-art, surreal, storytelling, clay-3d

Desert buggy escapes drones past industrial outpost prompt image

Desert buggy escapes drones past industrial outpost

Builds a 15-second desert chase video where a racer in an armored buggy escapes drones past an industrial outpost, using five reference images with set jobs.

video-prompt, hailuo, action, multi-shot, timed-beats, camera-movement, reference-image, character-consistency, negative-prompt, cinematic, sci-fi, cars, landscape, aerial-view, tracking-shot, photorealistic, sound-effects, structured-output, long-form, widescreen

1 view
Cozy Pixel Art Farm Island Village prompt image

Cozy Pixel Art Farm Island Village

Draws a cozy 16-bit pixel-art farming village across six small islands joined by bridges, with exact counts for buildings, animals and crops.

image-prompt, gpt-image, pixel-art, isometric, game-assets, concept-art, maps, worldbuilding, cozy, vibrant-colors, ocean, landscape, wide-shot, widescreen, game-development, video-games, structured-output, whimsical, summer, high-angle, aerial-view, animals, negative-prompt, cityscape

1 view

Reviews

Loading reviews…

Discussion

Loading comments…