Skip to main content

Lucid Memory - Modular reasoning graph for LLMs

Project description

License PyPI version Project Status Advanced Reasoning Project


๐Ÿ“š Table of Contents


๐Ÿง  llm-lucid-memory

Lucid Memory is an open-source project evolving towards a reasoning graph for LLMs. It enables smaller models to comprehend and reason about large codebases and document sets by breaking them into understandable chunks, pre-processing the logic within each chunk, and connecting them structurally.

Imagine: Your LLM navigating a graph of interconnected concepts and code logic, not just processing isolated text snippets.


๐Ÿš€ Quickstart: Lucid Memory in 60 Seconds

After installation:

pip install llm-lucid-memory

You can immediately launch the full Lucid Memory interface with a single command:

lucid-memory

โœ… No manual setup needed
โœ… Configure your local LLM if needed (Ollama, LMStudio, or external API)
โœ… Load a file (.md, .py, .txt)
โœ… Watch Lucid Memory chunk, digest, and prepare structured memories dynamically!

The UI allows you to:

  • Load and chunk documents
  • Digest chunks using your LLM backend
  • View the digested MemoryNodes
  • Test basic chat with digested memories
  • Inspect raw JSON memory output

Note:
The current GUI is built with lightweight Tkinter for rapid prototyping.
A more advanced, modern GUI (likely based on a web framework) is planned in upcoming versions to better support graph navigation and memory visualization. ๐Ÿš€

Dependencies:

pip install -r requirements.txt
# (Requires: fastapi, uvicorn, requests, PyYAML)

Configuration:

  1. Copy or rename lucid_memory/proxy_config.example.json to lucid_memory/proxy_config.json.
  2. Edit lucid_memory/proxy_config.json with your local LLM API endpoint (e.g., Ollama, LMStudio v1 compatible) and the desired model name.
  3. (Optional) Edit lucid_memory/prompts.yaml to customize LLM instructions.

Run the GUI:

Start a UI to configure and start a Proxy Server for LLM interaction:

python -m lucid_memory.gui

From the GUI

  1. Verify configuration.
  2. Click "Load Context File" to select a file (.md, .py, .txt) for chunking and digestion (can take time).
  3. Click "Start Proxy Server".
  4. Use the Chat interface to ask questions related to your loaded context.

For development:

Run tests:

pytest 

(Note: Tests need updating for chunking/new digestion process)

Run a simple ingestion demo:

