chroma-db 0.4.0 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 81e14d1d408077096602201fb386612b9d511a0294726efcba2942491a164763
4
- data.tar.gz: bc06c130d7ad215d5a2264527813e10900bda1b6d8975de8c59712a7cb438a99
3
+ metadata.gz: c10027541e8648c0ba4e6ebe73fef4fdb75d55a1c04c0e59bcd681a1b2108598
4
+ data.tar.gz: fb5a8a590885bc26fcabfd379b6ee5aaada355f99e05ab50eca32280a80bccea
5
5
  SHA512:
6
- metadata.gz: 109074182103666dca9fb201c0dc473220b0a5ee6849ff3022203a4debea1388347b98d74a0ce14d75361953a1c70b6c1a24ab62387c317e394712fe677e082a
7
- data.tar.gz: f075cf97229240b6d38ff72946188ec436a0010b8f0925d1b30a63e6c2b3dc0b6bf8436a16d0c3abf5c89cabe64951b73bd10eb036b95a3ae9a40b8f2c778cdd
6
+ metadata.gz: 3665995e6028599eae9daf8f546e2923075b441596b4f5854bfb33901405ea8c59c47d8ec237d1c079e8e9b68115181fb2cfb4db5fd794db1e2a5a8cd61bf755
7
+ data.tar.gz: 1f1a9534d6422b34b7d7f0a8a6cb1a4f331076e44dc030e46fcbf7af971b9181747b1fa38a616cf3c44636010a54ea0eaba357dd3f7878f94cf51c40d883d854
data/CHANGELOG.md CHANGED
@@ -1,5 +1,14 @@
1
1
  ## [Unreleased]
2
2
 
3
+ ## [0.6.0] 2023-06-05
4
+
5
+ - Fix failure to rescue from API call exceptions.
6
+ - Fix embedding loading into Embedding resource.
7
+
8
+ ## [0.5.0] 2023-05-26
9
+
10
+ - Adds method `get_or_create` to Collection class.
11
+
3
12
  ## [0.4.0] 2023-05-23
4
13
 
5
14
  - This version implements Chroma's API change where Collection uses its collection id for many operations. Changes in the
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- chroma-db (0.4.0)
4
+ chroma-db (0.6.0)
5
5
  dry-monads (~> 1.6)
6
6
  ruby-next (>= 0.15.0)
7
7
 
@@ -43,12 +43,12 @@ module Chroma
43
43
  include: include
44
44
  }
45
45
 
46
- result = self.class.execute_request(:post, "#{Chroma.api_url}/collections/#{name}/query", payload)
46
+ result = self.class.execute_request(:post, "#{Chroma.api_url}/collections/#{id}/query", payload)
47
47
 
48
48
  if result.success?
49
49
  build_embeddings_response(result.success.body)
50
50
  else
51
- raise_failure_error(result)
51
+ self.class.raise_failure_error(result)
52
52
  end
53
53
  end
54
54
 
@@ -92,7 +92,7 @@ module Chroma
92
92
  if result.success?
93
93
  build_embeddings_response(result.success.body)
94
94
  else
95
- raise_failure_error(result)
95
+ self.class.raise_failure_error(result)
96
96
  end
97
97
  end
98
98
 
@@ -116,7 +116,7 @@ module Chroma
116
116
 
117
117
  return true if result.success?
118
118
 
119
- raise_failure_error(result)
119
+ self.class.raise_failure_error(result)
120
120
  end
121
121
 
122
122
  # Delete embeddings from the collection.
@@ -142,7 +142,7 @@ module Chroma
142
142
 
143
143
  return result.success.body if result.success?
144
144
 
145
- raise_failure_error(result)
145
+ self.class.raise_failure_error(result)
146
146
  end
147
147
 
148
148
  # Update one or many embeddings to the collection.
@@ -166,7 +166,7 @@ module Chroma
166
166
 
167
167
  return true if result.success?
168
168
 
169
- raise_failure_error(result)
169
+ self.class.raise_failure_error(result)
170
170
  end
171
171
 
172
172
  # Upsert (insert or update) one or many embeddings to the collection.
