chroma-db 0.2.0 → 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/.rbnextrc +5 -0
- data/.ruby-version +1 -0
- data/CHANGELOG.md +8 -0
- data/Gemfile +2 -0
- data/Gemfile.lock +21 -0
- data/README.md +56 -18
- data/Rakefile +14 -0
- data/lib/.rbnext/2.7/chroma/api_operations/request.rb +139 -0
- data/lib/.rbnext/2.7/chroma/resources/collection.rb +389 -0
- data/lib/.rbnext/2.7/chroma/resources/database.rb +75 -0
- data/lib/.rbnext/3.1/chroma/api_operations/request.rb +130 -0
- data/lib/.rbnext/3.1/chroma/resources/collection.rb +385 -0
- data/lib/chroma/api_operations/request.rb +14 -9
- data/lib/chroma/resources/collection.rb +1 -0
- data/lib/chroma/resources/database.rb +2 -0
- data/lib/chroma/version.rb +1 -1
- data/lib/chroma-db.rb +13 -9
- metadata +39 -6
- data/notebook/Chroma Gem.ipynb +0 -851
- data/notebook/ruby.txt +0 -58
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5dd1f6255d588aae1829e27a9b6c75f33367fb131d22ed7aa6074aebe9f1a63e
|
4
|
+
data.tar.gz: 7b5d9c7bec11e80bcce275af7b2c1f4ea33965fd92a2955b8d835853d2ea5e2d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 453ba9818006fc4a28e56160919d1b85cd797c5f67de92a4b06c05a3a9edc15e33b32b1ef819fba96f6ee55a83f4557b3ff188892ec95b544406993e9d5ccc73
|
7
|
+
data.tar.gz: 05fa61d8ab158ec6e4f00b7f517dac756a4adba6a7ea1025d5d37f8a032de63613311157209b1888e32cd25c83783c8fbe8a6e8ffee98369826a1f5f58dc2c95
|
data/.rbnextrc
ADDED
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
3.2.2
|
data/CHANGELOG.md
CHANGED
@@ -1,5 +1,13 @@
|
|
1
1
|
## [Unreleased]
|
2
2
|
|
3
|
+
## [0.3.0] 2023-05-19
|
4
|
+
|
5
|
+
- Uses Ruby Next to transpile newer Ruby to older Ruby versions in order to support Ruby 2.7, 3.0, and 3.1
|
6
|
+
|
7
|
+
## [0.2.0] 2023-05-12
|
8
|
+
|
9
|
+
- Complete API to communicate with Chroma database via its API interface
|
10
|
+
|
3
11
|
## [0.1.0] - 2023-05-04
|
4
12
|
|
5
13
|
- Initial release
|
data/Gemfile
CHANGED
data/Gemfile.lock
CHANGED
@@ -3,6 +3,7 @@ PATH
|
|
3
3
|
specs:
|
4
4
|
chroma-db (0.2.0)
|
5
5
|
dry-monads (~> 1.6)
|
6
|
+
ruby-next (>= 0.15.0)
|
6
7
|
|
7
8
|
GEM
|
8
9
|
remote: https://rubygems.org/
|
@@ -13,6 +14,7 @@ GEM
|
|
13
14
|
concurrent-ruby (1.2.2)
|
14
15
|
crack (0.4.5)
|
15
16
|
rexml
|
17
|
+
diff-lcs (1.5.0)
|
16
18
|
dry-core (1.0.0)
|
17
19
|
concurrent-ruby (~> 1.0)
|
18
20
|
zeitwerk (~> 2.6)
|
@@ -28,10 +30,14 @@ GEM
|
|
28
30
|
parallel (1.23.0)
|
29
31
|
parser (3.2.2.1)
|
30
32
|
ast (~> 2.4.1)
|
33
|
+
psych (5.1.0)
|
34
|
+
stringio
|
31
35
|
public_suffix (5.0.1)
|
32
36
|
rainbow (3.1.1)
|
33
37
|
rake (13.0.6)
|
34
38
|
rbs (3.1.0)
|
39
|
+
rdoc (6.5.0)
|
40
|
+
psych (>= 4.0.0)
|
35
41
|
regexp_parser (2.8.0)
|
36
42
|
rexml (3.2.5)
|
37
43
|
rubocop (1.50.2)
|
@@ -49,7 +55,16 @@ GEM
|
|
49
55
|
rubocop-performance (1.16.0)
|
50
56
|
rubocop (>= 1.7.0, < 2.0)
|
51
57
|
rubocop-ast (>= 0.4.0)
|
58
|
+
ruby-next (0.15.3)
|
59
|
+
ruby-next-core (= 0.15.3)
|
60
|
+
ruby-next-parser (>= 3.1.1.0)
|
61
|
+
unparser (~> 0.6.0)
|
62
|
+
ruby-next-core (0.15.3)
|
63
|
+
ruby-next-parser (3.1.1.3)
|
64
|
+
parser (>= 3.0.3.1)
|
52
65
|
ruby-progressbar (1.13.0)
|
66
|
+
sdoc (2.6.1)
|
67
|
+
rdoc (>= 5.0)
|
53
68
|
standard (1.28.2)
|
54
69
|
language_server-protocol (~> 3.17.0.2)
|
55
70
|
lint_roller (~> 1.0)
|
@@ -61,7 +76,11 @@ GEM
|
|
61
76
|
standard-performance (1.0.1)
|
62
77
|
lint_roller (~> 1.0)
|
63
78
|
rubocop-performance (~> 1.16.0)
|
79
|
+
stringio (3.0.6)
|
64
80
|
unicode-display_width (2.4.2)
|
81
|
+
unparser (0.6.7)
|
82
|
+
diff-lcs (~> 1.3)
|
83
|
+
parser (>= 3.2.0)
|
65
84
|
webmock (3.18.1)
|
66
85
|
addressable (>= 2.8.0)
|
67
86
|
crack (>= 0.3.2)
|
@@ -77,6 +96,8 @@ DEPENDENCIES
|
|
77
96
|
minitest (~> 5.0)
|
78
97
|
rake (~> 13.0)
|
79
98
|
rbs (~> 3.1)
|
99
|
+
ruby-next (>= 0.15.0)
|
100
|
+
sdoc (~> 2.6, >= 2.6.1)
|
80
101
|
standard (~> 1.3)
|
81
102
|
webmock (~> 3.18, >= 3.18.1)
|
82
103
|
|
data/README.md
CHANGED
@@ -1,39 +1,77 @@
|
|
1
|
-
# Chroma
|
1
|
+
# Chroma's Ruby client
|
2
2
|
|
3
|
-
|
3
|
+
Chroma is the open-source embedding database. Chroma makes it easy to build LLM apps by making knowledge, facts, and skills pluggable for LLMs.
|
4
4
|
|
5
|
-
|
5
|
+
This Ruby gem is a client to connect to Chroma's database via its API.
|
6
6
|
|
7
|
-
|
7
|
+
Find more information about Chroma on how to install at their website. [https://www.trychroma.com/](https://www.trychroma.com/)
|
8
|
+
|
9
|
+
## Description
|
10
|
+
Chroma-rb is a Ruby client for Chroma Database. It works with version
|
11
|
+
0.3.22 or better.
|
12
|
+
|
13
|
+
A small example usage
|
14
|
+
|
15
|
+
```ruby
|
16
|
+
require "logger"
|
17
|
+
|
18
|
+
# Requiere Chroma Ruby client.
|
19
|
+
require "chroma-db"
|
20
|
+
|
21
|
+
# Configure Chroma's host. Here you can specify your own host.
|
22
|
+
Chroma.connect_host = "http://localhost:8000"
|
23
|
+
Chroma.logger = Logger.new($stdout)
|
24
|
+
Chroma.log_level = Chroma::LEVEL_ERROR
|
25
|
+
|
26
|
+
# Check current Chrome server version
|
27
|
+
version = Chroma::Resources::Database.version
|
28
|
+
puts version
|
8
29
|
|
9
|
-
|
30
|
+
# Create a new collection
|
31
|
+
collection = Chroma::Resources::Collection.create(collection_name, {lang: "ruby", gem: "chroma-db"})
|
10
32
|
|
11
|
-
|
33
|
+
# Add embeddings
|
34
|
+
embeddings = [
|
35
|
+
Chroma::Resources::Embedding.new(id: "1", embedding: [1.3, 2.6, 3.1], metadata: {client: "chroma-rb"}, document: "ruby"),
|
36
|
+
Chroma::Resources::Embedding.new(id: "2", embedding: [3.7, 2.8, 0.9], metadata: {client: "chroma-rb"}, document: "rails")
|
37
|
+
]
|
38
|
+
collection.add(embeddings)
|
39
|
+
```
|
12
40
|
|
13
|
-
|
41
|
+
For a complete example, please refer to the Jupyter Noterbook [Chroma gem](https://github.com/mariochavez/chroma/blob/main/notebook/Chroma%20Gem.ipynb)
|
42
|
+
|
43
|
+
## Requirements
|
44
|
+
- Ruby 2.7.8 or newer
|
45
|
+
- Chroma Database 0.3.22 or later running as a client/server model.
|
46
|
+
|
47
|
+
## Installation
|
48
|
+
To install the gem and add to the application's Gemfile, execute:
|
49
|
+
|
50
|
+
$ bundle add chroma-db
|
14
51
|
|
15
52
|
If bundler is not being used to manage dependencies, install the gem by executing:
|
16
53
|
|
17
|
-
$ gem install
|
54
|
+
$ gem install chroma-db
|
18
55
|
|
19
|
-
##
|
56
|
+
## Use the Jupyter notebook
|
57
|
+
To use the Jupyter Noterbook [Chroma gem](https://github.com/mariochavez/chroma/blob/main/notebook/Chroma%20Gem.ipynb) in this repository, please install python 3.9 or better, iruby and Jupyter notebook dependencies:
|
20
58
|
|
21
|
-
|
59
|
+
$ pip install jupyterlab notebook ipywidgets
|
60
|
+
$ gem install iruby
|
61
|
+
$ iruby register --force
|
22
62
|
|
23
|
-
## Development
|
63
|
+
## Development
|
64
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
24
65
|
|
25
|
-
|
66
|
+
To install this gem onto your local machine, run `bundle exec rake install`.
|
26
67
|
|
27
|
-
To
|
68
|
+
To generate Rdoc documentation for the gem, run `bundle exec rake rdoc`.
|
28
69
|
|
29
70
|
## Contributing
|
30
|
-
|
31
|
-
Bug reports and pull requests are welcome on GitHub at https://github.com/mariochavez/chroma. 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/mariochavez/chroma/blob/main/CODE_OF_CONDUCT.md).
|
71
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/mariochavez/chroma. 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/mariochavez/chroma/blob/main/CODE_OF_CONDUCT.md).
|
32
72
|
|
33
73
|
## License
|
34
|
-
|
35
|
-
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
74
|
+
The gem is available as open source under the terms of the [MIT License](https://github.com/mariochavez/chroma/blob/main/LICENSE.txt).
|
36
75
|
|
37
76
|
## Code of Conduct
|
38
|
-
|
39
77
|
Everyone interacting in the Chroma project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/mariochavez/chroma/blob/main/CODE_OF_CONDUCT.md).
|
data/Rakefile
CHANGED
@@ -12,3 +12,17 @@ end
|
|
12
12
|
require "standard/rake"
|
13
13
|
|
14
14
|
task default: %i[test standard]
|
15
|
+
|
16
|
+
require "sdoc" # and use your RDoc task the same way you used it before
|
17
|
+
require "rdoc/task" # ensure this file is also required in order to use `RDoc::Task`
|
18
|
+
|
19
|
+
RDoc::Task.new do |rdoc|
|
20
|
+
rdoc.rdoc_dir = "doc/rdoc" # name of output directory
|
21
|
+
rdoc.options << "--format=sdoc" # explictly set the sdoc generator
|
22
|
+
rdoc.template = "rails" # template used on api.rubyonrails.org
|
23
|
+
end
|
24
|
+
|
25
|
+
desc "Run Ruby Next nextify"
|
26
|
+
task :nextify do
|
27
|
+
sh "bundle exec ruby-next nextify -V"
|
28
|
+
end
|
@@ -0,0 +1,139 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Chroma
|
4
|
+
module APIOperations
|
5
|
+
# using RubyNext
|
6
|
+
|
7
|
+
# Request's response Data object.
|
8
|
+
#
|
9
|
+
# status - HTTP status code. It is zero when a request fails due to network error.
|
10
|
+
# body - Parsed JSON object or response body.
|
11
|
+
# headers - HTTP response headers.
|
12
|
+
# error - Exception or Net::HTTPResponse object if the response is not Net::HTTPSuccess
|
13
|
+
#
|
14
|
+
# NOTE: Not supported yet by Ruby Next
|
15
|
+
# Response = Data.define(:status, :body, :headers, :error)
|
16
|
+
Response = Struct.new("Response", :status, :body, :headers, :error)
|
17
|
+
|
18
|
+
# Request module provides functionality to perform HTTP requests.
|
19
|
+
module Request
|
20
|
+
module ClassMethods
|
21
|
+
include Dry::Monads[:result]
|
22
|
+
|
23
|
+
# Execute an HTTP request and return a monad wrapping the response.
|
24
|
+
#
|
25
|
+
# method - The HTTP method to use (e.g. 'GET', 'POST'). Method must be a `Symbol`.
|
26
|
+
# url - The URL to send the request to.
|
27
|
+
# params - The query parameters or request body. Params needs to be in a form of a Hash.
|
28
|
+
# options - Additional options to pass to the request.
|
29
|
+
#
|
30
|
+
# A `Dry::Monads::Result` monad wrapping the response, either a success or failure.
|
31
|
+
# The response is a `Chroma::APIOperations::Response` Data object.
|
32
|
+
#
|
33
|
+
# Examples
|
34
|
+
#
|
35
|
+
# result = execute_request(:get, "https://example.com", {name: "test request"})
|
36
|
+
# if result.success?
|
37
|
+
# puts "Response status: #{result.success.status}"
|
38
|
+
# puts "Response body: #{result.success.body}"
|
39
|
+
# else
|
40
|
+
# puts "Request failed with error: #{result.failure.error}"
|
41
|
+
# end
|
42
|
+
def execute_request(method, url, params = {}, options = {})
|
43
|
+
uri = URI.parse(url)
|
44
|
+
|
45
|
+
request = build_request(method, uri, params)
|
46
|
+
|
47
|
+
use_ssl = options.delete(:use_ssl) || false
|
48
|
+
response = Net::HTTP.start(uri.hostname, uri.port, use_ssl: use_ssl) do |http|
|
49
|
+
Chroma::Util.log_debug("Sending a request", {method: method, uri: uri, params: params})
|
50
|
+
http.request(request)
|
51
|
+
end
|
52
|
+
|
53
|
+
build_response(response)
|
54
|
+
rescue => ex
|
55
|
+
build_response(ex)
|
56
|
+
end
|
57
|
+
|
58
|
+
private def build_response(response)
|
59
|
+
puts "BUILDING RESPONSE OLD RUBY"
|
60
|
+
case; when ((__m__ = response)) && false
|
61
|
+
when (((success_response,) = nil) || ((Net::HTTPSuccess === __m__) && ((success_response = __m__) || true)))
|
62
|
+
|
63
|
+
|
64
|
+
Chroma::Util.log_info("Successful response", code: success_response.code)
|
65
|
+
|
66
|
+
build_response_details(success_response)
|
67
|
+
when (((redirect_response,) = nil) || ((Net::HTTPRedirection === __m__) && ((redirect_response = __m__) || true)))
|
68
|
+
|
69
|
+
|
70
|
+
Chroma::Util.log_info("Server redirect response", code: redirect_response.code, location: redirect_response["location"])
|
71
|
+
|
72
|
+
build_response_details(redirect_response)
|
73
|
+
when (((client_error_response,) = nil) || ((Net::HTTPClientError === __m__) && ((client_error_response = __m__) || true)))
|
74
|
+
|
75
|
+
|
76
|
+
Chroma::Util.log_error("Client error response", code: client_error_response.code, body: client_error_response.body)
|
77
|
+
|
78
|
+
build_response_details(client_error_response)
|
79
|
+
when (((server_error_response,) = nil) || ((Net::HTTPServerError === __m__) && ((server_error_response = __m__) || true)))
|
80
|
+
|
81
|
+
|
82
|
+
Chroma::Util.log_error("Server error response", code: server_error_response.code)
|
83
|
+
|
84
|
+
build_response_details(server_error_response, parse_body: false)
|
85
|
+
else
|
86
|
+
Chroma::Util.log_error("An error happened", error: response.to_s)
|
87
|
+
|
88
|
+
build_response_details(response, exception: true, parse_body: false)
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
92
|
+
private def build_response_details(response, exception: false, parse_body: true)
|
93
|
+
response_data = Chroma::APIOperations::Response.new(
|
94
|
+
exception ? 0 : response.code.to_i,
|
95
|
+
if exception
|
96
|
+
exception.to_s
|
97
|
+
else
|
98
|
+
(parse_body ? body_to_json(response.body) : response.body)
|
99
|
+
end,
|
100
|
+
exception ? {} : response.each_header.to_h,
|
101
|
+
response.is_a?(Net::HTTPSuccess) ? nil : response
|
102
|
+
)
|
103
|
+
|
104
|
+
case; when ((__m__ = response)) && false
|
105
|
+
when (Net::HTTPSuccess === __m__)
|
106
|
+
return Success(response_data)
|
107
|
+
else
|
108
|
+
return Failure(response_data)
|
109
|
+
end
|
110
|
+
end
|
111
|
+
|
112
|
+
private def body_to_json(content)
|
113
|
+
JSON.parse(content, symbolize_keys: true)
|
114
|
+
rescue JSON::ParserError, TypeError
|
115
|
+
content
|
116
|
+
end
|
117
|
+
|
118
|
+
private def build_request(method, uri, params)
|
119
|
+
request = case method
|
120
|
+
when :post then Net::HTTP::Post.new(uri)
|
121
|
+
when :put then Net::HTTP::Put.new(uri)
|
122
|
+
when :delete then Net::HTTP::Delete.new(uri)
|
123
|
+
else
|
124
|
+
Net::HTTP::Get.new(uri)
|
125
|
+
end
|
126
|
+
|
127
|
+
request.content_type = "application/json"
|
128
|
+
request.body = params.to_json if params.size > 0
|
129
|
+
|
130
|
+
request
|
131
|
+
end
|
132
|
+
end
|
133
|
+
|
134
|
+
def self.included(base)
|
135
|
+
base.extend(ClassMethods)
|
136
|
+
end
|
137
|
+
end
|
138
|
+
end
|
139
|
+
end
|