> ## Documentation Index
> Fetch the complete documentation index at: https://ai.tharung.in/llms.txt
> Use this file to discover all available pages before exploring further.

# FAISS Code

```python theme={null}
import faiss
from sentence_transformers import SentenceTransformer

# 1. Load the embedding model
model = SentenceTransformer("all-MiniLM-L6-v2")

# 2. Define sentences
sentences = [
    "I love learning machine learning",
    "Artificial intelligence is fascinating",
    "Python is a popular programming language",
    "I enjoy studying deep learning",
    "Pizza is my favorite food"
]

# 3. Convert sentences into embeddings
embeddings = model.encode(
    sentences,
    normalize_embeddings=True
)

# 4. Get embedding dimension
dimension = embeddings.shape[1]

# 5. Create a FAISS index
index = faiss.IndexFlatIP(dimension)

# 6. Add embeddings to the index
index.add(embeddings)

print("Number of vectors in index:", index.ntotal)

# 7. Define a new query sentence
query = "I am interested in artificial intelligence"

# 8. Convert the query into an embedding
query_embedding = model.encode(
    [query],
    normalize_embeddings=True
)

# 9. Search for the 3 most similar sentences
scores, indices = index.search(query_embedding, k=3)

# 10. Display results
print("\nQuery:", query)
print("\nMost similar sentences:")

for rank, sentence_index in enumerate(indices[0]):
    print(
        f"{rank + 1}. {sentences[sentence_index]}"
        f" | Similarity Score: {scores[0][rank]:.4f}"
    )
```

Output

```text theme={null}
python faiss_demo.py
Warning: You are sending unauthenticated requests to the HF Hub. Please set a HF_TOKEN to enable higher rate limits and faster downloads.
Loading weights: 100%|████████████████████████| 103/103 [00:00<00:00, 3029.50it/s]
Number of vectors in index:  5

Query: I am interested in artifical intelligence

Most similar sentences:
1. Artificial intelligence is fascinating | Similarity Score: 0.5165
2. I enjoy studying deep learning | Similarity Score: 0.2867
3. I love learning machine learning | Similarity Score: 0.2608
```

Install dependencies

```text theme={null}
pip install faiss-cpu sentence-transformers
```

***

### 1. Import FAISS

```python theme={null}
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`

```python theme={null}
from sentence_transformers import SentenceTransformer
```

`SentenceTransformer` converts text into numerical vectors called **embeddings**.

For example:

```text theme={null}
"I love machine learning"

↓

[0.12, -0.45, 0.78, ...]
```

These vectors represent the semantic meaning of the sentence.

***

### 3. Load the embedding model

```python theme={null}
model = SentenceTransformer("all-MiniLM-L6-v2")
```

This loads the `all-MiniLM-L6-v2` sentence embedding model.

The model converts sentences into vectors with **384 dimensions**.

Conceptually:

```text theme={null}
Sentence
   ↓
all-MiniLM-L6-v2
   ↓
384-dimensional embedding vector
```

For example:

```text theme={null}
"I love AI"

↓

[0.21, -0.43, 0.12, ..., 0.67]
```

***

### 4. Define the sentences

```python theme={null}
sentences = [
    "I love learning machine learning",
    "Artificial intelligence is fascinating",
    "Python is a popular programming language",
    "I enjoy studying deep learning",
    "Pizza is my favorite food",
]
```

This is the collection of sentences that will be stored in the vector index.

Each sentence will be converted into an embedding.

```text theme={null}
Sentence 1 → Embedding 1
Sentence 2 → Embedding 2
Sentence 3 → Embedding 3
Sentence 4 → Embedding 4
Sentence 5 → Embedding 5
```

The position of each sentence is important.

For example:

```python theme={null}
sentences[0]
```

returns:

```text theme={null}
I love learning machine learning
```

***

### 5. Convert sentences into embeddings

```python theme={null}
embeddings = model.encode(
    sentences,
    normalize_embeddings=True
)
```

The `encode()` method converts every sentence into a numerical vector.

Before:

```text theme={null}
[
    "I love learning machine learning",
    "Artificial intelligence is fascinating",
    ...
]
```