@@ -177,8 +177,8 @@ module Chroma
177
177
  #
178
178
  # collection = Chroma::Resource::Collection.get("ruby-documentation")
179
179
  # embeddings = [
180
- # Embedding.new(id: "Array#fetch", embeddings[9.8, 2.3, 2.9], metadata: {url: "https://..."}),
181
- # Embedding.new(id: "Array#select", embeddings[5.6, 3.1, 4.7], metadata: {url: "https://..."})
180
+ # Embedding.new(id: "Array#fetch", embeddings: [9.8, 2.3, 2.9], metadata: {url: "https://..."}),
181
+ # Embedding.new(id: "Array#select", embeddings: [5.6, 3.1, 4.7], metadata: {url: "https://..."})
182
182
  # ]
183
183
  # collection.upsert()
184
184
  #
@@ -193,7 +193,7 @@ module Chroma
193
193
 
194
194
  return true if result.success?
195
195
 
196
- raise_failure_error(result)
196
+ self.class.raise_failure_error(result)
197
197
  end
198
198
 
199
199
  # Count the number of embeddings in a collection.
@@ -209,7 +209,7 @@ module Chroma
209
209
 
210
210
  return result.success.body if result.success?
211
211
 
212
- raise_failure_error(result)
212
+ self.class.raise_failure_error(result)
213
213
  end
214
214
 
215
215
  # Modify the name and metadata of the current collection.
@@ -233,7 +233,7 @@ module Chroma
233
233
  @name = new_name
234
234
  @metadata = new_metadata
235
235
  else
236
- raise_failure_error(result)
236
+ self.class.raise_failure_error(result)
237
237
  end
238
238
  end
239
239
 
@@ -250,7 +250,7 @@ module Chroma
250
250
 
251
251
  return true if result.success?
252
252
 
253
- raise_failure_error(result)
253
+ self.class.raise_failure_error(result)
254
254
  end
255
255
 
256
256
  # Create a new collection on the database.
@@ -298,6 +298,31 @@ module Chroma
298
298
  end
299
299
  end
300
300
 
301
+ # Get or create a collection on the database.
302
+ #
303
+ # name - The name of the collection. Name needs to be between 3-63 characters, starts and ends
304
+ # with an alphanumeric character, contains only alphanumeric characters, underscores or hyphens (-), and
305
+ # contains no two consecutive periods
306
+ # metadata - A hash of additional metadata associated with the collection, this is used if collection is created.
307
+ #
308
+ # Examples
309
+ #
310
+ # collection = Chorma::Resources::Collection.get_or_create("ruby-documentation", {source: "Ruby lang website"})
311
+ #
312
+ # Returns the created collection object.
313
+ def self.get_or_create(name, metadata = nil)
314
+ payload = {name: name, metadata: metadata, get_or_create: true}
315
+
316
+ result = execute_request(:post, "#{Chroma.api_url}/collections", payload)
317
+
318
+ if result.success?
319
+ data = result.success.body
320
+ new(id: data["id"], name: data["name"], metadata: data["metadata"])
321
+ else
322
+ raise_failure_error(result)
323
+ end
324
+ end
325
+
301
326
  # Retrieves all collections in the database.
302
327
  #
303
328
  # Examples
@@ -351,7 +376,6 @@ module Chroma
351
376
  raise Chroma::APIError.new(result.failure.body, status: result.failure.status, body: result.failure.body)
352
377
  end
353
378
  end
354
- private_class_method :raise_failure_error
355
379
 
356
380
  private
357
381
 
@@ -372,7 +396,7 @@ module Chroma
372
396
  Chroma::Util.log_debug("Building embeddings from #{result.inspect}")
373
397
 
374
398
  result_ids = result.fetch("ids", []).flatten
375
- result_embeddings = (result.dig("embeddings") || []).flatten
399
+ result_embeddings = result.dig("embeddings") || []
376
400
  result_documents = (result.dig("documents") || []).flatten
377
401
  result_metadatas = (result.dig("metadatas") || []).flatten
378
402
  result_distances = (result.dig("distances") || []).flatten
@@ -43,12 +43,12 @@ module Chroma
43
43
  include: include
