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:
- Context Search: Find relevant facts and relationships
- Node Search: Search for specific entities
- 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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
656be90e17a565d0faa372d87adac8783e9000cf2572363fc2dc3a8becf41ac3
|
|
| MD5 |
86e983cc9f19e1155f4ccd0736e6ab95
|
|
| BLAKE2b-256 |
1a677ee5e793abf15e00ad77e96ac6f11ef8760048e7cebc7deacca8ac8ee2b4
|
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b31a4feabe1aa9035ea2a89fe650f7c2c5330b64152fa5fd3bd3f246793d7ff6
|
|
| MD5 |
8ee1b978d03a72f4b6ecd28ca5f87a51
|
|
| BLAKE2b-256 |
1ba70632482ed9ebf7f19645d96e98d28604b6b01f43ba96081773daec5a3de9
|