speaky 0.1.1 → 0.1.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b07781fbe4f9d2c3a4f117098558abe0937cc64bd979545979e84962a5c470e8
4
- data.tar.gz: 27ea5bea5cd0cf71edca5b296e4691fd078e44b7dfe1c0e8b28b66cb33d06ddc
3
+ metadata.gz: 6223b310b2223f1af3c8c2c45dc5ebe90aade3ed8cf040b9caad69a03b97e06c
4
+ data.tar.gz: 77b50bfbf7925c14f78504774c7502c1018c27ef1e80bd6ebebf743310ed511a
5
5
  SHA512:
6
- metadata.gz: 24522e88f88b854c59508f098e4b7a0c32f0a89c5f7f7d5aa0c58b808262420052d41bcfff449775ffefe4e6898e84bab1ffa8850aede74f072253ef69838016
7
- data.tar.gz: f7940dd8e5745a91ef7768ff17e25b5ac77fe3c5d1de1eb2cc624725108739cfa9af9927c6d1ee76a7b4d1c82cb4dec2e7086be1de23ab7ff0f5103818d0ff41
6
+ metadata.gz: cd78c665ea63fc51f7ff069e5e7eb1ee41635e9bddcdf1e5f6105762442bb5b7be12ac890742cf9d7e2d62b7aab903a12b11723b45bce6c385fc57c59a23a5eb
7
+ data.tar.gz: 491775c48104c32a125934f9e8ffbd5f5960aebc357b08f135e1b78cc640409b1df5923cf0b3e0ac855c1f01ebc161f99b2981d8e6c0b886cf9c4e44e3924783
@@ -12,24 +12,35 @@ module Speaky
12
12
 
13
13
  # Add a vector to the vectorstore.
14
14
  # NOTE: If the vector already exists, it will be updated.
15
+ # Returns a boolean.
15
16
  def add(id, data)
16
17
  raise NotImplementedError
17
18
  end
18
19
 
19
20
  # Update a vector in the vectorstore.
20
21
  # NOTE: If the vector does not exist, it will be added.
22
+ # Returns a boolean.
21
23
  def update(id, data)
22
24
  raise NotImplementedError
23
25
  end
24
26
 
25
27
  # Remove a vector from the vectorstore.
26
28
  # NOTE: If the vector does not exist, it will be ignored.
29
+ # Returns a boolean.
27
30
  def remove(id)
28
31
  raise NotImplementedError
29
32
  end
30
33
 
34
+ # Query the vectorstore with a question.
35
+ # Returns a string.
31
36
  def query(question)
32
37
  raise NotImplementedError
33
38
  end
39
+
40
+ # Reset the vectorstore.
41
+ # Returns a boolean.
42
+ def reset
43
+ raise NotImplementedError
44
+ end
34
45
  end
35
46
  end
@@ -18,26 +18,8 @@ module Speaky
18
18
  api_key: @config[:api_key]
19
19
  )
20
20
 
21
- # create collection if it doesn't exist
22
- collections_get = @client.collections.get(collection_name: @config[:collection_name])
23
- if !collections_get || collections_get.dig('status') != 'ok'
24
- collections_create = @client.collections.create(
25
- collection_name: @config[:collection_name],
26
- vectors: {
27
- distance: "Cosine",
28
- size: 1536
29
- }
30
- )
31
- if !collections_create || collections_create.dig('status') != 'ok'
32
- raise 'Failed to create collection'
33
- end
34
- end
35
-
36
- # create index for field "id" in collection
37
- collections_create_index = @client.collections.create_index(collection_name: @config[:collection_name], field_name: 'id', field_schema: 'keyword')
38
- if !collections_create_index || collections_create_index.dig('status') != 'ok'
39
- raise 'Failed to create index for field "id" on collection'
40
- end
21
+ # create collection
22
+ create_collection
41
23
  end
42
24
 
43
25
  def add(id, data)
@@ -98,5 +80,40 @@ module Speaky
98
80
 
99
81
  points_search.dig('result').first.dig('payload', 'content')
100
82
  end
83
+
84
+ def reset
85
+ collections_delete = @client.collections.delete(collection_name: @config[:collection_name])
86
+ if !collections_delete || collections_delete.dig('status') != 'ok'
87
+ raise 'Failed to delete collection'
88
+ end
89
+
90
+ create_collection
91
+ end
92
+
93
+ private
94
+
95
+ def create_collection
96
+ collections_get = @client.collections.get(collection_name: @config[:collection_name])
97
+ if !collections_get || collections_get.dig('status') != 'ok'
98
+ collections_create = @client.collections.create(
99
+ collection_name: @config[:collection_name],
100
+ vectors: {
101
+ distance: "Cosine",
102
+ size: 1536
103
+ }
104
+ )
105
+ if !collections_create || collections_create.dig('status') != 'ok'
106
+ raise 'Failed to create collection'
107
+ end
108
+ end
109
+
110
+ # create index for field "id" in collection
111
+ collections_create_index = @client.collections.create_index(collection_name: @config[:collection_name], field_name: 'id', field_schema: 'keyword')
112
+ if !collections_create_index || collections_create_index.dig('status') != 'ok'
113
+ raise 'Failed to create index for field "id" on collection'
114
+ end
115
+
116
+ true
117
+ end
101
118
  end
102
119
  end
@@ -1,3 +1,3 @@
1
1
  module Speaky
2
- VERSION = '0.1.1'
2
+ VERSION = '0.1.3'
3
3
  end
data/lib/speaky.rb CHANGED
@@ -71,6 +71,7 @@ module Speaky
71
71
  #
72
72
  # Example of usage:
73
73
  # Speaky.ask("What is the capital of France?")
74
+ # Speaky.ask("What is the capital of France?", template: "You are an AI assistant. You are asked a question and you provide an answer. Use the provided context to generate the answer to the question. Context: {{context}} Question: {{question}}")
74
75
  def ask(question, template: nil, **other_params)
75
76
  # load template
76
77
  default_template = <<~TEMPLATE
@@ -95,7 +96,31 @@ module Speaky
95
96
  end
96
97
 
97
98
  # ask the question
98
- llm.ask(prompt)
99
+ llm.chat(prompt)
100
+ end
101
+
102
+ # This is a method that re-embeds all Rails models into the vectorstore.
103
+ #
104
+ # Example of usage:
105
+ # Speaky.embed!
106
+ def embed!
107
+ # reset the vectorstore
108
+ vectorstore.reset
109
+
110
+ # re-embed all rails models that include the Concern module
111
+ Rails.application.eager_load!
112
+ rails_models = ActiveRecord::Base.descendants
113
+ rails_models.each do |rails_model|
114
+ next unless rails_model.instance_methods.include?(:create_for_speaky)
115
+
116
+ rails_model.find_in_batches do |batch|
117
+ batch.each do |record|
118
+ record.create_for_speaky
119
+ end
120
+ end
121
+ end
122
+
123
+ true
99
124
  end
100
125
  end
101
126
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: speaky
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Gregorio Galante