44
44
  }
45
45
 
46
- result = self.class.execute_request(:post, "#{Chroma.api_url}/collections/#{name}/query", payload)
46
+ result = self.class.execute_request(:post, "#{Chroma.api_url}/collections/#{id}/query", payload)
47
47
 
48
48
  if result.success?
49
49
  build_embeddings_response(result.success.body)
50
50
  else
51
- raise_failure_error(result)
51
+ self.class.raise_failure_error(result)
52
52
  end
53
53
  end
54
54
 
@@ -92,7 +92,7 @@ module Chroma
92
92
  if result.success?
93
93
  build_embeddings_response(result.success.body)
94
94
  else
95
- raise_failure_error(result)
95
+ self.class.raise_failure_error(result)
96
96
  end
97
97
  end
98
98
 
@@ -116,7 +116,7 @@ module Chroma
116
116
 
117
117
  return true if result.success?
118
118
 
119
- raise_failure_error(result)
119
+ self.class.raise_failure_error(result)
120
120
  end
121
121
 
122
122
  # Delete embeddings from the collection.
@@ -142,7 +142,7 @@ module Chroma
142
142
 
143
143
  return result.success.body if result.success?
144
144
 
145
- raise_failure_error(result)
145
+ self.class.raise_failure_error(result)
146
146
  end
147
147
 
148
148
  # Update one or many embeddings to the collection.
@@ -166,7 +166,7 @@ module Chroma
166
166
 
167
167
  return true if result.success?
168
168
 
169
- raise_failure_error(result)
169
+ self.class.raise_failure_error(result)
170
170
  end
171
171
 
172
172
  # Upsert (insert or update) one or many embeddings to the collection.
@@ -177,8 +177,8 @@ module Chroma
177
177
  #
178
178
  # collection = Chroma::Resource::Collection.get("ruby-documentation")
179
179
  # embeddings = [
180
- # Embedding.new(id: "Array#fetch", embeddings[9.8, 2.3, 2.9], metadata: {url: "https://..."}),
181
- # Embedding.new(id: "Array#select", embeddings[5.6, 3.1, 4.7], metadata: {url: "https://..."})
180
+ # Embedding.new(id: "Array#fetch", embeddings: [9.8, 2.3, 2.9], metadata: {url: "https://..."}),
181
+ # Embedding.new(id: "Array#select", embeddings: [5.6, 3.1, 4.7], metadata: {url: "https://..."})
182
182
  # ]
183
183
  # collection.upsert()
184
184
  #
@@ -193,7 +193,7 @@ module Chroma
193
193
 
194
194
  return true if result.success?
195
195
 
196
- raise_failure_error(result)
196
+ self.class.raise_failure_error(result)
197
197
  end
198
198
 
199
199
  # Count the number of embeddings in a collection.
@@ -209,7 +209,7 @@ module Chroma
209
209
 
210
210
  return result.success.body if result.success?
211
211
 
212
- raise_failure_error(result)
212
+ self.class.raise_failure_error(result)
213
213
  end
214
214
 
215
215
  # Modify the name and metadata of the current collection.
@@ -233,7 +233,7 @@ module Chroma
233
233
  @name = new_name
234
234
  @metadata = new_metadata
235
235
  else
236
- raise_failure_error(result)
236
+ self.class.raise_failure_error(result)
237
237
  end
238
238
  end
239
239
 
@@ -250,7 +250,7 @@ module Chroma
250
250
 
251
251
  return true if result.success?
252
252
 
253
- raise_failure_error(result)
253
+ self.class.raise_failure_error(result)
254
254
  end
255
255
 
256
256
  # Create a new collection on the database.
@@ -298,6 +298,31 @@ module Chroma
298
298
  end
299
299
  end
300
300
 