After:

```text theme={null}
[
    [0.12, -0.45, 0.78, ...],
    [0.34, 0.21, -0.11, ...],
    ...
]
```

Since there are 5 sentences, the shape will approximately be:

```python theme={null}
(5, 384)
```

Meaning:

```text theme={null}
5 vectors
×
384 dimensions each
```

***

### 6. Normalize the embeddings

```python theme={null}
normalize_embeddings=True
```

This normalizes every vector to have a length of `1`.

Mathematically:

```text theme={null}
Normalized Vector = Vector / ||Vector||
```

Normalization is useful because you are using:

```python theme={null}
faiss.IndexFlatIP()
```

After normalization:

```text theme={null}
Inner Product ≈ Cosine Similarity
```

This means:

```text theme={null}
Higher score = More similar
Lower score = Less similar
```

***

### 7. Get the embedding dimension

```python theme={null}
dimension = embeddings.shape[1]
```

`embeddings.shape` contains the dimensions of the embedding array.

Since there are 5 sentences and each embedding has 384 values:

```python theme={null}
embeddings.shape
```

will be:

```python theme={null}
(5, 384)
```

Therefore:

```python theme={null}
embeddings.shape[0]
```

means:

```text theme={null}
5 embeddings
```

And:

```python theme={null}
embeddings.shape[1]
```

means:

```text theme={null}
384 dimensions
```

So:

```python theme={null}
dimension = 384
```

***

### 8. Create a FAISS index

```python theme={null}
index = faiss.IndexFlatIP(dimension)
```

This creates a FAISS index.

`IndexFlatIP` means:

* `IndexFlat` → Performs exact search
* `IP` → Uses Inner Product

Since the embeddings are normalized:

```text theme={null}
Inner Product ≈ Cosine Similarity
```

The index expects vectors with:

```text theme={null}
384 dimensions
```

So the process is:

```text theme={null}
384-dimensional vectors
        ↓
IndexFlatIP(384)
        ↓
FAISS Index
```

***

### 9. Add embeddings to the index

```python theme={null}
index.add(embeddings)
```

This stores all 5 sentence embeddings inside the FAISS index.

Before:

```text theme={null}
FAISS Index

Empty
```

After:

```text theme={null}
FAISS Index

Vector 0 → I love learning machine learning
Vector 1 → Artificial intelligence is fascinating
Vector 2 → Python is a popular programming language
Vector 3 → I enjoy studying deep learning
Vector 4 → Pizza is my favorite food
```

The order of vectors matches the order of the original `sentences` list.

***

### 10. Print the number of vectors

```python theme={null}
print("Number of vectors in index: ", index.ntotal)
```

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

Output:

```text theme={null}
Number of vectors in index: 5
```

***

### 11. Define a new query

```python theme={null}
query = "I am interested in artifical intelligence"
```

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:

```text theme={null}
Query Text
    ↓
Embedding Model
    ↓
Query Vector
    ↓
FAISS Search
```

Small correction: `artifical` should be `artificial`.

```python theme={null}
query = "I am interested in artificial intelligence"
```

Although the embedding model can often still understand the misspelled version.

***

### 12. Convert the query into an embedding

```python theme={null}
query_embedding = model.encode(
    [query],
    normalize_embeddings=True
)
```

The query is placed inside a list:

```python theme={null}
[query]
```

because FAISS expects a batch of vectors.

The model converts:

```text theme={null}
"I am interested in artificial intelligence"
```

into:

```text theme={null}
[0.21, -0.34, 0.56, ..., 0.12]
```

The resulting shape will be:

```python theme={null}
(1, 384)
```

Meaning:

```text theme={null}
1 query vector
×
384 dimensions
```

Again:

```python theme={null}
normalize_embeddings=True
```

normalizes the query vector so it is compatible with the normalized vectors stored in the FAISS index.

***

### 13. Search the FAISS index

```python theme={null}
scores, indices = index.search(query_embedding, k=3)
```

This is the main search operation.

The parameters are:

```python theme={null}
index.search(query_embedding, k=3)
```

