Skip to main content

A library for context-aware conversational AI using graph databases

Project description

๐Ÿ” ctx-miner

๐Ÿš€ A Python library for context-aware conversational AI using graph databases. Built on top of Graphiti Core, ctx-miner provides an easy-to-use interface for storing, retrieving, and analyzing conversational context using graph-based knowledge representation.

โœจ Features

  • ๐Ÿ•ธ๏ธ Graph-based Context Storage: Store conversations as interconnected knowledge graphs
  • ๐Ÿ”Ž Semantic Search: Find relevant context using hybrid search (semantic + BM25)
  • ๐Ÿค– Multiple LLM Support: Configurable LLM providers (OpenAI, etc.)
  • ๐Ÿงฎ Flexible Embedding Models: Support for various embedding providers
  • ๐Ÿ—„๏ธ FalkorDB Integration: Redis-based graph database backend
  • โšก Async/Await Support: Built for high-performance async applications
  • ๐Ÿ”’ Type Safety: Full type hints and Pydantic models

๐Ÿ“ฆ Installation

๐Ÿ”ง Using uv (Recommended)

uv add ctx-miner

๐Ÿ Using pip

pip install ctx-miner

๐Ÿ’ป Development Installation

git clone https://github.com/hienhayho/ctx-miner.git
cd ctx-miner
uv sync

๐Ÿš€ Quick Start

1. ๐Ÿ” Environment Setup

Run falkor-db with docker:

docker run \
    -it -d \
    --restart always \
    -p 6379:6379 \
    -p 3000:3000 \
    --name falkordb \
    falkordb/falkordb:latest

Create a .env file with your configuration:

OPENAI_API_KEY=your_openai_api_key
FALKORDB_HOST=localhost
FALKORDB_PORT=6379
FALKORDB_USERNAME=
FALKORDB_PASSWORD=

2. ๐Ÿ’ก Quick Usage

import asyncio
from ctx_miner import CtxMiner
from ctx_miner.core.schemas import CtxMinerEpisode, CtxMinerMessage
from ctx_miner.utils.helpers import load_config

async def main():
    # Configure the library
    config = load_config(group_id="demo_conversation", auto_build_indices=True)
    
    # Initialize CtxMiner
    miner = CtxMiner(config=config)
    
    try:
        await miner.initialize()
        # Create a conversation episode
        episode = CtxMinerEpisode(
            messages=[
                CtxMinerMessage(role="user", content="Hello! I need help with my internet plan."),
                CtxMinerMessage(role="assistant", content="Hi! I'd be happy to help you with your internet plan. What specific questions do you have?"),
                CtxMinerMessage(role="user", content="What's the fastest speed you offer?"),
                CtxMinerMessage(role="assistant", content="Our fastest plan is Super200 with 200 Mbps download speed.")
            ]
        )
        
        # Add episode to the knowledge graph
        episode_uuid = await miner.add_episode(episode)
        print(f"Added episode: {episode_uuid}")
        
        # Search for relevant context
        results = await miner.search_context(
            query="internet speed plans",
            limit=5
        )
        
        print(f"Found {len(results)} relevant contexts:")
        for result in results:
            print(f"- {result['fact']}")
        
        # Get statistics
        stats = await miner.get_stats()
        print(f"Database contains {stats['episode_count']} episodes")
        
    finally:
        await miner.close()

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

โš™๏ธ Configuration

๐ŸŽ›๏ธ CtxMinerConfig

The main configuration object that combines all settings:

from ctx_miner.core.schemas import CtxMinerConfig, FalkorDBConfig, CtxMinerLLMConfig, EmbeddingConfig

config = CtxMinerConfig(
    falkordb_config=FalkorDBConfig(
        host="localhost",
        port=6379,
        database="my_app",
        username="",  # Optional
        password=""   # Optional
    ),
    llm_config=CtxMinerLLMConfig(
        provider="openai",
        model="gpt-4o-mini",
        temperature=0.0,
        max_tokens=8192
    ),
    embedding_config=EmbeddingConfig(
        provider="openai",
        model="text-embedding-3-small",
        dimensions=1536
    ),
    group_id="default",           # Logical grouping for conversations
    auto_build_indices=True       # Automatically create database indices
)

๐ŸŒ Environment Variables

Variable Description Default
OPENAI_API_KEY OpenAI API key Required
FALKORDB_HOST FalkorDB host localhost
FALKORDB_PORT FalkorDB port 6379
FALKORDB_USERNAME FalkorDB username (empty)
FALKORDB_PASSWORD FalkorDB password (empty)