301
+ # Get or create a collection on the database.
302
+ #
303
+ # name - The name of the collection. Name needs to be between 3-63 characters, starts and ends
304
+ # with an alphanumeric character, contains only alphanumeric characters, underscores or hyphens (-), and
305
+ # contains no two consecutive periods
306
+ # metadata - A hash of additional metadata associated with the collection, this is used if collection is created.
307
+ #
308
+ # Examples
309
+ #
310
+ # collection = Chorma::Resources::Collection.get_or_create("ruby-documentation", {source: "Ruby lang website"})
311
+ #
312
+ # Returns the created collection object.
313
+ def self.get_or_create(name, metadata = nil)
314
+ payload = {name: name, metadata: metadata, get_or_create: true}
315
+
316
+ result = execute_request(:post, "#{Chroma.api_url}/collections", payload)
317
+
318
+ if result.success?
319
+ data = result.success.body
320
+ new(id: data["id"], name: data["name"], metadata: data["metadata"])
321
+ else
322
+ raise_failure_error(result)
323
+ end
324
+ end
325
+
301
326
  # Retrieves all collections in the database.
302
327
  #
303
328
  # Examples
@@ -347,7 +372,6 @@ module Chroma
347
372
  raise Chroma::APIError.new(result.failure.body, status: result.failure.status, body: result.failure.body)
348
373
  end
349
374
  end
350
- private_class_method :raise_failure_error
351
375
 
352
376
  private
353
377
 
@@ -368,7 +392,7 @@ module Chroma
368
392
  Chroma::Util.log_debug("Building embeddings from #{result.inspect}")
369
393
 
370
394
  result_ids = result.fetch("ids", []).flatten
371
- result_embeddings = (result.dig("embeddings") || []).flatten
395
+ result_embeddings = result.dig("embeddings") || []
372
396
  result_documents = (result.dig("documents") || []).flatten
373
397
  result_metadatas = (result.dig("metadatas") || []).flatten
374
398
  result_distances = (result.dig("distances") || []).flatten
@@ -48,7 +48,7 @@ module Chroma
48
48
  if result.success?
49
49
  build_embeddings_response(result.success.body)
50
50
  else
51
- raise_failure_error(result)
51
+ self.class.raise_failure_error(result)
52
52
  end
53
53
  end
54
54
 
@@ -92,7 +92,7 @@ module Chroma
92
92
  if result.success?
93
93
  build_embeddings_response(result.success.body)
94
94
  else
95
- raise_failure_error(result)
95
+ self.class.raise_failure_error(result)
96
96
  end
97
97
  end
98
98
 
@@ -116,7 +116,7 @@ module Chroma
116
116
 
117
117
  return true if result.success?
118
118
 
119
- raise_failure_error(result)
119
+ self.class.raise_failure_error(result)
120
120
  end
121
121
 
122
122
  # Delete embeddings from the collection.
@@ -142,7 +142,7 @@ module Chroma
142
142
 
143
143
  return result.success.body if result.success?
144
144
 
145
- raise_failure_error(result)
145
+ self.class.raise_failure_error(result)
146
146
  end
147
147
 
148
148
  # Update one or many embeddings to the collection.
@@ -166,7 +166,7 @@ module Chroma
166
166
 
167
167
  return true if result.success?
168
168
 
169
- raise_failure_error(result)
169
+ self.class.raise_failure_error(result)
170
170
  end
171
171
 
172
172
  # Upsert (insert or update) one or many embeddings to the collection.
@@ -177,8 +177,8 @@ module Chroma
177
177
  #
178
178
  # collection = Chroma::Resource::Collection.get("ruby-documentation")
179
179
  # embeddings = [
180
- # Embedding.new(id: "Array#fetch", embeddings[9.8, 2.3, 2.9], metadata: {url: "https://..."}),
181
- # Embedding.new(id: "Array#select", embeddings[5.6, 3.1, 4.7], metadata: {url: "https://..."})
180
+ # Embedding.new(id: "Array#fetch", embeddings: [9.8, 2.3, 2.9], metadata: {url: "https://..."}),
181
+ # Embedding.new(id: "Array#select", embeddings: [5.6, 3.1, 4.7], metadata: {url: "https://..."})
182
182
  # ]
183
183
  # collection.upsert()
184
184
  #
@@ -193,7 +193,7 @@ module Chroma
193
193
 
194
194
  return true if result.success?
195
195
 
196
- raise_failure_error(result)
196
+ self.class.raise_failure_error(result)
197
197
  end
