Skip to main content
Output
Install dependencies

1. Import FAISS

faiss is used to create and search a vector index. In this project, it performs similarity search between:
  • Stored sentence embeddings
  • The new query embedding

2. Import SentenceTransformer

SentenceTransformer converts text into numerical vectors called embeddings. For example:
These vectors represent the semantic meaning of the sentence.

3. Load the embedding model

This loads the all-MiniLM-L6-v2 sentence embedding model. The model converts sentences into vectors with 384 dimensions. Conceptually:
For example:

4. Define the sentences

This is the collection of sentences that will be stored in the vector index. Each sentence will be converted into an embedding.
The position of each sentence is important. For example:
returns:

5. Convert sentences into embeddings

The encode() method converts every sentence into a numerical vector. Before:
After:
Since there are 5 sentences, the shape will approximately be:
Meaning:

6. Normalize the embeddings

This normalizes every vector to have a length of 1. Mathematically:
Normalization is useful because you are using:
After normalization:
This means:

7. Get the embedding dimension

embeddings.shape contains the dimensions of the embedding array. Since there are 5 sentences and each embedding has 384 values:
will be:
Therefore:
means:
And:
means:
So:

8. Create a FAISS index

This creates a FAISS index. IndexFlatIP means:
  • IndexFlat → Performs exact search
  • IP → Uses Inner Product
Since the embeddings are normalized:
The index expects vectors with:
So the process is:

9. Add embeddings to the index

This stores all 5 sentence embeddings inside the FAISS index. Before:
After:
The order of vectors matches the order of the original sentences list.

10. Print the number of vectors

index.ntotal returns the total number of vectors stored in the FAISS index. Output:

11. Define a new query

This is the sentence you want to search for. FAISS cannot directly compare text. So the query must also be converted into an embedding. The flow is:
Small correction: artifical should be artificial.
Although the embedding model can often still understand the misspelled version.

12. Convert the query into an embedding

The query is placed inside a list:
because FAISS expects a batch of vectors. The model converts:
into:
The resulting shape will be:
Meaning:
Again:
normalizes the query vector so it is compatible with the normalized vectors stored in the FAISS index.

13. Search the FAISS index

This is the main search operation. The parameters are:
Where:
  • query_embedding is the vector you want to search for.
  • k=3 means return the top 3 most similar vectors.
Conceptually:
FAISS returns two values.

14. Understanding scores

Contains the similarity scores. Example:
Since you are using normalized embeddings with IndexFlatIP:
For example:
The exact values can vary depending on the model version and environment.

15. Understanding indices

Contains the positions of the matching sentences in the original list. For example:
This means:
So FAISS does not directly return the sentence text. It returns the index positions, which you use to retrieve the original sentences.

16. Print the query

\n adds a new line before printing. Example:

17. Print the heading

Output:

18. Loop through the search results

Let’s assume:
Then:
returns:
enumerate() gives both the position and the value. The loop works like this:

19. Get the original sentence

If:
then:
returns:

20. Print the rank and similarity score

Let’s break it down.

rank + 1

Python indexing starts at 0, but rankings should start at 1.

sentences[sentence_index]

Retrieves the original sentence.

scores[0][rank]

Gets the similarity score for the current result. For example:
Then:
returns:

:.4f

Formats the number to 4 decimal places. For example:
becomes:

Complete Workflow

The main idea of your code is: