weaviate_record 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,101 @@
1
+ # frozen_string_literal: true
2
+
3
+ module WeaviateRecord
4
+ # This module contains methods that helps to build, maintain and read data from weaviate schema
5
+ class Schema
6
+ class << self
7
+ # :stopdoc:
8
+
9
+ STRUCTURE_FILE_BOILERPLATE = lambda do |schema|
10
+ <<~RUBY
11
+ # frozen_string_literal: true
12
+
13
+ module WeaviateRecord
14
+ # This class stores the schema of all Weaviate Collections
15
+ # Don't change it manually, use the WeaviateRecord::Schema.update! method to update the schema
16
+ class Schema
17
+ def self.all_collections # rubocop:disable Metrics/MethodLength
18
+ #{schema}
19
+ end
20
+ end
21
+ end
22
+ RUBY
23
+ end
24
+
25
+ # :startdoc:
26
+
27
+ # This method updates the local schema file with the latest schema from Weaviate
28
+ # The schema file path is configured by setting +WeaviateRecord.config.schema_file_path+
29
+ #
30
+ # If the rubocop is installed in the system, it will format the generated schema file too.
31
+ def update!
32
+ create_weaviate_db_dir!
33
+ File.write(WeaviateRecord.config.schema_file_path, STRUCTURE_FILE_BOILERPLATE[pretty_schema])
34
+ rubocop_format_file
35
+ nil
36
+ end
37
+
38
+ # This method checks if the local schema file is in sync with the schema in Weaviate database
39
+ def synced?
40
+ if File.exist?(WeaviateRecord.config.schema_file_path)
41
+ load WeaviateRecord.config.schema_file_path
42
+ WeaviateRecord::Schema.all_collections == schema_list
43
+ else
44
+ false
45
+ end
46
+ end
47
+
48
+ # This method returns the weaviate schema for the given collection class
49
+ def find_collection(klass)
50
+ load WeaviateRecord.config.schema_file_path
51
+ collection_schema = all_collections[:classes].find { |collection| collection[:class] == klass.to_s }
52
+ if collection_schema.nil?
53
+ raise WeaviateRecord::Errors::CollectionNotFound, "Collection #{klass} not found in the schema"
54
+ end
55
+
56
+ new(collection_schema)
57
+ end
58
+
59
+ # :stopdoc:
60
+
61
+ private
62
+
63
+ def schema_list
64
+ WeaviateRecord::Connection.new.schema_list
65
+ end
66
+
67
+ def pretty_schema
68
+ schema_list.pretty_inspect
69
+ end
70
+
71
+ def rubocop_format_file
72
+ # To prettify the generated file
73
+ system("rubocop -a #{WeaviateRecord.config.schema_file_path}", %i[err out] => File::NULL)
74
+ end
75
+
76
+ def create_weaviate_db_dir!
77
+ dir_path = WeaviateRecord.config.schema_file_path.delete_suffix('/schema.rb')
78
+ FileUtils.mkdir_p(dir_path) unless File.directory?(dir_path)
79
+ end
80
+ end
81
+
82
+ def initialize(schema)
83
+ @schema = schema
84
+ end
85
+
86
+ # :startdoc:
87
+ # This method returns the list of attributes for the collection
88
+ #
89
+ # ==== Usage
90
+ # WeaviateRecord::Schema.find_collection(Article).attributes_list
91
+ def attributes_list
92
+ @schema[:properties].map { |property| property[:name] }
93
+ end
94
+
95
+ # This attribute returns the schema of the collection in Hash format
96
+ attr_reader :schema
97
+
98
+ # :enddoc:
99
+ private_class_method :new
100
+ end
101
+ end
@@ -0,0 +1,38 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'zeitwerk'
4
+ require 'active_model'
5
+ require 'active_support/core_ext/string/conversions'
6
+
7
+ loader = Zeitwerk::Loader.for_gem
8
+ loader.setup
9
+
10
+ # Starting point of the gem
11
+ module WeaviateRecord
12
+ class << self
13
+ # Configuration object for WeaviateRecord
14
+ def config
15
+ @config ||= Struct.new(
16
+ :similarity_search_threshold,
17
+ :schema_file_path,
18
+ :sync_schema_on_load
19
+ ).new(0.55, "#{Object.const_defined?('Rails') ? Rails.root : Dir.pwd}/db/weaviate/schema.rb", false)
20
+ end
21
+
22
+ # Used to configure WeaviateRecord, accepts a block and yields the configuration object
23
+ #
24
+ # ==== Example:
25
+ # WeaviateRecord.configure do |config|
26
+ # config.similarity_search_threshold = 0.6
27
+ # config.schema_file_path = "#{Rails.root}/db/weaviate/schema.rb"
28
+ # config.sync_schema_on_load = true
29
+ # end
30
+ #
31
+ # When sync_schema_on_load is set to true, the local schema will be synced
32
+ # with the database schema when WeaviateRecord is loaded.
33
+ def configure
34
+ yield config if block_given?
35
+ WeaviateRecord::Schema.update! if config.sync_schema_on_load && !WeaviateRecord::Schema.synced?
36
+ end
37
+ end
38
+ end
metadata ADDED
@@ -0,0 +1,111 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: weaviate_record
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.3
5
+ platform: ruby
6
+ authors:
7
+ - Sriram V
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2024-06-08 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activemodel
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '5.1'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '5.1'
27
+ - !ruby/object:Gem::Dependency
28
+ name: weaviate-ruby
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0.1'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0.1'
41
+ - !ruby/object:Gem::Dependency
42
+ name: zeitwerk
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '2.4'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '2.4'
55
+ description: An ORM for Weaviate vector database that follows the same conventions
56
+ as the ActiveRecord. This gem uses weaviate-ruby internally to connect with weaviate
57
+ database.
58
+ email: srira.venkat@gmail.com
59
+ executables: []
60
+ extensions: []
61
+ extra_rdoc_files: []
62
+ files:
63
+ - lib/weaviate_record.rb
64
+ - lib/weaviate_record/base.rb
65
+ - lib/weaviate_record/concerns/attribute_concern.rb
66
+ - lib/weaviate_record/concerns/record_concern.rb
67
+ - lib/weaviate_record/connection.rb
68
+ - lib/weaviate_record/constants.rb
69
+ - lib/weaviate_record/errors.rb
70
+ - lib/weaviate_record/inspect.rb
71
+ - lib/weaviate_record/method_missing.rb
72
+ - lib/weaviate_record/queries/ask.rb
73
+ - lib/weaviate_record/queries/bm25.rb
74
+ - lib/weaviate_record/queries/count.rb
75
+ - lib/weaviate_record/queries/limit.rb
76
+ - lib/weaviate_record/queries/near_object.rb
77
+ - lib/weaviate_record/queries/near_text.rb
78
+ - lib/weaviate_record/queries/near_vector.rb
79
+ - lib/weaviate_record/queries/offset.rb
80
+ - lib/weaviate_record/queries/order.rb
81
+ - lib/weaviate_record/queries/select.rb
82
+ - lib/weaviate_record/queries/where.rb
83
+ - lib/weaviate_record/relation.rb
84
+ - lib/weaviate_record/relation/query_builder.rb
85
+ - lib/weaviate_record/schema.rb
86
+ homepage: https://rubygems.org/gems/weaviate_record
87
+ licenses:
88
+ - MIT
89
+ metadata:
90
+ source_code_uri: https://github.com/ruby-ist/weaviate_record
91
+ rubygems_mfa_required: 'true'
92
+ post_install_message:
93
+ rdoc_options: []
94
+ require_paths:
95
+ - lib
96
+ required_ruby_version: !ruby/object:Gem::Requirement
97
+ requirements:
98
+ - - ">="
99
+ - !ruby/object:Gem::Version
100
+ version: 2.6.0
101
+ required_rubygems_version: !ruby/object:Gem::Requirement
102
+ requirements:
103
+ - - ">="
104
+ - !ruby/object:Gem::Version
105
+ version: '0'
106
+ requirements: []
107
+ rubygems_version: 3.0.3
108
+ signing_key:
109
+ specification_version: 4
110
+ summary: An ORM for Weaviate vector database
111
+ test_files: []