Skip to main content

rig_core/vector_store/
mod.rs

1//! Vector-store insertion and similarity-search interfaces.
2//! [`VectorStoreIndex`] implementations also implement [`Tool`] when their
3//! filter is deserializable and uses JSON values.
4//!
5//! ```
6//! use rig_core::vector_store::in_memory_store::InMemoryVectorStore;
7//!
8//! let store = InMemoryVectorStore::<String>::from_documents([]);
9//! assert!(store.is_empty());
10//! ```
11
12use http::StatusCode;
13pub use request::VectorSearchRequest;
14use serde::{Deserialize, Serialize, de::DeserializeOwned};
15use serde_json::{Value, json};
16
17use crate::error::ProviderError;
18use crate::{
19    Embed,
20    embeddings::Embedding,
21    tool::{Tool, ToolContext},
22    vector_store::request::{FilterError, SearchFilter},
23    wasm_compat::{WasmCompatSend, WasmCompatSync},
24};
25
26pub mod in_memory_store;
27pub mod lsh;
28pub mod request;
29
30/// How the in-memory vector store picks the documents a search scores.
31#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
32pub enum IndexStrategy {
33    /// Scores every document: exact results.
34    #[default]
35    BruteForce,
36    /// Scores only the documents sharing an [`lsh::LSHIndex`] bucket with the
37    /// query, exactly. Faster on large stores, but a true neighbor in no
38    /// shared bucket is missed, so a search can return fewer or other results
39    /// than [`Self::BruteForce`].
40    LSH {
41        /// Hash tables; more find more true neighbors. `0` scores every
42        /// document, like [`Self::BruteForce`].
43        num_tables: usize,
44        /// Hyperplanes per table (at most 64); more make smaller buckets.
45        num_hyperplanes: usize,
46    },
47}
48
49/// Errors from vector store operations.
50#[derive(Debug, thiserror::Error)]
51pub enum VectorStoreError {
52    /// Embedding generation failed while preparing a vector query or insert.
53    #[error("Embedding error: {0}")]
54    EmbeddingError(#[from] ProviderError),
55
56    /// JSON serialization or deserialization failed.
57    #[error("Json error: {0}")]
58    JsonError(#[from] serde_json::Error),
59
60    #[cfg(not(target_family = "wasm"))]
61    /// Backend-specific datastore error.
62    #[error("Datastore error: {0}")]
63    DatastoreError(#[from] Box<dyn std::error::Error + Send + Sync + 'static>),
64
65    /// Filter construction or translation failed.
66    #[error("Filter error: {0}")]
67    FilterError(#[from] FilterError),
68
69    #[cfg(target_family = "wasm")]
70    /// Backend-specific datastore error.
71    #[error("Datastore error: {0}")]
72    DatastoreError(#[from] Box<dyn std::error::Error + 'static>),
73
74    /// A document was missing an ID required by the backend.
75    #[error("Missing Id: {0}")]
76    MissingIdError(String),
77
78    /// HTTP request failed for an external vector store service.
79    ///
80    /// Non-success responses arrive as the status-bearing
81    /// [`crate::http_client::Error`] variants, so callers can still inspect
82    /// the status code; response-less transport failures arrive as
83    /// [`crate::http_client::Error::Transient`] or
84    /// [`crate::http_client::Error::Permanent`].
85    #[error("HTTP request error: {0}")]
86    Http(#[from] crate::http_client::Error),
87
88    /// External vector store service returned an error response.
89    #[error("External call to API returned an error. Error code: {0} Message: {1}")]
90    ExternalAPIError(StatusCode, String),
91
92    /// The requested sample count exceeds what the backend can express.
93    #[error("Requested {requested} samples, but this vector store returns at most {max}")]
94    SamplesOutOfRange {
95        /// Sample count from the search request.
96        requested: u64,
97        /// Largest sample count the backend accepts.
98        max: u64,
99    },
100}
101
102impl VectorStoreError {
103    /// Wraps a backend error as [`VectorStoreError::DatastoreError`].
104    ///
105    /// Handles the wasm/non-wasm trait-bound split in one place; use as
106    /// `.map_err(VectorStoreError::datastore)`.
107    #[cfg(not(target_family = "wasm"))]
108    pub fn datastore(e: impl std::error::Error + Send + Sync + 'static) -> Self {
109        Self::DatastoreError(Box::new(e))
110    }
111
112    /// Wraps a backend error as [`VectorStoreError::DatastoreError`].
113    #[cfg(target_family = "wasm")]
114    pub fn datastore(e: impl std::error::Error + 'static) -> Self {
115        Self::DatastoreError(Box::new(e))
116    }
117}
118
119/// Serializes each document to JSON once, then applies `f` to every
120/// `(document, embedding)` pair, flattening the results into a single vector.
121///
122/// This is the shared shape of most [`InsertDocuments`] implementations:
123/// build one backend record per embedding, carrying the owning document's
124/// serialized form.
125pub fn flatten_embedded<Doc: Serialize, R>(
126    documents: Vec<(Doc, Vec<Embedding>)>,
127    mut f: impl FnMut(&Value, Embedding) -> Result<R, VectorStoreError>,
128) -> Result<Vec<R>, VectorStoreError> {
129    let mut records = Vec::new();
130    for (document, embeddings) in documents {
131        let json_document = serde_json::to_value(&document)?;
132        for embedding in embeddings {
133            records.push(f(&json_document, embedding)?);
134        }
135    }
136    Ok(records)
137}
138
139/// Trait for inserting documents and embeddings into a vector store.
140pub trait InsertDocuments: WasmCompatSend + WasmCompatSync {
141    /// Insert precomputed embeddings for each document.
142    ///
143    /// Callers must supply at least one embedding per document. Empty-list
144    /// behavior is backend-dependent and is not uniformly validated.
145    fn insert_documents<Doc: Serialize + Embed + WasmCompatSend>(
146        &self,
147        documents: Vec<(Doc, Vec<Embedding>)>,
148    ) -> impl std::future::Future<Output = Result<(), VectorStoreError>> + WasmCompatSend;
149}
150
151/// Trait for querying a vector store by similarity.
152pub trait VectorStoreIndex: WasmCompatSend + WasmCompatSync {
153    /// The filter type for this backend.
154    type Filter: SearchFilter + WasmCompatSend + WasmCompatSync;
155
156    /// Returns the top N most similar documents, most similar first.
157    fn top_n<T: DeserializeOwned + WasmCompatSend>(
158        &self,
159        req: VectorSearchRequest<Self::Filter>,
160    ) -> impl std::future::Future<Output = Result<Vec<VectorSearchResult<T>>, VectorStoreError>>
161    + WasmCompatSend;
162
163    /// Returns the top N most similar document IDs, most similar first.
164    fn top_n_ids(
165        &self,
166        req: VectorSearchRequest<Self::Filter>,
167    ) -> impl std::future::Future<Output = Result<Vec<VectorSearchIdResult>, VectorStoreError>>
168    + WasmCompatSend;
169}
170
171/// One document returned by [`VectorStoreIndex::top_n`].
172#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
173pub struct VectorSearchResult<T> {
174    /// Backend similarity score. Its scale and direction follow the backend's metric.
175    pub score: f64,
176    /// Backend document ID.
177    pub id: String,
178    /// The stored document, deserialized as `T`.
179    pub document: T,
180}
181
182/// One document ID returned by [`VectorStoreIndex::top_n_ids`].
183#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
184pub struct VectorSearchIdResult {
185    /// Backend similarity score. Its scale and direction follow the backend's metric.
186    pub score: f64,
187    /// Backend document ID.
188    pub id: String,
189}
190
191impl<T, F> Tool for T
192where
193    F: SearchFilter<Value = serde_json::Value>
194        + WasmCompatSend
195        + WasmCompatSync
196        + serde::de::DeserializeOwned,
197    T: VectorStoreIndex<Filter = F>,
198{
199    const NAME: &'static str = "search_vector_store";
200    type Error = VectorStoreError;
201    type Args = VectorSearchRequest<F>;
202    type Output = Vec<VectorSearchResult<Value>>;
203
204    fn description(&self) -> String {
205        "Retrieves the most relevant documents from a vector store based on a query.".to_string()
206    }
207
208    fn parameters(&self) -> serde_json::Value {
209        json!({
210            "type": "object",
211            "properties": {
212                "query": {
213                    "type": "string",
214                    "description": "The query string to search for relevant documents in the vector store."
215                },
216                "samples": {
217                    "type": "integer",
218                    "description": "The maximum number of samples / documents to retrieve.",
219                    "default": 5,
220                    "minimum": 1
221                },
222                "threshold": {
223                    "type": "number",
224                    "description": "Similarity search threshold. If present, any result with a distance less than this may be omitted from the final result."
225                }
226            },
227            "required": ["query", "samples"]
228        })
229    }
230
231    async fn call(
232        &self,
233        _: &mut ToolContext,
234        args: Self::Args,
235    ) -> Result<Self::Output, Self::Error> {
236        self.top_n(args).await
237    }
238}
239
240#[cfg(test)]
241mod tests;