rig_core/vector_store/
mod.rs1use 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#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
32pub enum IndexStrategy {
33 #[default]
35 BruteForce,
36 LSH {
41 num_tables: usize,
44 num_hyperplanes: usize,
46 },
47}
48
49#[derive(Debug, thiserror::Error)]
51pub enum VectorStoreError {
52 #[error("Embedding error: {0}")]
54 EmbeddingError(#[from] ProviderError),
55
56 #[error("Json error: {0}")]
58 JsonError(#[from] serde_json::Error),
59
60 #[cfg(not(target_family = "wasm"))]
61 #[error("Datastore error: {0}")]
63 DatastoreError(#[from] Box<dyn std::error::Error + Send + Sync + 'static>),
64
65 #[error("Filter error: {0}")]
67 FilterError(#[from] FilterError),
68
69 #[cfg(target_family = "wasm")]
70 #[error("Datastore error: {0}")]
72 DatastoreError(#[from] Box<dyn std::error::Error + 'static>),
73
74 #[error("Missing Id: {0}")]
76 MissingIdError(String),
77
78 #[error("HTTP request error: {0}")]
86 Http(#[from] crate::http_client::Error),
87
88 #[error("External call to API returned an error. Error code: {0} Message: {1}")]
90 ExternalAPIError(StatusCode, String),
91
92 #[error("Requested {requested} samples, but this vector store returns at most {max}")]
94 SamplesOutOfRange {
95 requested: u64,
97 max: u64,
99 },
100}
101
102impl VectorStoreError {
103 #[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 #[cfg(target_family = "wasm")]
114 pub fn datastore(e: impl std::error::Error + 'static) -> Self {
115 Self::DatastoreError(Box::new(e))
116 }
117}
118
119pub 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
139pub trait InsertDocuments: WasmCompatSend + WasmCompatSync {
141 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
151pub trait VectorStoreIndex: WasmCompatSend + WasmCompatSync {
153 type Filter: SearchFilter + WasmCompatSend + WasmCompatSync;
155
156 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 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#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
173pub struct VectorSearchResult<T> {
174 pub score: f64,
176 pub id: String,
178 pub document: T,
180}
181
182#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
184pub struct VectorSearchIdResult {
185 pub score: f64,
187 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;