New Jobs Simplified, AI University
← Back to courses

RAG — Retrieval-Augmented Generation

Vector Search & Retrieval

Vector search and retrieval is a technique used to find the nearest vectors to a query in a database. This is useful for tasks such as semantic search, where we want to find documents similar to a query. We can use various algorithms and techniques, such as cosine similarity and singular value decomposition, to perform vector search and retrieval.

Why It Matters

Vector search and retrieval is an important topic in AI because it helps us to efficiently search and retrieve data from large databases. This is useful in applications such as search engines, recommendation systems, and natural language processing. By using vector search and retrieval, we can improve the accuracy and efficiency of these applications.

Key Points

Vector search and retrieval involves finding the nearest vectors to a query in a database.
We can use various algorithms and techniques, such as cosine similarity, to perform vector search and retrieval.
Cosine similarity is a measure of similarity between two vectors, and it is calculated by taking the dot product of the two vectors and dividing by the product of their magnitudes.
Singular value decomposition (SVD) is a technique used to reduce the dimensionality of a matrix, and it is useful for vector search and retrieval.
We can use SVD to factorize a matrix into three matrices: U, D, and V, and then use the product of U and V to perform vector search and retrieval.
Vector search and retrieval is useful for tasks such as semantic search, where we want to find documents similar to a query.
We can use vector search and retrieval to improve the accuracy and efficiency of search engines, recommendation systems, and natural language processing applications.
Some algorithms for vector search and retrieval include A, bidirectional A, IDA, and RBFS.

Key Concepts

Cosine similarity

A measure of similarity between two vectors, calculated by taking the dot product of the two vectors and dividing by the product of their magnitudes.

Singular value decomposition (SVD)

A technique used to reduce the dimensionality of a matrix, useful for vector search and retrieval.

A* search

An algorithm used for finding the shortest path between two points in a weighted graph or network.

Bidirectional A* search

An algorithm used for finding the shortest path between two points in a weighted graph or network, with the ability to search in both directions.

Recursive best-first search (RBFS)

An algorithm used for finding the shortest path between two points in a weighted graph or network, with the ability to search recursively.

Code Examples

Calculates the cosine similarity between two vectors

import numpy as np

def cosine_similarity(vector1, vector2):
    return np.dot(vector1, vector2) / (np.linalg.norm(vector1) * np.linalg.norm(vector2))

Performs SVD on a matrix

import numpy as np

def svd(matrix):
    u, s, vh = np.linalg.svd(matrix)
    return u, s, vh
From the books
“search versus vector databases Once the query is embedded, we need to find the nearest vectors to it from our text archive as we can see in Figure 8-11. The most straightforward way to find the neares…”
“Fortunately, the similarity computation does not require iteration: it can be efficiently performed by using the built-in PyTorch matrix multiplication primitives: def search_documents(query, top_k=5)…”
“metrics. Retrieval-Augmented Generation (RAG) | 257 Summary In this chapter, we looked at different ways of using language models to improve existing search systems and even be the core of new, more p…”