Spaces:
Running
Running
Update semsearch.py
Browse files- semsearch.py +423 -422
semsearch.py
CHANGED
@@ -1,422 +1,423 @@
|
|
1 |
-
import weaviate
|
2 |
-
|
3 |
-
from sentence_transformers import SentenceTransformer
|
4 |
-
from langchain_community.document_loaders import BSHTMLLoader
|
5 |
-
from pathlib import Path
|
6 |
-
from lxml import html
|
7 |
-
import logging
|
8 |
-
from semantic_text_splitter import HuggingFaceTextSplitter
|
9 |
-
from tokenizers import Tokenizer
|
10 |
-
import json
|
11 |
-
import os
|
12 |
-
import re
|
13 |
-
import logging
|
14 |
-
|
15 |
-
import llama_cpp
|
16 |
-
from llama_cpp import Llama
|
17 |
-
import ipywidgets as widgets
|
18 |
-
from IPython.display import display, clear_output
|
19 |
-
|
20 |
-
|
21 |
-
weaviate_logger = logging.getLogger("httpx")
|
22 |
-
weaviate_logger.setLevel(logging.WARNING)
|
23 |
-
|
24 |
-
logger = logging.getLogger(__name__)
|
25 |
-
logging.basicConfig(level=logging.INFO)
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
######################################################################
|
30 |
-
# MAINLINE
|
31 |
-
#
|
32 |
-
logger.info("#### MAINLINE ENTERED.")
|
33 |
-
|
34 |
-
#pathString = "/Users/660565/KPSAllInOne/ProgramFilesX86/WebCopy/DownloadedWebSites/LLMPOC_HTML"
|
35 |
-
pathString = "/app/inputDocs"
|
36 |
-
chunks = []
|
37 |
-
webpageDocNames = []
|
38 |
-
page_contentArray = []
|
39 |
-
webpageChunks = []
|
40 |
-
webpageTitles = []
|
41 |
-
webpageChunksDocNames = []
|
42 |
-
|
43 |
-
#####################################################################
|
44 |
-
# Create UI widgets.
|
45 |
-
output_widget = widgets.Output()
|
46 |
-
with output_widget:
|
47 |
-
print("### Create widgets entered.")
|
48 |
-
|
49 |
-
systemTextArea = widgets.Textarea(
|
50 |
-
value='',
|
51 |
-
placeholder='Enter System Prompt.',
|
52 |
-
description='Sys Prompt: ',
|
53 |
-
disabled=False,
|
54 |
-
layout=widgets.Layout(width='300px', height='80px')
|
55 |
-
)
|
56 |
-
|
57 |
-
userTextArea = widgets.Textarea(
|
58 |
-
value='',
|
59 |
-
placeholder='Enter User Prompt.',
|
60 |
-
description='User Prompt: ',
|
61 |
-
disabled=False,
|
62 |
-
layout=widgets.Layout(width='435px', height='110px')
|
63 |
-
)
|
64 |
-
|
65 |
-
ragPromptTextArea = widgets.Textarea(
|
66 |
-
value='',
|
67 |
-
placeholder='App generated prompt with RAG information.',
|
68 |
-
description='RAG Prompt: ',
|
69 |
-
disabled=False,
|
70 |
-
layout=widgets.Layout(width='580px', height='180px')
|
71 |
-
)
|
72 |
-
|
73 |
-
responseTextArea = widgets.Textarea(
|
74 |
-
value='',
|
75 |
-
placeholder='LLM generated response.',
|
76 |
-
description='LLM Resp: ',
|
77 |
-
disabled=False,
|
78 |
-
layout=widgets.Layout(width='780px', height='200px')
|
79 |
-
)
|
80 |
-
|
81 |
-
selectRag = widgets.Checkbox(
|
82 |
-
value=False,
|
83 |
-
description='Use RAG',
|
84 |
-
disabled=False
|
85 |
-
)
|
86 |
-
|
87 |
-
submitButton = widgets.Button(
|
88 |
-
description='Run Model.',
|
89 |
-
disabled=False,
|
90 |
-
button_style='', # 'success', 'info', 'warning', 'danger' or ''
|
91 |
-
tooltip='Click',
|
92 |
-
icon='check' # (FontAwesome names without the `fa-` prefix)
|
93 |
-
)
|
94 |
-
|
95 |
-
|
96 |
-
#######################################################
|
97 |
-
# Read each text input file, parse it into a document,
|
98 |
-
# chunk it, collect chunks and document name.
|
99 |
-
logger.info("#### Read and chunk input text files.")
|
100 |
-
for filename in os.listdir(pathString):
|
101 |
-
logger.info(filename)
|
102 |
-
path = Path(pathString + "/" + filename)
|
103 |
-
filename = filename.rstrip(".html")
|
104 |
-
webpageDocNames.append(filename)
|
105 |
-
htmlLoader = BSHTMLLoader(path,"utf-8")
|
106 |
-
htmlData = htmlLoader.load()
|
107 |
-
|
108 |
-
title = htmlData[0].metadata['title']
|
109 |
-
page_content = htmlData[0].page_content
|
110 |
-
|
111 |
-
# Clean data. Remove multiple newlines, etc.
|
112 |
-
page_content = re.sub(r'\n+', '\n',page_content)
|
113 |
-
|
114 |
-
page_contentArray.append(page_content);
|
115 |
-
webpageTitles.append(title)
|
116 |
-
max_tokens = 1000
|
117 |
-
tokenizer = Tokenizer.from_pretrained("bert-base-uncased")
|
118 |
-
logger.debug(f"### tokenizer: {tokenizer}")
|
119 |
-
splitter = HuggingFaceTextSplitter(tokenizer, trim_chunks=True)
|
120 |
-
chunksOnePage = splitter.chunks(page_content, chunk_capacity=50)
|
121 |
-
|
122 |
-
chunks = []
|
123 |
-
for chnk in chunksOnePage:
|
124 |
-
logger.debug(f"#### chnk in file: {chnk}")
|
125 |
-
chunks.append(chnk)
|
126 |
-
logger.debug(f"chunks: {chunks}")
|
127 |
-
webpageChunks.append(chunks)
|
128 |
-
webpageChunksDocNames.append(filename + "Chunks")
|
129 |
-
|
130 |
-
logger.debug(f"### filename, title: {filename}, {title}")
|
131 |
-
|
132 |
-
logger.debug(f"### webpageDocNames: {webpageDocNames}")
|
133 |
-
|
134 |
-
|
135 |
-
######################################################
|
136 |
-
# Connect to the Weaviate vector database.
|
137 |
-
logger.info("#### Create Weaviate db client connection.")
|
138 |
-
client = weaviate.connect_to_custom(
|
139 |
-
http_host="127.0.0.1",
|
140 |
-
http_port=8080,
|
141 |
-
http_secure=False,
|
142 |
-
grpc_host="127.0.0.1",
|
143 |
-
grpc_port=50051,
|
144 |
-
grpc_secure=False
|
145 |
-
|
146 |
-
#
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
#
|
154 |
-
#
|
155 |
-
|
156 |
-
|
157 |
-
|
158 |
-
|
159 |
-
|
160 |
-
|
161 |
-
"
|
162 |
-
"
|
163 |
-
"
|
164 |
-
|
165 |
-
"
|
166 |
-
|
167 |
-
|
168 |
-
|
169 |
-
"
|
170 |
-
|
171 |
-
|
172 |
-
|
173 |
-
|
174 |
-
|
175 |
-
"
|
176 |
-
"
|
177 |
-
"
|
178 |
-
"
|
179 |
-
|
180 |
-
|
181 |
-
"
|
182 |
-
"
|
183 |
-
|
184 |
-
|
185 |
-
|
186 |
-
|
187 |
-
|
188 |
-
"
|
189 |
-
|
190 |
-
|
191 |
-
|
192 |
-
|
193 |
-
|
194 |
-
"
|
195 |
-
"
|
196 |
-
"
|
197 |
-
|
198 |
-
|
199 |
-
"
|
200 |
-
|
201 |
-
|
202 |
-
|
203 |
-
|
204 |
-
|
205 |
-
|
206 |
-
|
207 |
-
|
208 |
-
|
209 |
-
|
210 |
-
|
211 |
-
|
212 |
-
|
213 |
-
"
|
214 |
-
"
|
215 |
-
"
|
216 |
-
|
217 |
-
|
218 |
-
|
219 |
-
|
220 |
-
|
221 |
-
"
|
222 |
-
|
223 |
-
|
224 |
-
|
225 |
-
|
226 |
-
|
227 |
-
"
|
228 |
-
"
|
229 |
-
"
|
230 |
-
"
|
231 |
-
|
232 |
-
|
233 |
-
"
|
234 |
-
"
|
235 |
-
|
236 |
-
|
237 |
-
|
238 |
-
|
239 |
-
|
240 |
-
"
|
241 |
-
|
242 |
-
|
243 |
-
|
244 |
-
"
|
245 |
-
"
|
246 |
-
|
247 |
-
|
248 |
-
|
249 |
-
|
250 |
-
"
|
251 |
-
|
252 |
-
|
253 |
-
|
254 |
-
|
255 |
-
|
256 |
-
|
257 |
-
|
258 |
-
|
259 |
-
|
260 |
-
|
261 |
-
|
262 |
-
|
263 |
-
|
264 |
-
|
265 |
-
|
266 |
-
|
267 |
-
|
268 |
-
|
269 |
-
|
270 |
-
|
271 |
-
"
|
272 |
-
"
|
273 |
-
|
274 |
-
|
275 |
-
|
276 |
-
|
277 |
-
|
278 |
-
|
279 |
-
|
280 |
-
|
281 |
-
"
|
282 |
-
"
|
283 |
-
"
|
284 |
-
|
285 |
-
|
286 |
-
|
287 |
-
|
288 |
-
|
289 |
-
|
290 |
-
|
291 |
-
|
292 |
-
text
|
293 |
-
|
294 |
-
|
295 |
-
|
296 |
-
|
297 |
-
|
298 |
-
|
299 |
-
|
300 |
-
|
301 |
-
|
302 |
-
|
303 |
-
|
304 |
-
|
305 |
-
|
306 |
-
|
307 |
-
|
308 |
-
|
309 |
-
|
310 |
-
|
311 |
-
|
312 |
-
|
313 |
-
|
314 |
-
|
315 |
-
|
316 |
-
|
317 |
-
|
318 |
-
|
319 |
-
|
320 |
-
|
321 |
-
|
322 |
-
|
323 |
-
|
324 |
-
|
325 |
-
|
326 |
-
|
327 |
-
|
328 |
-
|
329 |
-
|
330 |
-
|
331 |
-
|
332 |
-
|
333 |
-
|
334 |
-
|
335 |
-
|
336 |
-
|
337 |
-
|
338 |
-
|
339 |
-
|
340 |
-
|
341 |
-
|
342 |
-
|
343 |
-
|
344 |
-
|
345 |
-
|
346 |
-
|
347 |
-
|
348 |
-
|
349 |
-
|
350 |
-
|
351 |
-
|
352 |
-
|
353 |
-
|
354 |
-
|
355 |
-
|
356 |
-
|
357 |
-
|
358 |
-
|
359 |
-
|
360 |
-
|
361 |
-
|
362 |
-
|
363 |
-
|
364 |
-
|
365 |
-
|
366 |
-
|
367 |
-
|
368 |
-
|
369 |
-
|
370 |
-
|
371 |
-
|
372 |
-
|
373 |
-
|
374 |
-
|
375 |
-
|
376 |
-
|
377 |
-
|
378 |
-
|
379 |
-
display(
|
380 |
-
display(
|
381 |
-
display(
|
382 |
-
display(
|
383 |
-
display(
|
384 |
-
|
385 |
-
|
386 |
-
|
387 |
-
|
388 |
-
|
389 |
-
|
390 |
-
|
391 |
-
|
392 |
-
|
393 |
-
|
394 |
-
|
395 |
-
|
396 |
-
|
397 |
-
|
398 |
-
|
399 |
-
|
400 |
-
|
401 |
-
|
402 |
-
|
403 |
-
|
404 |
-
|
405 |
-
|
406 |
-
|
407 |
-
|
408 |
-
|
409 |
-
|
410 |
-
|
411 |
-
|
412 |
-
|
413 |
-
|
414 |
-
|
415 |
-
|
416 |
-
|
417 |
-
|
418 |
-
|
419 |
-
|
420 |
-
client.
|
421 |
-
|
422 |
-
|
|
|
|
1 |
+
import weaviate
|
2 |
+
|
3 |
+
from sentence_transformers import SentenceTransformer
|
4 |
+
from langchain_community.document_loaders import BSHTMLLoader
|
5 |
+
from pathlib import Path
|
6 |
+
from lxml import html
|
7 |
+
import logging
|
8 |
+
from semantic_text_splitter import HuggingFaceTextSplitter
|
9 |
+
from tokenizers import Tokenizer
|
10 |
+
import json
|
11 |
+
import os
|
12 |
+
import re
|
13 |
+
import logging
|
14 |
+
|
15 |
+
import llama_cpp
|
16 |
+
from llama_cpp import Llama
|
17 |
+
import ipywidgets as widgets
|
18 |
+
from IPython.display import display, clear_output
|
19 |
+
|
20 |
+
|
21 |
+
weaviate_logger = logging.getLogger("httpx")
|
22 |
+
weaviate_logger.setLevel(logging.WARNING)
|
23 |
+
|
24 |
+
logger = logging.getLogger(__name__)
|
25 |
+
logging.basicConfig(level=logging.INFO)
|
26 |
+
|
27 |
+
|
28 |
+
|
29 |
+
######################################################################
|
30 |
+
# MAINLINE
|
31 |
+
#
|
32 |
+
logger.info("#### MAINLINE ENTERED.")
|
33 |
+
|
34 |
+
#pathString = "/Users/660565/KPSAllInOne/ProgramFilesX86/WebCopy/DownloadedWebSites/LLMPOC_HTML"
|
35 |
+
pathString = "/app/inputDocs"
|
36 |
+
chunks = []
|
37 |
+
webpageDocNames = []
|
38 |
+
page_contentArray = []
|
39 |
+
webpageChunks = []
|
40 |
+
webpageTitles = []
|
41 |
+
webpageChunksDocNames = []
|
42 |
+
|
43 |
+
#####################################################################
|
44 |
+
# Create UI widgets.
|
45 |
+
output_widget = widgets.Output()
|
46 |
+
with output_widget:
|
47 |
+
print("### Create widgets entered.")
|
48 |
+
|
49 |
+
systemTextArea = widgets.Textarea(
|
50 |
+
value='',
|
51 |
+
placeholder='Enter System Prompt.',
|
52 |
+
description='Sys Prompt: ',
|
53 |
+
disabled=False,
|
54 |
+
layout=widgets.Layout(width='300px', height='80px')
|
55 |
+
)
|
56 |
+
|
57 |
+
userTextArea = widgets.Textarea(
|
58 |
+
value='',
|
59 |
+
placeholder='Enter User Prompt.',
|
60 |
+
description='User Prompt: ',
|
61 |
+
disabled=False,
|
62 |
+
layout=widgets.Layout(width='435px', height='110px')
|
63 |
+
)
|
64 |
+
|
65 |
+
ragPromptTextArea = widgets.Textarea(
|
66 |
+
value='',
|
67 |
+
placeholder='App generated prompt with RAG information.',
|
68 |
+
description='RAG Prompt: ',
|
69 |
+
disabled=False,
|
70 |
+
layout=widgets.Layout(width='580px', height='180px')
|
71 |
+
)
|
72 |
+
|
73 |
+
responseTextArea = widgets.Textarea(
|
74 |
+
value='',
|
75 |
+
placeholder='LLM generated response.',
|
76 |
+
description='LLM Resp: ',
|
77 |
+
disabled=False,
|
78 |
+
layout=widgets.Layout(width='780px', height='200px')
|
79 |
+
)
|
80 |
+
|
81 |
+
selectRag = widgets.Checkbox(
|
82 |
+
value=False,
|
83 |
+
description='Use RAG',
|
84 |
+
disabled=False
|
85 |
+
)
|
86 |
+
|
87 |
+
submitButton = widgets.Button(
|
88 |
+
description='Run Model.',
|
89 |
+
disabled=False,
|
90 |
+
button_style='', # 'success', 'info', 'warning', 'danger' or ''
|
91 |
+
tooltip='Click',
|
92 |
+
icon='check' # (FontAwesome names without the `fa-` prefix)
|
93 |
+
)
|
94 |
+
|
95 |
+
|
96 |
+
#######################################################
|
97 |
+
# Read each text input file, parse it into a document,
|
98 |
+
# chunk it, collect chunks and document name.
|
99 |
+
logger.info("#### Read and chunk input text files.")
|
100 |
+
for filename in os.listdir(pathString):
|
101 |
+
logger.info(filename)
|
102 |
+
path = Path(pathString + "/" + filename)
|
103 |
+
filename = filename.rstrip(".html")
|
104 |
+
webpageDocNames.append(filename)
|
105 |
+
htmlLoader = BSHTMLLoader(path,"utf-8")
|
106 |
+
htmlData = htmlLoader.load()
|
107 |
+
|
108 |
+
title = htmlData[0].metadata['title']
|
109 |
+
page_content = htmlData[0].page_content
|
110 |
+
|
111 |
+
# Clean data. Remove multiple newlines, etc.
|
112 |
+
page_content = re.sub(r'\n+', '\n',page_content)
|
113 |
+
|
114 |
+
page_contentArray.append(page_content);
|
115 |
+
webpageTitles.append(title)
|
116 |
+
max_tokens = 1000
|
117 |
+
tokenizer = Tokenizer.from_pretrained("bert-base-uncased")
|
118 |
+
logger.debug(f"### tokenizer: {tokenizer}")
|
119 |
+
splitter = HuggingFaceTextSplitter(tokenizer, trim_chunks=True)
|
120 |
+
chunksOnePage = splitter.chunks(page_content, chunk_capacity=50)
|
121 |
+
|
122 |
+
chunks = []
|
123 |
+
for chnk in chunksOnePage:
|
124 |
+
logger.debug(f"#### chnk in file: {chnk}")
|
125 |
+
chunks.append(chnk)
|
126 |
+
logger.debug(f"chunks: {chunks}")
|
127 |
+
webpageChunks.append(chunks)
|
128 |
+
webpageChunksDocNames.append(filename + "Chunks")
|
129 |
+
|
130 |
+
logger.debug(f"### filename, title: {filename}, {title}")
|
131 |
+
|
132 |
+
logger.debug(f"### webpageDocNames: {webpageDocNames}")
|
133 |
+
|
134 |
+
|
135 |
+
######################################################
|
136 |
+
# Connect to the Weaviate vector database.
|
137 |
+
logger.info("#### Create Weaviate db client connection.")
|
138 |
+
client = weaviate.connect_to_custom(
|
139 |
+
http_host="127.0.0.1",
|
140 |
+
http_port=8080,
|
141 |
+
http_secure=False,
|
142 |
+
grpc_host="127.0.0.1",
|
143 |
+
grpc_port=50051,
|
144 |
+
grpc_secure=False,
|
145 |
+
timeout=[600,600]
|
146 |
+
#read_timeout=600,
|
147 |
+
#write_timeout=90
|
148 |
+
)
|
149 |
+
client.connect()
|
150 |
+
|
151 |
+
|
152 |
+
######################################################
|
153 |
+
# Create database webpage and chunks collections.
|
154 |
+
#wpCollection = createWebpageCollection()
|
155 |
+
#wpChunkCollection = createChunksCollection()
|
156 |
+
logger.info("#### createWebpageCollection() entered.")
|
157 |
+
if client.collections.exists("Documents"):
|
158 |
+
client.collections.delete("Documents")
|
159 |
+
|
160 |
+
class_obj = {
|
161 |
+
"class": "Documents",
|
162 |
+
"description": "For first attempt at loading a Weviate database.",
|
163 |
+
"vectorizer": "text2vec-transformers",
|
164 |
+
"moduleConfig": {
|
165 |
+
"text2vec-transformers": {
|
166 |
+
"vectorizeClassName": False
|
167 |
+
}
|
168 |
+
},
|
169 |
+
"vectorIndexType": "hnsw",
|
170 |
+
"vectorIndexConfig": {
|
171 |
+
"distance": "cosine",
|
172 |
+
},
|
173 |
+
"properties": [
|
174 |
+
{
|
175 |
+
"name": "title",
|
176 |
+
"dataType": ["text"],
|
177 |
+
"description": "HTML doc title.",
|
178 |
+
"vectorizer": "text2vec-transformers",
|
179 |
+
"moduleConfig": {
|
180 |
+
"text2vec-transformers": {
|
181 |
+
"vectorizePropertyName": True,
|
182 |
+
"skip": False,
|
183 |
+
"tokenization": "lowercase"
|
184 |
+
}
|
185 |
+
},
|
186 |
+
"invertedIndexConfig": {
|
187 |
+
"bm25": {
|
188 |
+
"b": 0.75,
|
189 |
+
"k1": 1.2
|
190 |
+
},
|
191 |
+
}
|
192 |
+
},
|
193 |
+
{
|
194 |
+
"name": "content",
|
195 |
+
"dataType": ["text"],
|
196 |
+
"description": "HTML page content.",
|
197 |
+
"moduleConfig": {
|
198 |
+
"text2vec-transformers": {
|
199 |
+
"vectorizePropertyName": True,
|
200 |
+
"tokenization": "whitespace"
|
201 |
+
}
|
202 |
+
}
|
203 |
+
}
|
204 |
+
]
|
205 |
+
}
|
206 |
+
wpCollection = client.collections.create_from_dict(class_obj)
|
207 |
+
|
208 |
+
logger.info("#### createChunksCollection() entered.")
|
209 |
+
if client.collections.exists("Chunks"):
|
210 |
+
client.collections.delete("Chunks")
|
211 |
+
|
212 |
+
class_obj = {
|
213 |
+
"class": "Chunks",
|
214 |
+
"description": "Collection for document chunks.",
|
215 |
+
"vectorizer": "text2vec-transformers",
|
216 |
+
"moduleConfig": {
|
217 |
+
"text2vec-transformers": {
|
218 |
+
"vectorizeClassName": True
|
219 |
+
}
|
220 |
+
},
|
221 |
+
"vectorIndexType": "hnsw",
|
222 |
+
"vectorIndexConfig": {
|
223 |
+
"distance": "cosine",
|
224 |
+
},
|
225 |
+
"properties": [
|
226 |
+
{
|
227 |
+
"name": "chunk",
|
228 |
+
"dataType": ["text"],
|
229 |
+
"description": "Single webpage chunk.",
|
230 |
+
"vectorizer": "text2vec-transformers",
|
231 |
+
"moduleConfig": {
|
232 |
+
"text2vec-transformers": {
|
233 |
+
"vectorizePropertyName": False,
|
234 |
+
"skip": False,
|
235 |
+
"tokenization": "lowercase"
|
236 |
+
}
|
237 |
+
}
|
238 |
+
},
|
239 |
+
{
|
240 |
+
"name": "chunk_index",
|
241 |
+
"dataType": ["int"]
|
242 |
+
},
|
243 |
+
{
|
244 |
+
"name": "webpage",
|
245 |
+
"dataType": ["Documents"],
|
246 |
+
"description": "Webpage content chunks.",
|
247 |
+
|
248 |
+
"invertedIndexConfig": {
|
249 |
+
"bm25": {
|
250 |
+
"b": 0.75,
|
251 |
+
"k1": 1.2
|
252 |
+
}
|
253 |
+
}
|
254 |
+
}
|
255 |
+
]
|
256 |
+
}
|
257 |
+
wpChunkCollection = client.collections.create_from_dict(class_obj)
|
258 |
+
|
259 |
+
|
260 |
+
###########################################################
|
261 |
+
# Create document and chunks objects in the database.
|
262 |
+
logger.info("#### Create page/doc and chunk db objects.")
|
263 |
+
for i, className in enumerate(webpageDocNames):
|
264 |
+
title = webpageTitles[i]
|
265 |
+
logger.debug(f"## className, title: {className}, {title}")
|
266 |
+
# Create Webpage Object
|
267 |
+
page_content = page_contentArray[i]
|
268 |
+
# Insert the document.
|
269 |
+
wpCollectionObj_uuid = wpCollection.data.insert(
|
270 |
+
{
|
271 |
+
"name": className,
|
272 |
+
"title": title,
|
273 |
+
"content": page_content
|
274 |
+
}
|
275 |
+
)
|
276 |
+
|
277 |
+
# Insert the chunks for the document.
|
278 |
+
for i2, chunk in enumerate(webpageChunks[i]):
|
279 |
+
chunk_uuid = wpChunkCollection.data.insert(
|
280 |
+
{
|
281 |
+
"title": title,
|
282 |
+
"chunk": chunk,
|
283 |
+
"chunk_index": i2,
|
284 |
+
"references":
|
285 |
+
{
|
286 |
+
"webpage": wpCollectionObj_uuid
|
287 |
+
}
|
288 |
+
}
|
289 |
+
)
|
290 |
+
|
291 |
+
###############################################################################
|
292 |
+
# text contains prompt for vector DB.
|
293 |
+
text = "human-made computer cognitive ability"
|
294 |
+
|
295 |
+
|
296 |
+
###############################################################################
|
297 |
+
# Initial the the sentence transformer and encode the query prompt.
|
298 |
+
logger.info(f"#### Encode text query prompt to create vectors. {text}")
|
299 |
+
model = SentenceTransformer('/app/multi-qa-MiniLM-L6-cos-v1')
|
300 |
+
|
301 |
+
vector = model.encode(text)
|
302 |
+
vectorList = []
|
303 |
+
|
304 |
+
logger.debug("#### Print vectors.")
|
305 |
+
for vec in vector:
|
306 |
+
vectorList.append(vec)
|
307 |
+
logger.debug(f"vectorList: {vectorList[2]}")
|
308 |
+
|
309 |
+
# Fetch chunks and print chunks.
|
310 |
+
logger.info("#### Retrieve semchunks from db using vectors from prompt.")
|
311 |
+
semChunks = wpChunkCollection.query.near_vector(
|
312 |
+
near_vector=vectorList,
|
313 |
+
distance=0.7,
|
314 |
+
limit=3
|
315 |
+
)
|
316 |
+
logger.debug(f"### semChunks[0]: {semChunks}")
|
317 |
+
|
318 |
+
# Print chunks, corresponding document and document title.
|
319 |
+
logger.info("#### Print individual retrieved chunks.")
|
320 |
+
for chunk in enumerate(semChunks.objects):
|
321 |
+
logger.info(f"#### chunk: {chunk}")
|
322 |
+
webpage_uuid = chunk[1].properties['references']['webpage']
|
323 |
+
logger.info(f"webpage_uuid: {webpage_uuid}")
|
324 |
+
wpFromChunk = wpCollection.query.fetch_object_by_id(webpage_uuid)
|
325 |
+
logger.info(f"### wpFromChunk title: {wpFromChunk.properties['title']}")
|
326 |
+
|
327 |
+
|
328 |
+
|
329 |
+
####################################################################
|
330 |
+
#
|
331 |
+
collection = client.collections.get("Chunks")
|
332 |
+
#model = SentenceTransformer('../multi-qa-MiniLM-L6-cos-v1')
|
333 |
+
|
334 |
+
#################################################################
|
335 |
+
# Initialize the LLM.
|
336 |
+
model_path = "/app/llama-2-7b-chat.Q4_0.gguf"
|
337 |
+
llm = Llama(model_path,
|
338 |
+
#*,
|
339 |
+
n_gpu_layers=0,
|
340 |
+
split_mode=llama_cpp.LLAMA_SPLIT_MODE_LAYER,
|
341 |
+
main_gpu=0,
|
342 |
+
tensor_split=None,
|
343 |
+
vocab_only=False,
|
344 |
+
use_mmap=True,
|
345 |
+
use_mlock=False,
|
346 |
+
kv_overrides=None,
|
347 |
+
seed=llama_cpp.LLAMA_DEFAULT_SEED,
|
348 |
+
n_ctx=512,
|
349 |
+
n_batch=512,
|
350 |
+
n_threads=8,
|
351 |
+
n_threads_batch=16,
|
352 |
+
rope_scaling_type=llama_cpp.LLAMA_ROPE_SCALING_TYPE_UNSPECIFIED,
|
353 |
+
pooling_type=llama_cpp.LLAMA_POOLING_TYPE_UNSPECIFIED,
|
354 |
+
rope_freq_base=0.0,
|
355 |
+
rope_freq_scale=0.0,
|
356 |
+
yarn_ext_factor=-1.0,
|
357 |
+
yarn_attn_factor=1.0,
|
358 |
+
yarn_beta_fast=32.0,
|
359 |
+
yarn_beta_slow=1.0,
|
360 |
+
yarn_orig_ctx=0,
|
361 |
+
logits_all=False,
|
362 |
+
embedding=False,
|
363 |
+
offload_kqv=True,
|
364 |
+
last_n_tokens_size=64,
|
365 |
+
lora_base=None,
|
366 |
+
lora_scale=1.0,
|
367 |
+
lora_path=None,
|
368 |
+
numa=False,
|
369 |
+
chat_format=None,
|
370 |
+
chat_handler=None,
|
371 |
+
draft_model=None,
|
372 |
+
tokenizer=None,
|
373 |
+
type_k=None,
|
374 |
+
type_v=None,
|
375 |
+
verbose=True
|
376 |
+
)
|
377 |
+
|
378 |
+
|
379 |
+
display(systemTextArea)
|
380 |
+
display(userTextArea)
|
381 |
+
display(ragPromptTextArea)
|
382 |
+
display(responseTextArea)
|
383 |
+
display(selectRag)
|
384 |
+
display(submitButton)
|
385 |
+
|
386 |
+
def setPrompt(pprompt,ragFlag):
|
387 |
+
print("\n### setPrompt() entered. ragFlag: ",ragFlag)
|
388 |
+
if ragFlag:
|
389 |
+
ragPrompt = setRagPrompt(pprompt)
|
390 |
+
userPrompt = pprompt + "\n" + ragPrompt
|
391 |
+
prompt = userPrompt
|
392 |
+
else:
|
393 |
+
userPrompt = pprompt
|
394 |
+
prompt = f""" <s> [INST] <<SYS>> {systemTextArea.value} </SYS>> Q: {userPrompt} A: [/INST]"""
|
395 |
+
return prompt
|
396 |
+
|
397 |
+
def runModel(prompt):
|
398 |
+
output = llm.create_completion(
|
399 |
+
prompt, # Prompt
|
400 |
+
max_tokens=4096, # Generate up to 32 tokens
|
401 |
+
#stop = ["Q:", "\n"], # Stop generating just before the model would generate a new question
|
402 |
+
echo = False # Echo the prompt back in the output
|
403 |
+
)
|
404 |
+
responseTextArea.value = output["choices"][0]["text"]
|
405 |
+
|
406 |
+
def on_submitButton_clicked(b):
|
407 |
+
with output_widget:
|
408 |
+
clear_output(wait=True)
|
409 |
+
ragPromptTextArea.value = ""
|
410 |
+
responseTextArea.value = ""
|
411 |
+
log.debug(f"### selectRag: {selectRag.value}")
|
412 |
+
prompt = setPrompt(userTextArea.value,selectRag.value)
|
413 |
+
log.debug("### prompt: " + prompt)
|
414 |
+
runModel(prompt)
|
415 |
+
|
416 |
+
submitButton.on_click(on_submitButton_clicked)
|
417 |
+
display(output_widget)
|
418 |
+
|
419 |
+
|
420 |
+
logger.info("#### Closing client db connection.")
|
421 |
+
client.close()
|
422 |
+
|
423 |
+
logger.info("#### Program terminating.")
|