speaky 0.1.6 → 0.1.8
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 +4 -4
- data/lib/speaky/concern.rb +1 -0
- data/lib/speaky/vectorstore_base.rb +1 -1
- data/lib/speaky/vectorstore_faiss.rb +30 -4
- data/lib/speaky/vectorstore_qdrant.rb +11 -4
- data/lib/speaky/version.rb +1 -1
- data/lib/speaky.rb +1 -1
- 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: d65d925dd1dc599e493c9ebb83f4e7eff915aa5ffffbc2daf4248bb83d1801fd
|
4
|
+
data.tar.gz: 6714917b885cc8c34992b75ff3688a60076568a341d6ccee577f8b5959946da8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6a9c1ca13995cf513c4600725fb9a5f3ed6a2749a9d020ecd204dacf07e528356dd9164cb64d9ff70b744fae5938efe274825186ea374cec9599aa51bed07187
|
7
|
+
data.tar.gz: 3ffc25af7d5884ab66720ef78fc03b6afe10892b0a3296be83a53e150654ebf09410bdae9eb33e164cc2ec485add2771aa84f72623c605892f4761c32ec8a0cc
|
data/lib/speaky/concern.rb
CHANGED
@@ -1,10 +1,10 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
+
require 'faiss'
|
4
|
+
|
3
5
|
module Speaky
|
4
6
|
class VectorstoreFaiss < VectorstoreBase
|
5
7
|
def initialize(config)
|
6
|
-
raise 'This class is not implemented yet.' # TEMP
|
7
|
-
|
8
8
|
@config = config
|
9
9
|
|
10
10
|
# check if the index path is set
|
@@ -15,11 +15,37 @@ module Speaky
|
|
15
15
|
@index = Faiss::Index.load(@config[:index_path])
|
16
16
|
else
|
17
17
|
# create a new index
|
18
|
-
|
18
|
+
index = Faiss::IndexFlatL2.new(1536)
|
19
|
+
@index = Faiss::IndexIDMap.new(index)
|
19
20
|
@index.save(@config[:index_path])
|
20
21
|
end
|
21
22
|
end
|
22
23
|
|
23
|
-
|
24
|
+
def add(id, data)
|
25
|
+
embeddings = Speaky.llm.embed(data)
|
26
|
+
|
27
|
+
@index.add_with_ids([embeddings], [string_id_to_unique_int_id(id)])
|
28
|
+
|
29
|
+
true
|
30
|
+
end
|
31
|
+
|
32
|
+
def remove(id)
|
33
|
+
# remove is not supported by Faiss
|
34
|
+
true
|
35
|
+
end
|
36
|
+
|
37
|
+
def query(question)
|
38
|
+
# TODO: implement query
|
39
|
+
|
40
|
+
[]
|
41
|
+
end
|
42
|
+
|
43
|
+
private
|
44
|
+
|
45
|
+
# This method is used to convert a string ID to a unique integer ID
|
46
|
+
# that can be used by the Qdrant API.
|
47
|
+
def string_id_to_unique_int_id(string_id)
|
48
|
+
string_id.to_s.hash.abs
|
49
|
+
end
|
24
50
|
end
|
25
51
|
end
|
@@ -1,5 +1,6 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
+
require 'rails'
|
3
4
|
require 'qdrant'
|
4
5
|
|
5
6
|
module Speaky
|
@@ -29,7 +30,7 @@ module Speaky
|
|
29
30
|
collection_name: @config[:collection_name],
|
30
31
|
points: [
|
31
32
|
{
|
32
|
-
id: id,
|
33
|
+
id: string_id_to_unique_int_id(id),
|
33
34
|
vector: embeddings,
|
34
35
|
payload: {
|
35
36
|
content: data
|
@@ -50,7 +51,7 @@ module Speaky
|
|
50
51
|
def remove(id)
|
51
52
|
points_delete = @client.points.delete(
|
52
53
|
collection_name: @config[:collection_name],
|
53
|
-
points: [id],
|
54
|
+
points: [string_id_to_unique_int_id(id)],
|
54
55
|
)
|
55
56
|
|
56
57
|
if !points_delete || points_delete.dig('status') != 'ok'
|
@@ -66,7 +67,7 @@ module Speaky
|
|
66
67
|
|
67
68
|
points_search = @client.points.search(
|
68
69
|
collection_name: @config[:collection_name],
|
69
|
-
limit:
|
70
|
+
limit: 5,
|
70
71
|
vector: embeddings,
|
71
72
|
with_payload: true,
|
72
73
|
with_vector: false
|
@@ -77,7 +78,7 @@ module Speaky
|
|
77
78
|
raise 'Failed to search vectors'
|
78
79
|
end
|
79
80
|
|
80
|
-
points_search.dig('result').
|
81
|
+
points_search.dig('result').map { |r| r.dig('payload', 'content') }
|
81
82
|
end
|
82
83
|
|
83
84
|
def reset
|
@@ -117,5 +118,11 @@ module Speaky
|
|
117
118
|
|
118
119
|
true
|
119
120
|
end
|
121
|
+
|
122
|
+
# This method is used to convert a string ID to a unique integer ID
|
123
|
+
# that can be used by the Qdrant API.
|
124
|
+
def string_id_to_unique_int_id(string_id)
|
125
|
+
string_id.to_s.hash.abs
|
126
|
+
end
|
120
127
|
end
|
121
128
|
end
|
data/lib/speaky/version.rb
CHANGED
data/lib/speaky.rb
CHANGED
@@ -87,7 +87,7 @@ module Speaky
|
|
87
87
|
template ||= default_template
|
88
88
|
|
89
89
|
# load context
|
90
|
-
context = vectorstore.query(question)
|
90
|
+
context = vectorstore.query(question).join("\n")
|
91
91
|
|
92
92
|
# generate prompt
|
93
93
|
prompt = template.gsub("{{context}}", context).gsub("{{question}}", question)
|