Where:

* `query_embedding` is the vector you want to search for.
* `k=3` means return the top 3 most similar vectors.

Conceptually:

```text theme={null}
Query:
"I am interested in artificial intelligence"

                ↓

          Query Embedding

                ↓

        FAISS Similarity Search

                ↓

Top 3 Matching Sentences
```

FAISS returns two values.

***

## 14. Understanding `scores`

```python theme={null}
scores
```

Contains the similarity scores.

Example:

```python theme={null}
[[0.82, 0.61, 0.55]]
```

Since you are using normalized embeddings with `IndexFlatIP`:

```text theme={null}
Higher score = More similar
```

For example:

```text theme={null}
0.82 → Highly similar
0.61 → Moderately similar
0.55 → Less similar
```

The exact values can vary depending on the model version and environment.

***

## 15. Understanding `indices`

```python theme={null}
indices
```

Contains the positions of the matching sentences in the original list.

For example:

```python theme={null}
[[1, 3, 0]]
```

This means:

```python theme={null}
sentences[1]
```

```text theme={null}
Artificial intelligence is fascinating
```

```python theme={null}
sentences[3]
```

```text theme={null}
I enjoy studying deep learning
```

```python theme={null}
sentences[0]
```

```text theme={null}
I love learning machine learning
```

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

```python theme={null}
print("\nQuery:", query)
```

`\n` adds a new line before printing.

Example:

```text theme={null}
Query: I am interested in artificial intelligence
```

***

### 17. Print the heading

```python theme={null}
print("\nMost similar sentences:")
```

Output:

```text theme={null}
Most similar sentences:
```

***

### 18. Loop through the search results

```python theme={null}
for rank, sentence_index in enumerate(indices[0]):
```

Let's assume:

```python theme={null}
indices = [[1, 3, 0]]
```

Then:

```python theme={null}
indices[0]
```

returns:

```python theme={null}
[1, 3, 0]
```

`enumerate()` gives both the position and the value.

The loop works like this:

```text theme={null}
Iteration 1:
rank = 0
sentence_index = 1

Iteration 2:
rank = 1
sentence_index = 3

Iteration 3:
rank = 2
sentence_index = 0
```

***

### 19. Get the original sentence

```python theme={null}
sentences[sentence_index]
```

If:

```python theme={null}
sentence_index = 1
```

then:

```python theme={null}
sentences[1]
```

returns:

```text theme={null}
Artificial intelligence is fascinating
```

***

### 20. Print the rank and similarity score

```python theme={null}
print(
    f"{rank + 1}. {sentences[sentence_index]}"
    f" | Similarity Score: {scores[0][rank]:.4f}"
)
```

Let's break it down.

### `rank + 1`

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

```text theme={null}
rank = 0 → Rank 1
rank = 1 → Rank 2
rank = 2 → Rank 3
```

### `sentences[sentence_index]`

Retrieves the original sentence.

### `scores[0][rank]`

Gets the similarity score for the current result.

For example:

```python theme={null}
scores = [[0.82, 0.61, 0.55]]
```

Then:

```python theme={null}
scores[0][0]
```

returns:

```text theme={null}
0.82
```

### `:.4f`

Formats the number to 4 decimal places.

For example:

```python theme={null}
0.8234567
```

becomes:

```text theme={null}
0.8235
```

***

## Complete Workflow

```text theme={null}
5 Sentences
     ↓
SentenceTransformer
     ↓
5 Embeddings
     ↓
Normalize Embeddings
     ↓
FAISS IndexFlatIP
     ↓
Store Vectors
     ↓
─────────────────────
New Query Sentence
     ↓
SentenceTransformer
     ↓
Query Embedding
     ↓
Normalize Embedding
     ↓
FAISS Search
     ↓
Top 3 Indices + Scores
     ↓
Retrieve Original Sentences
```

The main idea of your code is:

```text theme={null}
Text
 ↓
Embedding
 ↓
FAISS Vector Index
 ↓
New Query
 ↓
Embedding
 ↓
Similarity Search
 ↓
Most Similar Sentences
```
