Class: RagEmbeddings::Database
- Inherits:
-
Object
- Object
- RagEmbeddings::Database
- Defined in:
- lib/rag_embeddings/database.rb
Instance Method Summary collapse
- #all ⇒ Object
-
#initialize(path = "embeddings.db") ⇒ Database
constructor
A new instance of Database.
- #insert(text, embedding) ⇒ Object
-
#top_k_similar(query_text, k: 5) ⇒ Object
“Raw” search: returns the N texts most similar to the query.
Constructor Details
#initialize(path = "embeddings.db") ⇒ Database
Returns a new instance of Database.
5 6 7 8 9 10 11 12 13 14 |
# File 'lib/rag_embeddings/database.rb', line 5 def initialize(path = "embeddings.db") @db = SQLite3::Database.new(path) @db.execute <<~SQL CREATE TABLE IF NOT EXISTS embeddings ( id INTEGER PRIMARY KEY AUTOINCREMENT, content TEXT NOT NULL, embedding BLOB NOT NULL ); SQL end |
Instance Method Details
#all ⇒ Object
21 22 23 24 25 |
# File 'lib/rag_embeddings/database.rb', line 21 def all @db.execute("SELECT id, content, embedding FROM embeddings").map do |id, content, blob| [id, content, blob.unpack("f*")] end end |
#insert(text, embedding) ⇒ Object
16 17 18 19 |
# File 'lib/rag_embeddings/database.rb', line 16 def insert(text, ) blob = .pack("f*") @db.execute("INSERT INTO embeddings (content, embedding) VALUES (?, ?)", [text, blob]) end |
#top_k_similar(query_text, k: 5) ⇒ Object
“Raw” search: returns the N texts most similar to the query
28 29 30 31 32 33 34 35 36 37 |
# File 'lib/rag_embeddings/database.rb', line 28 def top_k_similar(query_text, k: 5) = RagEmbeddings.(query_text) query_obj = RagEmbeddings::Embedding.from_array() all.map do |id, content, emb| emb_obj = RagEmbeddings::Embedding.from_array(emb) similarity = emb_obj.cosine_similarity(query_obj) [id, content, similarity] end.sort_by { |_,_,sim| -sim }.first(k) end |