Skip to main content

Views

A view, in this context, is a virtual table that is based on result-set of an SQL query. Using views, one can create a similarity search to go over the vector embeddings and get the most relevant results.

For example, we embedded our data using vector embeddings but now we want to do a semantic search on it. We will use the VIEW.

CREATE VIEW similarity(query String "Description of the query") AS
WITH tbl AS (
SELECT CAST(embed($query) AS `Array`(`Float32`)) AS query
)
SELECT
p.id as id,
p.content as content,
cosineDistance(embeddings, query) as similarity,
p.filename as company
FROM
pdf_embeddings AS pe
JOIN
pdfs AS p ON p.id = pe.id
CROSS JOIN
tbl
ORDER BY
similarity ASC
LIMIT 5

This VIEW generates embeddings for the query argument and compares them with those in the pdf_embeddings table (using their cosineDistances) to find the five most similar text segments.