python -m examples.simple_digest_demo ```

๐Ÿ”Œ How to Use with Local LLMs (Ollama, LMStudio, etc.)

  • Start your local LLM server (ensure it exposes an OpenAI-compatible API endpoint).
  • Edit lucid_memory/proxy_config.json:
    • Set backend_url to your LLM's API endpoint (e.g., http://localhost:11434/v1/chat/completions).
    • Set model_name to the identifier your local LLM uses.
  • Launch the Lucid Memory GUI: python -m lucid_memory.gui
  • Use the GUI to "Load Context File" (this performs chunking & digestion).
  • Start the Proxy Server via the GUI button.
  • Chat using the GUI's chat interface. The proxy injects graph-retrieved context.

๐Ÿงช Example Usage

(Note: Examples are still in development. Current demos focus on basic chunking and digestion.)

(Conceptual Example with Digested Code)

Digested Node (Function Chunk):

  • ID: file_server_utils_py_func_start_secure_server_1
  • Summary: Starts a server socket, binds to a port, and wraps connections with TLS for secure communication.
  • Logical Steps:
    • Create TCP socket.
    • Set socket options (e.g., reuse address).
    • Bind socket to host and port.
    • Listen for incoming connections.
    • Load TLS certificate and key.
    • Create TLS context.
    • Enter main loop: accept connection.
    • Wrap accepted socket with TLS context.
    • Handle client request over TLS (delegate).
  • Key Variables: input: port (int), input: certfile (str), input: keyfile (str), internal: sock (socket), internal: context (SSLContext), internal: conn (socket)
  • Tags: server, socket, network, tls, ssl, security, listener

User Question: โ€œHow are secure connections handled when the server starts?โ€

Graph Retrieval:

  1. Keyword search finds file_server_utils_py_func_start_secure_server_1.
  2. (Future) Graph traversal might pull related nodes if referenced.

Proxy Prompt Context (Simplified):

Relevant Memory: --- Memory 1 (ID: file_server_utils_py_func_start_secure_server_1) --- Summary: Starts a server socket, binds to a port, and wraps connections with TLS for secure communication. Key Concepts/Logic:

  • Create TCP socket
  • Bind socket to host and port
  • Listen for incoming connections
  • Load TLS certificate and key
  • Create TLS context
  • Accept connection
  • Wrap accepted socket with TLS context
  • Handle client request over TLS

Tags: server, socket, network, tls, ssl, security

Based ONLY on the memories provided, answer: Question: How are secure connections handled when the server starts?

LLM Drafted Answer: The server handles secure connections by: loading a TLS certificate/key, creating a TLS context, accepting incoming connections, and then wrapping the connection socket with the TLS context before handling the client request.

โœจ Vision: The Reasoning Graph

We are building a system that allows LLMs to: - Chunk large documents and codebases into meaningful, context-aware units (sections, functions, classes). - Digest each chunk to extract not just summaries, but pre-processed understanding (like logical steps in code) using LLMs. - Connect these digested chunks (Memory Nodes) into a graph representing structural and potentially logical relationships. - Retrieve knowledge not just by keywords or semantic similarity, but by traversing the graph to gather relevant, linked context. - Reason over this structured, interconnected knowledge graph, enabling deeper comprehension and more accurate outputs, even with limited context windows.

Helping small models think big by building structured, navigable "mental models" of information. ๐Ÿš€


๐ŸŒŸ Why Lucid Memory?

  • Go Beyond Context Limits: Overcome the input size limitations of smaller LLMs.
  • Deep Understanding: Move past simple RAG retrieval towards comprehending structure and logic.
  • Structured Knowledge: Represent information as an interconnected graph, not just isolated vector chunks.
  • Code Comprehension: Specifically designed to pre-process and understand the logic within code functions/classes.
  • Efficient Retrieval: Graph traversal allows targeted retrieval of necessary linked context.
  • Modular & Extensible: Core components (chunker, digestor, graph, retriever) are designed for flexibility.

๐Ÿงฉ Core Concept: Graph-Based Reasoning

The Problem:

  • Small LLMs struggle with large, complex inputs (codebases, documentation).
  • Simple RAG often retrieves irrelevant or incomplete context due to lack of structural awareness.
  • LLMs need systems that mirror structured thinking and knowledge linking.

The Lucid Memory Solution:

  1. Structure-Aware Chunking: Break down input based on its type (Markdown headers, code functions/classes via Abstract Syntax Trees).
  2. Multi-faceted Digestion: For each chunk, use targeted LLM calls to extract:
    • Concise Summary
    • Key Concepts / Logical Steps (Chain-of-Thought for code)
    • Key Variables / Entities (for code)
    • Relevant Tags
  3. Memory Node Creation: Store digested chunk information in a MemoryNode.
  4. Graph Construction: Link MemoryNodes based on document structure (e.g., sequence, hierarchy) and potentially code calls (future).
  5. Graph Retrieval: Find relevant nodes via semantic/keyword search, then traverse the graph to gather connected, contextual information.
  6. Contextual Generation: Provide the structured, retrieved graph context to the LLM for informed reasoning and answer generation.

๐Ÿ”ฅ Enhanced Digestion Process (Chunking & Pre-processing)

Instead of digesting entire files, Lucid Memory first chunks the input intelligently based on its type. Each chunk is then digested using multiple targeted LLM calls:

Text Document Digestion (e.g., Markdown)

  1. Chunking: Split by semantic sections (e.g., using ## or ### headers).
  2. LLM Calls per Chunk:
    • Summary: Generate a 1-sentence summary of the section.
    • Key Concepts: Extract core ideas or topics discussed in the section (keywords/short phrases).
    • Tags: Generate relevant keyword tags for the section.
    • Follow-up Questions: Identify ambiguities or areas needing clarification within the section.
  3. Node Creation: Create a MemoryNode for the section, storing digested info and linking it (e.g., to the previous section).

Code Digestion (Python Example)

  1. Chunking: Use the ast module to parse the Python file. Create chunks for each function (FunctionDef) or class (ClassDef).
  2. LLM Calls per Code Chunk (Function/Method):
    • Summary: Generate a 1-sentence summary of the function's purpose (akin to a docstring).
    • Logical Steps (CoT): Extract the high-level conceptual steps the function performs (its internal logic).
    • Key Variables: Identify key input parameters, important local variables, and return values.
    • Tags: Generate relevant technical tags (e.g., api call, database query, validation).
  3. Node Creation: Create a MemoryNode for the function/class, storing the detailed digested info and linking it to its parent module/class node.

This chunk-based, multi-faceted digestion creates rich, structured nodes ready for graph construction and retrieval.


๐Ÿ”„ Core Reasoning Flow (Conceptual v0.3+)

flowchart TD

  %% Ingestion Phase (โœ… Done)
  subgraph Ingestion Phase
    A[Raw Knowledge - File or Text] --> B{Structure-Aware Chunker}
    B --> C(Semantic Chunk)
    style A fill:#d4fcd4,stroke:#333,stroke-width:2px,color:#222222
    style B fill:#d4fcd4,stroke:#333,stroke-width:2px,color:#222222
    style C fill:#d4fcd4,stroke:#333,stroke-width:2px,color:#222222
  end

  %% Digestion Phase (โœ… Done)
  subgraph Digestion Phase
    C --> D{Multi-Call LLM Digestor}
    style D fill:#d4fcd4,stroke:#333,stroke-width:2px,color:#222222
  end

  %% Memory Node Phase (โœ… Done)
  subgraph Memory Node
    D --> M1[Summary]
    D --> M2[Logic Paths]
    D --> M3[Variables Extracted]
    D --> M4[Tags / Topics]
    style M1 fill:#d4fcd4,stroke:#333,stroke-width:2px,color:#222222
    style M2 fill:#d4fcd4,stroke:#333,stroke-width:2px,color:#222222
    style M3 fill:#d4fcd4,stroke:#333,stroke-width:2px,color:#222222
    style M4 fill:#d4fcd4,stroke:#333,stroke-width:2px,color:#222222
  end

  %% Memory Storage (๐Ÿšง In Progress)
  subgraph Memory Storage
    M1 --> G((Memory + Relationships))
    M2 --> G
    M3 --> G
    M4 --> G
    style G fill:#fff4cc,stroke:#333,stroke-width:2px,color:#222222
  end

  %% Convert into Graph storage (๐Ÿ“ Todo)
  subgraph Graph Conversion
    G --> G2{Graph Storage}
    style G2 fill:#eeeeee,stroke:#333,stroke-width:2px,color:#222222
  end

  %% User Node
  subgraph User
    H(User Question)
  end

  %% Query Phase (๐Ÿ“ Todo)
  subgraph Query Phase
    H --> I{Graph Retriever 
    Search + Traversal}
    G2 --> I
    I --> J[Collect Relevant Context Nodes]
    style I fill:#eeeeee,stroke:#333,stroke-width:2px,color:#222222
    style J fill:#eeeeee,stroke:#333,stroke-width:2px,color:#222222
  end

  %% Reasoning Phase (๐Ÿ“ Todo)
  subgraph Reasoning Phase
    J --> K{Chain Of Draft Engine}
    H --> K
    K --> L[Logical Answer]
    style K fill:#eeeeee,stroke:#333,stroke-width:2px
    style L fill:#eeeeee,stroke:#333,stroke-width:2px
  end

%% Legend
  subgraph Legend
    Legend1(Done โœ…):::done
    Legend2(In Progress ๐Ÿšง):::inprogress
    Legend3(Todo ๐Ÿ“):::todo
    classDef done fill:#d4fcd4,stroke:#333,stroke-width:2px,color:#222;
    classDef inprogress fill:#fff4cc,stroke:#333,stroke-width:2px,color:#222;
    classDef todo fill:#eeeeee,stroke:#333,stroke-width:2px,color:#222;
  end

๐Ÿง  Advanced Research Directions

๐Ÿง  Future Optimization: Monte Carlo Chain Voting for Memory Refinement

  • Monte Carlo Tree Search (MCTS) for Reasoning: Explore MCTS not just for voting on final answers (as planned later) but potentially for navigating the memory graph during retrieval to find optimal reasoning paths. (inspired by: "More Reliable Code Generation via Monte Carlo Tree Search" from MIT (summarized here))
  • Graph Optimization: Use MCCV or other techniques during "sleep time" to analyze the memory graph, identify weak/redundant nodes or paths, and potentially refine summaries or links based on usage patterns.
  • Automated Relationship Extraction: Develop more sophisticated techniques (LLM-based analysis, advanced static analysis) to automatically infer complex relationships like code call graphs, data flow, or conceptual similarities between nodes.
  • Fine-tuning for Graph Reasoning: Create datasets and methods specifically for fine-tuning smaller LLMs to better utilize the structured graph context provided by Lucid Memory during generation.

๐Ÿš€ v0.3 Roadmap: Building the Graph Foundation

The focus for v0.3 is implementing the core chunking, digestion, and basic graph structure.

Feature Status Notes
Core: Structure-Aware Chunking โœ… In Progress Basic MD (headers) & Python (ast functions/methods) implemented.
Core: Implement Basic Node Linking ๐Ÿš€ Next Up! Store sequence_index, parent_identifier based on chunking.
Core: Update MemoryNode Fields ๐Ÿ“ Planned Add linking fields (sequence_index, parent_identifier).
Core: Update prompts.yaml (Code Focus) ๐Ÿ“ Planned Add/Refine prompts for logical_steps, key_variables (Code).
Core: Refactor Digestor (Code Focus) ๐Ÿ“ Planned Add logic to use code-specific prompts based on chunk type.
Core: Update Processor Logic ๐Ÿ“ Planned Pass linking info during node creation/storage.
Core: Add PyYAML Dependency โœ… Done Added to requirements.
Enhancement: Update Retriever (Basic Graph) ๐Ÿ“ Planned Add simple neighbour fetching based on stored links.
Enhancement: Update ProxyServer Prompting ๐Ÿ“ Planned Use logical_steps/key_variables from nodes in context.
Future: Parallel Chunk Digestion โœ… Done Using ThreadPoolExecutor in Processor.
Future: Advanced Relationship Extraction ๐Ÿ’ค Future Code call graphs, conceptual links.
ChainOfDraftEngine ๐Ÿ’ค Future Postponed - Requires solid graph retrieval first.
Monte Carlo Chain Voting ๐Ÿ’ค Future Postponed - Depends on ChainOfDraftEngine.
Stay tuned as we build the foundation for true graph-based reasoning! ๐Ÿง ๐Ÿ•ธ๏ธ

๐Ÿงช Tests

Tests are actively being updated to match the new v0.3 chunking, digestion, and memory graph architecture.

Basic tests for components like the Digestor and MemoryNode exist, but full integration tests are still in progress.

To run existing tests:

pytest

๐Ÿ“ฆ Project Structure

llm-lucid-memory/
โ”œโ”€โ”€ README.md # Project overview (This file)
โ”œโ”€โ”€ LICENSE # Apache 2.0 License
โ”œโ”€โ”€ requirements.txt # Project dependencies (fastapi, uvicorn, requests, PyYAML)
โ”œโ”€โ”€ setup.py # Optional pip packaging
โ”œโ”€โ”€ lucid_memory/ # Core library source code
โ”‚ โ”œโ”€โ”€ init.py
โ”‚ โ”œโ”€โ”€ prompts.yaml # LLM prompt templates
โ”‚ โ”œโ”€โ”€ proxy_config.json # Runtime configuration for LLM backend / proxy port
โ”‚ โ”œโ”€โ”€ chunker.py # NEW: Module for structure-aware chunking
โ”‚ โ”œโ”€โ”€ processor.py # NEW: Handles parallel chunk processing & digestion
โ”‚ โ”œโ”€โ”€ digestor.py # Digests text chunks using LLM calls (via prompts.yaml)
โ”‚ โ”œโ”€โ”€ memory_node.py # Definition of a single node in the memory graph
โ”‚ โ”œโ”€โ”€ memory_graph.py # Manages the collection of MemoryNodes
โ”‚ โ”œโ”€โ”€ retriever.py # Retrieves nodes (keyword -> graph traversal planned)
โ”‚ โ”œโ”€โ”€ proxy_server.py # FastAPI proxy intercepting chat, adding context
โ”‚ โ””โ”€โ”€ gui.py # Tkinter GUI application (Refactored)
โ”œโ”€โ”€ examples/ # Demo scripts (May need updates for v0.3)
โ”‚ โ”œโ”€โ”€ simple_digest_demo.py
โ”‚ โ”œโ”€โ”€ full_pipeline_demo.py
โ”‚ โ””โ”€โ”€ test_client.py
โ””โ”€โ”€ tests/ # Unit and integration tests (Need updates)
โ”œโ”€โ”€ test_digestor.py
โ”œโ”€โ”€ test_memory_graph.py
โ”œโ”€โ”€ test_memory_node.py
โ”œโ”€โ”€ test_retriever.py
โ””โ”€โ”€ (more planned for chunking, processor, etc.)

๐Ÿ“œ License

Apache 2.0 โ€” free for commercial and research use with attribution.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

llm_lucid_memory-0.2.3.tar.gz (33.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

llm_lucid_memory-0.2.3-py3-none-any.whl (35.8 kB view details)

Uploaded Python 3

File details

Details for the file llm_lucid_memory-0.2.3.tar.gz.

File metadata

  • Download URL: llm_lucid_memory-0.2.3.tar.gz
  • Upload date:
  • Size: 33.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for llm_lucid_memory-0.2.3.tar.gz
Algorithm Hash digest
SHA256 4cbecc273c9f38124a3b04cf55b9fb2ecf15c469d35e2faf58834be68dbb30e9
MD5 1831a7ecb0eb3196b76df708d16110f7
BLAKE2b-256 5505f96aa3e586aeda2a207088bd153848d51a2a4937cd2f459aeec079a5efb9

See more details on using hashes here.

Provenance

The following attestation bundles were made for llm_lucid_memory-0.2.3.tar.gz:

Publisher: publish.yml on benschneider/llm-lucid-memory

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file llm_lucid_memory-0.2.3-py3-none-any.whl.

File metadata

File hashes

Hashes for llm_lucid_memory-0.2.3-py3-none-any.whl
Algorithm Hash digest
SHA256 3d49f02ef1a2f7b006caed2642823c87f84cb5ab2d4160de80895c96684339fe
MD5 fda188b92c52fcca3f1c07a60f7b2097
BLAKE2b-256 8f1e575ccac3b128aaf724d32f96fcae6905ae14b8b7bada8fabf173b4dcf678

See more details on using hashes here.

Provenance

The following attestation bundles were made for llm_lucid_memory-0.2.3-py3-none-any.whl:

Publisher: publish.yml on benschneider/llm-lucid-memory

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page