chroma-db 0.8.1 → 0.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 5a4fc64ab9c97c9630f0f438467d4a126c7665a33992ad0f357ca9ddfaab7eeb
4
- data.tar.gz: d5b48661b75917eaf8923dd7d2125230f9701144d3cffb18a5addeb8e90419e6
3
+ metadata.gz: 22e02f3e43fda5efcba180824acf3dd334c483750e06aa9f8ad6d326c19fafb4
4
+ data.tar.gz: 659ea81895f326b248f488682dfddb0c4ecf7f2058c6b48566b43fb5c71167cb
5
5
  SHA512:
6
- metadata.gz: 986a2f8366a22ebca4b8f7aa831aa4bd04ef373768f62d7811b7f0f45d45dcc58d6fbe2758dc1af85ea2a8111d3aeee8185930b4639af412fb912d8067f5c970
7
- data.tar.gz: ddbf174e31dd7999a6dd0462f9fe03b3e0a439a71bb0cb7487d1d09f558ebd6a67aa6f543e28babd432d5f9fb24804c52ad322a8f53e5737a396ba9c09222274
6
+ metadata.gz: 69c153e50ce71e7cf2c720b805d280700fcea66cbbda1e0135f0eebe57b6b01b021e43b1cb45d6f8441c70dbbcd739c5de9af7141eb84634029a38e558ac78b5
7
+ data.tar.gz: 4555bdb0f7bb65444a76cc0d0842d9b17597e170b8c6a519c8350ffe795c222c5262536fc42b17a126aad663a68142814c51dc56fd55e0ad898cba2a877fd972
data/CHANGELOG.md CHANGED
@@ -1,5 +1,13 @@
1
1
  ## [Unreleased]
2
2
 
3
+ ## [0.9.0] 2026-06-11
4
+
5
+ - Support Chroma v2 API.
6
+
7
+ ## 0.8.2 2024-10-16
8
+
9
+ - Fixes API change in Collection#query method. Parameters `where` and `where_document` are now optional.
10
+
3
11
  ## 0.8.1 2024-10-16
4
12
 
5
13
  - Adds support for Chroma hosted service. See README for more details how to set your `api_key`, `tenant`, and `database`.
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- chroma-db (0.8.1)
4
+ chroma-db (0.9.0)
5
5
  dry-monads (~> 1.6)
6
6
  ruby-next (~> 1.0, >= 1.0.3)
7
7
  zeitwerk (~> 2.6.0)
@@ -103,6 +103,7 @@ PLATFORMS
103
103
  arm64-darwin-22
104
104
  arm64-darwin-24
105
105
  x86_64-darwin-22
106
+ x86_64-darwin-23
106
107
  x86_64-linux
107
108
 
108
109
  DEPENDENCIES
data/README.md CHANGED
@@ -55,8 +55,9 @@ Chroma.database = "my_database" # Optional
55
55
  ## Requirements
56
56
 
57
57
  - Ruby 3.1.4 or newer
58
- - Chroma Database 0.4.24 or later running as a client/server model.
58
+ - Chroma Database 0.6.3 or later running as a client/server model with Chroma v2 API.
59
59
 
60
+ For Chroma database 0.6.2 or older (Chroma v1 API), please use version 0.8.2 of this gem.
60
61
  For Chroma database 0.3.22 or older, please use version 0.3.0 of this gem.
61
62
 
62
63
  ## Installation
@@ -124,6 +124,21 @@ module Chroma
124
124
  request["X-Chroma-Token"] = api_key unless api_key.nil?
125
125
  request
126
126
  end
127
+
128
+ private def raise_failure_error(result)
129
+ case result.failure.error
130
+ in Exception => exception
131
+ raise Chroma::APIConnectionError.new(exception.message)
132
+ in Net::HTTPInternalServerError => response
133
+ if response.body.is_a?(String) && (response.body.include?("ValueError") || response.body.include?("IndexError") || response.body.include?("TypeError"))
134
+ raise Chroma::InvalidRequestError.new(result.failure.body, status: result.failure.status, body: result.failure.body)
135
+ else
136
+ raise Chroma::APIConnectionError.new(result.failure.body, status: result.failure.status, body: result.failure.body)
137
+ end
138
+ else
139
+ raise Chroma::APIError.new(result.failure.body, status: result.failure.status, body: result.failure.body)
140
+ end
141
+ end
127
142
  end
128
143
 
129
144
  def self.included(base)
@@ -94,7 +94,7 @@ module Chroma
94
94
 
95
95
  def initialize
96
96
  @api_base = "api"
97
- @api_version = "v1"
97
+ @api_version = "v2"
98
98
 
99
99
  @log_level = Chroma::LEVEL_INFO
100
100
 
@@ -34,7 +34,7 @@ module Chroma
34
34
  # embeddings = collection.query(query_embeddings: [[1.5, 2.9, 3.3]], results: 5)
35
35
  #
36
36
  # Return an Array of Embedding with query results.
37
- def query(query_embeddings:, results: 10, where: {}, where_document: {}, include: %w[metadatas documents distances])
37
+ def query(query_embeddings:, results: 10, where: nil, where_document: nil, include: %w[metadatas documents distances])
38
38
  payload = {
39
39
  query_embeddings:,
40
40
  n_results: results,
@@ -43,7 +43,9 @@ module Chroma
43
43
  include:
44
44
  }
45
45
 
46
- result = self.class.execute_request(:post, "#{Chroma.api_url}/collections/#{id}/query", payload)
46
+ payload.delete_if { |_key, value| value.nil? }
47
+
48
+ result = self.class.execute_request(:post, "#{Chroma.api_url}/tenants/#{Chroma.tenant}/databases/#{Chroma.database}/collections/#{id}/query", payload)
47
49
 
48
50
  if result.success?
49
51
  build_embeddings_response(result.success.body)
@@ -87,7 +89,7 @@ module Chroma
87
89
  include:
88
90
  }
89
91
 
90
- result = self.class.execute_request(:post, "#{Chroma.api_url}/collections/#{id}/get", payload)
92
+ result = self.class.execute_request(:post, "#{Chroma.api_url}/tenants/#{Chroma.tenant}/databases/#{Chroma.database}/collections/#{id}/get", payload)
91
93
 
92
94
  if result.success?
93
95
  build_embeddings_response(result.success.body)
@@ -112,7 +114,7 @@ module Chroma
112
114
 
113
115
  payload = build_embeddings_payload(embeddings_array)
114
116
 
115
- result = self.class.execute_request(:post, "#{Chroma.api_url}/collections/#{id}/add", payload)
117
+ result = self.class.execute_request(:post, "#{Chroma.api_url}/tenants/#{Chroma.tenant}/databases/#{Chroma.database}/collections/#{id}/add", payload)
116
118
 
117
119
  return true if result.success?
118
120
 
@@ -138,13 +140,36 @@ module Chroma
138
140
  where_document:
139
141
  }
140
142
 
141
- result = self.class.execute_request(:post, "#{Chroma.api_url}/collections/#{id}/delete", payload)
143
+ result = self.class.execute_request(:post, "#{Chroma.api_url}/tenants/#{Chroma.tenant}/databases/#{Chroma.database}/collections/#{id}/delete", payload)
142
144
 
143
145
  return result.success.body if result.success?
144
146
 
145
147
  self.class.raise_failure_error(result)
146
148
  end
147
149
 
150
+ # Fork an existing collection.
151
+ #
152
+ # new_name [String] The new name of the collection (optional).
153
+ #
154
+ # Examples
155
+ #
156
+ # collection = Chroma::Resource::Collection.get("ruby-documentation")
157
+ # new_collection = collection.fork("new_collection_name")
158
+ #
159
+ # Returns a new collection with the specified name and containing identical data to the current collection.
160
+ def fork(new_name)
161
+ payload = {new_name: new_name}
162
+
163
+ result = self.class.execute_request(:post, "#{Chroma.api_url}/tenants/#{Chroma.tenant}/databases/#{Chroma.database}/collections/#{id}/fork", payload)
164
+
165
+ if result.success?
166
+ data = result.success.body
167
+ self.class.new(id: data["id"], name: new_name, metadata: data["metadata"])
168
+ else
169
+ self.class.raise_failure_error(result)
170
+ end
171
+ end
172
+
148
173
  # Update one or many embeddings to the collection.
149
174
  #
150
175
  # embeddings - An Array of Embeddings or one Embedding to add.
@@ -162,7 +187,7 @@ module Chroma
162
187
  payload = build_embeddings_payload(embeddings_array)
163
188
  payload.delete(:increment_index)
164
189
 
165
- result = self.class.execute_request(:post, "#{Chroma.api_url}/collections/#{id}/update", payload)
190
+ result = self.class.execute_request(:post, "#{Chroma.api_url}/tenants/#{Chroma.tenant}/databases/#{Chroma.database}/collections/#{id}/update", payload)
166
191
 
167
192
  return true if result.success?
168
193
 
@@ -189,7 +214,7 @@ module Chroma
189
214
 
190
215
  payload = build_embeddings_payload(embeddings_array)
191
216
 
