On this tutorial, we implement IWE: an open-source, Rust-powered private information administration system that treats markdown notes as a navigable information graph. Since IWE is a CLI/LSP software designed for native editors. We construct a sensible developer information base from scratch, wire up wiki-links and markdown hyperlinks right into a directed graph, after which stroll by means of each main IWE operation: fuzzy search with discover, context-aware retrieval with retrieve, hierarchy show with tree, doc consolidation with squash, statistics with stats, and DOT graph export for visualization. We then transcend the CLI by integrating OpenAI to energy IWE-style AI transforms: summarization, hyperlink suggestion, and todo extraction, immediately in opposition to our information graph. Lastly, we assemble a full agentic RAG pipeline the place an AI agent navigates the graph utilizing function-calling instruments, performs multi-hop reasoning throughout interconnected paperwork, identifies information gaps, and even generates new notes that slot into the present construction.
import subprocess, sys
def _install(pkg):
subprocess.check_call([sys.executable, "-m", "pip", "install", "-q", pkg])
_install("openai")
_install("graphviz")
import re, json, textwrap, os, getpass
from collections import defaultdict
from dataclasses import dataclass, discipline
from typing import Non-obligatory
from datetime import datetime
strive:
from google.colab import userdata
OPENAI_API_KEY = userdata.get("OPENAI_API_KEY")
if not OPENAI_API_KEY:
increase ValueError
print("โ
Loaded OPENAI_API_KEY from Colab secrets and techniques.")
besides Exception:
OPENAI_API_KEY = getpass.getpass("๐ Enter your OpenAI API key: ")
print("โ
API key acquired.")
os.environ["OPENAI_API_KEY"] = OPENAI_API_KEY
from openai import OpenAI
consumer = OpenAI(api_key=OPENAI_API_KEY)
print("n" + "=" * 72)
print(" IWE Superior Tutorial โ Information Graph + AI Brokers")
print("=" * 72)
@dataclass
class Part:
degree: int
title: str
content material: str
youngsters: listing = discipline(default_factory=listing)
@dataclass
class Doc:
key: str
title: str
raw_content: str
sections: listing = discipline(default_factory=listing)
outgoing_links: listing = discipline(default_factory=listing)
tags: listing = discipline(default_factory=listing)
created: str = ""
modified: str = ""
class KnowledgeGraph:
def __init__(self):
self.paperwork: dict[str, Document] = {}
self.backlinks: dict[str, set] = defaultdict(set)
_WIKI_LINK = re.compile(r"[[([^]|]+)(?:|([^]]+))?]]")
_MD_LINK = re.compile(r"[([^]]+)](([^)]+))")
_HEADER = re.compile(r"^(#{1,6})s+(.+)", re.MULTILINE)
_TAG = re.compile(r"#([a-zA-Z][w/-]*)")
def _extract_links(self, textual content: str) -> listing[str]:
hyperlinks = []
for match in self._WIKI_LINK.finditer(textual content):
hyperlinks.append(match.group(1).strip())
for match in self._MD_LINK.finditer(textual content):
goal = match.group(2).strip()
if not goal.startswith("http"):
goal = goal.exchange(".md", "")
hyperlinks.append(goal)
return hyperlinks
def _parse_sections(self, textual content: str) -> listing[Section]:
sections = []
elements = self._HEADER.break up(textual content)
i = 1
whereas i < len(elements) - 1:
degree = len(elements[i])
title = elements[i + 1].strip()
physique = elements[i + 2] if i + 2 < len(elements) else ""
sections.append(Part(degree=degree, title=title, content material=physique.strip()))
i += 3
return sections
def _extract_tags(self, textual content: str) -> listing[str]:
tags = set()
for line in textual content.break up("n"):
if line.strip().startswith("#") and " " in line.strip():
stripped = re.sub(r"^#{1,6}s+.*", "", line)
for m in self._TAG.finditer(stripped):
tags.add(m.group(1))
else:
for m in self._TAG.finditer(line):
tags.add(m.group(1))
return sorted(tags)
def add_document(self, key: str, content material: str) -> Doc:
sections = self._parse_sections(content material)
title = sections[0].title if sections else key
hyperlinks = self._extract_links(content material)
tags = self._extract_tags(content material)
now = datetime.now().strftime("%Y-%m-%d %H:%M")
doc = Doc(
key=key, title=title, raw_content=content material,
sections=sections, outgoing_links=hyperlinks, tags=tags,
created=now, modified=now,
)
self.paperwork[key] = doc
for goal in hyperlinks:
self.backlinks[target].add(key)
return doc
def get(self, key: str) -> Non-obligatory[Document]:
return self.paperwork.get(key)
def discover(self, question: str, roots_only: bool = False, restrict: int = 10) -> listing[str]:
q = question.decrease()
scored = []
for key, doc in self.paperwork.objects():
rating = 0
if q in doc.title.decrease():
rating += 10
if q in doc.raw_content.decrease():
rating += doc.raw_content.decrease().depend(q)
if q in key.decrease():
rating += 5
for tag in doc.tags:
if q in tag.decrease():
rating += 3
if rating > 0:
scored.append((key, rating))
scored.kind(key=lambda x: -x[1])
outcomes = [k for k, _ in scored[:limit]]
if roots_only:
outcomes = [k for k in results if not self.backlinks.get(k)]
return outcomes
def retrieve(self, key: str, depth: int = 1, context: int = 1,
exclude: set = None) -> str:
exclude = exclude or set()
elements = []
if context > 0:
parents_of = listing(self.backlinks.get(key, set()) - exclude)
for p in parents_of[:context]:
pdoc = self.get(p)
if pdoc:
elements.append(f"[CONTEXT: {pdoc.title}]n{pdoc.raw_content[:300]}...n")
exclude.add(p)
doc = self.get(key)
if not doc:
return f"โ Doc '{key}' not discovered."
elements.append(doc.raw_content)
exclude.add(key)
if depth > 0:
for hyperlink in doc.outgoing_links:
if hyperlink not in exclude:
little one = self.get(hyperlink)
if little one:
elements.append(f"n---n[LINKED: {child.title}]n")
elements.append(
self.retrieve(hyperlink, depth=depth - 1,
context=0, exclude=exclude)
)
return "n".be a part of(elements)
def tree(self, key: str, indent: int = 0, _visited: set = None) -> str:
_visited = _visited if _visited just isn't None else set()
doc = self.get(key)
if not doc:
return ""
prefix = " " * indent + ("โโ " if indent else "")
if key in _visited:
return f"{prefix}{doc.title} ({key}) โฉ (round ref)"
_visited.add(key)
traces = [f"{prefix}{doc.title} ({key})"]
for hyperlink in doc.outgoing_links:
if self.get(hyperlink):
traces.append(self.tree(hyperlink, indent + 1, _visited))
return "n".be a part of(traces)
def squash(self, key: str, visited: set = None) -> str:
visited = visited or set()
doc = self.get(key)
if not doc or key in visited:
return ""
visited.add(key)
elements = [doc.raw_content]
for hyperlink in doc.outgoing_links:
child_content = self.squash(hyperlink, visited)
if child_content:
elements.append(f"n{'โ' * 40}n")
elements.append(child_content)
return "n".be a part of(elements)
def stats(self) -> dict:
total_words = sum(len(d.raw_content.break up()) for d in self.paperwork.values())
total_links = sum(len(d.outgoing_links) for d in self.paperwork.values())
orphans = [k for k in self.documents if not self.backlinks.get(k)
and not self.documents[k].outgoing_links]
all_tags = set()
for d in self.paperwork.values():
all_tags.replace(d.tags)
return {
"total_documents": len(self.paperwork),
"total_words": total_words,
"total_links": total_links,
"unique_tags": len(all_tags),
"tags": sorted(all_tags),
"orphan_notes": orphans,
"avg_words_per_doc": total_words // max(len(self.paperwork), 1),
}
def export_dot(self, highlight_key: str = None) -> str:
traces = ['digraph KnowledgeGraph {',
' rankdir=LR;',
' node [shape=box, style="rounded,filled", fillcolor="#f0f4ff", '
'fontname="Helvetica", fontsize=10];',
' edge [color="#666666", arrowsize=0.7];']
for key, doc in self.paperwork.objects():
label = doc.title[:30]
shade="#ffe4b5" if highlight_key == key else '#f0f4ff'
traces.append(f' "{key}" [label="{label}", fillcolor="{color}"];')
for key, doc in self.paperwork.objects():
for hyperlink in doc.outgoing_links:
if hyperlink in self.paperwork:
traces.append(f' "{key}" -> "{hyperlink}";')
traces.append("}")
return "n".be a part of(traces)
print("nโ
Part 1 full โ KnowledgeGraph class outlined.n")
We set up the required dependencies, securely settle for the OpenAI API key by means of Colab secrets and techniques or a password immediate, and initialize the OpenAI consumer. We then outline the three foundational knowledge courses, Part, Doc, and KnowledgeGraph, that mirror IWEโs arena-based graph structure the place each markdown file is a node and each hyperlink is a directed edge. We implement the total suite of IWE CLI operations on the KnowledgeGraph class, together with markdown parsing for wiki-links and headers, fuzzy search with discover, context-aware retrieval with retrieve, cycle-safe hierarchy show with tree, doc consolidation with squash, information base analytics with stats, and DOT graph export for Graphviz visualization.
kg = KnowledgeGraph()
kg.add_document("project-index", """# Internet App Mission
That is the **Map of Content material** for our internet utility mission.
## Structure
- [Authentication System](authentication)
- [Database Design](database-design)
- [API Design](api-design)
## Growth
- [Frontend Stack](frontend-stack)
- [Deployment Pipeline](deployment)
## Analysis
- [[caching-strategies]]
- [[performance-notes]]
""")
kg.add_document("authentication", """# Authentication System
Our app makes use of **JWT-based authentication** with refresh tokens.
## Stream
1. Person submits credentials to `/api/auth/login`
2. Server validates in opposition to [Database Design](database-design) consumer desk
3. Returns short-lived entry token (15 min) + refresh token (7 days)
4. Consumer shops refresh token in HTTP-only cookie
## Safety Concerns
- Passwords hashed with bcrypt (price issue 12)
- Charge limiting on login endpoint: 5 makes an attempt / minute
- Refresh token rotation on every use
- See [[caching-strategies]] for session caching
#safety #jwt #auth
""")
kg.add_document("database-design", """# Database Design
We use **PostgreSQL 16** with the next core tables.
## Customers Desk
```sql
CREATE TABLE customers (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
e mail VARCHAR(255) UNIQUE NOT NULL,
password VARCHAR(255) NOT NULL,
created_at TIMESTAMPTZ DEFAULT NOW()
);
```
## Periods Desk
```sql
CREATE TABLE periods (
id UUID PRIMARY KEY,
user_id UUID REFERENCES customers(id),
token_hash VARCHAR(255) NOT NULL,
expires_at TIMESTAMPTZ NOT NULL
);
```
## Indexing Technique
- B-tree on `customers.e mail` for login lookups
- B-tree on `periods.token_hash` for token validation
- See [[performance-notes]] for question optimization
#database #postgresql #schema
""")
kg.add_document("api-design", """# API Design
RESTful API following OpenAPI 3.0 specification.
## Endpoints
| Methodology | Path | Description |
|--------|------|-------------|
| POST | /api/auth/login | Authenticate consumer |
| POST | /api/auth/refresh | Refresh entry token |
| GET | /api/customers/me | Get present consumer profile |
| PUT | /api/customers/me | Replace profile |
## Error Dealing with
All errors return JSON with `{ "error": "code", "message": "..." }`.
Authentication endpoints documented in [Authentication System](authentication).
Information fashions align with [Database Design](database-design).
#api #relaxation #openapi
""")
kg.add_document("frontend-stack", """# Frontend Stack
## Expertise Selections
- **Framework**: React 19 with Server Parts
- **Styling**: Tailwind CSS v4
- **State Administration**: Zustand for consumer state
- **Information Fetching**: TanStack Question v5
## Auth Integration
The frontend consumes the [API Design](api-design) endpoints.
Entry tokens are saved in reminiscence (not localStorage) for safety.
Refresh dealt with transparently through Axios interceptors.
#frontend #react #tailwind
""")
kg.add_document("deployment", """# Deployment Pipeline
## Infrastructure
- **Container Runtime**: Docker with multi-stage builds
- **Orchestration**: Kubernetes on GKE
- **CI/CD**: GitHub Actions โ Google Artifact Registry โ GKE
## Pipeline Phases
1. Lint & type-check
2. Unit checks (Jest + pytest)
3. Construct Docker pictures
4. Push to Artifact Registry
5. Deploy to staging (auto)
6. Deploy to manufacturing (handbook approval)
## Monitoring
- Prometheus + Grafana for metrics
- Structured logging with correlation IDs
- See [[performance-notes]] for SLOs
#devops #kubernetes #cicd
""")
kg.add_document("caching-strategies", """# Caching Methods
## Utility-Stage Caching
- **Redis** for session storage and charge limiting
- Cache-aside sample for incessantly accessed consumer profiles
- TTL: 5 minutes for profiles, quarter-hour for config
## HTTP Caching
- `Cache-Management: personal, max-age=0` for authenticated endpoints
- `Cache-Management: public, max-age=3600` for static belongings
- ETag help for conditional requests
## Cache Invalidation
- Occasion-driven invalidation through pub/sub
- Versioned cache keys: `consumer:{id}:v{model}`
Associated: [Authentication System](authentication) makes use of Redis for refresh tokens.
#caching #redis #efficiency
""")
kg.add_document("performance-notes", """# Efficiency Notes
## Database Question Optimization
- Use `EXPLAIN ANALYZE` earlier than deploying new queries
- Connection pooling with PgBouncer (max 50 connections)
- Keep away from N+1 queries โ use JOINs or DataLoader sample
## SLO Targets
| Metric | Goal | Present |
|--------|--------|---------|
| p99 latency | < 200ms | 180ms |
| Availability | 99.9% | 99.95% |
| Error charge | < 0.1% | 0.05% |
## Load Testing
- k6 scripts in `/checks/load/`
- Baseline: 1000 RPS sustained
- Spike: 5000 RPS for 60 seconds
Associated to [Database Design](database-design) indexing and [[caching-strategies]].
#efficiency #slo #monitoring
""")
print("โ
Part 2 full โ 8 paperwork loaded into information graph.n")
print("โ" * 72)
print(" 3A ยท iwe discover โ Search the Information Graph")
print("โ" * 72)
outcomes = kg.discover("authentication")
print(f"n๐ discover('authentication'): {outcomes}")
outcomes = kg.discover("efficiency")
print(f"๐ discover('efficiency'): {outcomes}")
outcomes = kg.discover("cache", roots_only=True)
print(f"๐ discover('cache', roots_only=True): {outcomes}")
print("n" + "โ" * 72)
print(" 3B ยท iwe tree โ Doc Hierarchy")
print("โ" * 72)
print()
print(kg.tree("project-index"))
print("n" + "โ" * 72)
print(" 3C ยท iwe stats โ Information Base Statistics")
print("โ" * 72)
stats = kg.stats()
for ok, v in stats.objects():
print(f" {ok:>25s}: {v}")
print("n" + "โ" * 72)
print(" 3D ยท iwe retrieve โ Context-Conscious Retrieval")
print("โ" * 72)
print("n๐ Retrieving 'authentication' with depth=1, context=1:n")
retrieved = kg.retrieve("authentication", depth=1, context=1)
print(retrieved[:800] + "n... (truncated)")
print("n" + "โ" * 72)
print(" 3E ยท iwe squash โ Mix Paperwork")
print("โ" * 72)
squashed = kg.squash("project-index")
print(f"n๐ Squashed 'project-index': {len(squashed)} characters, "
f"{len(squashed.break up())} phrases")
print("n" + "โ" * 72)
print(" 3F ยท iwe export dot โ Graph Visualization")
print("โ" * 72)
dot_output = kg.export_dot(highlight_key="project-index")
print(f"n๐จ DOT output ({len(dot_output)} chars):n")
print(dot_output[:500] + "n...")
strive:
import graphviz
src = graphviz.Supply(dot_output)
src.render("knowledge_graph", format="png", cleanup=True)
print("nโ
Graph rendered to 'knowledge_graph.png'")
strive:
from IPython.show import Picture, show
show(Picture("knowledge_graph.png"))
besides ImportError:
print(" (Run in Colab/Jupyter to see the picture inline)")
besides Exception as e:
print(f" โ Graphviz rendering skipped: {e}")
print("nโ
Part 3 full โ all graph operations demonstrated.n")
We instantiate the KnowledgeGraph and populate it with eight interconnected markdown paperwork that type a sensible developer information base, spanning authentication, database design, API design, frontend, deployment, caching, and efficiency, all organized underneath a Map of Content material entry level, precisely as we’d construction notes in IWE. We then train each graph operation in opposition to this information base: we search with discover, show the total doc hierarchy with tree, pull statistics with stats, carry out context-aware retrieval that follows hyperlinks with retrieve, consolidate your complete graph right into a single doc with squash, and export the construction as a DOT graph. We render the graph visually utilizing Graphviz and show it inline, giving us a transparent image of how all our notes join to one another.
print("โ" * 72)
print(" 4 ยท AI-Powered Doc Transforms")
print("โ" * 72)
def ai_transform(textual content: str, motion: str, context: str = "",
mannequin: str = "gpt-4o-mini") -> str:
prompts = {
"rewrite": (
"Rewrite the next textual content to enhance readability and readability. "
"Preserve the markdown formatting. Return ONLY the rewritten textual content."
),
"summarize": (
"Summarize the next textual content in 2-3 concise bullet factors. "
"Give attention to the important thing choices and technical decisions."
),
"increase": (
"Increase the next textual content with extra technical element and examples. "
"Preserve the identical construction and add depth."
),
"extract_todos": (
"Extract all actionable objects from this textual content and format them as "
"a markdown todo listing. If there are not any actionable objects, counsel "
"related subsequent steps based mostly on the content material."
),
"generate_links": (
"Analyze the next be aware and counsel associated matters that ought to "
"be linked. Format as a markdown listing of wiki-links: [[topic-name]]. "
"Solely counsel matters which might be genuinely associated."
),
}
system_msg = prompts.get(motion, prompts["rewrite"])
if context:
system_msg += f"nnDocument context:n{context[:500]}"
messages = [
{"role": "system", "content": system_msg},
{"role": "user", "content": text},
]
response = consumer.chat.completions.create(
mannequin=mannequin, messages=messages, temperature=0.3, max_tokens=1000,
)
return response.decisions[0].message.content material.strip()
auth_doc = kg.get("authentication")
print("n๐ Rework: SUMMARIZE โ Authentication Systemn")
abstract = ai_transform(auth_doc.raw_content, "summarize")
print(abstract)
print("nn๐ Rework: GENERATE_LINKS โ Authentication Systemn")
hyperlinks = ai_transform(auth_doc.raw_content, "generate_links")
print(hyperlinks)
print("nnโ
Rework: EXTRACT_TODOS โ Efficiency Notesn")
perf_doc = kg.get("performance-notes")
todos = ai_transform(perf_doc.raw_content, "extract_todos")
print(todos)
print("nโ
Part 4 full โ AI transforms demonstrated.n")
We outline the ai_transform operate that mirrors IWEโs config.toml motion system, supporting 5 rework varieties: rewrite, summarize, increase, extract_todos, and generate_links, every backed by a tailor-made system immediate despatched to OpenAI. We run three dwell demonstrations in opposition to our information base: we summarize the Authentication System doc into concise bullet factors, analyze it for advised wiki hyperlinks to associated matters, and extract actionable to-do objects from the Efficiency Notes doc. We see how IWEโs AI motion sample, choosing a doc, selecting a rework, and making use of it in-place, interprets immediately right into a reusable Python operate that works with any be aware in our graph.
print("โ" * 72)
print(" 5 ยท Agentic RAG โ AI Navigates Your Information Graph")
print("โ" * 72)
AGENT_TOOLS = [
{
"type": "function",
"function": {
"name": "iwe_find",
"description": "Search the knowledge graph for documents matching a query. Returns a list of document keys.",
"parameters": {
"type": "object",
"properties": {
"query": {"type": "string", "description": "Search query"},
"roots_only": {"type": "boolean", "description": "Only return root/MOC documents", "default": False},
},
"required": ["query"],
},
},
},
{
"kind": "operate",
"operate": {
"title": "iwe_retrieve",
"description": "Retrieve a doc's content material with linked context. Use depth>0 to comply with outgoing hyperlinks, context>0 to incorporate dad or mum paperwork.",
"parameters": {
"kind": "object",
"properties": {
"key": {"kind": "string", "description": "Doc key to retrieve"},
"depth": {"kind": "integer", "description": "What number of ranges of kid hyperlinks to comply with (0-2)", "default": 1},
"context": {"kind": "integer", "description": "What number of ranges of dad or mum context (0-1)", "default": 0},
},
"required": ["key"],
},
},
},
{
"kind": "operate",
"operate": {
"title": "iwe_tree",
"description": "Present the doc hierarchy ranging from a given key.",
"parameters": {
"kind": "object",
"properties": {
"key": {"kind": "string", "description": "Root doc key"},
},
"required": ["key"],
},
},
},
{
"kind": "operate",
"operate": {
"title": "iwe_stats",
"description": "Get statistics about your complete information base.",
"parameters": {"kind": "object", "properties": {}},
},
},
]
def execute_tool(title: str, args: dict) -> str:
if title == "iwe_find":
outcomes = kg.discover(args["query"], roots_only=args.get("roots_only", False))
return json.dumps({"outcomes": outcomes})
elif title == "iwe_retrieve":
content material = kg.retrieve(
args["key"],
depth=args.get("depth", 1),
context=args.get("context", 0),
)
return content material[:3000]
elif title == "iwe_tree":
return kg.tree(args["key"])
elif title == "iwe_stats":
return json.dumps(kg.stats(), indent=2)
return "Unknown software"
def run_agent(query: str, max_turns: int = 6, mannequin: str = "gpt-4o-mini") -> str:
system_prompt = textwrap.dedent("""
You're an AI assistant with entry to a private information graph (IWE).
Use the offered instruments to navigate the graph and reply questions.
Workflow:
1. Use iwe_find to find related paperwork
2. Use iwe_retrieve to learn content material (set depth=1 to comply with hyperlinks)
3. Comply with relationships to construct complete understanding
4. Synthesize info from a number of paperwork
Be particular and cite which paperwork you discovered info in.
For those who can't discover sufficient info, say so clearly.
""")
messages = [
{"role": "system", "content": system_prompt},
{"role": "user", "content": question},
]
for flip in vary(max_turns):
response = consumer.chat.completions.create(
mannequin=mannequin, messages=messages, instruments=AGENT_TOOLS,
tool_choice="auto",
)
msg = response.decisions[0].message
if msg.tool_calls:
messages.append(msg)
for tc in msg.tool_calls:
fn_name = tc.operate.title
fn_args = json.masses(tc.operate.arguments)
print(f" ๐ง Agent calls: {fn_name}({fn_args})")
consequence = execute_tool(fn_name, fn_args)
messages.append({
"function": "software",
"tool_call_id": tc.id,
"content material": consequence,
})
else:
return msg.content material
return "Agent reached most turns with out finishing."
questions = [
"How does our authentication system work, and what database tables does it depend on?",
"What is our deployment pipeline, and what are the performance SLO targets?",
"Give me a high-level overview of the entire project architecture.",
]
for i, q in enumerate(questions, 1):
print(f"n{'โ' * 72}")
print(f" Query {i}: {q}")
print(f"{'โ' * 72}n")
reply = run_agent(q)
print(f"n๐ก Agent Reply:n{reply}n")
print("nโ
Part 5 full โ Agentic RAG demonstrated.n")
We construct the total agentic retrieval pipeline that embodies IWEโs โContext Bridgeโ idea:ย an AI agent that navigates our information graph utilizing OpenAI operate calling with 4 instruments: iwe_find for discovery, iwe_retrieve for context-aware content material fetching, iwe_tree for hierarchy exploration, and iwe_stats for information base analytics. We wire up the software executor that dispatches every operate name to our KnowledgeGraph occasion, and we implement the agent loop that iterates by means of search-retrieve-synthesize cycles till it assembles an entire reply. We then run three progressively advanced demo questions, asking about authentication dependencies, deployment and SLO targets, and a full mission structure overview, and watch the agent autonomously name instruments, comply with hyperlinks between paperwork, and produce complete solutions grounded in our notes.
print("โ" * 72)
print(" 6 ยท AI-Powered Information Graph Upkeep")
print("โ" * 72)
def analyze_knowledge_gaps(mannequin: str = "gpt-4o-mini") -> str:
stats_info = json.dumps(kg.stats(), indent=2)
titles = [f"- {d.title} ({k}): links to {d.outgoing_links}"
for k, d in kg.documents.items()]
graph_overview = "n".be a part of(titles)
response = consumer.chat.completions.create(
mannequin=mannequin,
messages=[
{"role": "system", "content": (
"You are a knowledge management consultant. Analyze this "
"knowledge graph and identify: (1) missing topics that should "
"exist, (2) documents that should be linked but aren't, "
"(3) areas that need more detail. Be specific and actionable."
)},
{"role": "user", "content": (
f"Knowledge base stats:n{stats_info}nn"
f"Document structure:n{graph_overview}"
)},
],
temperature=0.4, max_tokens=1000,
)
return response.decisions[0].message.content material.strip()
def generate_new_note(matter: str, related_keys: listing[str],
mannequin: str = "gpt-4o-mini") -> str:
context_parts = []
for key in related_keys[:3]:
doc = kg.get(key)
if doc:
context_parts.append(f"## {doc.title}n{doc.raw_content[:400]}")
context = "nn".be a part of(context_parts)
response = consumer.chat.completions.create(
mannequin=mannequin,
messages=[
{"role": "system", "content": (
"You are a technical writer. Generate a new markdown note "
"about the given topic. Use wiki-links [[like-this]] to "
"reference associated current paperwork. Embrace related "
"headers, code examples the place applicable, and hashtag tags."
)},
{"function": "consumer", "content material": (
f"Matter: {matter}nn"
f"Associated current notes for context:n{context}nn"
f"Out there paperwork to hyperlink to: {listing(kg.paperwork.keys())}"
)},
],
temperature=0.5, max_tokens=1200,
)
return response.decisions[0].message.content material.strip()
print("n๐ Analyzing information gaps...n")
gaps = analyze_knowledge_gaps()
print(gaps)
print("nn๐ Producing a brand new be aware: 'Error Dealing with Technique'...n")
new_note = generate_new_note(
"Error Dealing with Technique",
related_keys=["api-design", "authentication", "frontend-stack"],
)
print(new_note[:1000] + "n... (truncated)")
kg.add_document("error-handling", new_note)
print(f"nโ
Added 'error-handling' to information graph. "
f"Whole paperwork: {len(kg.paperwork)}")
dot_output = kg.export_dot(highlight_key="error-handling")
strive:
import graphviz
src = graphviz.Supply(dot_output)
src.render("knowledge_graph_v2", format="png", cleanup=True)
print("โ
Up to date graph rendered to 'knowledge_graph_v2.png'")
strive:
from IPython.show import Picture, show
show(Picture("knowledge_graph_v2.png"))
besides ImportError:
cross
besides Exception as e:
print(f" โ Graphviz rendering skipped: {e}")
print("nโ
Part 6 full โ AI-powered upkeep demonstrated.n")
print("โ" * 72)
print(" 7 ยท Multi-Hop Reasoning Throughout the Information Graph")
print("โ" * 72)
complex_question = (
"If we improve our visitors from 1000 RPS to 5000 RPS sustained, "
"what adjustments could be wanted throughout your complete stack โ from database "
"connection pooling, to caching, to authentication token dealing with, "
"to deployment infrastructure?"
)
print(f"n๐ง Advanced multi-hop query:n {complex_question}n")
reply = run_agent(complex_question, max_turns=8)
print(f"n๐ก Agent Reply:n{reply}")
print("nn" + "=" * 72)
print(" โ
TUTORIAL COMPLETE")
print("=" * 72)
print("""
You have explored all of the core ideas of IWE:
1. Information Graph โ Paperwork as nodes, hyperlinks as edges
2. Markdown Parsing โ Wiki-links, headers, tags
3. Maps of Content material โ Hierarchical organisation (MOC)
4. Graph Operations โ discover, retrieve, tree, squash, stats, export
5. AI Transforms โ Rewrite, summarize, increase, extract todos
6. Agentic Retrieval โ AI agent navigating your information graph
7. Graph Upkeep โ AI-powered hole evaluation and be aware era
8. Multi-Hop Reasoning โ Cross-document synthesis
To make use of IWE for actual (together with your editor):
โ https://github.com/iwe-org/iwe
โ https://iwe.md/quick-start
IWE helps VS Code, Neovim, Zed, and Helix through LSP.
""")
We use AI to research our information graph for structural gaps, figuring out lacking matters, unlinked paperwork, and areas that want extra depth. We then routinely generate a brand new โError Dealing with Techniqueโ be aware that references current paperwork through wiki hyperlinks and add it to the dwell graph. We re-render the up to date Graphviz visualization, highlighting the brand new node to indicate how the information base grows organically as AI and human contributions layer on high of one another. We shut with a fancy multi-hop reasoning problem, asking what adjustments are wanted throughout your complete stack if we scale from 1000 to 5000 RPS,ย the place the agent should traverse database, caching, authentication, and deployment paperwork to synthesize a cross-cutting reply that no single be aware might present alone.
In conclusion, we now have an entire, working implementation of IWEโs core concepts operating in Colab atmosphere. Now we have seen how structuring notes as a graph, fairly than treating them as flat recordsdata, unlocks highly effective capabilities: relationships turn out to be navigable paths, context flows naturally from dad or mum to little one paperwork, and AI brokers can uncover, traverse, and synthesize information precisely as we arrange it. Now we have constructed the total pipeline from markdown parsing and backlink indexing to graph traversal operations, AI-powered doc transforms, agentic retrieval with tool-calling, information hole evaluation, and multi-hop reasoning spanning your complete information base. All the pieces we construct right here maps on to IWEโs actual options: the discover, retrieve, tree, squash, and export instructions, the config.toml AI actions and the Context Bridge philosophy, which positions your private information graph as shared reminiscence between you and your AI brokers.
Tryย theย Full Notebook here.ย Additionally,ย be happy to comply with us onย Twitterย and donโt neglect to hitch ourย 120k+ ML SubRedditย and Subscribe toย our Newsletter. Wait! are you on telegram?ย now you can join us on telegram as well.
Michal Sutter is an information science skilled with a Grasp of Science in Information Science from the College of Padova. With a strong basis in statistical evaluation, machine studying, and knowledge engineering, Michal excels at reworking advanced datasets into actionable insights.
