Skip to content

Instantly share code, notes, and snippets.

@andreibondarev
Last active October 2, 2023 23:26
Show Gist options
  • Save andreibondarev/ab3d9f66b63dae81f66d1e130a13e188 to your computer and use it in GitHub Desktop.
Save andreibondarev/ab3d9f66b63dae81f66d1e130a13e188 to your computer and use it in GitHub Desktop.
Code for "Adding Intelligent Search to a Rails application with Weaviate"
require "weaviate"
client = Weaviate::Client.new(
url: ENV['WEAVIATE_URL'],
api_key: ENV['WEAVIATE_API_KEY'],
# Configure Weaviate to use OpenAI to create vectors and use it for querying
# You can also use Cohere or Hugging Face and pass their API key here instead
model_service: :openai,
model_service_api_key: ENV['OPENAI_API_KEY']
)
client.schema.create(
class_name: "Recipes", # Name of the collection
description: "A collection of recipes", # Description of the collection
vectorizer: "text2vec-openai", # OpenAI will be used to create vectors
module_config: {
"qna-openai": { # Weviate's OpenAI's Q&A module
model: "text-davinci-003", # OpenAI's LLM to be used
maxTokens: 3500, # Maximum number of tokens to generate in the completion
temperature: 0.0, # How deterministic the output will be
topP: 1, # Nucleus sampling
frequencyPenalty: 0.0,
presencePenalty: 0.0
}
},
properties: [
{
dataType: ["int"],
description: "Recipe ID",
name: "recipe_id" # Our PostgreSQL's recipes.id
},
{
dataType: ["text"],
description: "Recipe content",
name: "content" # Recipes' concatenated content
}
]
)
client.query.aggs(
class_name: "Recipes",
fields: "meta { count }"
)
client.query.get(
class_name: "Recipes",
limit: "1",
fields: """
recipe_id
_additional {
answer {
result
}
}
""",
ask: "{ question: \"What is a vegan dish that could be made for Thanksgiving?\" }"
)
client.query.get(
class_name: "Recipes",
limit: "1",
fields: """
recipe_id
_additional {
answer {
result
}
}
""",
ask: "{ question: \"What is a traditional French breakfast?\" }"
)
client.query.get(
class_name: "Recipes",
limit: "3",
fields: "recipe_id content",
near_text: "{ concepts: [\"latin american pescado\"] }"
)
# app/models/recipe.rb
# Table name: recipes
#
# id :bigint
# title :string
# description :text
# total_time :integer
# ingredients :text
# instructions :text
# nutrients :jsonb
# yields :string
class Recipe < ActiveRecord::Base
def text_blob
[
"Title: #{title}",
"Description: #{description}",
"Total time: #{total_time} minutes",
"Ingredients: #{ingredients.join(", ")}",
"Instructions: #{instructions}",
"Nutrients: #{nutrients}",
"Serving size: #{yields}"
]
end
end
namespace :weaviate do
require "weaviate"
desc "Importing recipes into Weaviate"
task import_recipes: :environment do
Recipe.find_in_batches do |group|
objects = group.map do |recipe|
{
class: "Recipes", # Each object must contain the schema name
properties: {
content: recipe.text_blob, # See below for the formatted data
recipe_id: recipe.id # Recipe ID from the recipes PSQL table
}
}
end
weaviate_client.objects.batch_create(
objects: objects
)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment