Imagine you have thousands of photos, and you need to find that special one where you're making a funny face. You probably don’t even need to imagine - we've all been there. What if you could just type "hot summer on a beach, making a funny face" and find it instantly?
You might be surprised how easy it is to build such an application using a large language model. They provide a unique interface between humans and machines. Read on to see how to implement it easily using Ruby on Rails.
Theory
Please read the previous article about Embeddings with Ruby to acquire all the needed theoretical knowledge.
Vector Search
Vector search is a technique used to find items by comparing vectors (embeddings) representing those items. Each item, like a phrase or image, is encoded as a vector in a high-dimensional space, where similar items have vectors that are close to each other.
Vector search is especially useful where keyword or tag-based search methods fall short. Vector Search is great for finding items that represent similar concepts rather than exact string matches.
Example #1: Describe 10 different people with their looks and character traits. Then, use a phrase like "calm and good looking" and compare embeddings of the descriptions with the one of the searched phrase. The description with the most similar embedding will match the description best. What's interesting, you could just as easily look for "composed and appealing" and get a very similar result.
Example #2:
Storing embeddings
Databases most frequently used with Ruby on Rails applications aren't well-suited for storing vectors by default. Thankfully, there are custom extensions which can be enabled.
At the moment of writing this article (Nov, 2024), the most mature extension is available for PostgreSQL and is called pgvector. There are options for other relational databases, but they are in an experimental state - squlite-vec for SQLite, Vector for MariaDB and HeatWave, required for searching with MySQL.
Alternatively, you can use a NoSQL database, which is also well established in the Ruby on Rails ecosystem. You may use Redis with neighbour-redis gem.
Embeddings with pgvector and Ruby on Rails
In order to enable pgvector, you have to first install it on your machine and add an appropriate gem to the application:
Generate an embedding and fill the new column of your model with data:
client=OpenAI::Client.new(access_token: OPENAI_API_KEY)response=client.embeddings(parameters: {model: "text-embedding-3-large",input: "Ruby is great for writing AI software"})item=Item.create(embedding: response.dig("data",0,"embedding"))
With data in the database, we can query for similar vectors using operators provided by the pgvector extension, for example:
The above SQL code orders the found items by cosine similarity to the given embedding. The available operators are:
<=> for cosine
<#> for inner product
<-> for euclidean
<+> for taxicab
However, the goal is not to use SQL directly, but to embrace ActiveRecord and the Rails way.
Find the most similar item with Ruby on Rails
Thankfully, the same gem can be used for finding similar items using ActiveRecord. Its usage is very simple and follows the well-known syntax of Rails. It can be used with Postgres, SQLite, MariaDB or MySQL.
You've already added it to the Gemfile:
gem"neighbor"
Follow it up by declaring it in your ActiveRecord model definition:
And that is it! A few lines of code allow us to grasp all the power of Vector Search!
Note: An alternative solution may be achieved using a more complex library - Langchain.rb. However, its usage is beyond the scope of this article.
Find the lost image
With the knowledge gathered in the article, let's create a Ruby on Rails application to find photos by their descriptions. The only additional tool you'll need is the image-to-text transcript as we need to create a text description of each image. Fortunately, this can also be done using an LLM.
Convert image to text using Ruby and ChatGPT
Using such a powerful tool as an LLM it's actually very easy. Let's take a cute dog image and see how to do it:
encoded_image=Base64.strict_encode64("/path/to/dog.jpg")messages=[{"type":"text","text":"What’s in this image?"},{"type":"image_url","image_url":{"url":"data:image/png;base64,#{encoded_image}"}}]response=client.chat(parameters: {model: "gpt-4o-mini",messages: [{role: "user",content: messages}]})putsresponse.dig("choices",0,"message","content")# => "The image depicts a small dog with curly fur that appears to be playfully biting on a shoe. The dog has large, expressive eyes and is engaging with the shoe, which is tan with a light blue interior. The background is removed, making the focus solely on the dog and the shoe."
Vector Search application with Ruby on Rails
The rest of the application's design is straightforward and follows the Rails conventions. The best part is - you don't even need to code it yourself! Check out a ready application at https://github.com/pstrzalk/image-finder-vector-search
The main element of the solution is the Image model, which processes the attachment after creation. It gathers the image’s description and embeds it using ChatGPT.
# https://github.com/pstrzalk/image-finder-vector-search/blob/main/app/models/image.rbclassImage<ApplicationRecordhas_one_attached:filehas_neighbors:embeddingafter_create_commit:recalculate_embeddingdefrecalculate_embeddingreturnunlessfile.attachment.present?client=OpenAI::Client.new(access_token: Rails.configuration.x.openai_api_key)base64_encoded_image=Base64.strict_encode64(file.download)messages=[{"type":"text","text":"What’s in this image?"},{"type":"image_url","image_url":{"url":"data:image/png;base64,#{base64_encoded_image}"}}]response=client.chat(parameters: {model: "gpt-4o-mini",messages: [{role: "user",content: messages}]})self.description=response.dig("choices",0,"message","content")response=client.embeddings(parameters: {model: "text-embedding-3-large",input: description})self.embedding=response.dig("data",0,"embedding")self.saveendend
The second key component is the ImageController , which by default shows all the available images.
Finds the image with the closest vector embedding to the query
Infinite possibilities
By combining the power of vector search with Ruby on Rails and tools like ChatGPT, you can build innovative and intuitive applications that go beyond traditional search capabilities. The only limit is your imagination. Check out the image-finder-vector-search repository to explore the example implementation and see how you can enhance your own projects with these cutting-edge techniques.