Spaces:
Running
Running
MVPilgrim
commited on
Commit
·
423d6ab
1
Parent(s):
4d1c68b
llm query work.
Browse files- semsearch.py +63 -55
- startup.sh +2 -2
semsearch.py
CHANGED
@@ -295,48 +295,6 @@ if not client.collections.exists("Chunks") :
|
|
295 |
}
|
296 |
)
|
297 |
|
298 |
-
###############################################################################
|
299 |
-
# text contains prompt for vector DB.
|
300 |
-
text = "human-made computer cognitive ability"
|
301 |
-
|
302 |
-
|
303 |
-
###############################################################################
|
304 |
-
# Initial the the sentence transformer and encode the query prompt.
|
305 |
-
logger.info(f"#### Encode text query prompt to create vectors. {text}")
|
306 |
-
model = SentenceTransformer('/app/multi-qa-MiniLM-L6-cos-v1')
|
307 |
-
|
308 |
-
vector = model.encode(text)
|
309 |
-
vectorList = []
|
310 |
-
|
311 |
-
logger.debug("#### Print vectors.")
|
312 |
-
for vec in vector:
|
313 |
-
vectorList.append(vec)
|
314 |
-
logger.debug(f"vectorList: {vectorList[2]}")
|
315 |
-
|
316 |
-
# Fetch chunks and print chunks.
|
317 |
-
logger.info("#### Retrieve semchunks from db using vectors from prompt.")
|
318 |
-
semChunks = wpChunkCollection.query.near_vector(
|
319 |
-
near_vector=vectorList,
|
320 |
-
distance=0.7,
|
321 |
-
limit=3
|
322 |
-
)
|
323 |
-
logger.debug(f"### semChunks[0]: {semChunks}")
|
324 |
-
|
325 |
-
# Print chunks, corresponding document and document title.
|
326 |
-
logger.info("#### Print individual retrieved chunks.")
|
327 |
-
for chunk in enumerate(semChunks.objects):
|
328 |
-
logger.info(f"#### chunk: {chunk}")
|
329 |
-
webpage_uuid = chunk[1].properties['references']['webpage']
|
330 |
-
logger.info(f"webpage_uuid: {webpage_uuid}")
|
331 |
-
wpFromChunk = wpCollection.query.fetch_object_by_id(webpage_uuid)
|
332 |
-
logger.info(f"### wpFromChunk title: {wpFromChunk.properties['title']}")
|
333 |
-
|
334 |
-
|
335 |
-
|
336 |
-
####################################################################
|
337 |
-
#
|
338 |
-
collection = client.collections.get("Chunks")
|
339 |
-
#model = SentenceTransformer('../multi-qa-MiniLM-L6-cos-v1')
|
340 |
|
341 |
#################################################################
|
342 |
# Initialize the LLM.
|
@@ -383,6 +341,43 @@ llm = Llama(model_path,
|
|
383 |
)
|
384 |
|
385 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
386 |
display(systemTextArea)
|
387 |
display(userTextArea)
|
388 |
display(ragPromptTextArea)
|
@@ -390,25 +385,38 @@ display(responseTextArea)
|
|
390 |
display(selectRag)
|
391 |
display(submitButton)
|
392 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
393 |
def setPrompt(pprompt,ragFlag):
|
394 |
print("\n### setPrompt() entered. ragFlag: ",ragFlag)
|
395 |
if ragFlag:
|
396 |
-
ragPrompt =
|
397 |
userPrompt = pprompt + "\n" + ragPrompt
|
398 |
prompt = userPrompt
|
|
|
|
|
|
|
399 |
else:
|
400 |
userPrompt = pprompt
|
401 |
-
prompt = f""" <s> [INST] <<SYS>> {systemTextArea.value} </SYS>> Q: {userPrompt} A: [/INST]"""
|
402 |
-
return
|
403 |
-
|
404 |
-
def runModel(prompt):
|
405 |
-
output = llm.create_completion(
|
406 |
-
prompt, # Prompt
|
407 |
-
max_tokens=4096, # Generate up to 32 tokens
|
408 |
-
#stop = ["Q:", "\n"], # Stop generating just before the model would generate a new question
|
409 |
-
echo = False # Echo the prompt back in the output
|
410 |
-
)
|
411 |
-
responseTextArea.value = output["choices"][0]["text"]
|
412 |
|
413 |
def on_submitButton_clicked(b):
|
414 |
with output_widget:
|
@@ -418,7 +426,7 @@ def on_submitButton_clicked(b):
|
|
418 |
log.debug(f"### selectRag: {selectRag.value}")
|
419 |
prompt = setPrompt(userTextArea.value,selectRag.value)
|
420 |
log.debug("### prompt: " + prompt)
|
421 |
-
|
422 |
|
423 |
submitButton.on_click(on_submitButton_clicked)
|
424 |
display(output_widget)
|
|
|
295 |
}
|
296 |
)
|
297 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
298 |
|
299 |
#################################################################
|
300 |
# Initialize the LLM.
|
|
|
341 |
)
|
342 |
|
343 |
|
344 |
+
def getRagData(promptText):
|
345 |
+
###############################################################################
|
346 |
+
# Initial the the sentence transformer and encode the query prompt.
|
347 |
+
logger.info(f"#### Encode text query prompt to create vectors. {text}")
|
348 |
+
model = SentenceTransformer('/app/multi-qa-MiniLM-L6-cos-v1')
|
349 |
+
|
350 |
+
vector = model.encode(promptText)
|
351 |
+
vectorList = []
|
352 |
+
|
353 |
+
logger.debug("#### Print vectors.")
|
354 |
+
for vec in vector:
|
355 |
+
vectorList.append(vec)
|
356 |
+
logger.debug(f"vectorList: {vectorList[2]}")
|
357 |
+
|
358 |
+
# Fetch chunks and print chunks.
|
359 |
+
logger.info("#### Retrieve semchunks from db using vectors from prompt.")
|
360 |
+
semChunks = wpChunkCollection.query.near_vector(
|
361 |
+
near_vector=vectorList,
|
362 |
+
distance=0.7,
|
363 |
+
limit=3
|
364 |
+
)
|
365 |
+
logger.debug(f"### semChunks[0]: {semChunks}")
|
366 |
+
|
367 |
+
# Print chunks, corresponding document and document title.
|
368 |
+
ragData = ""
|
369 |
+
logger.info("#### Print individual retrieved chunks.")
|
370 |
+
for chunk in enumerate(semChunks.objects):
|
371 |
+
logger.info(f"#### chunk: {chunk}")
|
372 |
+
ragData = ragData + "\n" + chunk[0]
|
373 |
+
webpage_uuid = chunk[1].properties['references']['webpage']
|
374 |
+
logger.info(f"webpage_uuid: {webpage_uuid}")
|
375 |
+
wpFromChunk = wpCollection.query.fetch_object_by_id(webpage_uuid)
|
376 |
+
logger.info(f"### wpFromChunk title: {wpFromChunk.properties['title']}")
|
377 |
+
#collection = client.collections.get("Chunks")
|
378 |
+
return ragData
|
379 |
+
|
380 |
+
# Display UI
|
381 |
display(systemTextArea)
|
382 |
display(userTextArea)
|
383 |
display(ragPromptTextArea)
|
|
|
385 |
display(selectRag)
|
386 |
display(submitButton)
|
387 |
|
388 |
+
def runLLM(prompt):
|
389 |
+
max_tokens = 1000
|
390 |
+
temperature = 0.3
|
391 |
+
top_p = 0.1
|
392 |
+
echo = True
|
393 |
+
stop = ["Q", "\n"]
|
394 |
+
|
395 |
+
modelOutput = llm(
|
396 |
+
prompt,
|
397 |
+
max_tokens=max_tokens,
|
398 |
+
temperature=temperature,
|
399 |
+
top_p=top_p,
|
400 |
+
echo=echo,
|
401 |
+
stop=stop,
|
402 |
+
)
|
403 |
+
result = modelOutput["choices"][0]["text"].strip()
|
404 |
+
return(result)
|
405 |
+
|
406 |
def setPrompt(pprompt,ragFlag):
|
407 |
print("\n### setPrompt() entered. ragFlag: ",ragFlag)
|
408 |
if ragFlag:
|
409 |
+
ragPrompt = getRagData(pprompt)
|
410 |
userPrompt = pprompt + "\n" + ragPrompt
|
411 |
prompt = userPrompt
|
412 |
+
userPrompt = "Using this information: " + ragPrompt
|
413 |
+
+ "process the following statement or question and produce a a response"
|
414 |
+
+ intialPrompt
|
415 |
else:
|
416 |
userPrompt = pprompt
|
417 |
+
#prompt = f""" <s> [INST] <<SYS>> {systemTextArea.value} </SYS>> Q: {userPrompt} A: [/INST]"""
|
418 |
+
return userPrompt
|
419 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
420 |
|
421 |
def on_submitButton_clicked(b):
|
422 |
with output_widget:
|
|
|
426 |
log.debug(f"### selectRag: {selectRag.value}")
|
427 |
prompt = setPrompt(userTextArea.value,selectRag.value)
|
428 |
log.debug("### prompt: " + prompt)
|
429 |
+
runLLM(prompt)
|
430 |
|
431 |
submitButton.on_click(on_submitButton_clicked)
|
432 |
display(output_widget)
|
startup.sh
CHANGED
@@ -56,13 +56,13 @@ env
|
|
56 |
/app/weaviate/weaviate --host 127.0.0.1 --port 8080 --scheme http --write-timeout 600s & #2>& 1 | tee /data/var/lib/weaviate/ws.log &
|
57 |
|
58 |
echo "#### Before sleep."
|
59 |
-
sleep
|
60 |
|
61 |
echo "#### Before /app/semsearch.py"
|
62 |
python /app/semsearch.py & #2>& 1 | tee /data/var/lib/weaviate/ss.log &
|
63 |
|
64 |
# Display timestamps.
|
65 |
-
for (( ; ; )) do date; sleep 60; done &
|
66 |
|
67 |
wait
|
68 |
|
|
|
56 |
/app/weaviate/weaviate --host 127.0.0.1 --port 8080 --scheme http --write-timeout 600s & #2>& 1 | tee /data/var/lib/weaviate/ws.log &
|
57 |
|
58 |
echo "#### Before sleep."
|
59 |
+
sleep 30
|
60 |
|
61 |
echo "#### Before /app/semsearch.py"
|
62 |
python /app/semsearch.py & #2>& 1 | tee /data/var/lib/weaviate/ss.log &
|
63 |
|
64 |
# Display timestamps.
|
65 |
+
#for (( ; ; )) do date; sleep 60; done &
|
66 |
|
67 |
wait
|
68 |
|