File size: 13,936 Bytes
8ef2d83 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 |
//! # Python Bindings
//!
//! PyO3 bindings for ARMS-HAT, enabling Python integration with LLMs.
//!
//! ## Python API
//!
//! ```python
//! from arms_hat import HatIndex, SearchResult
//!
//! # Create index for OpenAI embeddings (1536 dims)
//! index = HatIndex.cosine(1536)
//!
//! # Add embeddings
//! id = index.add([0.1, 0.2, ...]) # Auto-generates ID
//! index.add_with_id("custom_id", [0.1, 0.2, ...]) # Custom ID
//!
//! # Query
//! results = index.near([0.1, 0.2, ...], k=10)
//! for result in results:
//! print(f"{result.id}: {result.score}")
//!
//! # Session management
//! index.new_session()
//! index.new_document()
//!
//! # Persistence
//! index.save("memory.hat")
//! loaded = HatIndex.load("memory.hat")
//! ```
use pyo3::prelude::*;
use pyo3::exceptions::{PyValueError, PyIOError};
use crate::core::{Id, Point};
use crate::adapters::index::{HatIndex as RustHatIndex, HatConfig, ConsolidationConfig, Consolidate};
use crate::ports::Near;
/// Python wrapper for search results
#[pyclass(name = "SearchResult")]
#[derive(Clone)]
pub struct PySearchResult {
/// The ID as a hex string
#[pyo3(get)]
pub id: String,
/// The similarity/distance score
#[pyo3(get)]
pub score: f32,
}
#[pymethods]
impl PySearchResult {
fn __repr__(&self) -> String {
format!("SearchResult(id='{}', score={:.4})", self.id, self.score)
}
fn __str__(&self) -> String {
format!("{}: {:.4}", self.id, self.score)
}
}
/// Python wrapper for HAT index configuration
#[pyclass(name = "HatConfig")]
#[derive(Clone)]
pub struct PyHatConfig {
inner: HatConfig,
}
#[pymethods]
impl PyHatConfig {
#[new]
fn new() -> Self {
Self { inner: HatConfig::default() }
}
/// Set beam width for search (default: 3)
fn with_beam_width(mut slf: PyRefMut<'_, Self>, width: usize) -> PyRefMut<'_, Self> {
slf.inner.beam_width = width;
slf
}
/// Set temporal weight (0.0 = pure semantic, 1.0 = pure temporal)
fn with_temporal_weight(mut slf: PyRefMut<'_, Self>, weight: f32) -> PyRefMut<'_, Self> {
slf.inner.temporal_weight = weight;
slf
}
/// Set propagation threshold for sparse updates
fn with_propagation_threshold(mut slf: PyRefMut<'_, Self>, threshold: f32) -> PyRefMut<'_, Self> {
slf.inner.propagation_threshold = threshold;
slf
}
fn __repr__(&self) -> String {
format!(
"HatConfig(beam_width={}, temporal_weight={:.2}, propagation_threshold={:.3})",
self.inner.beam_width, self.inner.temporal_weight, self.inner.propagation_threshold
)
}
}
/// Session summary for coarse-grained retrieval
#[pyclass(name = "SessionSummary")]
#[derive(Clone)]
pub struct PySessionSummary {
#[pyo3(get)]
pub id: String,
#[pyo3(get)]
pub score: f32,
#[pyo3(get)]
pub chunk_count: usize,
#[pyo3(get)]
pub timestamp_ms: u64,
}
#[pymethods]
impl PySessionSummary {
fn __repr__(&self) -> String {
format!(
"SessionSummary(id='{}', score={:.4}, chunks={})",
self.id, self.score, self.chunk_count
)
}
}
/// Document summary for mid-level retrieval
#[pyclass(name = "DocumentSummary")]
#[derive(Clone)]
pub struct PyDocumentSummary {
#[pyo3(get)]
pub id: String,
#[pyo3(get)]
pub score: f32,
#[pyo3(get)]
pub chunk_count: usize,
}
#[pymethods]
impl PyDocumentSummary {
fn __repr__(&self) -> String {
format!(
"DocumentSummary(id='{}', score={:.4}, chunks={})",
self.id, self.score, self.chunk_count
)
}
}
/// Index statistics
#[pyclass(name = "HatStats")]
#[derive(Clone)]
pub struct PyHatStats {
#[pyo3(get)]
pub global_count: usize,
#[pyo3(get)]
pub session_count: usize,
#[pyo3(get)]
pub document_count: usize,
#[pyo3(get)]
pub chunk_count: usize,
}
#[pymethods]
impl PyHatStats {
/// Total number of indexed points
#[getter]
fn total_points(&self) -> usize {
self.chunk_count
}
fn __repr__(&self) -> String {
format!(
"HatStats(points={}, sessions={}, documents={}, chunks={})",
self.chunk_count, self.session_count, self.document_count, self.chunk_count
)
}
}
/// Hierarchical Attention Tree Index
///
/// A semantic memory index optimized for conversation history retrieval.
/// Uses hierarchical structure (session -> document -> chunk) to enable
/// O(log n) queries while maintaining high recall.
#[pyclass(name = "HatIndex")]
pub struct PyHatIndex {
inner: RustHatIndex,
}
#[pymethods]
impl PyHatIndex {
/// Create a new HAT index with cosine similarity
///
/// Args:
/// dimensionality: Number of embedding dimensions (e.g., 1536 for OpenAI)
#[staticmethod]
fn cosine(dimensionality: usize) -> Self {
Self {
inner: RustHatIndex::cosine(dimensionality),
}
}
/// Create a new HAT index with custom configuration
///
/// Args:
/// dimensionality: Number of embedding dimensions
/// config: HatConfig instance
#[staticmethod]
fn with_config(dimensionality: usize, config: &PyHatConfig) -> Self {
Self {
inner: RustHatIndex::cosine(dimensionality).with_config(config.inner.clone()),
}
}
/// Add an embedding to the index
///
/// Args:
/// embedding: List of floats (must match dimensionality)
///
/// Returns:
/// str: The generated ID as a hex string
fn add(&mut self, embedding: Vec<f32>) -> PyResult<String> {
let point = Point::new(embedding);
let id = Id::now();
self.inner.add(id, &point)
.map_err(|e| PyValueError::new_err(format!("{}", e)))?;
Ok(format!("{}", id))
}
/// Add an embedding with a custom ID
///
/// Args:
/// id_hex: 32-character hex string for the ID
/// embedding: List of floats (must match dimensionality)
fn add_with_id(&mut self, id_hex: &str, embedding: Vec<f32>) -> PyResult<()> {
let id = parse_id_hex(id_hex)?;
let point = Point::new(embedding);
self.inner.add(id, &point)
.map_err(|e| PyValueError::new_err(format!("{}", e)))?;
Ok(())
}
/// Find k nearest neighbors to a query embedding
///
/// Args:
/// query: Query embedding (list of floats)
/// k: Number of results to return
///
/// Returns:
/// List[SearchResult]: Results sorted by relevance (best first)
fn near(&self, query: Vec<f32>, k: usize) -> PyResult<Vec<PySearchResult>> {
let point = Point::new(query);
let results = self.inner.near(&point, k)
.map_err(|e| PyValueError::new_err(format!("{}", e)))?;
Ok(results.into_iter().map(|r| PySearchResult {
id: format!("{}", r.id),
score: r.score,
}).collect())
}
/// Start a new session (conversation boundary)
///
/// Call this when starting a new conversation or context.
fn new_session(&mut self) {
self.inner.new_session();
}
/// Start a new document within the current session
///
/// Call this for logical groupings within a conversation
/// (e.g., topic change, user turn).
fn new_document(&mut self) {
self.inner.new_document();
}
/// Get index statistics
fn stats(&self) -> PyHatStats {
let s = self.inner.stats();
PyHatStats {
global_count: s.global_count,
session_count: s.session_count,
document_count: s.document_count,
chunk_count: s.chunk_count,
}
}
/// Get the number of indexed points
fn __len__(&self) -> usize {
self.inner.len()
}
/// Check if the index is empty
fn is_empty(&self) -> bool {
self.inner.is_empty()
}
/// Remove a point by ID
///
/// Args:
/// id_hex: 32-character hex string for the ID
fn remove(&mut self, id_hex: &str) -> PyResult<()> {
let id = parse_id_hex(id_hex)?;
self.inner.remove(id)
.map_err(|e| PyValueError::new_err(format!("{}", e)))?;
Ok(())
}
/// Find similar sessions (coarse-grained search)
///
/// Args:
/// query: Query embedding
/// k: Number of sessions to return
///
/// Returns:
/// List[SessionSummary]: Most relevant sessions
fn near_sessions(&self, query: Vec<f32>, k: usize) -> PyResult<Vec<PySessionSummary>> {
let point = Point::new(query);
let results = self.inner.near_sessions(&point, k)
.map_err(|e| PyValueError::new_err(format!("{}", e)))?;
Ok(results.into_iter().map(|s| PySessionSummary {
id: format!("{}", s.id),
score: s.score,
chunk_count: s.chunk_count,
timestamp_ms: s.timestamp,
}).collect())
}
/// Find similar documents within a session
///
/// Args:
/// session_id: Session ID (hex string)
/// query: Query embedding
/// k: Number of documents to return
///
/// Returns:
/// List[DocumentSummary]: Most relevant documents in the session
fn near_documents(&self, session_id: &str, query: Vec<f32>, k: usize) -> PyResult<Vec<PyDocumentSummary>> {
let sid = parse_id_hex(session_id)?;
let point = Point::new(query);
let results = self.inner.near_documents(sid, &point, k)
.map_err(|e| PyValueError::new_err(format!("{}", e)))?;
Ok(results.into_iter().map(|d| PyDocumentSummary {
id: format!("{}", d.id),
score: d.score,
chunk_count: d.chunk_count,
}).collect())
}
/// Find chunks within a specific document
///
/// Args:
/// doc_id: Document ID (hex string)
/// query: Query embedding
/// k: Number of results to return
///
/// Returns:
/// List[SearchResult]: Most relevant chunks in the document
fn near_in_document(&self, doc_id: &str, query: Vec<f32>, k: usize) -> PyResult<Vec<PySearchResult>> {
let did = parse_id_hex(doc_id)?;
let point = Point::new(query);
let results = self.inner.near_in_document(did, &point, k)
.map_err(|e| PyValueError::new_err(format!("{}", e)))?;
Ok(results.into_iter().map(|r| PySearchResult {
id: format!("{}", r.id),
score: r.score,
}).collect())
}
/// Run light consolidation (background maintenance)
///
/// This optimizes the index structure. Call periodically
/// (e.g., after every 100 inserts).
fn consolidate(&mut self) {
self.inner.consolidate(ConsolidationConfig::light());
}
/// Run full consolidation (more thorough optimization)
fn consolidate_full(&mut self) {
self.inner.consolidate(ConsolidationConfig::full());
}
/// Save the index to a file
///
/// Args:
/// path: File path to save to
fn save(&self, path: &str) -> PyResult<()> {
self.inner.save_to_file(std::path::Path::new(path))
.map_err(|e| PyIOError::new_err(format!("{}", e)))
}
/// Load an index from a file
///
/// Args:
/// path: File path to load from
///
/// Returns:
/// HatIndex: The loaded index
#[staticmethod]
fn load(path: &str) -> PyResult<Self> {
let inner = RustHatIndex::load_from_file(std::path::Path::new(path))
.map_err(|e| PyIOError::new_err(format!("{}", e)))?;
Ok(Self { inner })
}
/// Serialize the index to bytes
///
/// Returns:
/// bytes: Serialized index data
fn to_bytes<'py>(&self, py: Python<'py>) -> PyResult<Bound<'py, pyo3::types::PyBytes>> {
let data = self.inner.to_bytes()
.map_err(|e| PyIOError::new_err(format!("{}", e)))?;
Ok(pyo3::types::PyBytes::new_bound(py, &data))
}
/// Load an index from bytes
///
/// Args:
/// data: Serialized index data
///
/// Returns:
/// HatIndex: The loaded index
#[staticmethod]
fn from_bytes(data: &[u8]) -> PyResult<Self> {
let inner = RustHatIndex::from_bytes(data)
.map_err(|e| PyIOError::new_err(format!("{}", e)))?;
Ok(Self { inner })
}
fn __repr__(&self) -> String {
let stats = self.inner.stats();
format!(
"HatIndex(points={}, sessions={})",
stats.chunk_count, stats.session_count
)
}
}
/// Parse a hex string to an Id
fn parse_id_hex(hex: &str) -> PyResult<Id> {
if hex.len() != 32 {
return Err(PyValueError::new_err(
format!("ID must be 32 hex characters, got {}", hex.len())
));
}
let mut bytes = [0u8; 16];
for (i, chunk) in hex.as_bytes().chunks(2).enumerate() {
let high = hex_char_to_nibble(chunk[0])?;
let low = hex_char_to_nibble(chunk[1])?;
bytes[i] = (high << 4) | low;
}
Ok(Id::from_bytes(bytes))
}
fn hex_char_to_nibble(c: u8) -> PyResult<u8> {
match c {
b'0'..=b'9' => Ok(c - b'0'),
b'a'..=b'f' => Ok(c - b'a' + 10),
b'A'..=b'F' => Ok(c - b'A' + 10),
_ => Err(PyValueError::new_err(format!("Invalid hex character: {}", c as char))),
}
}
/// ARMS-HAT Python module
#[pymodule]
fn arms_hat(m: &Bound<'_, PyModule>) -> PyResult<()> {
m.add_class::<PyHatIndex>()?;
m.add_class::<PyHatConfig>()?;
m.add_class::<PySearchResult>()?;
m.add_class::<PySessionSummary>()?;
m.add_class::<PyDocumentSummary>()?;
m.add_class::<PyHatStats>()?;
// Add module docstring
m.add("__doc__", "ARMS-HAT: Hierarchical Attention Tree for AI memory retrieval")?;
m.add("__version__", env!("CARGO_PKG_VERSION"))?;
Ok(())
}
|