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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
use reqwest::multipart;
use serde::{Deserialize, Serialize};
use serde_json::json;
use std::collections::HashMap;
use std::error::Error;
pub use crate::api::{API, APIResponse, IndexResults, IndexResultsBatch};
/// Embeddings definition
pub struct Embeddings {
api: API
}
/// Embeddings implementation
impl Embeddings {
/// Creates an Embeddings instance.
///
pub fn new() -> Embeddings {
Embeddings {
api: API::new()
}
}
/// Creates an Embeddings instance.
///
/// # Arguments
/// * `url` - API url
pub fn with_url(url: &str) -> Embeddings {
Embeddings {
api: API::with_url(url)
}
}
/// Creates an Embeddings instance.
///
/// # Arguments
/// * `url` - API url
/// * `token` - API token
pub fn with_url_token(url: &str, token: &str) -> Embeddings {
Embeddings {
api: API::with_url_token(url, token)
}
}
/// Runs an Embeddings search. Returns Response. This method allows
/// callers to customize the serialization of the response.
///
/// # Arguments
/// * `query` - query text
/// * `limit` - maximum results
/// * `weights` - hybrid score weights, if applicable
/// * `index` - index name, if applicable
pub async fn query(&self, query: &str, limit: i32, weights: Option<f32>, index: Option<&str>) -> APIResponse {
// Query parameters
let mut params = vec![("query", query)];
let limitl = limit.to_string();
let weightsl = weights.unwrap_or(-1.0).to_string();
let indexl = index.unwrap_or("").to_string();
if limitl != "" {
params.push(("limit", &limitl));
}
if weightsl != "-1.0" {
params.push(("weights", &weightsl));
}
if indexl != "" {
params.push(("index", &indexl));
}
// Execute API call
Ok(self.api.get("search", ¶ms).await?)
}
/// Finds documents in the embeddings model most similar to the input query. Returns
/// a list of {id: value, score: value} sorted by highest score, where id is the
/// document id in the embeddings model.
///
/// # Arguments
/// * `query` - query text
/// * `limit` - maximum results
/// * `weights` - hybrid score weights, if applicable
/// * `index` - index name, if applicable
pub async fn search(&self, query: &str, limit: i32, weights: Option<f32>, index: Option<&str>) -> SearchResults {
// Execute API call and map JSON
Ok(self.query(query, limit, weights, index).await?.json().await?)
}
/// Finds documents in the embeddings model most similar to the input queries. Returns
/// a list of {id: value, score: value} sorted by highest score per query, where id is
/// the document id in the embeddings model.
///
/// # Arguments
/// * `queries` - queries text
/// * `limit` - maximum results
/// * `weights` - hybrid score weights, if applicable
/// * `index` - index name, if applicable
pub async fn batchsearch(&self, queries: &Vec<&str>, limit: i32, weights: Option<f32>, index: Option<&str>) -> SearchResultsBatch {
// Post parameters
let params = json!({
"queries": queries,
"limit": limit,
"weights": weights,
"index": index
});
// Execute API call
Ok(self.api.post("batchsearch", ¶ms).await?.json().await?)
}
/// Adds a batch of documents for indexing.
///
/// # Arguments
/// * `documents` - list of {id: value, text: value}
pub async fn add<T: Serialize>(&self, documents: &Vec<T>) -> APIResponse {
// Execute API call
Ok(self.api.post("add", &json!(documents)).await?)
}
/// Builds an embeddings index for previously batched documents.
pub async fn index(&self) -> APIResponse {
// Execute API call
Ok(self.api.get("index", &[]).await?)
}
/// Runs an embeddings upsert operation for previously batched documents.
pub async fn upsert(&self) -> APIResponse {
// Execute API call
Ok(self.api.get("upsert", &[]).await?)
}
/// Deletes from an embeddings index. Returns list of ids deleted.
///
/// # Arguments
/// * `ids` - list of ids to delete
pub async fn delete(&self, ids: &Vec<&str>) -> Ids {
// Execute API call
Ok(self.api.post("delete", &json!(ids)).await?.json().await?)
}
/// Recreates this embeddings index using config. This method only works if document content storage is enabled.
pub async fn reindex(&self, config: HashMap<&str, &str>, function: Option<&str>) -> APIResponse {
// Post parameters
let params = json!({
"config": config,
"function": function
});
// Execute API call
Ok(self.api.post("reindex", ¶ms).await?)
}
/// Total number of elements in this embeddings index.
pub async fn count(&self) -> Count {
Ok(self.api.get("count", &[]).await?.json().await?)
}
/// Computes the similarity between query and list of text. Returns a list of
/// {id: value, score: value} sorted by highest score, where id is the index
/// in texts.
///
/// # Arguments
/// * `query` - query text
/// * `texts` - list of text
pub async fn similarity(&self, query: &str, texts: &Vec<&str>) -> IndexResults {
// Post parameters
let params = json!({"query": query, "texts": texts});
// Execute API call
Ok(self.api.post("similarity", ¶ms).await?.json().await?)
}
/// Computes the similarity between list of queries and list of text. Returns a list
/// of {id: value, score: value} sorted by highest score per query, where id is the
/// index in texts.
///
/// # Arguments
/// * `queries` - queries text
/// * `texts` - list of text
pub async fn batchsimilarity(&self, queries: &Vec<&str>, texts: &Vec<&str>) -> IndexResultsBatch {
// Post parameters
let params = json!({"queries": queries, "texts": texts});
// Execute API call
Ok(self.api.post("batchsimilarity", ¶ms).await?.json().await?)
}
/// Transforms text into an embeddings array.
///
/// # Arguments
/// * `text` - input text
pub async fn transform(&self, text: &str) -> Embedding {
// Query parameters
let params = [("text", text)];
// Execute API call
Ok(self.api.get("transform", ¶ms).await?.json().await?)
}
/// Transforms list of text into embeddings arrays.
///
/// # Arguments
/// * `texts` - lists of text
pub async fn batchtransform(&self, texts: &str) -> EmbeddingBatch {
// Execute API call
Ok(self.api.post("batchtransform", &json!(texts)).await?.json().await?)
}
/// Adds a batch of binary objects for indexing.
///
/// # Arguments
/// * `data` - list of binary data
/// * `uid` - list of corresponding ids (optional)
/// * `field` - optional object field name
pub async fn addobject(&self, data: Vec<Vec<u8>>, uid: Option<Vec<&str>>, field: Option<&str>) -> APIResponse {
let mut form = multipart::Form::new();
// Add binary data
for (i, bytes) in data.into_iter().enumerate() {
let part = multipart::Part::bytes(bytes)
.file_name(format!("file{}", i))
.mime_str("application/octet-stream")?;
form = form.part("data", part);
}
// Add uid values
if let Some(ids) = uid {
for id in ids {
form = form.text("uid", id.to_string());
}
}
// Add field
if let Some(f) = field {
form = form.text("field", f.to_string());
}
// Execute API call
Ok(self.api.post_multipart("addobject", form).await?)
}
/// Adds a batch of images for indexing.
///
/// # Arguments
/// * `data` - list of image data
/// * `uid` - list of corresponding ids
/// * `field` - optional object field name
pub async fn addimage(&self, data: Vec<Vec<u8>>, uid: Option<Vec<&str>>, field: Option<&str>) -> APIResponse {
let mut form = multipart::Form::new();
// Add image data
for (i, bytes) in data.into_iter().enumerate() {
let part = multipart::Part::bytes(bytes)
.file_name(format!("image{}.jpg", i))
.mime_str("image/jpeg")?;
form = form.part("data", part);
}
// Add uid values
if let Some(ids) = uid {
for id in ids {
form = form.text("uid", id.to_string());
}
}
// Add field
if let Some(f) = field {
form = form.text("field", f.to_string());
}
// Execute API call
Ok(self.api.post_multipart("addimage", form).await?)
}
}
// Embeddings return types
pub type Embedding = Result<Vec<f32>, Box<dyn Error>>;
pub type EmbeddingBatch = Result<Vec<Vec<f32>>, Box<dyn Error>>;
pub type Ids = Result<Vec<String>, Box<dyn Error>>;
pub type Count = Result<usize, Box<dyn Error>>;
pub type SearchResults = Result<Vec<SearchResult>, Box<dyn Error>>;
pub type SearchResultsBatch = Result<Vec<Vec<SearchResult>>, Box<dyn Error>>;
/// Input document
#[derive(Debug, Serialize)]
pub struct Document {
pub id: String,
pub text: String
}
// Search result
#[derive(Debug, Deserialize)]
pub struct SearchResult {
pub id: String,
pub score: f32
}