๐Ÿ“š Core Concepts

๐Ÿ“ Episodes

Episodes are the fundamental units of conversation stored in the graph. Each episode contains a sequence of messages and gets processed to extract entities and relationships.

from ctx_miner.core.schemas import CtxMinerEpisode, CtxMinerMessage

episode = CtxMinerEpisode(
    messages=[
        CtxMinerMessage(role="user", content="What are your business hours?"),
        CtxMinerMessage(role="assistant", content="We're open Monday-Friday, 9 AM to 6 PM EST.")
    ]
)

๐Ÿ” Search and Retrieval

ctx-miner provides multiple search methods:

  1. Context Search: Find relevant facts and relationships
  2. Node Search: Search for specific entities
  3. Graph-based Reranking: Use graph distance for improved relevance
# Basic context search
results = await miner.search_context("business hours", limit=10)

# Search with center node reranking
results = await miner.search_context(
    query="customer support",
    center_node_uuid="some-node-uuid",
    limit=5
)

# Direct node search
nodes = await miner.search_nodes("customer", limit=5)

๐ŸŽฏ Advanced Usage

๐Ÿ“ฆ Batch Processing

# Add multiple episodes efficiently
episodes = [
    CtxMinerEpisode(messages=[...]),
    CtxMinerEpisode(messages=[...]),
    # ... more episodes
]

uuids = await miner.add_episodes(episodes)
print(f"Added {len(uuids)} episodes")

๐Ÿ—‚๏ธ Episode Management

# List episodes
episodes = await miner.list_episodes(limit=50, offset=0)

# Get specific episode
episode_data = await miner.get_episode(episode_uuid)

# Delete episode
success = await miner.delete_episode(episode_uuid)

# Clear all episodes in group
await miner.clear_all()

๐Ÿ”ง Custom Search Configuration

from graphiti_core.search.search_config_recipes import NODE_HYBRID_SEARCH_RRF

# Use custom search configuration
custom_config = NODE_HYBRID_SEARCH_RRF.model_copy(deep=True)
custom_config.limit = 20

results = await miner.search_nodes(
    query="customer preferences",
    search_config=custom_config
)

๐Ÿ“‹ Requirements

  • ๐Ÿ Python 3.8+
  • ๐Ÿ—„๏ธ FalkorDB instance (Redis with graph capabilities)
  • ๐Ÿ”‘ OpenAI API key (or other supported LLM provider)

๐Ÿ“š Dependencies

  • ๐Ÿ•ธ๏ธ graphiti-core[falkordb]: Graph-based knowledge management
  • ๐Ÿ“ loguru: Structured logging
  • โœ… pydantic: Data validation and serialization
  • ๐Ÿ“Š tqdm: Progress bars for batch operations
  • ๐Ÿ” python-dotenv: Environment variable management

๐Ÿ“„ License

[Add your license information here]

๐Ÿค Contributing

[Add contribution guidelines here]

๐Ÿ’ฌ Support

For issues and questions:

  • ๐Ÿ“ Check the examples directory
  • ๐Ÿ“– Review the documentation
  • ๐Ÿ› Open an issue on GitHub

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

ctx_miner-0.1.0.tar.gz (58.1 kB view details)

Uploaded Source

Built Distribution

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

ctx_miner-0.1.0-py3-none-any.whl (15.8 kB view details)

Uploaded Python 3

File details

Details for the file ctx_miner-0.1.0.tar.gz.

File metadata

  • Download URL: ctx_miner-0.1.0.tar.gz
  • Upload date:
  • Size: 58.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.6.10

File hashes

Hashes for ctx_miner-0.1.0.tar.gz
Algorithm Hash digest
SHA256 656be90e17a565d0faa372d87adac8783e9000cf2572363fc2dc3a8becf41ac3
MD5 86e983cc9f19e1155f4ccd0736e6ab95
BLAKE2b-256 1a677ee5e793abf15e00ad77e96ac6f11ef8760048e7cebc7deacca8ac8ee2b4

See more details on using hashes here.

File details

Details for the file ctx_miner-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: ctx_miner-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 15.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.6.10

File hashes

Hashes for ctx_miner-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 b31a4feabe1aa9035ea2a89fe650f7c2c5330b64152fa5fd3bd3f246793d7ff6
MD5 8ee1b978d03a72f4b6ecd28ca5f87a51
BLAKE2b-256 1ba70632482ed9ebf7f19645d96e98d28604b6b01f43ba96081773daec5a3de9

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