Skip to main content
Output:

1. Import required libraries

SentenceTransformer

This imports the SentenceTransformer class. It is used to load a pretrained model that converts sentences into numerical vectors called embeddings.

cosine_similarity

This imports the cosine similarity function from scikit-learn. It is used to compare two embeddings and determine how similar their meanings are.

2. Load the sentence embedding model

This loads the pretrained model:
The model converts text like:
into a numerical vector similar to:
This model produces embeddings with 384 dimensions. The same model is used for both:
  • FAQ sentences
  • User query
Using the same model ensures that the embeddings can be meaningfully compared.

3. Define FAQ sentences

This creates a Python list containing six FAQ questions. Conceptually:
These are the documents that we want to search.

4. Generate embeddings for FAQ sentences

The encode() method converts every FAQ sentence into an embedding. Before:
After:
Since there are 6 FAQ sentences, you get 6 embeddings. With all-MiniLM-L6-v2, the shape will approximately be:
This means:

5. Define a user query

This represents the question entered by a user. Although the user does not use the exact words:
the sentence has a similar meaning. This is the advantage of semantic search. Keyword search might look for exact words, while sentence embeddings try to understand the meaning.

6. Generate an embedding for the query

The query is placed inside a list:
This becomes:
The model converts it into an embedding. The resulting shape is approximately:
Meaning:

7. Calculate cosine similarity

This is the most important part of the retrieval process. The cosine similarity function compares:
Conceptually:
The result before [0] looks like:
Since there is only one query, [0] extracts the first row:
Result:
Each score corresponds to one FAQ. For example:
A higher score generally means the sentences are more semantically similar.

8. Combine FAQs with similarity scores

The zip() function combines each FAQ with its corresponding similarity score. For example:
And:
After:
You get pairs like:
The list() converts the result into a list:

9. Sort results from highest to lowest

This sorts the FAQ results according to their similarity score.

lambda x: x[1]

Each item in results looks like:
So:
is:
And:
is:
Therefore:
means:
Sort the results using the similarity score.

reverse=True

By default, sorting happens from smallest to largest.
Using:
changes it to:
So the most relevant FAQ appears first.

10. Print the user query

The f before the string means this is an f-string. It allows you to insert variables directly.
is replaced with:
\n adds a new line. Output:

11. Print a heading

This simply prints:

12. Loop through all results

Each item in results contains two values:
For example:
Python automatically assigns:
and:
The loop repeats for every FAQ.

13. Print each FAQ and its score

{score:.4f}

This formats the score to four decimal places. For example:
becomes:
The output might look like:

14. Select the best matching FAQ

Since the list was already sorted in descending order:
The first item contains the best match. For example:
might be:
Python then separates the values:

15. Print the best match

Output:
Then:
Output:

Complete Retrieval Flow

Main Concept

The important part of this program is that the user can write:
while the stored FAQ says:
The words are not exactly the same, but their meanings are similar. Sentence embeddings and cosine similarity allow the program to identify that relationship.