chromable 0.1.5 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +1 -1
- data/README.md +33 -10
- data/lib/chromable/version.rb +1 -1
- data/lib/chromable.rb +93 -29
- 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: d33a705c79d42fb0495bb63e4ad7d04e8e3eea823400ea213f362af3e4ce09c8
|
4
|
+
data.tar.gz: d18bc615f1a6b22530d76b06b471e07688be422a9af55a92d81419ae48eb4d84
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: cfc65982c4f7952170630630eba0c6bf2bdc7708fae70727cb013d1cc1fe2b88b55e2bf08d9e0231864a0790373db2dfb1db95584c8ed328f0131c9e6a4c8d10
|
7
|
+
data.tar.gz: 6a0b4a7af862ada818d888ed6ae672a23d85057726df9da40f3c715cd1c80fa71ebfac10e00833dd0cf9908b678d3746d3ead9d9cc6f73fc4ae23436a1e3a13e
|
data/.rubocop.yml
CHANGED
data/README.md
CHANGED
@@ -2,6 +2,8 @@
|
|
2
2
|
|
3
3
|
Ruby on Rails integration for ChromaDB based on `chroma-db` gem.
|
4
4
|
|
5
|
+
`chromable` were tested with Ruby 3.2.2 and Rails 7.1.2.
|
6
|
+
|
5
7
|
## Installation
|
6
8
|
|
7
9
|
Install `chromable` and add it to the application's Gemfile by executing:
|
@@ -30,33 +32,54 @@ Then, include `Chromable` module in your model and initialize it:
|
|
30
32
|
class Post < ApplicationRecord
|
31
33
|
include Chromable
|
32
34
|
|
33
|
-
chromable document: :content, metadata: %i[author category], embedder: :embed
|
35
|
+
chromable document: :content, metadata: %i[author category], embedder: :embed, keep_document: false
|
36
|
+
|
37
|
+
def self.embed(text, **options)
|
38
|
+
options[:is_query] ||= false
|
34
39
|
|
35
|
-
|
36
|
-
|
40
|
+
if options[:is_query]
|
41
|
+
# Call OpenAI API to embed `text` as a search query.
|
42
|
+
else
|
43
|
+
# Call OpenAI API to embed `text` as a post content.
|
44
|
+
end
|
37
45
|
end
|
38
46
|
end
|
39
47
|
```
|
40
48
|
|
41
49
|
Where:
|
42
|
-
- `document:` is a callable represents the text content you want to embed and store in ChromaDB.
|
43
|
-
- `metadata:` is
|
44
|
-
- `embedder:` is a callable returns the embedding representation for the
|
50
|
+
- `document:` is a callable represents the text content you want to embed and store in ChromaDB (e.g. Could be a model attribute).
|
51
|
+
- `metadata:` is a list of callables to be evaluated and passed to ChromaDB as metadata to be used to filter (e.g. Could be an instance method).
|
52
|
+
- `embedder:` is a callable defined at the model level that returns the embedding representation for the given `text` based on some `options`.
|
53
|
+
- `keep_document:` tells chromable to pass the `document:` to ChromaDB and save it or not. It is useful if you just want to have the embeddings in ChromaDB and the rest of the data in your Rails application database to reduce memory footprint. `keep_document:` is `true` by default.
|
45
54
|
|
46
55
|
Optionaly you can pass `collection_name:`. If not passed, the plural form of the model name will be used.
|
47
56
|
|
48
|
-
|
57
|
+
The only required option for `chromable` is `document:`.
|
58
|
+
|
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
|
+
|
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.
|
62
|
+
|
63
|
+
```ruby
|
64
|
+
puts Post.collection.count # Gets the number of documents inside the collection. Should always match Post.count.
|
65
|
+
|
66
|
+
Post.query(
|
67
|
+
query: params[:query],
|
68
|
+
results: 20,
|
69
|
+
where: chroma_search_filters,
|
70
|
+
is_query: true # `is_query` here will be passed to `Post.embed` as an option.
|
71
|
+
)
|
72
|
+
```
|
49
73
|
|
50
|
-
|
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.
|
51
75
|
|
52
|
-
To interact with the ChromaDB collection, `chromable` provides `Model.collection` method to retrieve the collection instance.
|
53
76
|
Also, `chromable` provides the following methods for each model instance:
|
54
77
|
|
55
78
|
- `embedding`: Retrieves the instance's ChromaDB embedding object.
|
56
79
|
- `upsert_embedding`: Creates or updates the instance's ChromaDB embedding object.
|
57
80
|
- `destroy_embedding`: Destroys the instance's ChromaDB embedding object.
|
58
81
|
|
59
|
-
All these methods (including `Model.collection`) are available with `chroma_` prefix, if you have similar methods defined in your model.
|
82
|
+
All these methods (including `Model.query` and `Model.collection`) are available with `chroma_` prefix, if you have similar methods defined in your model.
|
60
83
|
|
61
84
|
## Development
|
62
85
|
|
data/lib/chromable/version.rb
CHANGED
data/lib/chromable.rb
CHANGED
@@ -4,52 +4,116 @@ require_relative 'chromable/version'
|
|
4
4
|
|
5
5
|
# Ruby on Rails integration for ChromaDB.
|
6
6
|
module Chromable
|
7
|
-
def self.included(base)
|
7
|
+
def self.included(base)
|
8
8
|
base.extend ClassMethods
|
9
|
-
base.
|
10
|
-
|
11
|
-
base.class_attribute :metadata
|
12
|
-
base.class_attribute :embedder
|
13
|
-
|
9
|
+
base.include InstanceMethods
|
10
|
+
|
14
11
|
base.after_save :chroma_upsert_embedding
|
15
12
|
base.after_destroy :chroma_destroy_embedding
|
16
13
|
end
|
17
14
|
|
15
|
+
# Chromable settings class to hide them from Rails models.
|
16
|
+
class Settings
|
17
|
+
attr_accessor :document, :collection_name, :metadata, :embedder, :keep_document
|
18
|
+
|
19
|
+
def initialize(document:, metadata: nil, embedder: nil, collection_name: nil, keep_document: true)
|
20
|
+
@collection_name = collection_name
|
21
|
+
@document = document
|
22
|
+
@metadata = metadata
|
23
|
+
@embedder = embedder
|
24
|
+
@keep_document = keep_document
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
# Methods to be added to the model class.
|
18
29
|
module ClassMethods
|
19
|
-
def
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
30
|
+
def self.extended(base)
|
31
|
+
class << base
|
32
|
+
alias_method :collection, :chroma_collection unless method_defined? :collection
|
33
|
+
alias_method :delete_collection, :chroma_delete_collection unless method_defined? :delete_collection
|
34
|
+
alias_method :query, :chroma_query unless method_defined? :query
|
35
|
+
end
|
36
|
+
|
37
|
+
base.cattr_accessor :chromable_settings
|
38
|
+
end
|
39
|
+
|
40
|
+
def chromable(**options)
|
41
|
+
options[:collection_name] ||= name.underscore.pluralize
|
42
|
+
|
43
|
+
self.chromable_settings = Settings.new(**options)
|
24
44
|
end
|
25
45
|
|
26
46
|
def chroma_collection
|
27
|
-
Chroma::Resources::Collection.get_or_create(collection_name)
|
47
|
+
Chroma::Resources::Collection.get_or_create(chromable_settings.collection_name)
|
28
48
|
end
|
29
49
|
|
30
|
-
|
31
|
-
|
50
|
+
def chroma_delete_collection
|
51
|
+
Chroma::Resources::Collection.delete(chromable_settings.collection_name)
|
52
|
+
end
|
32
53
|
|
33
|
-
|
34
|
-
|
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
|
+
)
|
62
|
+
find(chroma_collection.query(
|
63
|
+
query_embeddings: [send(chromable_settings.embedder, text, **embedder_options)],
|
64
|
+
results: results,
|
65
|
+
where: where,
|
66
|
+
where_document: where_document,
|
67
|
+
include: include
|
68
|
+
).map(&:id))
|
69
|
+
end
|
35
70
|
end
|
36
71
|
|
37
|
-
|
38
|
-
|
72
|
+
# Methods to be added to the model instances.
|
73
|
+
module InstanceMethods
|
74
|
+
def self.included(base)
|
75
|
+
base.instance_eval do
|
76
|
+
# rubocop:disable Style/Alias
|
77
|
+
alias_method :embedding, :chroma_embedding unless method_defined? :embedding
|
78
|
+
alias_method :upsert_embedding, :chroma_upsert_embedding unless method_defined? :upsert_embedding
|
79
|
+
alias_method :destroy_embedding, :chroma_destroy_embedding unless method_defined? :destroy_embedding
|
80
|
+
# rubocop:enable Style/Alias
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
def chroma_embedding
|
85
|
+
self.class.chroma_collection.get(ids: [id])[0]
|
86
|
+
end
|
87
|
+
|
88
|
+
def chroma_upsert_embedding
|
89
|
+
self.class.chroma_collection.upsert(build_embedding)
|
90
|
+
end
|
91
|
+
|
92
|
+
def chroma_destroy_embedding
|
93
|
+
self.class.chroma_collection.delete(ids: [id])
|
94
|
+
end
|
95
|
+
|
96
|
+
private
|
97
|
+
|
98
|
+
def build_embedding
|
39
99
|
Chroma::Resources::Embedding.new(
|
40
100
|
id: id,
|
41
|
-
document:
|
42
|
-
embedding:
|
43
|
-
metadata:
|
101
|
+
document: document_to_embed,
|
102
|
+
embedding: document_embedding,
|
103
|
+
metadata: embedding_metadata
|
44
104
|
)
|
45
|
-
|
46
|
-
end
|
105
|
+
end
|
47
106
|
|
48
|
-
|
49
|
-
|
50
|
-
|
107
|
+
def document_to_embed
|
108
|
+
chromable_settings.keep_document ? send(chromable_settings.document) : nil
|
109
|
+
end
|
51
110
|
|
52
|
-
|
53
|
-
|
54
|
-
|
111
|
+
def document_embedding
|
112
|
+
chromable_settings.embedder && self.class.send(chromable_settings.embedder, send(chromable_settings.document))
|
113
|
+
end
|
114
|
+
|
115
|
+
def embedding_metadata
|
116
|
+
chromable_settings.metadata&.index_with { |attribute| send(attribute) }
|
117
|
+
end
|
118
|
+
end
|
55
119
|
end
|
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.
|
4
|
+
version: 0.3.0
|
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-12 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: chroma-db
|