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
//! Vector store trait for storing and searching vector embeddings.
use BTreeSet;
use async_trait;
use crate;
use crateResult;
use crate;
use crateScope;
/// How a collection is created.
///
/// `filterable` declares the metadata keys a [`Filter`] may name. A backend stores each as
/// a column or field of its own where it needs one — LanceDB and Agent Retrieval do — and
/// [`RagPipeline`](crate::RagPipeline) refuses a filter naming any other key with
/// [`RagError::UnsupportedFilter`](crate::RagError::UnsupportedFilter) before any I/O.
/// `document_id`, `source_uri`, and the chunk ID are always filterable.
///
/// # Example
///
/// ```rust
/// use adk_rag::CollectionSpec;
///
/// let spec = CollectionSpec::new(768).with_filterable(["lang", "section"]);
/// assert_eq!(spec.dimensions, 768);
/// assert!(spec.filterable.contains("lang"));
/// ```
/// A similarity search within one scope.
///
/// # Example
///
/// ```rust
/// use adk_rag::{Field, Filter, VectorQuery};
///
/// let embedding = [0.1, 0.9];
/// let english = Filter::eq(Field::metadata("lang"), "en");
/// let query = VectorQuery::new(&embedding, 5).with_filter(&english);
/// assert_eq!(query.top_k, 5);
/// ```
/// A storage backend for vector embeddings with similarity search.
///
/// Implementations manage named collections of [`Chunk`]s. Every operation on stored chunks
/// names a [`Scope`], and a stored chunk is identified by its collection, its scope, and its
/// chunk ID together:
///
/// - `upsert` replaces a chunk with the same ID in the same scope and never touches another
/// scope's chunk with that ID.
/// - `search` considers only the scope's chunks, applying the scope inside the backend's
/// search before the top `k` are chosen, so a small scope beside a large one still gets
/// its nearest chunks.
/// - `delete`, `delete_where`, `count`, and `purge_scope` act on the scope's chunks only.
///
/// No operation reads, writes, or deletes across scopes, apart from `delete_collection`.
///
/// `search`, `count`, and `delete_where` take a [`Filter`], which the backend compiles into
/// its own query language with bound parameters or typed expressions and applies with the
/// scope, before the top `k` are chosen. A backend that cannot express a filter returns
/// [`RagError::UnsupportedFilter`](crate::RagError::UnsupportedFilter) before any I/O, and so
/// does every backend for a filter that fails [`Filter::validate`].
///
/// # Example
///
/// ```rust
/// use adk_rag::{Chunk, CollectionSpec, InMemoryVectorStore, Scope, VectorQuery, VectorStore};
///
/// # #[tokio::main]
/// # async fn main() -> adk_rag::Result<()> {
/// let store = InMemoryVectorStore::new();
/// store.create_collection("docs", &CollectionSpec::new(2)).await?;
///
/// let tenant_a = Scope::new("tenant-a")?;
/// let tenant_b = Scope::new("tenant-b")?;
/// let chunk = Chunk::new("doc_0", "doc", "tenant a's text").with_embedding(vec![1.0, 0.0]);
/// store.upsert("docs", &tenant_a, &[chunk]).await?;
///
/// let query = VectorQuery::new(&[1.0, 0.0], 5);
/// assert_eq!(store.search("docs", &tenant_a, &query).await?.len(), 1);
/// assert!(store.search("docs", &tenant_b, &query).await?.is_empty());
/// # Ok(())
/// # }
/// ```