chromable 0.3.0 → 0.3.2

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: d33a705c79d42fb0495bb63e4ad7d04e8e3eea823400ea213f362af3e4ce09c8
4
- data.tar.gz: d18bc615f1a6b22530d76b06b471e07688be422a9af55a92d81419ae48eb4d84
3
+ metadata.gz: 27a945b9d1998457000926a7b02a95651e7093792d456d3c87de7d2c0afe64e8
4
+ data.tar.gz: 4608b17041ae86a4fbebb80c95433c5527373b7b53440a505aafb25ad32daeb8
5
5
  SHA512:
6
- metadata.gz: cfc65982c4f7952170630630eba0c6bf2bdc7708fae70727cb013d1cc1fe2b88b55e2bf08d9e0231864a0790373db2dfb1db95584c8ed328f0131c9e6a4c8d10
7
- data.tar.gz: 6a0b4a7af862ada818d888ed6ae672a23d85057726df9da40f3c715cd1c80fa71ebfac10e00833dd0cf9908b678d3746d3ead9d9cc6f73fc4ae23436a1e3a13e
6
+ metadata.gz: 36ecd1c084d1b9fa9272974009075fae3286de90a8939b14da614f0a5aa68146b555a207b52c0ae7ddba57769f9f7d894433168c6dec4761fa826a24db7c236e
7
+ data.tar.gz: 8800c6c0971741ea9e6bc29d1bff504795e233fe41ce7d6ca0a9553d9c838e021732e614527440cc7907e07e18d32b74518646f4d2ac802470db51a174df5f8d
data/README.md CHANGED
@@ -58,28 +58,32 @@ The only required option for `chromable` is `document:`.
58
58
 
59
59
  At this point, `chromable` will create, update, and destroy the ChromaDB embeddings for your objects based on Rails `after_save` and `after_destroy` callbacks.
60
60
 
61
- To interact with the ChromaDB collection, `chromable` provides `Model.query` method to query the collection and `Model.collection` method to access the collection directly.
61
+ To interact with the ChromaDB collection, `chromable` provides:
62
+ - `Model.query` to query the collection using similar API used in `chroma-db` gem, except for accepting `text:` instead of `query_embeddings:`. Extra arguments will be passed to the `embedder:` as `options`. Behind the scenes, `Model.query` will embed the given `text:`, then query the collection, and return the closest `results:` records.
63
+ - `Model.collection` to access the collection directly.
64
+ - `Model.delete_collection` to delete the entire collection.
62
65
 
63
66
  ```ruby
64
67
  puts Post.collection.count # Gets the number of documents inside the collection. Should always match Post.count.
65
68
 
66
69
  Post.query(
67
- query: params[:query],
70
+ text: params[:query],
68
71
  results: 20,
69
72
  where: chroma_search_filters,
70
73
  is_query: true # `is_query` here will be passed to `Post.embed` as an option.
71
74
  )
72
- ```
73
75
 
74
- `Model.query` accepts the same arguments accepted by `chroma-db` gem `query` method. Extra arguments will be passed to the `embedder:` as `options`. Behind the scenes, `Model.query` will embed the given `query:` text, then query the collection, and return the closest `results:` records.
76
+ Post.first.neighbors(results: 20) # => [#<Post:0x0000ffff9e0b5f10 id: "0beb0f98...", ...>, ...]
77
+ ```
75
78
 
76
79
  Also, `chromable` provides the following methods for each model instance:
77
80
 
78
81
  - `embedding`: Retrieves the instance's ChromaDB embedding object.
79
82
  - `upsert_embedding`: Creates or updates the instance's ChromaDB embedding object.
80
83
  - `destroy_embedding`: Destroys the instance's ChromaDB embedding object.
84
+ - `neighbors`: Gets the closest `results:` records to the current record.
81
85
 
82
- All these methods (including `Model.query` and `Model.collection`) are available with `chroma_` prefix, if you have similar methods defined in your model.
86
+ All these methods (including `Model.query`, `Model.collection`, and `Model.delete_collection`) are available with `chroma_` prefix, if you have similar methods defined in your model.
83
87
 
84
88
  ## Development
85
89
 
@@ -89,7 +93,7 @@ To install this gem onto your local machine, run `bundle install`. To release a
89
93
 
90
94
  ## Contributing
91
95
 
92
- Bug reports and pull requests are welcome on GitHub at https://github.com/AliOsm/chromable. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/chromable/blob/main/CODE_OF_CONDUCT.md).
96
+ Bug reports and pull requests are welcome on GitHub at https://github.com/AliOsm/chromable. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/AliOsm/chromable/blob/main/CODE_OF_CONDUCT.md).
93
97
 
94
98
  ## License
95
99
 
@@ -97,4 +101,4 @@ The gem is available as open source under the terms of the [MIT License](https:/
97
101
 
98
102
  ## Code of Conduct
99
103
 
100
- Everyone interacting in the Chromable project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/chromable/blob/main/CODE_OF_CONDUCT.md).
104
+ Everyone interacting in the Chromable project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/AliOsm/chromable/blob/main/CODE_OF_CONDUCT.md).
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Chromable
4
- VERSION = '0.3.0'
4
+ VERSION = '0.3.2'
5
5
  end
data/lib/chromable.rb CHANGED
@@ -51,20 +51,12 @@ module Chromable
51
51
  Chroma::Resources::Collection.delete(chromable_settings.collection_name)
52
52
  end
53
53
 
54
- def chroma_query( # rubocop:disable Metrics/ParameterLists
55
- text:,
56
- results: 10,
57
- where: {},
58
- where_document: {},
59
- include: %w[metadatas documents distances],
60
- **embedder_options
61
- )
54
+ def chroma_query(text:, results: 10, where: {}, where_document: {}, **embedder_options)
62
55
  find(chroma_collection.query(
63
56
  query_embeddings: [send(chromable_settings.embedder, text, **embedder_options)],
64
57
  results: results,
65
58
  where: where,
66
- where_document: where_document,
67
- include: include
59
+ where_document: where_document
68
60
  ).map(&:id))
69
61
  end
70
62
  end
@@ -77,6 +69,7 @@ module Chromable
77
69
  alias_method :embedding, :chroma_embedding unless method_defined? :embedding
78
70
  alias_method :upsert_embedding, :chroma_upsert_embedding unless method_defined? :upsert_embedding
79
71
  alias_method :destroy_embedding, :chroma_destroy_embedding unless method_defined? :destroy_embedding
72
+ alias_method :neighbors, :chroma_neighbors unless method_defined? :neighbors
80
73
  # rubocop:enable Style/Alias
81
74
  end
82
75
  end
@@ -93,6 +86,19 @@ module Chromable
93
86
  self.class.chroma_collection.delete(ids: [id])
94
87
  end
95
88
 
89
+ def chroma_neighbors(results: 10, where: {}, where_document: {})
90
+ collection = self.class.chroma_collection
91
+
92
+ embedding = collection.get(ids: [id], include: [:embeddings])[0].embedding
93
+
94
+ self.class.find(collection.query(
95
+ query_embeddings: [embedding],
96
+ results: results,
97
+ where: where,
98
+ where_document: where_document
99
+ ).map(&:id))
100
+ end
101
+
96
102
  private
97
103
 
98
104
  def build_embedding
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: chromable
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.3.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ali Hamdi Ali Fadel
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-12-12 00:00:00.000000000 Z
11
+ date: 2023-12-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: chroma-db