The Problem
Most local LLM tools are black boxes: you ask a question and get an answer with no visibility into how the model got there. This project makes the reasoning process inspectable by streaming each step and rendering it as a knowledge graph where edge weights are semantic similarities between steps. It works entirely against a local Ollama instance, so nothing leaves the machine.
What This Does
Local_Knowledge_Graph is a Flask app that asks a local Llama chat model to reason through a question step by step, embeds each step using an Ollama embedding model, and draws the steps as a graph with cosine-similarity edges. The core logic lives in src/mpe_lkg/: app.py wires the HTTP routes and streaming, backends.py abstracts Ollama chat and embedding calls, store.py persists vectors in a database, and reasoning.py orchestrates the question-answering loop.
The front end is a single page (src/mpe_lkg/templates/index.html) with vendor JavaScript for Markdown rendering and graph visualization. A CLI (src/mpe_lkg/cli.py) provides mpe-lkg doctor for environment checks. The package is published on PyPI as mpe-lkg.
How It Is Wired
Execution starts at main in src/mpe_lkg/cli.py:43, which reaches 39 functions. The Flask app entry is run at src/mpe_lkg/app.py:269, which calls health and list_models — the shortest path to a network call is run -> health -> list_models -> session.get. The reasoning worker path is worker -> embed, which reaches the filesystem via out.parent.mkdir and hashes text with hashlib.sha256.
The most-connected modules form a cycle: src/mpe_lkg/app.py, __init__.py, and cli.py import each other, so changing any one of them risks breaking the others. backends.py is the hub — 32 functions, called from 11 files, and it owns the network calls, database access, and hashing. The top-called functions (normal_script at 43 call sites, flask_client at 31, embed at 25) are where changes have the widest blast radius.
How To Use It
pip install mpe-lkg
mpe-lkg
# open http://localhost:5100
You need Ollama running locally with a chat model (ollama pull llama3.2:3b) and optionally an embedding model (ollama pull nomic-embed-text). Configuration is via environment variables: OLLAMA_URL (default http://localhost:11434), LKG_CHAT_MODEL, LKG_EMBED_MODEL, LKG_HOST/LKG_PORT (default 127.0.0.1:5100). For source installs, the repo's Makefile and pyproject.toml support pip install -e . in a venv; python app.py also works as a shim.
Real-World Use
A support engineer debugging why a model misidentified a code bug runs mpe-lkg, asks "why did this function fail on empty input?", and sees the reasoning graph: each step is a node, edges show which steps the model considered similar, and weak edges reveal where the reasoning jumped tracks. The mpe-lkg doctor command automates the same environment check in CI scripts.
Code Health & Issues
Static analysis found 9 findings (4 high, 5 medium). High severity: an import cycle among src/mpe_lkg/__init__.py, app.py, and cli.py, and deep nesting (depth 6) in app.py, reasoning.py, and tests/test_stream.py. Medium: duplicated 6-line blocks across scripts/layer_sweep.py, scripts/measure.py, backends.py, and layers.py, plus high branching density in tests/test_stream.py (64 branches over 163 lines).
SDLC observations: no LICENSE file (default all-rights-reserved), no dependency lockfile, and GitHub Actions pinned to moving tags like pypa/gh-action-pypi-publish@release/v1 instead of commit SHAs. CI workflows lack least-privilege token permissions and timeout limits. Tests and CI are present across Linux, macOS, and Windows.
The Bottom Line
A well-structured tool for anyone who wants to see how a local LLM reasons, with a clean separation between app, backends, and storage. The import cycle and missing license are the main blockers for serious reuse. Best suited for developers debugging prompt chains or building local-first AI tooling.