Text as numbers
“Can I send my order back?” and “Returns are free within 30 days” share no word that matters – yet one answers the other. A search for words finds nothing; a search for meaning does. For that, an embedding model turns each text into a list of numbers, a vector: texts with a similar meaning get vectors that point in a similar direction.
RubyLLM.embed asks for one. Chunky Mini's embeddings have 64 numbers (real ones 768 to 3072):
install_gem "ruby_llm"
require "ruby_llm"
require "chunky_llm" # Chunky Mini, the little model in this page
embedding = RubyLLM.embed("Chunky loves bacon")
vector = embedding.vectors
puts "#{vector.size} numbers, the first: #{vector.first(4)}"
embedding.modelHow similar? The cosine
How alike two directions are is measured by the cosine similarity: multiply the vectors number by number, add up, divide by both lengths. 1 means the same direction, 0 has nothing in common:
def cosine(a, b)
dot = a.zip(b).sum { |x, y| x * y }
dot / (Math.sqrt(a.sum { |x| x * x }) * Math.sqrt(b.sum { |x| x * x }))
end
bacon = RubyLLM.embed("Chunky loves bacon").vectors
food = RubyLLM.embed("The fox eats speck for breakfast").vectors
rain = RubyLLM.embed("It is raining in Bern").vectors
[cosine(bacon, food).round(2), cosine(bacon, rain).round(2)]“Chunky” and “the fox”, “bacon” and “speck” are different words with a similar meaning, and the vectors know it: real models learned that from billions of sentences, Chunky Mini from a little list of words that belong together. The rain has nothing to do with it.
Searching a FAQ
embed also takes an array and returns one vector per text. So a FAQ is embedded once; a question then finds the entry whose vector is closest:
faq = [
"Returns are free within 30 days.",
"Parcels ship within two working days.",
"You can pay by card or with TWINT.",
"The shop is open Monday to Saturday.",
"Gift vouchers never expire."
]
faq_vectors = RubyLLM.embed(faq).vectors
def closest(question, texts, vectors)
q = RubyLLM.embed(question).vectors
texts.zip(vectors).max_by { |_text, v| cosine(q, v) }.first
end
closest("Can I send my order back?", faq, faq_vectors)closest("How long does the delivery take?", faq, faq_vectors)RAG: retrieve, then generate
Chunky Mini does not know the shop – neither does a big model, it has never seen this FAQ:
RubyLLM.chat.ask("Can I pay with my card?").contentBut we can find the right entry and hand it to the model with the question. That is RAG, retrieval-augmented generation: search first, then let the model answer from what was found:
question = "Can I pay with my card?"
note = closest(question, faq, faq_vectors)
RubyLLM.chat.with_instructions("Answer from these notes:\n#{note}").ask(question).contentFor five notes we could just send all of them. A shop with 20,000 help pages cannot: they do not fit into the model's context, and every token costs. The search picks the few that matter – and the model answers from your texts instead of making something up.
A real embedding model: RubyLLM.embed(texts, model: "text-embedding-3-small") (OpenAI, 1536 numbers), or a local one with Ollama. Embed your documents once and keep the vectors in a database that can search them: PostgreSQL with pgvector and the gem neighbor (has_neighbors :embedding, then nearest_neighbors(:embedding, q, distance: "cosine")), or SQLite with sqlite-vec. RubyLLM.rerank sorts the hits once more with a model that reads question and text together.
shop_answer(question, texts, vectors) that answers questions about the shop, like closest with the texts and their vectors: it finds the closest entry and returns the reply of a chat that has this entry in its instructions. Try it: shop_answer("When is the shop open?", faq, faq_vectors).install_gem "ruby_llm"
require "ruby_llm"
require "chunky_llm" # Chunky Mini, the little model in this page
# def shop_answer(question, texts, vectors)
# ...
# end
#
# shop_answer("When is the shop open?", faq, faq_vectors)