chroma-db 0.8.2 → 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: 8f7a0ef254b27e7e3d06c9720893c4b416f971948aeda404da63d18325309c6f
4
- data.tar.gz: 356777eec892cd99f11635cadcd4e510e5041cc40b8b04c2a8e4ecaa1179da65
3
+ metadata.gz: 22e02f3e43fda5efcba180824acf3dd334c483750e06aa9f8ad6d326c19fafb4
4
+ data.tar.gz: 659ea81895f326b248f488682dfddb0c4ecf7f2058c6b48566b43fb5c71167cb
5
5
  SHA512:
6
- metadata.gz: cbb667f3547c3a08f0a3859d3dc3fe4802bbbca46722af363cfcfad556c163c83e3606b48254fd43c8b1186ace9c63e764e32568e7e7eef4987b8e89632847f0
7
- data.tar.gz: 1822f4ff02a4dafffbb32078ed186da5d9ccb0228725bb8fbf5b61f8d1f89fd78c0297605e49a9e050e2086ce81ac67771b0a273db25ed59970b5930729a2baa
6
+ metadata.gz: 69c153e50ce71e7cf2c720b805d280700fcea66cbbda1e0135f0eebe57b6b01b021e43b1cb45d6f8441c70dbbcd739c5de9af7141eb84634029a38e558ac78b5
7
+ data.tar.gz: 4555bdb0f7bb65444a76cc0d0842d9b17597e170b8c6a519c8350ffe795c222c5262536fc42b17a126aad663a68142814c51dc56fd55e0ad898cba2a877fd972
data/CHANGELOG.md CHANGED
@@ -1,5 +1,9 @@
1
1
  ## [Unreleased]
2
2
 
3
+ ## [0.9.0] 2026-06-11
4
+
5
+ - Support Chroma v2 API.
6
+
3
7
  ## 0.8.2 2024-10-16
4
8
 
5
9
  - Fixes API change in Collection#query method. Parameters `where` and `where_document` are now optional.
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- chroma-db (0.8.2)
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
 
@@ -45,7 +45,7 @@ module Chroma
45
45
 
46
46
  payload.delete_if { |_key, value| value.nil? }
47
47
 
48
- result = self.class.execute_request(:post, "#{Chroma.api_url}/collections/#{id}/query", payload)
48
+ result = self.class.execute_request(:post, "#{Chroma.api_url}/tenants/#{Chroma.tenant}/databases/#{Chroma.database}/collections/#{id}/query", payload)
49
49
 
50
50
  if result.success?
51
51
  build_embeddings_response(result.success.body)
@@ -89,7 +89,7 @@ module Chroma
89
89
  include:
90
90
  }
91
91
 
92
- 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)
93
93
 
94
94
  if result.success?
95
95
  build_embeddings_response(result.success.body)
@@ -114,7 +114,7 @@ module Chroma
114
114
 
115
115
  payload = build_embeddings_payload(embeddings_array)
116
116
 
117
- 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)
118
118
 
119
119
  return true if result.success?
120
120
 
@@ -140,13 +140,36 @@ module Chroma
140
140
  where_document:
141
141
  }
142
142
 
143
- 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)
144
144
 
145
145
  return result.success.body if result.success?
146
146
 
147
147
  self.class.raise_failure_error(result)
148
148
  end
149
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
+
150
173
  # Update one or many embeddings to the collection.
151
174
  #
152
175
  # embeddings - An Array of Embeddings or one Embedding to add.
@@ -164,7 +187,7 @@ module Chroma
164
187
  payload = build_embeddings_payload(embeddings_array)
165
188
  payload.delete(:increment_index)
166
189
 
167
- 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)
168
191
 
169
192
  return true if result.success?
170
193
 
@@ -191,7 +214,7 @@ module Chroma
191
214
 
192
215
  payload = build_embeddings_payload(embeddings_array)
193
216
 
194
- 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)
195
218
 
196
219
  return true if result.success?
197
220
 
@@ -207,7 +230,7 @@ module Chroma
207
230
  #
208
231
  # Returns the count of embeddings in the collection.
209
232
  def count
210
- 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")
211
234
 
212
235
  return result.success.body if result.success?
213
236
 
@@ -229,7 +252,7 @@ module Chroma
229
252
  payload = {new_name:}
230
253
  payload[:new_metadata] = new_metadata if new_metadata.any?
231
254
 
232
- 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)
233
256
 
234
257
  if result.success?
235
258
  @name = new_name
@@ -254,7 +277,7 @@ module Chroma
254
277
  def self.create(name, metadata = nil)
255
278
  payload = {name:, metadata:, get_or_create: false}
256
279
 
257
- 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)
258
281
 
259
282
  if result.success?
260
283
  data = result.success.body
@@ -274,7 +297,7 @@ module Chroma
274
297
  #
275
298
  # Returns The retrieved collection object. Raises Chroma::InvalidRequestError if not found.
276
299
  def self.get(name)
277
- 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}")
278
301
 
279
302
  if result.success?
280
303
  data = result.success.body
@@ -299,7 +322,7 @@ module Chroma
299
322
  def self.get_or_create(name, metadata = nil)
300
323
  payload = {name:, metadata:, get_or_create: true}
301
324
 
302
- 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)
303
326
 
304
327
  if result.success?
305
328
  data = result.success.body
@@ -317,7 +340,7 @@ module Chroma
317
340
  #
318
341
  # Returns An array of all collections in the database.
319
342
  def self.list
320
- result = execute_request(:get, "#{Chroma.api_url}/collections")
343
+ result = execute_request(:get, "#{Chroma.api_url}/tenants/#{Chroma.tenant}/databases/#{Chroma.database}/collections")
321
344
 
322
345
  if result.success?
323
346
  data = result.success.body
@@ -337,14 +360,29 @@ module Chroma
337
360
  #
338
361
  # Returns true if the collection was successfully deleted, raise Chroma::InvalidRequestError otherwise.
339
362
  def self.delete(name)
340
- 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}")
341
364
 
342
365
  return true if result.success?
343
366
 
344
367
  raise_failure_error(result)
345
368
  end
346
369
 
347
- 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)
348
386
  case result.failure.error
349
387
  in Exception => exception
350
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.2"
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.2
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-12-11 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.23
132
+ rubygems_version: 3.5.16
132
133
  signing_key:
133
134
  specification_version: 4
134
135
  summary: Ruby client for Chroma DB.