neomemx 0.1.2

A high-performance memory library for AI agents with semantic search
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
use crate::error::{NeomemxError, Result};
use serde::de::DeserializeOwned;

/// Extract JSON from a string that might contain other text
pub fn extract_json<T: DeserializeOwned>(text: &str) -> Result<T> {
    if let Some(start) = text.find('{') {
        if let Some(end) = text.rfind('}') {
            let json_str = &text[start..=end];
            return serde_json::from_str(json_str)
                .map_err(|e| NeomemxError::LlmError(format!("Failed to parse JSON: {}", e)));
        }
    }
    serde_json::from_str(text)
        .map_err(|e| NeomemxError::LlmError(format!("Failed to parse JSON: {}", e)))
}