How-To Add Recommendations to a Rails App with pgvector and OpenAI

Maurício Maia
2 min readMay 16

--

While creating The AI Job Network, I wanted to show similar job recommendations when the user sees a job post. Building it is straightforward, using pgvector, ruby-openai, and the neighbor gem.

Your PostgreSQL server needs to support pgvector. Install your own or pick a hosted provider.

Install necessary gems

First, add the ruby-openai and Neighbor gems to your Gemfile:

gem "neighbor"
gem "ruby-openai"

Then, run bundle install:

bundle install

Enable pgvector in PostgreSQL

In a Rails migration, include:

class EnablePgVector < ActiveRecord::Migration[7.0]
def change
enable_extension "vector"
end
end

Setup your model

Create a vector column to store the embedding:

class AddNeighborVectorToJobs < ActiveRecord::Migration[7.0]
def change
add_column :jobs, :embedding, :vector, limit: 1536
end
end

Add neighbors to your model and use OpenAI’s embedding model to create the vector embedding. Here I’m generating the embedding in a callback, but you probably want to do it in a background job.

class Job < ApplicationRecord
has_neighbors :embedding

after_validation :generate_vector_embed

def embed_input
<<~EOS
Title: #{title}
Category: #{category}
Keywords: #{keywords}
EOS
end

private
def generate_vector_embed
client = OpenAI::Client.new
response = client.embeddings(
parameters: {
model: "text-embedding-ada-002",
input: embed_input,
},
)
self.embedding = response.dig("data", 0, "embedding")
end
end

Getting Recommendations

You can get similar records from any item with nearest_neighbors and check their similarity with neighbor_distance.

job = Job.first
job.nearest_neighbors(:embedding, distance: "inner_product").first(3).each do | job|
puts "#{job.title} - #{job.neighbor_distance}"
end
Showing similar job posts

Interested in Ruby and AI? Check opportunities at leading AI companies.

--

--