mem0_rust/vector_stores/
traits.rs1use async_trait::async_trait;
4use crate::errors::VectorStoreError;
5use crate::models::{Filters, MemoryRecord, Payload, ScoredMemory};
6
7#[derive(Debug, Clone)]
9pub struct VectorSearchResult {
10 pub id: String,
12 pub score: f32,
14 pub payload: Payload,
16}
17
18#[async_trait]
20pub trait VectorStore: Send + Sync {
21 async fn insert(
23 &self,
24 id: &str,
25 embedding: Vec<f32>,
26 payload: Payload,
27 ) -> Result<(), VectorStoreError>;
28
29 async fn search(
31 &self,
32 embedding: &[f32],
33 limit: usize,
34 filters: Option<&Filters>,
35 ) -> Result<Vec<VectorSearchResult>, VectorStoreError>;
36
37 async fn get(&self, id: &str) -> Result<Option<VectorSearchResult>, VectorStoreError>;
39
40 async fn delete(&self, id: &str) -> Result<(), VectorStoreError>;
42
43 async fn update(
45 &self,
46 id: &str,
47 embedding: Option<Vec<f32>>,
48 payload: Payload,
49 ) -> Result<(), VectorStoreError>;
50
51 async fn list(
53 &self,
54 filters: Option<&Filters>,
55 limit: usize,
56 ) -> Result<Vec<VectorSearchResult>, VectorStoreError>;
57
58 async fn delete_all(&self, filters: Option<&Filters>) -> Result<usize, VectorStoreError>;
60
61 async fn collection_exists(&self) -> Result<bool, VectorStoreError>;
63
64 async fn create_collection(&self) -> Result<(), VectorStoreError>;
66}
67
68impl VectorSearchResult {
70 pub fn to_memory_record(&self) -> MemoryRecord {
72 MemoryRecord {
73 id: uuid::Uuid::parse_str(&self.id).unwrap_or_else(|_| uuid::Uuid::new_v4()),
74 content: self.payload.data.clone(),
75 metadata: self.payload.metadata.clone(),
76 user_id: self.payload.user_id.clone(),
77 agent_id: self.payload.agent_id.clone(),
78 run_id: self.payload.run_id.clone(),
79 hash: self.payload.hash.clone(),
80 created_at: self.payload.created_at,
81 updated_at: self.payload.created_at, }
83 }
84
85 pub fn to_scored_memory(&self) -> ScoredMemory {
87 ScoredMemory {
88 record: self.to_memory_record(),
89 score: self.score,
90 }
91 }
92}