192
- result = self.class.execute_request(:post, "#{Chroma.api_url}/collections/#{id}/upsert", payload)
217
+ result = self.class.execute_request(:post, "#{Chroma.api_url}/tenants/#{Chroma.tenant}/databases/#{Chroma.database}/collections/#{id}/upsert", payload)
193
218
 
194
219
  return true if result.success?
195
220
 
@@ -205,7 +230,7 @@ module Chroma
205
230
  #
206
231
  # Returns the count of embeddings in the collection.
207
232
  def count
208
- result = self.class.execute_request(:get, "#{Chroma.api_url}/collections/#{id}/count")
233
+ result = self.class.execute_request(:get, "#{Chroma.api_url}/tenants/#{Chroma.tenant}/databases/#{Chroma.database}/collections/#{id}/count")
209
234
 
210
235
  return result.success.body if result.success?
211
236
 
@@ -227,7 +252,7 @@ module Chroma
227
252
  payload = {new_name:}
228
253
  payload[:new_metadata] = new_metadata if new_metadata.any?
229
254
 
230
- result = self.class.execute_request(:put, "#{Chroma.api_url}/collections/#{id}", payload)
255
+ result = self.class.execute_request(:put, "#{Chroma.api_url}/tenants/#{Chroma.tenant}/databases/#{Chroma.database}/collections/#{id}", payload)
231
256
 
232
257
  if result.success?
233
258
  @name = new_name
@@ -252,7 +277,7 @@ module Chroma
252
277
  def self.create(name, metadata = nil)
253
278
  payload = {name:, metadata:, get_or_create: false}
254
279
 
255
- result = execute_request(:post, "#{Chroma.api_url}/collections", payload)
280
+ result = execute_request(:post, "#{Chroma.api_url}/tenants/#{Chroma.tenant}/databases/#{Chroma.database}/collections", payload)
256
281
 
257
282
  if result.success?
258
283
  data = result.success.body
@@ -272,7 +297,7 @@ module Chroma
272
297
  #
273
298
  # Returns The retrieved collection object. Raises Chroma::InvalidRequestError if not found.
274
299
  def self.get(name)
275
- result = execute_request(:get, "#{Chroma.api_url}/collections/#{name}")
300
+ result = execute_request(:get, "#{Chroma.api_url}/tenants/#{Chroma.tenant}/databases/#{Chroma.database}/collections/#{name}")
276
301
 
277
302
  if result.success?
278
303
  data = result.success.body
@@ -297,7 +322,7 @@ module Chroma
297
322
  def self.get_or_create(name, metadata = nil)
298
323
  payload = {name:, metadata:, get_or_create: true}
299
324
 
300
- result = execute_request(:post, "#{Chroma.api_url}/collections", payload)
325
+ result = execute_request(:post, "#{Chroma.api_url}/tenants/#{Chroma.tenant}/databases/#{Chroma.database}/collections", payload)
301
326
 
302
327
  if result.success?
303
328
  data = result.success.body
@@ -315,7 +340,7 @@ module Chroma
315
340
  #
316
341
  # Returns An array of all collections in the database.
317
342
  def self.list
318
- result = execute_request(:get, "#{Chroma.api_url}/collections")
343
+ result = execute_request(:get, "#{Chroma.api_url}/tenants/#{Chroma.tenant}/databases/#{Chroma.database}/collections")
319
344
 
320
345
  if result.success?
321
346
  data = result.success.body
@@ -335,14 +360,29 @@ module Chroma
335
360
  #
336
361
  # Returns true if the collection was successfully deleted, raise Chroma::InvalidRequestError otherwise.
337
362
  def self.delete(name)
338
- result = execute_request(:delete, "#{Chroma.api_url}/collections/#{name}")
363
+ result = execute_request(:delete, "#{Chroma.api_url}/tenants/#{Chroma.tenant}/databases/#{Chroma.database}/collections/#{name}")
339
364
 
340
365
  return true if result.success?
341
366
 
342
367
  raise_failure_error(result)
343
368
  end
344
369
 
345
- def self.raise_failure_error(result)
370
+ # Count the total number of collections in a given database.
371
+ #
372
+ # Examples
373
+ #
374
+ # Chroma::Resource::Collection.collections_count
375
+ #
376
+ # Returns the count of collections in the database.
377
+ def self.collections_count
378
+ result = execute_request(:get, "#{Chroma.api_url}/tenants/#{Chroma.tenant}/databases/#{Chroma.database}/collections_count")
379
+
380
+ return result.success.body if result.success?
381
+
382
+ raise_failure_error(result)
383
+ end
384
+
385
+ def self.raise_failure_error1(result)
346
386
  case result.failure.error
