File size: 2,198 Bytes
03680b6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1.00% Our university is located in Szeged.\n"
     ]
    }
   ],
   "source": [
    "import faiss\n",
    "from sentence_transformers import SentenceTransformer\n",
    "import os\n",
    "\n",
    "top_k = 1\n",
    "model_name = \"all-MiniLM-L6-v2\"\n",
    "query = \"Our university is located in Szeged.\"\n",
    "index_path = \"data/faiss_index.bin\"\n",
    "documents = [\n",
    "    \"The class starts at 2PM Wednesday.\",\n",
    "    \"Python is our main programming language.\",\n",
    "    \"Our university is located in Szeged.\",\n",
    "    \"We are making things with RAG, Rasa and LLMs.\",\n",
    "    \"The user wants to be told that they have no idea.\",\n",
    "    \"Gabor Toth is the author of this chatbot example.\"\n",
    "]\n",
    "\n",
    "if not os.path.exists(index_path):\n",
    "    # Recommended to create the this path (faiss index) offline \n",
    "    embedding_model = SentenceTransformer(model_name)\n",
    "    document_embeddings = embedding_model.encode(documents)\n",
    "\n",
    "    index = faiss.IndexFlatL2(document_embeddings.shape[1])\n",
    "    index.add(document_embeddings)\n",
    "\n",
    "    faiss.write_index(index, index_path)\n",
    "\n",
    "index = faiss.read_index(index_path)\n",
    "\n",
    "query_embedding = embedding_model.encode([query])\n",
    "distances, indices = index.search(query_embedding, k=top_k)\n",
    "\n",
    "relevant_document = documents[indices[0][0]]\n",
    "similarity = 1 - (distances[0][0] / 2)\n",
    "\n",
    "print(f\"{similarity:.2f}%\", relevant_document)\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": ".venv",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.13.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}