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
//! [ChromaDB](https://www.trychroma.com/) client library for Rust.
//!
//! The library provides 2 modules to interact with the ChromaDB server via the API:
//! * `client` - To interface with the ChromaDB server.
//! * `collection` - To interface with an associated ChromaDB collection.
//!
//! ### Instantiating [ChromaClient](crate::ChromaClient)
//! ```
//! use chromadb::client::{ChromaAuthMethod, ChromaClient, ChromaClientOptions, ChromaTokenHeader};
//! use chromadb::collection::{ChromaCollection, GetResult, GetOptions};
//! use serde_json::json;
//!
//!# async fn doc_client_demo() -> anyhow::Result<()> {
//! // With default ChromaClientOptions
//! // Defaults to http://localhost:8000
//! let client: ChromaClient = ChromaClient::new(Default::default()).await.unwrap();
//!
//! // With custom ChromaClientOptions
//! let auth = ChromaAuthMethod::TokenAuth {
//! token: "<TOKEN>".to_string(),
//! header: ChromaTokenHeader::Authorization
//! };
//! let client: ChromaClient = ChromaClient::new(ChromaClientOptions {
//! url: Some("<CHROMADB_URL>".to_string()),
//! database: "<DATABASE>".to_string(),
//! auth
//! }).await.unwrap();
//!
//! # Ok(())
//! # }
//! ```
//! Now that a client is instantiated, we can interface with the ChromaDB server and execute queries.
//!
//! ### Collection Queries
//!
//! ```
//!# use chromadb::ChromaClient;
//!# use chromadb::collection::{ChromaCollection, GetResult, CollectionEntries, GetOptions};
//!# use serde_json::json;
//!# async fn doc_client_create_collection(client: &ChromaClient) -> anyhow::Result<()> {
//! // Get or create a collection with the given name and no metadata.
//! let collection: ChromaCollection = client.get_or_create_collection("my_collection", None).await?;
//!
//! // Get the UUID of the collection
//! let collection_uuid = collection.id();
//! println!("Collection UUID: {}", collection_uuid);
//!
//! // Upsert some embeddings with documents and no metadata.
//! let collection_entries = CollectionEntries {
//! ids: vec!["demo-id-1", "demo-id-2"],
//! embeddings: Some(vec![vec![0.0_f32; 768], vec![0.0_f32; 768]]),
//! metadatas: None,
//! documents: Some(vec![
//! "Some document about 9 octopus recipies",
//! "Some other document about DCEU Superman Vs CW Superman"
//! ])
//! };
//!
//! let result = collection.upsert(collection_entries, None).await?;
//!
//! // Create a filter object to filter by document content.
//! let where_document = json!({
//! "$contains": "Superman"
//! });
//!
//! // Get embeddings from a collection with filters and limit set to 1.
//! // An empty IDs vec will return all embeddings.
//!
//! let get_query = GetOptions {
//! ids: vec![],
//! where_metadata: None,
//! limit: Some(1),
//! offset: None,
//! where_document: Some(where_document),
//! include: Some(vec!["documents".into(),"embeddings".into()])
//! };
//!
//! let get_result: GetResult = collection.get(get_query).await?;
//! println!("Get result: {:?}", get_result);
//!# Ok(())
//!# }
//! ```
//!Find more information about on the available filters and options in the [get()](crate::ChromaCollection::get) documentation.
//!
//!
//! ### Perform a similarity search.
//! ```
//!# use chromadb::collection::{ChromaCollection, QueryResult, QueryOptions};
//!# use serde_json::json;
//!# async fn doc_query_collection(collection: &ChromaCollection) -> anyhow::Result<()> {
//! //Instantiate QueryOptions to perform a similarity search on the collection
//! //Alternatively, an embedding_function can also be provided with query_texts to perform the search
//! let query = QueryOptions {
//! query_texts: None,
//! query_embeddings: Some(vec![vec![0.0_f32; 768], vec![0.0_f32; 768]]),
//! where_metadata: None,
//! where_document: None,
//! n_results: Some(5),
//! include: None,
//! };
//!
//! let query_result: QueryResult = collection.query(query, None).await?;
//! println!("Query result: {:?}", query_result);
//!# Ok(())
//!# }
//! ```
//!
//! ### Support for Embedding providers
//! This crate has built-in support for OpenAI and SBERT embeddings.
//!
//! To use [OpenAI](https://platform.openai.com/docs/guides/embeddings) embeddings, enable the `openai` feature in your Cargo.toml.
//!
//! ```ignore
//!# use chromadb::ChromaClient;
//!# use chromadb::collection::{ChromaCollection, GetResult, CollectionEntries, GetOptions};
//!# use chromadb::embeddings::openai::OpenAIEmbeddings;
//!# use serde_json::json;
//!# async fn doc_client_create_collection(client: &ChromaClient) -> anyhow::Result<()> {
//! let collection: ChromaCollection = client.get_or_create_collection("openai_collection",
//! None).await?;
//!
//! let collection_entries = CollectionEntries {
//! ids: vec!["demo-id-1", "demo-id-2"],
//! embeddings: None,
//! metadatas: None,
//! documents: Some(vec![
//! "Some document about 9 octopus recipies",
//! "Some other document about DCEU Superman Vs CW Superman"])
//! };
//!
//! // Use OpenAI embeddings
//! let openai_embeddings = OpenAIEmbeddings::new(Default::default());
//! collection.upsert(collection_entries, Some(Box::new(openai_embeddings))).await?;
//! Ok(())
//!# }
//! ```
//!
//! To use [SBERT](https://docs.rs/crate/rust-bert/latest) embeddings, enable the `bert` feature in your Cargo.toml.
//!
//! ```ignore
//!# use chromadb::ChromaClient;
//!# use chromadb::collection::{ChromaCollection, GetResult, CollectionEntries, GetOptions};
//!# use serde_json::json;
//!# use chromadb::embeddings::bert::{SentenceEmbeddingsBuilder, SentenceEmbeddingsModelType};
//!# async fn doc_client_create_collection(client: &ChromaClient) -> anyhow::Result<()> {
//! let collection: ChromaCollection = client.get_or_create_collection("sbert_collection",
//! None).await?;
//!
//! let collection_entries = CollectionEntries {
//! ids: vec!["demo-id-1", "demo-id-2"],
//! embeddings: None,
//! metadatas: None,
//! documents: Some(vec![
//! "Some document about 9 octopus recipies",
//! "Some other document about DCEU Superman Vs CW Superman"])
//! };
//!
//! // Use SBERT embeddings
//! let sbert_embeddings = SentenceEmbeddingsBuilder::remote(
//! SentenceEmbeddingsModelType::AllMiniLmL6V2
//! ).create_model()?;
//!
//! collection.upsert(collection_entries, Some(Box::new(sbert_embeddings))).await?;
//!# Ok(())
//!# }
//! ```
pub use ChromaClient;
pub use ChromaCollection;