347
387
  in Exception => exception
348
388
  raise Chroma::APIConnectionError.new(exception.message)
@@ -7,6 +7,28 @@ module Chroma
7
7
  using RubyNext
8
8
 
9
9
  include Chroma::APIOperations::Request
10
+
11
+ attr_reader :id
12
+ attr_reader :name
13
+ attr_reader :tenant
14
+
15
+ def initialize(id:, name:, tenant:)
16
+ @id = id
17
+ @name = name
18
+ @tenant = tenant
19
+ end
20
+
21
+ # Get the current user's identity, tenant, and databases of the Chroma database server.
22
+ #
23
+ # Returns the current user's identity, tenant, and databases of the Chroma database server.
24
+ def self.auth_identity
25
+ result = execute_request(:get, "#{Chroma.api_url}/auth/identity")
26
+
27
+ return result.success.body if result.success?
28
+
29
+ raise_failure_error(result)
30
+ end
31
+
10
32
  # Get the version of the Chroma database server.
11
33
  #
12
34
  # Returns the version of the Chroma database server.
@@ -29,6 +51,17 @@ module Chroma
29
51
  raise_failure_error(result)
30
52
  end
31
53
 
54
+ # Check the hearlthcheck of the Chroma database server.
55
+ #
56
+ # Return a Hash with a boolean.
57
+ def self.healthcheck
58
+ result = execute_request(:get, "#{Chroma.api_url}/healthcheck")
59
+
60
+ return result.success.body if result.success?
61
+
62
+ raise_failure_error(result)
63
+ end
64
+
32
65
  # Check the heartbeat of the Chroma database server.
33
66
  #
34
67
  # Return a Hash with a timestamp.
@@ -40,21 +73,106 @@ module Chroma
40
73
  raise_failure_error(result)
41
74
  end
42
75
 
43
- def self.raise_failure_error(result)
44
- case result.failure.error
45
- in Exception => exception
46
- raise Chroma::APIConnectionError.new(exception.message)
47
- in Net::HTTPInternalServerError => response
48
- if response.body.is_a?(String) && (response.body.include?("ValueError") || response.body.include?("IndexError") || response.body.include?("TypeError"))
49
- raise Chroma::InvalidRequestError.new(result.failure.body, status: result.failure.status, body: result.failure.body)
50
- else
51
- raise Chroma::APIConnectionError.new(result.failure.body, status: result.failure.status, body: result.failure.body)
52
- end
76
+ # Check the pre-flight checks of the Chroma database server.
77
+ #
78
+ # Return a Hash with a timestamp.
79
+ def self.pre_flight_checks
80
+ result = execute_request(:get, "#{Chroma.api_url}/pre-flight-checks")
81
+
82
+ return result.success.body if result.success?
83
+
84
+ raise_failure_error(result)
85
+ end
86
+
87
+ # Lists all databases from the tenant.
88
+ #
89
+ # Examples
90
+ #
91
+ # Chroma::Resources::Database.list
92
+ #
93
+ # Returns an array of all databases in the tenant.
94
+ def self.list
95
+ result = execute_request(:get, "#{Chroma.api_url}/tenants/#{Chroma.tenant}/databases")
96
+
97
+ if result.success?
98
+ data = result.success.body
99
+ data.map { |item| new(id: item["id"], name: item["name"], tenant: item["tenant"]) }
100
+ else
101
+ raise_failure_error(result)
102
+ end
103
+ end
104
+
105
+ # Create a new database on the tenant.
106
+ #
107
+ # name - The name of the database.
108
+ #
109
+ # Examples
110
+ #
111
+ # database = Chorma::Resources::Database.create("database-name")
112
+ #
113
+ # Returns true if the database was successfully created, raises Chroma::APIError otherwise.
114
+ def self.create(name)
115
+ payload = {name: name}
116
+
117
+ result = execute_request(:post, "#{Chroma.api_url}/tenants/#{Chroma.tenant}/databases", payload)
118
+
119
+ return true if result.success?
120
+
121
+ raise_failure_error(result)
122
+ end
123
+
124
+ # Retrieves a database from the tenant.
125
+ #
126
+ # database_name - The name of the database to retrieve.
127
+ #
128
+ # Examples
129
+ #
130
+ # Chroma::Resources::Database.get("database-name")
131
+ #
132
+ # Returns The retrieved database object. Raises Chroma::APIError if not found.
133
+ def self.get(database_name)
134
+ result = execute_request(:get, "#{Chroma.api_url}/tenants/#{Chroma.tenant}/databases/#{database_name}")
135
+
136
+ if result.success?
137
+ data = result.success.body
138
+ new(id: data["id"], name: data["name"], tenant: data["tenant"])
53
139
  else
