diff --git a/.circleci/config.yml b/.circleci/config.yml new file mode 100644 index 0000000..9bd46a7 --- /dev/null +++ b/.circleci/config.yml @@ -0,0 +1,13 @@ +version: 2.1 +jobs: + build: + docker: + - image: ruby:3.0.0 + steps: + - checkout + - run: + name: Run the default task + command: | + gem install bundler -v 2.4.0 + bundle install + bundle exec rake diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b04a8c8 --- /dev/null +++ b/.gitignore @@ -0,0 +1,11 @@ +/.bundle/ +/.yardoc +/_yardoc/ +/coverage/ +/doc/ +/pkg/ +/spec/reports/ +/tmp/ + +# rspec failure tracking +.rspec_status diff --git a/.rspec b/.rspec new file mode 100644 index 0000000..34c5164 --- /dev/null +++ b/.rspec @@ -0,0 +1,3 @@ +--format documentation +--color +--require spec_helper diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..2f48a89 --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,5 @@ +## [Unreleased] + +## [0.1.0] - 2023-03-24 + +- Initial release diff --git a/Gemfile b/Gemfile new file mode 100644 index 0000000..58915b3 --- /dev/null +++ b/Gemfile @@ -0,0 +1,15 @@ +# frozen_string_literal: true + +source "https://rubygems.org" + +# Specify your gem's dependencies in weaviate.gemspec +gemspec + +gem "rake", "~> 13.0" + +gem "rspec", "~> 3.0" +gem "standard", "~> 1.25.0" + +group :test do + gem "webmock", "~> 3.0" +end diff --git a/Gemfile.lock b/Gemfile.lock new file mode 100644 index 0000000..a57ae95 --- /dev/null +++ b/Gemfile.lock @@ -0,0 +1,92 @@ +PATH + remote: . + specs: + weaviate (0.1.0) + faraday (~> 2.7) + pry-byebug (~> 3.9) + +GEM + remote: https://rubygems.org/ + specs: + addressable (2.8.1) + public_suffix (>= 2.0.2, < 6.0) + ast (2.4.2) + byebug (11.1.3) + coderay (1.1.3) + crack (0.4.5) + rexml + diff-lcs (1.5.0) + faraday (2.7.4) + faraday-net_http (>= 2.0, < 3.1) + ruby2_keywords (>= 0.0.4) + faraday-net_http (3.0.2) + hashdiff (1.0.1) + json (2.6.3) + language_server-protocol (3.17.0.3) + method_source (1.0.0) + parallel (1.22.1) + parser (3.2.1.1) + ast (~> 2.4.1) + pry (0.14.2) + coderay (~> 1.1) + method_source (~> 1.0) + pry-byebug (3.10.1) + byebug (~> 11.0) + pry (>= 0.13, < 0.15) + public_suffix (5.0.1) + rainbow (3.1.1) + rake (13.0.6) + regexp_parser (2.7.0) + rexml (3.2.5) + rspec (3.11.0) + rspec-core (~> 3.11.0) + rspec-expectations (~> 3.11.0) + rspec-mocks (~> 3.11.0) + rspec-core (3.11.0) + rspec-support (~> 3.11.0) + rspec-expectations (3.11.0) + diff-lcs (>= 1.2.0, < 2.0) + rspec-support (~> 3.11.0) + rspec-mocks (3.11.1) + diff-lcs (>= 1.2.0, < 2.0) + rspec-support (~> 3.11.0) + rspec-support (3.11.0) + rubocop (1.48.1) + json (~> 2.3) + parallel (~> 1.10) + parser (>= 3.2.0.0) + rainbow (>= 2.2.2, < 4.0) + regexp_parser (>= 1.8, < 3.0) + rexml (>= 3.2.5, < 4.0) + rubocop-ast (>= 1.26.0, < 2.0) + ruby-progressbar (~> 1.7) + unicode-display_width (>= 2.4.0, < 3.0) + rubocop-ast (1.28.0) + parser (>= 3.2.1.0) + rubocop-performance (1.16.0) + rubocop (>= 1.7.0, < 2.0) + rubocop-ast (>= 0.4.0) + ruby-progressbar (1.13.0) + ruby2_keywords (0.0.5) + standard (1.25.3) + language_server-protocol (~> 3.17.0.2) + rubocop (~> 1.48.1) + rubocop-performance (~> 1.16.0) + unicode-display_width (2.4.2) + webmock (3.18.1) + addressable (>= 2.8.0) + crack (>= 0.3.2) + hashdiff (>= 0.4.0, < 2.0.0) + +PLATFORMS + x86_64-darwin-19 + +DEPENDENCIES + rake (~> 13.0) + rspec (~> 3.0) + standard (~> 1.25.0) + weaviate! + webmock (~> 3.0) + +BUNDLED WITH + 2.4.0 diff --git a/LICENSE.txt b/LICENSE.txt new file mode 100644 index 0000000..3666e67 --- /dev/null +++ b/LICENSE.txt @@ -0,0 +1,21 @@ +The MIT License (MIT) + +Copyright (c) 2023 Andrei Bondarev + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..1c63b22 --- /dev/null +++ b/README.md @@ -0,0 +1,64 @@ +# Weaviate + +TODO: Delete this and the text below, and describe your gem + +Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/weaviate`. To experiment with that code, run `bin/console` for an interactive prompt. + +## Installation + +TODO: Replace `UPDATE_WITH_YOUR_GEM_NAME_PRIOR_TO_RELEASE_TO_RUBYGEMS_ORG` with your gem name right after releasing it to RubyGems.org. Please do not do it earlier due to security reasons. Alternatively, replace this section with instructions to install your gem from git if you don't plan to release to RubyGems.org. + +Install the gem and add to the application's Gemfile by executing: + + $ bundle add UPDATE_WITH_YOUR_GEM_NAME_PRIOR_TO_RELEASE_TO_RUBYGEMS_ORG + +If bundler is not being used to manage dependencies, install the gem by executing: + + $ gem install UPDATE_WITH_YOUR_GEM_NAME_PRIOR_TO_RELEASE_TO_RUBYGEMS_ORG + +## Usage + +```ruby +client = Weaviate::Client.new( + scheme: 'https', + host: 'some-endpoint.weaviate.network', // Replace with your endpoint +) + +client.schema.get() + +class_obj = { + "class": "Article", + "description": "A written text, for example a news article or blog post", + "properties": [ + { + "dataType": [ + "string" + ], + "description": "Title of the article", + "name": "title", + }, + { + "dataType": [ + "text" + ], + "description": "The content of the article", + "name": "content" + } + ] +} +client.schema.create_class(class_obj) +``` + +## Development + +After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment. + +To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org). + +## Contributing + +Bug reports and pull requests are welcome on GitHub at https://github.com/andreibondarev/weaviate. + +## License + +The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT). diff --git a/Rakefile b/Rakefile new file mode 100644 index 0000000..b6ae734 --- /dev/null +++ b/Rakefile @@ -0,0 +1,8 @@ +# frozen_string_literal: true + +require "bundler/gem_tasks" +require "rspec/core/rake_task" + +RSpec::Core::RakeTask.new(:spec) + +task default: :spec diff --git a/bin/console b/bin/console new file mode 100755 index 0000000..ddfd0dc --- /dev/null +++ b/bin/console @@ -0,0 +1,22 @@ +#!/usr/bin/env ruby +# frozen_string_literal: true + +require "bundler/setup" +require "weaviate" + +# You can add fixtures and/or initialization code here to make experimenting +# with your gem easier. You can also use a different console, if you like. + +# (If you use this, don't forget to add pry to your Gemfile!) +# require "pry" +# Pry.start + +client = Weaviate::Client.new( + scheme: "http", + host: ENV["WEAVIATE_HOST"], + model_service: :openai, + model_service_api_key: ENV["MODEL_SERVICE_API_KEY"] +) + +require "irb" +IRB.start(__FILE__) diff --git a/bin/setup b/bin/setup new file mode 100755 index 0000000..dce67d8 --- /dev/null +++ b/bin/setup @@ -0,0 +1,8 @@ +#!/usr/bin/env bash +set -euo pipefail +IFS=$'\n\t' +set -vx + +bundle install + +# Do any other automated setup that you need to do here diff --git a/lib/weaviate.rb b/lib/weaviate.rb new file mode 100644 index 0000000..9f50de0 --- /dev/null +++ b/lib/weaviate.rb @@ -0,0 +1,20 @@ +# frozen_string_literal: true + +require_relative "weaviate/version" + +module Weaviate + autoload :Base, "weaviate/base" + autoload :Client, "weaviate/client" + autoload :Error, "weaviate/error" + autoload :Schema, "weaviate/schema" + autoload :Meta, "weaviate/meta" + autoload :Objects, "weaviate/objects" + autoload :OIDC, "weaviate/oidc" + + module Response + autoload :Base, "weaviate/response/base" + autoload :Object, "weaviate/response/object" + autoload :Class, "weaviate/response/class" + autoload :Collection, "weaviate/response/collection" + end +end diff --git a/lib/weaviate/base.rb b/lib/weaviate/base.rb new file mode 100644 index 0000000..09612ba --- /dev/null +++ b/lib/weaviate/base.rb @@ -0,0 +1,11 @@ +# frozen_string_literal: true + +module Weaviate + class Base + attr_reader :client + + def initialize(client:) + @client = client + end + end +end \ No newline at end of file diff --git a/lib/weaviate/client.rb b/lib/weaviate/client.rb new file mode 100644 index 0000000..42ff805 --- /dev/null +++ b/lib/weaviate/client.rb @@ -0,0 +1,67 @@ +# frozen_string_literal: true + +require 'faraday' + +module Weaviate + class Client + attr_reader :scheme, :host, :model_service, :model_service_api_key, :adapter + + API_VERSION = "v1" + + API_KEY_HEADERS = { + openai: 'X-OpenAI-Api-Key', + cohere: 'X-Cohere-Api-Key', + huggingface: 'X-HuggingFace-Api-Key' + } + + def initialize( + scheme:, + host:, + model_service: nil, + model_service_api_key: nil, + adapter: Faraday.default_adapter + ) + validate_model_service!(model_service) unless model_service.nil? + + @scheme = scheme + @host = host + @model_service = model_service + @model_service_api_key = model_service_api_key + @adapter = adapter + end + + def oidc + Weaviate::OIDC.new(client: self).get + end + + def schema + @schema ||= Weaviate::Schema.new(client: self) + end + + def meta + Weaviate::Meta.new(client: self).get + end + + def objects + @objects ||= Weaviate::Objects.new(client: self) + end + + def connection + @connection ||= Faraday.new(url: "#{scheme}://#{host}/#{API_VERSION}/") do |faraday| + faraday.request :json + faraday.response :json, content_type: /\bjson$/ + faraday.adapter adapter + + faraday.headers[API_KEY_HEADERS[model_service]] = model_service_api_key if (model_service && model_service_api_key) + end + end + + private + + def validate_model_service!(model_service) + unless API_KEY_HEADERS.keys.include?(model_service) + raise ArgumentError, "Invalid model service: #{model_service}. Acceptable values are: #{API_KEY_HEADERS.keys.join(', ')}" + end + end + end +end \ No newline at end of file diff --git a/lib/weaviate/error.rb b/lib/weaviate/error.rb new file mode 100644 index 0000000..27c3282 --- /dev/null +++ b/lib/weaviate/error.rb @@ -0,0 +1,6 @@ +# frozen_string_literal: true + +module Weaviate + class Error + end +end diff --git a/lib/weaviate/meta.rb b/lib/weaviate/meta.rb new file mode 100644 index 0000000..51b324e --- /dev/null +++ b/lib/weaviate/meta.rb @@ -0,0 +1,12 @@ +# frozen_string_literal: true + +module Weaviate + class Meta < Base + PATH = "meta" + + def get + response = client.connection.get(PATH) + response.body + end + end +end diff --git a/lib/weaviate/objects.rb b/lib/weaviate/objects.rb new file mode 100644 index 0000000..dc393b9 --- /dev/null +++ b/lib/weaviate/objects.rb @@ -0,0 +1,108 @@ +# frozen_string_literal: true + +module Weaviate + class Objects < Base + PATH = 'objects' + + # TESTED + # Lists all data objects in reverse order of creation. The data will be returned as an array of objects. + def list( + class_name: nil, + limit: nil, + offset: nil, + after: nil, + include: nil, + sort: nil, + order: nil + ) + response = client.connection.get(PATH) do |req| + req.params['class'] = class_name unless class_name.nil? + req.params['limit'] = limit unless limit.nil? + req.params['offset'] = offset unless offset.nil? + req.params['after'] = after unless after.nil? + req.params['include'] = include unless include.nil? + req.params['sort'] = sort unless sort.nil? + req.params['order'] = order unless order.nil? + end + + Response::Collection.from_response(response, key: 'objects', type: Response::Object) + end + + # TESTED + # Create a new data object. The provided meta-data and schema values are validated. + def create( + class_name:, + properties:, + id: nil, + vector: nil + ) + client.connection.post(PATH) do |req| + req.body = {} + req.body['class'] = class_name + req.body['properties'] = properties + req.body['id'] = id unless id.nil? + req.body['vector'] = vector unless vector.nil? + end + end + + def batch_create(objects:) + client.connection.post("batch/#{PATH}") do |req| + req.body = { objects: objects } + end + end + + # TESTED + # Get a single data object. + def get( + class_name:, + id:, + include: nil + ) + # TODO: validate `include` param values + # include | query | param | string | Include additional information, such as classification info. Allowed values include: classification, vector. + + response = client.connection.get("#{PATH}/#{class_name}/#{id}") do |req| + req.params['include'] = include unless include.nil? + end + + if status.success? + Weaviate::Response::Object.new(response.body) + end + end + + # TESTED + # Check if a data object exists + def exists?(class_name:, id:) + response = client.connection.head("#{PATH}/#{class_name}/#{id}") + response.status == 204 + end + + # TESTED + # Update an individual data object based on its uuid. + def update( + class_name:, + id:, + properties:, + vector: nil + ) + client.connection.put("#{PATH}/#{class_name}/#{id}") do |req| + req.body = {} + req.body['id'] = id + req.body['class'] = class_name + req.body['properties'] = properties + req.body['vector'] = vector unless vector.nil? + end + end + + # TESTED + # Delete an individual data object from Weaviate. + def delete(class_name:, id:) + response = client.connection.delete("#{PATH}/#{class_name}/#{id}") + response.status == 200 && response.body.empty? + end + + # Validate a data object + # def validate + # end + end +end \ No newline at end of file diff --git a/lib/weaviate/oidc.rb b/lib/weaviate/oidc.rb new file mode 100644 index 0000000..2826a05 --- /dev/null +++ b/lib/weaviate/oidc.rb @@ -0,0 +1,12 @@ +# frozen_string_literal: true + +module Weaviate + class OIDC < Base + PATH = ".well-known/openid-configuration" + + def get + response = client.connection.get(PATH) + response.body + end + end +end diff --git a/lib/weaviate/response/base.rb b/lib/weaviate/response/base.rb new file mode 100644 index 0000000..591ef3c --- /dev/null +++ b/lib/weaviate/response/base.rb @@ -0,0 +1,23 @@ +# frozen_string_literal: true + +require "ostruct" + +module Weaviate + module Response + class Base < OpenStruct + def initialize(attributes) + super to_ostruct(attributes) + end + + def to_ostruct(obj) + if obj.is_a?(Hash) + OpenStruct.new(obj.map { |key, val| [key, to_ostruct(val)] }.to_h) + elsif obj.is_a?(Array) + obj.map { |o| to_ostruct(o) } + else # Assumed to be a primitive value + obj + end + end + end + end +end \ No newline at end of file diff --git a/lib/weaviate/response/class.rb b/lib/weaviate/response/class.rb new file mode 100644 index 0000000..eb1b2a1 --- /dev/null +++ b/lib/weaviate/response/class.rb @@ -0,0 +1,8 @@ +# frozen_string_literal: true + +module Weaviate + module Response + class Class < Base + end + end +end \ No newline at end of file diff --git a/lib/weaviate/response/collection.rb b/lib/weaviate/response/collection.rb new file mode 100644 index 0000000..522a9a3 --- /dev/null +++ b/lib/weaviate/response/collection.rb @@ -0,0 +1,22 @@ +# frozen_string_literal: true + +module Weaviate + module Response + class Collection + attr_reader :data, :total_results + + def self.from_response(response, key:, type:) + body = response.body + new( + data: body[key].map { |attrs| type.new(attrs) }, + total_results: body["totalResults"] + ) + end + + def initialize(data:, total_results:) + @data = data + @total_results = total_results + end + end + end +end diff --git a/lib/weaviate/response/object.rb b/lib/weaviate/response/object.rb new file mode 100644 index 0000000..21a0136 --- /dev/null +++ b/lib/weaviate/response/object.rb @@ -0,0 +1,8 @@ +# frozen_string_literal: true + +module Weaviate + module Response + class Object < Base + end + end +end \ No newline at end of file diff --git a/lib/weaviate/schema.rb b/lib/weaviate/schema.rb new file mode 100644 index 0000000..5b82876 --- /dev/null +++ b/lib/weaviate/schema.rb @@ -0,0 +1,120 @@ +# frozen_string_literal: true + +module Weaviate + class Schema < Base + PATH = 'schema' + + # TESTED + # Dumps the current Weaviate schema. The result contains an array of objects. + def list + response = client.connection.get(PATH) + Response::Collection.from_response(response, key: 'classes', type: Response::Class) + end + + # TESTED + # Get a single class from the schema + def get(class_name:) + response = client.connection.get("#{PATH}/#{class_name}") + + if status.success? + Response::Class.new(response.body) + end + end + + # TESTED + # Create a new data object class in the schema. + def create( + class_name:, + description:, + properties:, + vector_index_type: nil, + vector_index_config: nil, + vectorizer: nil, + module_config: nil, + inverted_index_config: nil, + replication_config: nil + ) + response = client.connection.post(PATH) do |req| + req.body = {} + req.body['class'] = class_name unless class_name.nil? + req.body['description'] = description unless description.nil? + req.body['vectorIndexType'] = vector_index_type unless vector_index_type.nil? + req.body['vectorIndexConfig'] = vector_index_config unless vector_index_config.nil? + req.body['vectorizer'] = vectorizer unless vectorizer.nil? + req.body['moduleConfig'] = module_config unless module_config.nil? + req.body['properties'] = properties unless properties.nil? + req.body['invertedIndexConfig'] = inverted_index_config unless inverted_index_config.nil? + req.body['replicationConfig'] = replication_config unless replication_config.nil? + end + + if response.success? + Response::Class.new(response.body) + else + response.body + end + end + + # TESTED + # Remove a class (and all data in the instances) from the schema. + def delete(class_name:) + response = client.connection.delete("#{PATH}/#{class_name}") + response.status == 200 && response.body.empty? + end + + # TESTED + # Update settings of an existing schema class. + def update( + class_name:, + description: nil, + vector_index_type: nil, + vector_index_config: nil, + vectorizer: nil, + module_config: nil, + properties: nil, + inverted_index_config: nil, + replication_config: nil + ) + response = client.connection.put("#{PATH}/#{class_name}") do |req| + req.body = {} + req.body['class'] = class_name unless class_name.nil? + req.body['description'] = description unless description.nil? + req.body['vectorIndexType'] = vector_index_type unless vector_index_type.nil? + req.body['vectorIndexConfig'] = vector_index_config unless vector_index_config.nil? + req.body['vectorizer'] = vectorizer unless vectorizer.nil? + req.body['moduleConfig'] = module_config unless module_config.nil? + req.body['properties'] = properties unless properties.nil? + req.body['invertedIndexConfig'] = inverted_index_config unless inverted_index_config.nil? + req.body['replicationConfig'] = replication_config unless replication_config.nil? + end + + if response.success? + Response::Class.new(response.body) + else + response.body + end + end + + # Inspect the shards of a class + def shards(class_name:) + client.connection.get("#{PATH}/#{class_name}/shards") + end + + # Update shard status + def update_shard_status(class_name:, shard_name:, status:) + validate_status!(status) + + client.connection.put("#{PATH}/#{class_name}/shards/#{shard_name}") do |req| + req.body = {} + req.body['status'] = status + end + end + + private + + def validate_status!(status) + unless %w[READONLY READY].include?(status.to_s.upcase) + raise ArgumentError, 'status must be either "READONLY" or "READY"' + end + end + end +end diff --git a/lib/weaviate/version.rb b/lib/weaviate/version.rb new file mode 100644 index 0000000..95d4db1 --- /dev/null +++ b/lib/weaviate/version.rb @@ -0,0 +1,5 @@ +# frozen_string_literal: true + +module Weaviate + VERSION = "0.1.0" +end diff --git a/sig/weaviate.rbs b/sig/weaviate.rbs new file mode 100644 index 0000000..7ac30df --- /dev/null +++ b/sig/weaviate.rbs @@ -0,0 +1,4 @@ +module Weaviate + VERSION: String + # See the writing guide of rbs: https://github.com/ruby/rbs#guides +end diff --git a/spec/fixtures/class.json b/spec/fixtures/class.json new file mode 100644 index 0000000..e69de29 diff --git a/spec/fixtures/meta.json b/spec/fixtures/meta.json new file mode 100644 index 0000000..46f1742 --- /dev/null +++ b/spec/fixtures/meta.json @@ -0,0 +1,26 @@ +{ + "hostname": "http://[::]:8080", + "modules": { + "generative-openai": { + "documentationHref": "https://beta.openai.com/docs/api-reference/completions", + "name": "Generative Search - OpenAI" + }, + "qna-openai": { + "documentationHref": "https://beta.openai.com/docs/api-reference/completions", + "name": "OpenAI Question & Answering Module" + }, + "text2vec-cohere": { + "documentationHref": "https://docs.cohere.ai/embedding-wiki/", + "name": "Cohere Module" + }, + "text2vec-huggingface": { + "documentationHref": "https://huggingface.co/docs/api-inference/detailed_parameters#feature-extraction-task", + "name": "Hugging Face Module" + }, + "text2vec-openai": { + "documentationHref": "https://beta.openai.com/docs/guides/embeddings/what-are-embeddings", + "name": "OpenAI Module" + } + }, + "version": "1.18.1" +} \ No newline at end of file diff --git a/spec/fixtures/object.json b/spec/fixtures/object.json new file mode 100644 index 0000000..e69de29 diff --git a/spec/fixtures/oidc.json b/spec/fixtures/oidc.json new file mode 100644 index 0000000..4c7e11f --- /dev/null +++ b/spec/fixtures/oidc.json @@ -0,0 +1,4 @@ +{ + "href": "http://my-token-issuer/auth/realms/my-weaviate-usecase", + "cliendID": "my-weaviate-client" +} \ No newline at end of file diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb new file mode 100644 index 0000000..8a883d0 --- /dev/null +++ b/spec/spec_helper.rb @@ -0,0 +1,16 @@ +# frozen_string_literal: true + +require "weaviate" + +RSpec.configure do |config| + # Enable flags like --only-failures and --next-failure + config.example_status_persistence_file_path = ".rspec_status" + + # Disable RSpec exposing methods globally on `Module` and `main` + config.disable_monkey_patching! + + config.expect_with :rspec do |c| + c.syntax = :expect + end +end + diff --git a/spec/weaviate/client_spec.rb b/spec/weaviate/client_spec.rb new file mode 100644 index 0000000..e74eaa4 --- /dev/null +++ b/spec/weaviate/client_spec.rb @@ -0,0 +1,64 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe Weaviate::Client do + let(:client) { Weaviate::Client.new( + scheme: 'http', + host: 'localhost:8080', + model_service: :openai, + model_service_api_key: '123' + )} + + describe '#initialize' do + it 'creates a client' do + expect(client).to be_a(Weaviate::Client) + end + end + + describe '#schema' do + it 'returns a schema client' do + expect(client.schema).to be_a(Weaviate::Schema) + end + end + + describe '#meta' do + let(:fixture) { JSON.parse(File.read('spec/fixtures/meta.json')) } + let(:response) { OpenStruct.new(body: fixture) } + + before do + allow_any_instance_of(Faraday::Connection).to receive(:get) + .with('meta') + .and_return(response) + end + + it 'returns meta information' do + response = client.meta + expect(response).to be_a(Hash) + expect(response['hostname']).to eq('http://[::]:8080') + end + end + + describe '#objects' do + it 'returns an objects client' do + expect(client.objects).to be_a(Weaviate::Objects) + end + end + + describe '#oidc' do + let(:fixture) { JSON.parse(File.read('spec/fixtures/oidc.json')) } + let(:response) { OpenStruct.new(body: fixture) } + + before do + allow_any_instance_of(Faraday::Connection).to receive(:get) + .with('.well-known/openid-configuration') + .and_return(response) + end + + it 'returns an oidc client' do + response = client.oidc + expect(response).to be_a(Hash) + expect(response['cliendID']).to eq("my-weaviate-client") + end + end +end \ No newline at end of file diff --git a/spec/weaviate/objects_spec.rb b/spec/weaviate/objects_spec.rb new file mode 100644 index 0000000..eeff3e0 --- /dev/null +++ b/spec/weaviate/objects_spec.rb @@ -0,0 +1,6 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe Weaviate::Objects do +end \ No newline at end of file diff --git a/spec/weaviate_spec.rb b/spec/weaviate_spec.rb new file mode 100644 index 0000000..b8f2da5 --- /dev/null +++ b/spec/weaviate_spec.rb @@ -0,0 +1,7 @@ +# frozen_string_literal: true + +RSpec.describe Weaviate do + it "has a version number" do + expect(Weaviate::VERSION).not_to be nil + end +end diff --git a/weaviate.gemspec b/weaviate.gemspec new file mode 100644 index 0000000..a8ab8bb --- /dev/null +++ b/weaviate.gemspec @@ -0,0 +1,42 @@ +# frozen_string_literal: true + +require_relative "lib/weaviate/version" + +Gem::Specification.new do |spec| + spec.name = "weaviate" + spec.version = Weaviate::VERSION + spec.authors = ["Andrei Bondarev"] + spec.email = ["andrei@sourcelabs.io", "andrei.bondarev13@gmail.com"] + + spec.summary = "Ruby wrapper for the Weaviate.io API" + spec.description = "Ruby wrapper for the Weaviate.io API" + spec.homepage = "https://github.com/andreibondarev/weaviate" + spec.license = "MIT" + spec.required_ruby_version = ">= 2.6.0" + + spec.metadata["allowed_push_host"] = "TODO: Set to your gem server 'https://example.com'" + + spec.metadata["homepage_uri"] = spec.homepage + spec.metadata["source_code_uri"] = "https://github.com/andreibondarev/weaviate" + spec.metadata["changelog_uri"] = "https://github.com/andreibondarev/weaviate/CHANGELOG.md" + + # Specify which files should be added to the gem when it is released. + # The `git ls-files -z` loads the files in the RubyGem that have been added into git. + spec.files = Dir.chdir(__dir__) do + `git ls-files -z`.split("\x0").reject do |f| + (f == __FILE__) || f.match(%r{\A(?:(?:bin|test|spec|features)/|\.(?:git|circleci)|appveyor)}) + end + end + spec.bindir = "exe" + spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) } + spec.require_paths = ["lib"] + + # Uncomment to register a new dependency of your gem + # spec.add_dependency "example-gem", "~> 1.0" + + # For more information and examples about making a new gem, check out our + # guide at: https://bundler.io/guides/creating_gem.html + + spec.add_dependency "faraday", "~> 2.7" + spec.add_dependency "pry-byebug", "~> 3.9" +end