GaborToth2 commited on
Commit
03680b6
·
1 Parent(s): 264a47b

RAG notebook handling with requirements

Browse files
Files changed (4) hide show
  1. .gitignore +177 -0
  2. app.py +2 -4
  3. main.ipynb +77 -0
  4. requirements.txt +3 -1
.gitignore ADDED
@@ -0,0 +1,177 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Custom
2
+ data/
3
+
4
+ # Byte-compiled / optimized / DLL files
5
+ __pycache__/
6
+ *.py[cod]
7
+ *$py.class
8
+
9
+ # C extensions
10
+ *.so
11
+
12
+ # Distribution / packaging
13
+ .Python
14
+ build/
15
+ develop-eggs/
16
+ dist/
17
+ downloads/
18
+ eggs/
19
+ .eggs/
20
+ lib/
21
+ lib64/
22
+ parts/
23
+ sdist/
24
+ var/
25
+ wheels/
26
+ share/python-wheels/
27
+ *.egg-info/
28
+ .installed.cfg
29
+ *.egg
30
+ MANIFEST
31
+
32
+ # PyInstaller
33
+ # Usually these files are written by a python script from a template
34
+ # before PyInstaller builds the exe, so as to inject date/other infos into it.
35
+ *.manifest
36
+ *.spec
37
+
38
+ # Installer logs
39
+ pip-log.txt
40
+ pip-delete-this-directory.txt
41
+
42
+ # Unit test / coverage reports
43
+ htmlcov/
44
+ .tox/
45
+ .nox/
46
+ .coverage
47
+ .coverage.*
48
+ .cache
49
+ nosetests.xml
50
+ coverage.xml
51
+ *.cover
52
+ *.py,cover
53
+ .hypothesis/
54
+ .pytest_cache/
55
+ cover/
56
+
57
+ # Translations
58
+ *.mo
59
+ *.pot
60
+
61
+ # Django stuff:
62
+ *.log
63
+ local_settings.py
64
+ db.sqlite3
65
+ db.sqlite3-journal
66
+
67
+ # Flask stuff:
68
+ instance/
69
+ .webassets-cache
70
+
71
+ # Scrapy stuff:
72
+ .scrapy
73
+
74
+ # Sphinx documentation
75
+ docs/_build/
76
+
77
+ # PyBuilder
78
+ .pybuilder/
79
+ target/
80
+
81
+ # Jupyter Notebook
82
+ .ipynb_checkpoints
83
+
84
+ # IPython
85
+ profile_default/
86
+ ipython_config.py
87
+
88
+ # pyenv
89
+ # For a library or package, you might want to ignore these files since the code is
90
+ # intended to run in multiple environments; otherwise, check them in:
91
+ # .python-version
92
+
93
+ # pipenv
94
+ # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
95
+ # However, in case of collaboration, if having platform-specific dependencies or dependencies
96
+ # having no cross-platform support, pipenv may install dependencies that don't work, or not
97
+ # install all needed dependencies.
98
+ #Pipfile.lock
99
+
100
+ # UV
101
+ # Similar to Pipfile.lock, it is generally recommended to include uv.lock in version control.
102
+ # This is especially recommended for binary packages to ensure reproducibility, and is more
103
+ # commonly ignored for libraries.
104
+ #uv.lock
105
+
106
+ # poetry
107
+ # Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
108
+ # This is especially recommended for binary packages to ensure reproducibility, and is more
109
+ # commonly ignored for libraries.
110
+ # https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
111
+ #poetry.lock
112
+
113
+ # pdm
114
+ # Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
115
+ #pdm.lock
116
+ # pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
117
+ # in version control.
118
+ # https://pdm.fming.dev/latest/usage/project/#working-with-version-control
119
+ .pdm.toml
120
+ .pdm-python
121
+ .pdm-build/
122
+
123
+ # PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
124
+ __pypackages__/
125
+
126
+ # Celery stuff
127
+ celerybeat-schedule
128
+ celerybeat.pid
129
+
130
+ # SageMath parsed files
131
+ *.sage.py
132
+
133
+ # Environments
134
+ .env
135
+ .venv
136
+ env/
137
+ venv/
138
+ ENV/
139
+ env.bak/
140
+ venv.bak/
141
+
142
+ # Spyder project settings
143
+ .spyderproject
144
+ .spyproject
145
+
146
+ # Rope project settings
147
+ .ropeproject
148
+
149
+ # mkdocs documentation
150
+ /site
151
+
152
+ # mypy
153
+ .mypy_cache/
154
+ .dmypy.json
155
+ dmypy.json
156
+
157
+ # Pyre type checker
158
+ .pyre/
159
+
160
+ # pytype static type analyzer
161
+ .pytype/
162
+
163
+ # Cython debug symbols
164
+ cython_debug/
165
+
166
+ # PyCharm
167
+ # JetBrains specific template is maintained in a separate JetBrains.gitignore that can
168
+ # be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
169
+ # and can be added to the global gitignore or merged into this file. For a more nuclear
170
+ # option (not recommended) you can uncomment the following to ignore the entire idea folder.
171
+ #.idea/
172
+
173
+ # Ruff stuff:
174
+ .ruff_cache/
175
+
176
+ # PyPI configuration file
177
+ .pypirc
app.py CHANGED
@@ -23,7 +23,6 @@ index.add(document_embeddings_np)
23
 
24
 
25
  client = InferenceClient("meta-llama/Llama-3.2-3B-Instruct")
26
- COHERE_API_KEY = os.getenv("COHERE_API_KEY")
27
 
28
  def respond(
29
  message,
@@ -34,9 +33,8 @@ def respond(
34
  top_p,
35
  ):
36
 
37
- query_embedding = embedding_model.encode([message], convert_to_tensor=True)
38
- query_embedding_np = query_embedding.cpu().numpy()
39
- distances, indices = index.search(query_embedding_np, k=1)
40
  relevant_document = documents[indices[0][0]]
41
  messages = [{"role": "system", "content": system_message},{"role": "system", "content": f"context: {relevant_document}"}]
42
 
 
23
 
24
 
25
  client = InferenceClient("meta-llama/Llama-3.2-3B-Instruct")
 
26
 
27
  def respond(
28
  message,
 
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
 
main.ipynb ADDED
@@ -0,0 +1,77 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ }
requirements.txt CHANGED
@@ -1,3 +1,5 @@
1
  huggingface_hub==0.25.2
2
  faiss-cpu
3
- sentence_transformers
 
 
 
1
  huggingface_hub==0.25.2
2
  faiss-cpu
3
+ sentence_transformers
4
+ notebook
5
+ ipywidgets