54
- raise Chroma::APIError.new(result.failure.body, status: result.failure.status, body: result.failure.body)
140
+ raise_failure_error(result)
55
141
  end
56
142
  end
57
- private_class_method :raise_failure_error
143
+
144
+ # Deletes a database from the tenant.
145
+ #
146
+ # database_name - The name of the database to retrieve.
147
+ #
148
+ # Examples
149
+ #
150
+ # Chroma::Resources::Database.delete("database-name")
151
+ #
152
+ # Returns true if the database was successfully deleted, raises Chroma::APIError otherwise.
153
+ def self.delete(database_name)
154
+ result = execute_request(:delete, "#{Chroma.api_url}/tenants/#{Chroma.tenant}/databases/#{database_name}")
155
+
156
+ return true if result.success?
157
+
158
+ raise_failure_error(result)
159
+ end
160
+
161
+ # Count the total number of collections in the database object.
162
+ #
163
+ # Examples
164
+ #
165
+ # database = Chroma::Resources::Database.get("database-name")
166
+ # database.collections_count
167
+ #
168
+ # Returns the count of collections in the database.
169
+ def collections_count
170
+ result = self.class.execute_request(:get, "#{Chroma.api_url}/tenants/#{Chroma.tenant}/databases/#{name}/collections_count")
171
+
172
+ return result.success.body if result.success?
173
+
174
+ self.class.raise_failure_error(result)
175
+ end
58
176
  end
59
177
  end
60
178
  end
@@ -0,0 +1,57 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Chroma
4
+ module Resources
5
+ # A Tenant class represents a tenant by its ID and name.
6
+ class Tenant
7
+ include Chroma::APIOperations::Request
8
+
9
+ attr_reader :id
10
+ attr_reader :name
11
+
12
+ def initialize(id:, name:)
13
+ @id = id
14
+ @name = name
15
+ end
16
+
17
+ # Create a new tenant.
18
+ #
19
+ # name - The name of the tenant.
20
+ #
21
+ # Examples
22
+ #
23
+ # tenant = Chorma::Resources::Tenant.create("tenant-name")
24
+ #
25
+ # Returns true if the tenant was successfully created, raises Chroma::APIError otherwise.
26
+ def self.create(name)
27
+ payload = {name: name}
28
+
29
+ result = execute_request(:post, "#{Chroma.api_url}/tenants", payload)
30
+
31
+ return true if result.success?
32
+
33
+ raise_failure_error(result)
34
+ end
35
+
36
+ # Retrieves a tenant.
37
+ #
38
+ # tenant_name - The name of the tenant to retrieve.
39
+ #
40
+ # Examples
41
+ #
42
+ # Chroma::Resources::Tenant.get("tenant-name")
43
+ #
44
+ # Returns The retrieved tenant object. Raises Chroma::APIError if not found.
45
+ def self.get(tenant_name)
46
+ result = execute_request(:get, "#{Chroma.api_url}/tenants/#{tenant_name}")
47
+
48
+ if result.success?
49
+ data = result.success.body
50
+ new(id: data["id"], name: data["name"])
51
+ else
52
+ raise_failure_error(result)
53
+ end
54
+ end
55
+ end
56
+ end
57
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Chroma
4
- VERSION = "0.8.1"
4
+ VERSION = "0.9.0"
5
5
  end
data/lib/chroma-db.rb CHANGED
@@ -20,3 +20,4 @@ require "chroma/errors"
20
20
  require "chroma/resources/embedding"
21
21
  require "chroma/resources/collection"
22
22
  require "chroma/resources/database"
23
+ require "chroma/resources/tenant"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: chroma-db
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.8.1
4
+ version: 0.9.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mario Alberto Chávez
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2024-10-16 00:00:00.000000000 Z
11
+ date: 2026-06-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: dry-monads
@@ -103,6 +103,7 @@ files:
103
103
  - lib/chroma/resources/collection.rb
104
104
  - lib/chroma/resources/database.rb
105
105
  - lib/chroma/resources/embedding.rb
106
+ - lib/chroma/resources/tenant.rb
106
107
  - lib/chroma/util.rb
107
108
  - lib/chroma/version.rb
108
109
  - sig/chroma.rbs
@@ -128,7 +129,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
128
129
  - !ruby/object:Gem::Version
129
130
  version: '0'
130
131
  requirements: []
131
- rubygems_version: 3.5.21
132
+ rubygems_version: 3.5.16
132
133
  signing_key:
133
134
  specification_version: 4
134
135
  summary: Ruby client for Chroma DB.