Spaces:
Sleeping
Sleeping
Commit
·
2636944
1
Parent(s):
03680b6
refresh chatbot and creating notebook
Browse files- app.py +8 -10
- main.ipynb +0 -77
- original.ipynb +152 -0
app.py
CHANGED
@@ -5,6 +5,8 @@ import faiss
|
|
5 |
from transformers import pipeline
|
6 |
from sentence_transformers import SentenceTransformer
|
7 |
|
|
|
|
|
8 |
documents = [
|
9 |
"The class starts at 2PM Wednesday.",
|
10 |
"Python is our main programming language.",
|
@@ -13,14 +15,10 @@ documents = [
|
|
13 |
"The user wants to be told that they have no idea.",
|
14 |
"Gabor Toth is the author of this chatbot."
|
15 |
]
|
16 |
-
|
17 |
embedding_model = SentenceTransformer('all-MiniLM-L6-v2')
|
18 |
-
document_embeddings = embedding_model.encode(documents
|
19 |
-
|
20 |
-
|
21 |
-
index = faiss.IndexFlatL2(document_embeddings_np.shape[1])
|
22 |
-
index.add(document_embeddings_np)
|
23 |
-
|
24 |
|
25 |
client = InferenceClient("meta-llama/Llama-3.2-3B-Instruct")
|
26 |
|
@@ -33,13 +31,13 @@ def respond(
|
|
33 |
top_p,
|
34 |
):
|
35 |
|
|
|
36 |
query_embedding = embedding_model.encode([message])
|
37 |
distances, indices = index.search(query_embedding, k=1)
|
38 |
relevant_document = documents[indices[0][0]]
|
39 |
-
messages = [{"role": "system", "content": system_message},{"role": "system", "content": f"context: {relevant_document}"}]
|
40 |
-
|
41 |
-
|
42 |
|
|
|
|
|
43 |
for val in history:
|
44 |
if val[0]:
|
45 |
messages.append({"role": "user", "content": val[0]})
|
|
|
5 |
from transformers import pipeline
|
6 |
from sentence_transformers import SentenceTransformer
|
7 |
|
8 |
+
|
9 |
+
# Documents converted into FAISS vectors
|
10 |
documents = [
|
11 |
"The class starts at 2PM Wednesday.",
|
12 |
"Python is our main programming language.",
|
|
|
15 |
"The user wants to be told that they have no idea.",
|
16 |
"Gabor Toth is the author of this chatbot."
|
17 |
]
|
|
|
18 |
embedding_model = SentenceTransformer('all-MiniLM-L6-v2')
|
19 |
+
document_embeddings = embedding_model.encode(documents)
|
20 |
+
index = faiss.IndexFlatL2(document_embeddings.shape[1])
|
21 |
+
index.add(document_embeddings)
|
|
|
|
|
|
|
22 |
|
23 |
client = InferenceClient("meta-llama/Llama-3.2-3B-Instruct")
|
24 |
|
|
|
31 |
top_p,
|
32 |
):
|
33 |
|
34 |
+
# Get relevant document
|
35 |
query_embedding = embedding_model.encode([message])
|
36 |
distances, indices = index.search(query_embedding, k=1)
|
37 |
relevant_document = documents[indices[0][0]]
|
|
|
|
|
|
|
38 |
|
39 |
+
# Set prompt
|
40 |
+
messages = [{"role": "system", "content": system_message},{"role": "system", "content": f"context: {relevant_document}"}]
|
41 |
for val in history:
|
42 |
if val[0]:
|
43 |
messages.append({"role": "user", "content": val[0]})
|
main.ipynb
DELETED
@@ -1,77 +0,0 @@
|
|
1 |
-
{
|
2 |
-
"cells": [
|
3 |
-
{
|
4 |
-
"cell_type": "code",
|
5 |
-
"execution_count": null,
|
6 |
-
"metadata": {},
|
7 |
-
"outputs": [
|
8 |
-
{
|
9 |
-
"name": "stdout",
|
10 |
-
"output_type": "stream",
|
11 |
-
"text": [
|
12 |
-
"1.00% Our university is located in Szeged.\n"
|
13 |
-
]
|
14 |
-
}
|
15 |
-
],
|
16 |
-
"source": [
|
17 |
-
"import faiss\n",
|
18 |
-
"from sentence_transformers import SentenceTransformer\n",
|
19 |
-
"import os\n",
|
20 |
-
"\n",
|
21 |
-
"top_k = 1\n",
|
22 |
-
"model_name = \"all-MiniLM-L6-v2\"\n",
|
23 |
-
"query = \"Our university is located in Szeged.\"\n",
|
24 |
-
"index_path = \"data/faiss_index.bin\"\n",
|
25 |
-
"documents = [\n",
|
26 |
-
" \"The class starts at 2PM Wednesday.\",\n",
|
27 |
-
" \"Python is our main programming language.\",\n",
|
28 |
-
" \"Our university is located in Szeged.\",\n",
|
29 |
-
" \"We are making things with RAG, Rasa and LLMs.\",\n",
|
30 |
-
" \"The user wants to be told that they have no idea.\",\n",
|
31 |
-
" \"Gabor Toth is the author of this chatbot example.\"\n",
|
32 |
-
"]\n",
|
33 |
-
"\n",
|
34 |
-
"if not os.path.exists(index_path):\n",
|
35 |
-
" # Recommended to create the this path (faiss index) offline \n",
|
36 |
-
" embedding_model = SentenceTransformer(model_name)\n",
|
37 |
-
" document_embeddings = embedding_model.encode(documents)\n",
|
38 |
-
"\n",
|
39 |
-
" index = faiss.IndexFlatL2(document_embeddings.shape[1])\n",
|
40 |
-
" index.add(document_embeddings)\n",
|
41 |
-
"\n",
|
42 |
-
" faiss.write_index(index, index_path)\n",
|
43 |
-
"\n",
|
44 |
-
"index = faiss.read_index(index_path)\n",
|
45 |
-
"\n",
|
46 |
-
"query_embedding = embedding_model.encode([query])\n",
|
47 |
-
"distances, indices = index.search(query_embedding, k=top_k)\n",
|
48 |
-
"\n",
|
49 |
-
"relevant_document = documents[indices[0][0]]\n",
|
50 |
-
"similarity = 1 - (distances[0][0] / 2)\n",
|
51 |
-
"\n",
|
52 |
-
"print(f\"{similarity:.2f}%\", relevant_document)\n"
|
53 |
-
]
|
54 |
-
}
|
55 |
-
],
|
56 |
-
"metadata": {
|
57 |
-
"kernelspec": {
|
58 |
-
"display_name": ".venv",
|
59 |
-
"language": "python",
|
60 |
-
"name": "python3"
|
61 |
-
},
|
62 |
-
"language_info": {
|
63 |
-
"codemirror_mode": {
|
64 |
-
"name": "ipython",
|
65 |
-
"version": 3
|
66 |
-
},
|
67 |
-
"file_extension": ".py",
|
68 |
-
"mimetype": "text/x-python",
|
69 |
-
"name": "python",
|
70 |
-
"nbconvert_exporter": "python",
|
71 |
-
"pygments_lexer": "ipython3",
|
72 |
-
"version": "3.13.0"
|
73 |
-
}
|
74 |
-
},
|
75 |
-
"nbformat": 4,
|
76 |
-
"nbformat_minor": 2
|
77 |
-
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 46,
|
13 |
+
"metadata": {},
|
14 |
+
"outputs": [],
|
15 |
+
"source": [
|
16 |
+
"%%capture\n",
|
17 |
+
"!pip install faiss-cpu sentence_transformers"
|
18 |
+
]
|
19 |
+
},
|
20 |
+
{
|
21 |
+
"cell_type": "markdown",
|
22 |
+
"metadata": {},
|
23 |
+
"source": [
|
24 |
+
"For this example we will use FAISS (Facebook AI Similarity Search), which is an open-source library optimized for fast nearest neighbor search in high-dimensional spaces."
|
25 |
+
]
|
26 |
+
},
|
27 |
+
{
|
28 |
+
"cell_type": "code",
|
29 |
+
"execution_count": 47,
|
30 |
+
"metadata": {},
|
31 |
+
"outputs": [],
|
32 |
+
"source": [
|
33 |
+
"import faiss # We will use FAISS for similarity search\n",
|
34 |
+
"from sentence_transformers import SentenceTransformer # This will provide us with the embedding model\n",
|
35 |
+
"import os # Read and Write files (for FAISS to speed up later searching)"
|
36 |
+
]
|
37 |
+
},
|
38 |
+
{
|
39 |
+
"cell_type": "markdown",
|
40 |
+
"metadata": {},
|
41 |
+
"source": [
|
42 |
+
"We will also use `all-MiniLM-L6-v2` embedding model, which is used to convert text into dense vector embeddings, capturing semantic meaning. These embeddings can then be utilized for various NLP tasks such as similarity search, clustering, information retrieval, and retrieval-augmented generation (RAG)."
|
43 |
+
]
|
44 |
+
},
|
45 |
+
{
|
46 |
+
"cell_type": "code",
|
47 |
+
"execution_count": 48,
|
48 |
+
"metadata": {},
|
49 |
+
"outputs": [],
|
50 |
+
"source": [
|
51 |
+
"top_k = 6 # The amount of top documents to retrieve (the best k documents)\n",
|
52 |
+
"index_path = \"data/faiss_index.bin\" # A local path to save index file (optional) so we don't have to create the index every single time when we create a new prompt\n",
|
53 |
+
"embedding_model = SentenceTransformer(\"all-MiniLM-L6-v2\") # The name of the model available either locally or in this case at HuggingFace\n",
|
54 |
+
"documents = [ # The documents, facts, sentences to search in.\n",
|
55 |
+
" \"The class starts at 2PM Wednesday.\",\n",
|
56 |
+
" \"Python is our main programming language.\",\n",
|
57 |
+
" \"Our university is located in Szeged.\",\n",
|
58 |
+
" \"We are making things with RAG, Rasa and LLMs.\",\n",
|
59 |
+
" \"The user wants to be told that they have no idea.\",\n",
|
60 |
+
" \"Gabor Toth is the author of this chatbot example.\"\n",
|
61 |
+
"] "
|
62 |
+
]
|
63 |
+
},
|
64 |
+
{
|
65 |
+
"cell_type": "markdown",
|
66 |
+
"metadata": {},
|
67 |
+
"source": [
|
68 |
+
"Now we will create an index file from the documents using the model. Usually this is part is the most resource intensive part, so it's recommended to create this file offline."
|
69 |
+
]
|
70 |
+
},
|
71 |
+
{
|
72 |
+
"cell_type": "code",
|
73 |
+
"execution_count": 49,
|
74 |
+
"metadata": {},
|
75 |
+
"outputs": [],
|
76 |
+
"source": [
|
77 |
+
"document_embeddings = embedding_model.encode(documents) # The model encodes the documents\n",
|
78 |
+
"index = faiss.IndexFlatL2(document_embeddings.shape[1]) # Create an index for the shape of the encoded documents\n",
|
79 |
+
"index.add(document_embeddings) # Fill the index with the encoded documents\n",
|
80 |
+
"faiss.write_index(index, index_path) # Write the index to the file"
|
81 |
+
]
|
82 |
+
},
|
83 |
+
{
|
84 |
+
"cell_type": "code",
|
85 |
+
"execution_count": 50,
|
86 |
+
"metadata": {},
|
87 |
+
"outputs": [
|
88 |
+
{
|
89 |
+
"name": "stdout",
|
90 |
+
"output_type": "stream",
|
91 |
+
"text": [
|
92 |
+
"0.90633893 Gabor Toth is the author of this chatbot example.\n",
|
93 |
+
"1.3333331 We are making things with RAG, Rasa and LLMs.\n",
|
94 |
+
"1.5074873 The user wants to be told that they have no idea.\n",
|
95 |
+
"1.7030394 Our university is located in Szeged.\n",
|
96 |
+
"1.7619381 Python is our main programming language.\n",
|
97 |
+
"1.8181174 The class starts at 2PM Wednesday.\n"
|
98 |
+
]
|
99 |
+
}
|
100 |
+
],
|
101 |
+
"source": [
|
102 |
+
"index = faiss.read_index(index_path) # Reading the index from file back to the variable\n",
|
103 |
+
"query_embedding = embedding_model.encode([\"Who created this LLM chat interface?\"]) # Try out different prompts\n",
|
104 |
+
"distances, indices = index.search(query_embedding, k=top_k) # Distances and the permutation of indices of our documents\n",
|
105 |
+
"\n",
|
106 |
+
"for rank, i in enumerate(indices[0]): # List the Distance and the documents in order of distance.\n",
|
107 |
+
" print(distances[0][rank], documents[i]) # Lower distance means more similar sentence."
|
108 |
+
]
|
109 |
+
},
|
110 |
+
{
|
111 |
+
"cell_type": "code",
|
112 |
+
"execution_count": 51,
|
113 |
+
"metadata": {},
|
114 |
+
"outputs": [
|
115 |
+
{
|
116 |
+
"data": {
|
117 |
+
"text/plain": [
|
118 |
+
"'Gabor Toth is the author of this chatbot example.'"
|
119 |
+
]
|
120 |
+
},
|
121 |
+
"execution_count": 51,
|
122 |
+
"metadata": {},
|
123 |
+
"output_type": "execute_result"
|
124 |
+
}
|
125 |
+
],
|
126 |
+
"source": [
|
127 |
+
"documents[indices[0][0]] # The most similar document has the lowest distance."
|
128 |
+
]
|
129 |
+
}
|
130 |
+
],
|
131 |
+
"metadata": {
|
132 |
+
"kernelspec": {
|
133 |
+
"display_name": ".venv",
|
134 |
+
"language": "python",
|
135 |
+
"name": "python3"
|
136 |
+
},
|
137 |
+
"language_info": {
|
138 |
+
"codemirror_mode": {
|
139 |
+
"name": "ipython",
|
140 |
+
"version": 3
|
141 |
+
},
|
142 |
+
"file_extension": ".py",
|
143 |
+
"mimetype": "text/x-python",
|
144 |
+
"name": "python",
|
145 |
+
"nbconvert_exporter": "python",
|
146 |
+
"pygments_lexer": "ipython3",
|
147 |
+
"version": "3.13.0"
|
148 |
+
}
|
149 |
+
},
|
150 |
+
"nbformat": 4,
|
151 |
+
"nbformat_minor": 2
|
152 |
+
}
|