Skip to main content

Graph-based AI memory system with Neo4j, recall scoring, and conflict analysis

Project description

Neur0 (nyron-ai)

Neur0 is a graph-based AI memory system designed for long-term personalization. It uses Neo4j to build isolated user subgraphs, resolves memory conflicts using semantic analysis, ranks retrieved memories using recall scoring, and keeps short-term context utilizing MongoDB.


Features

  • Graph-Based Memory: Build highly structured, isolated memory subgraphs for individual users.
  • Semantic Conflict Resolution: Automatically detect when new facts contradict existing memories and mark old facts as conflicted. -Recall Scoring: Rank memories dynamically based on a combination of semantic similarity, recency, and reinforcement (access frequency). -Short-Term Context: Manage short-term context and resolve pronouns/references using MongoDB. -OpenAI Integration: Cleanly extract facts and embeddings using OpenAI models. -Built-in API Server: Instantly run a FastAPI server using the CLI to add, search, and manage memories via HTTP requests.

Prerequisites

  • Python: >=3.9
  • Neo4j Database: A running instance (e.g. Neo4j Desktop, AuraDB, or Docker).
  • MongoDB (Optional): Required if you want to enable the short-term memory store.
  • OpenAI API Key: Required for fact extraction, query resolution, and embeddings.

Installation

You can install Neur0 from PyPI:

pip install nyron-ai

For local development and testing, clone the repository and run:

pip install -e .

Quickstart

1. Configure Environment Variables

Create a .env file in your project root with your database credentials and API keys:

OPENAI_API_KEY=your-openai-key
NEO4J_URI=bolt://localhost:7687
NEO4J_USERNAME=neo4j
NEO4J_PASSWORD=password

# Optional: MongoDB for short-term memory
MONGO_URI=mongodb://localhost:27017
MONGO_DB=neur0

2. Python Code Usage

Here is how to initialize the memory client, save a memory, and query it:

import asyncio
from neuro import (
    MemoryClient,
    Neo4jGraphStore,
    MongoStore,
    OpenAIProvider,
    OpenAIEmbeddingProvider,
)

async def main():
    # 1. Initialize Stores
    graph_store = Neo4jGraphStore(
        uri="bolt://localhost:7687",
        username="neo4j",
        password="password",
    )
    graph_store.initialize()

    # Optional MongoDB short-term store
    mongo_store = MongoStore(uri="mongodb://localhost:27017", db_name="neur0")
    await mongo_store.initialize()

    # 2. Setup AI Providers
    llm = OpenAIProvider(api_key="your-api-key", model="gpt-4o-mini")
    embedder = OpenAIEmbeddingProvider(api_key="your-api-key", model="text-embedding-3-small")

    # 3. Create Memory Client
    memory = MemoryClient(
        graph_store=graph_store,
        llm=llm,
        embedder=embedder,
        short_term_store=mongo_store,
    )

    # 4. Save a Memory
    user_id = "user_128r"
    result = await memory.add(
        user_id=user_id,
        message="I studied computer science at Cambridge University.",
    )
    print("Saved facts:", result["saved_count"])

    # 5. Search memories
    search_results = await memory.search(
        user_id=user_id,
        query="Where did I go to college?",
    )
    for res in search_results:
        fact = res["fact"]
        print(f"Fact: {fact.subject} {fact.predicate} {fact.object}")
        print(f"Recall Score: {res['recall_score']}")

    # Clean up connections
    graph_store.close()
    mongo_store.close()

if __name__ == "__main__":
    asyncio.run(main())

Running the API Server

Neur0 includes a built-in command line utility to start a FastAPI backend.

Simply run:

neuro-api

The API will start running at http://localhost:8000.

Available Endpoints

  • POST /add_memory: Add a new user message, extract facts, and handle conflicts.
    {
      "user_id": "user_128r",
      "message": "I study computer science at Cambridge."
    }
    
  • POST /search_memory: Search and retrieve relevant memories.
    {
      "user_id": "user_128r",
      "query": "Where do I study?",
      "limit": 5
    }
    
  • DELETE /delete_subgraph/{user_id}: Delete all graph nodes and memories for a specific user.

License

MIT License.

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

nyron_ai-0.1.3.tar.gz (25.6 kB view details)

Uploaded Source

Built Distribution

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

nyron_ai-0.1.3-py3-none-any.whl (23.1 kB view details)

Uploaded Python 3

File details

Details for the file nyron_ai-0.1.3.tar.gz.

File metadata

  • Download URL: nyron_ai-0.1.3.tar.gz
  • Upload date:
  • Size: 25.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.9

File hashes

Hashes for nyron_ai-0.1.3.tar.gz
Algorithm Hash digest
SHA256 0dc086234bbfa5eff73056058c60c509ababe2cfb2a5dbc8164561e75c0c4431
MD5 401008e39d547530d8b63330ed00d015
BLAKE2b-256 87bf369a24b41ebbf1d6e6726f559a5884fab378b7df0171604ed9ef0cdef6da

See more details on using hashes here.

File details

Details for the file nyron_ai-0.1.3-py3-none-any.whl.

File metadata

  • Download URL: nyron_ai-0.1.3-py3-none-any.whl
  • Upload date:
  • Size: 23.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.9

File hashes

Hashes for nyron_ai-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 3bf82f97f467979d9e2b2315838bdbe0adcebe2e074d0df8c507ba72e8589bc3
MD5 d693026437cdc79f74b846977d906776
BLAKE2b-256 b5e55daa38e7ca39bc2b11618ea1931e4e9013f37d91778c47791d5aaea8ca59

See more details on using hashes here.

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