chromable 0.3.0 → 0.3.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +11 -7
- data/lib/chromable/version.rb +1 -1
- data/lib/chromable.rb +16 -8
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ad740d4c1d70642aa0e03518c74cb5e2b77a68df923a9e46e0797eb42908c312
|
4
|
+
data.tar.gz: 4cdb45e1599c5f579082cac30ff9c3887adaee9921d2bf73f20b6f96401769cd
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 19ee5d2e13a7b61962442766faa52f52930acc221b346ac91609eab01c1c9cb6aad01bfd96c444451925434c39ede78d7b949923a0824ef7ae89276b11c49869
|
7
|
+
data.tar.gz: 8de31a2f2b16402acef0d391e39a1049622dcbc3a69968ce0e8485492e1acbb7dae77ab88ae782bc1ef07ae42a43102929e5a47791a8c5443dd67f758e054c4b
|
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
|
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
|
-
|
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
|
-
|
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.
|
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/
|
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/
|
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).
|
data/lib/chromable/version.rb
CHANGED
data/lib/chromable.rb
CHANGED
@@ -51,14 +51,7 @@ module Chromable
|
|
51
51
|
Chroma::Resources::Collection.delete(chromable_settings.collection_name)
|
52
52
|
end
|
53
53
|
|
54
|
-
def chroma_query(
|
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,
|
@@ -77,6 +70,7 @@ module Chromable
|
|
77
70
|
alias_method :embedding, :chroma_embedding unless method_defined? :embedding
|
78
71
|
alias_method :upsert_embedding, :chroma_upsert_embedding unless method_defined? :upsert_embedding
|
79
72
|
alias_method :destroy_embedding, :chroma_destroy_embedding unless method_defined? :destroy_embedding
|
73
|
+
alias_method :neighbors, :chroma_neighbors unless method_defined? :neighbors
|
80
74
|
# rubocop:enable Style/Alias
|
81
75
|
end
|
82
76
|
end
|
@@ -93,6 +87,20 @@ module Chromable
|
|
93
87
|
self.class.chroma_collection.delete(ids: [id])
|
94
88
|
end
|
95
89
|
|
90
|
+
def chroma_neighbors(results: 10, where: {}, where_document: {})
|
91
|
+
collection = self.class.chroma_collection
|
92
|
+
|
93
|
+
embedding = collection.get(ids: [id], include: [:embeddings])[0].embedding
|
94
|
+
|
95
|
+
self.class.find(collection.query(
|
96
|
+
query_embeddings: [embedding],
|
97
|
+
results: results,
|
98
|
+
where: where,
|
99
|
+
where_document: where_document,
|
100
|
+
include: include
|
101
|
+
).map(&:id))
|
102
|
+
end
|
103
|
+
|
96
104
|
private
|
97
105
|
|
98
106
|
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.
|
4
|
+
version: 0.3.1
|
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-
|
11
|
+
date: 2023-12-13 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: chroma-db
|