-
Notifications
You must be signed in to change notification settings - Fork 77
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Demonstration of using pgvector to improve searching
By using embeddings generated from a LLM we can hope to gain better results when search for existing petitions and also when people are search generally.
- Loading branch information
Showing
34 changed files
with
445 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
class UpdatePetitionEmbeddingJob < ApplicationJob | ||
retry_on Embedding::GenerationError, wait: :polynomially_longer, attempts: 10 | ||
|
||
def perform(petition) | ||
petition.update_columns(embedding: Embedding.generate(petition.content)) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
require 'faraday' | ||
require 'aws-sdk-bedrockruntime' | ||
|
||
module Embedding | ||
class GenerationError < RuntimeError; end | ||
|
||
module Backends | ||
class Ollama | ||
with_options instance_writer: false do | ||
class_attribute :url, default: ENV.fetch('OLLAMA_URL', 'http://127.0.0.1:11434') | ||
class_attribute :path, default: '/api/embed' | ||
class_attribute :model, default: ENV.fetch('OLLAMA_MODEL', 'mxbai-embed-large') | ||
class_attribute :headers, default: { content_type: 'application/json' } | ||
class_attribute :open_timeout, default: 5 | ||
class_attribute :timeout, default: 5 | ||
end | ||
|
||
def generate(input) | ||
body = { input: input, model: model }.to_json | ||
|
||
response = faraday.post(path, body, headers) do |request| | ||
request.options[:timeout] = timeout | ||
request.options[:open_timeout] = open_timeout | ||
end | ||
|
||
response.body.fetch('embeddings').first | ||
rescue StandardError => e | ||
raise Embedding::GenerationError, "Unable to generate an embedding using Ollama" | ||
end | ||
|
||
private | ||
|
||
def faraday | ||
@faraday ||= Faraday.new(url) do |f| | ||
f.response :follow_redirects | ||
f.response :json | ||
f.response :raise_error | ||
f.adapter :net_http_persistent | ||
end | ||
end | ||
end | ||
|
||
class AmazonBedrock | ||
with_options instance_writer: false do | ||
class_attribute :model_id, default: ENV.fetch('BEDROCK_MODEL_ID', 'amazon.titan-embed-text-v2:0') | ||
end | ||
|
||
def generate(input) | ||
params = { | ||
body: { | ||
inputText: input, | ||
dimensions: 1024, | ||
embeddingTypes: ['float'] | ||
}.to_json, | ||
content_type: 'application/json', | ||
accept: 'application/json', | ||
model_id: model_id | ||
} | ||
|
||
response = bedrock.invoke_model(**params) | ||
json = JSON.parse(response.body.read) | ||
|
||
json['embedding'] | ||
rescue StandardError => e | ||
raise Embedding::GenerationError, "Unable to generate an embedding using Amazon Bedrock" | ||
end | ||
|
||
private | ||
|
||
def bedrock | ||
@bedrock ||= Aws::BedrockRuntime::Client.new | ||
end | ||
end | ||
end | ||
|
||
class << self | ||
def generate(input) | ||
client.generate(input) | ||
end | ||
|
||
def backend | ||
Backends.const_get(ENV.fetch('EMBEDDING_BACKEND', 'Ollama')) | ||
end | ||
|
||
def client | ||
Thread.current[:__embedding__] ||= backend.new | ||
end | ||
|
||
def reload | ||
Thread.current[:__embedding__] = nil | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
module NearestNeighbours | ||
extend ActiveSupport::Concern | ||
|
||
module ClassMethods | ||
def nearest_neighbours(embedding, column: :embedding) | ||
reorder(arel_table[column].nearest(embedding)) | ||
end | ||
end | ||
|
||
def nearest_neighbours(column: :embedding) | ||
self.class.excluding(self).nearest_neighbours(read_attribute(column), column: column) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.