speaky 0.1.2 → 0.1.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/speaky/vectorstore_base.rb +11 -0
- data/lib/speaky/vectorstore_qdrant.rb +37 -20
- data/lib/speaky/version.rb +1 -1
- data/lib/speaky.rb +25 -0
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6223b310b2223f1af3c8c2c45dc5ebe90aade3ed8cf040b9caad69a03b97e06c
|
4
|
+
data.tar.gz: 77b50bfbf7925c14f78504774c7502c1018c27ef1e80bd6ebebf743310ed511a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
22
|
-
|
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
|
data/lib/speaky/version.rb
CHANGED
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
|
@@ -97,5 +98,29 @@ module Speaky
|
|
97
98
|
# ask the question
|
98
99
|
llm.chat(prompt)
|
99
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
|
124
|
+
end
|
100
125
|
end
|
101
126
|
end
|