198
198
 
199
199
  # Count the number of embeddings in a collection.
@@ -209,7 +209,7 @@ module Chroma
209
209
 
210
210
  return result.success.body if result.success?
211
211
 
212
- raise_failure_error(result)
212
+ self.class.raise_failure_error(result)
213
213
  end
214
214
 
215
215
  # Modify the name and metadata of the current collection.
@@ -233,7 +233,7 @@ module Chroma
233
233
  @name = new_name
234
234
  @metadata = new_metadata
235
235
  else
236
- raise_failure_error(result)
236
+ self.class.raise_failure_error(result)
237
237
  end
238
238
  end
239
239
 
@@ -250,7 +250,7 @@ module Chroma
250
250
 
251
251
  return true if result.success?
252
252
 
253
- raise_failure_error(result)
253
+ self.class.raise_failure_error(result)
254
254
  end
255
255
 
256
256
  # Create a new collection on the database.
@@ -298,6 +298,31 @@ module Chroma
298
298
  end
299
299
  end
300
300
 
301
+ # Get or create a collection on the database.
302
+ #
303
+ # name - The name of the collection. Name needs to be between 3-63 characters, starts and ends
304
+ # with an alphanumeric character, contains only alphanumeric characters, underscores or hyphens (-), and
305
+ # contains no two consecutive periods
306
+ # metadata - A hash of additional metadata associated with the collection, this is used if collection is created.
307
+ #
308
+ # Examples
309
+ #
310
+ # collection = Chorma::Resources::Collection.get_or_create("ruby-documentation", {source: "Ruby lang website"})
311
+ #
312
+ # Returns the created collection object.
313
+ def self.get_or_create(name, metadata = nil)
314
+ payload = {name:, metadata:, get_or_create: true}
315
+
316
+ result = execute_request(:post, "#{Chroma.api_url}/collections", payload)
317
+
318
+ if result.success?
319
+ data = result.success.body
320
+ new(id: data["id"], name: data["name"], metadata: data["metadata"])
321
+ else
322
+ raise_failure_error(result)
323
+ end
324
+ end
325
+
301
326
  # Retrieves all collections in the database.
302
327
  #
303
328
  # Examples
@@ -347,7 +372,6 @@ module Chroma
347
372
  raise Chroma::APIError.new(result.failure.body, status: result.failure.status, body: result.failure.body)
348
373
  end
349
374
  end
350
- private_class_method :raise_failure_error
351
375
 
352
376
  private
353
377
 
@@ -368,7 +392,7 @@ module Chroma
368
392
  Chroma::Util.log_debug("Building embeddings from #{result.inspect}")
369
393
 
370
394
  result_ids = result.fetch("ids", []).flatten
371
- result_embeddings = (result.dig("embeddings") || []).flatten
395
+ result_embeddings = result.dig("embeddings") || []
372
396
  result_documents = (result.dig("documents") || []).flatten
373
397
  result_metadatas = (result.dig("metadatas") || []).flatten
374
398
  result_distances = (result.dig("distances") || []).flatten
data/lib/chroma/util.rb CHANGED
@@ -63,7 +63,7 @@ module Chroma
63
63
  def self.log_internal(message, data = {}, level:, logger:)
64
64
  data_str = data.reject { |_k, v| v.nil? }.map { |(k, v)| "#{k}=#{v}" }.join(" ")
65
65
 
66
- logger&.log(level, "message=#{message} #{data_str}".strip)
66
+ logger&.add(level, "message=#{message} #{data_str}".strip)
67
67
  end
68
68
  private_class_method :log_internal
69
69
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Chroma
4
- VERSION = "0.4.0"
4
+ VERSION = "0.6.0"
5
5
  end
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.4.0
4
+ version: 0.6.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: 2023-05-23 00:00:00.000000000 Z
11
+ date: 2023-06-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: dry-monads
@@ -25,7 +25,7 @@ dependencies:
25
25
  - !ruby/object:Gem::Version
26
26
  version: '1.6'
27
27
  - !ruby/object:Gem::Dependency
28
- name: ruby-next-core
28
+ name: ruby-next
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
31
  - - ">="