Spaces:
Sleeping
Sleeping
MVPilgrim
commited on
Commit
·
3882e3f
1
Parent(s):
c84172b
restore
Browse files- Dockerfile +71 -0
- docker-compose.yml +36 -0
- requirements.txt +18 -0
- semsearch.py +443 -0
- startup.sh +65 -0
Dockerfile
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
###############################################################################
|
| 2 |
+
#python environment, main app and startup script.
|
| 3 |
+
FROM python:3.11.5
|
| 4 |
+
#FROM python:3.11.9-slim
|
| 5 |
+
#FROM python:3.11.9-alpine
|
| 6 |
+
#FROM python:3.11-bookworm
|
| 7 |
+
|
| 8 |
+
ENTRYPOINT ["/app/startup.sh"]
|
| 9 |
+
#RUN apt-get update && \
|
| 10 |
+
# apt-get install -y libc6 && \
|
| 11 |
+
# rm -rf /var/lib/apt/lists/*
|
| 12 |
+
WORKDIR /app
|
| 13 |
+
|
| 14 |
+
#RUN ls -l / || ls -l /lib || ls -l /usr || ls -l /usr/lib6 || echo "### An ls failed."
|
| 15 |
+
|
| 16 |
+
COPY ./requirements.txt /app/requirements.txt
|
| 17 |
+
COPY ./semsearch.py /app/semsearch.py
|
| 18 |
+
COPY ./startup.sh /app/startup.sh
|
| 19 |
+
RUN chmod 755 /app/startup.sh
|
| 20 |
+
|
| 21 |
+
COPY ./multi-qa-MiniLM-L6-cos-v1 /app/multi-qa-MiniLM-L6-cos-v1
|
| 22 |
+
|
| 23 |
+
RUN mkdir -p /app/inputDocs
|
| 24 |
+
COPY ./inputDocs/* /app/inputDocs
|
| 25 |
+
RUN pip install --no-cache-dir --upgrade -r /app/requirements.txt
|
| 26 |
+
RUN pip install https://files.pythonhosted.org/packages/13/87/e0cb08c2d4bd7d38ab63816b306c8b1e7cfdc0e59bd54462e8b0df069078/semantic_text_splitter-0.6.3-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
|
| 27 |
+
RUN pip show semantic-text-splitter
|
| 28 |
+
|
| 29 |
+
RUN pip install llama_cpp_python
|
| 30 |
+
|
| 31 |
+
##############################################################################
|
| 32 |
+
# Install Weaviate
|
| 33 |
+
WORKDIR /app/weaviate
|
| 34 |
+
RUN wget -qO- https://github.com/weaviate/weaviate/releases/download/v1.24.10/weaviate-v1.24.10-linux-amd64.tar.gz | tar -xzf -
|
| 35 |
+
RUN ls -al /app/weaviate
|
| 36 |
+
|
| 37 |
+
# Set environment variables for Weaviate
|
| 38 |
+
ENV PATH="/app:/app/weaviate-v1.24.10-linux-x86_64:${PATH}"
|
| 39 |
+
# Expose the Weaviate port
|
| 40 |
+
EXPOSE 8080
|
| 41 |
+
|
| 42 |
+
#COPY Llama-2-7B-Chat-GGUF/llama-2-7b-chat.Q4_0.gguf /app
|
| 43 |
+
RUN cd /app; wget -v https://huggingface.co/TheBloke/Llama-2-7B-Chat-GGUF/resolve/main/llama-2-7b-chat.Q4_0.gguf
|
| 44 |
+
|
| 45 |
+
##############################################################################
|
| 46 |
+
# Install text2vec-transformers
|
| 47 |
+
WORKDIR /app/text2vec-transformers
|
| 48 |
+
COPY --from=semitechnologies/transformers-inference:sentence-transformers-multi-qa-MiniLM-L6-cos-v1 /app /app/text2vec-transformers
|
| 49 |
+
COPY --from=semitechnologies/transformers-inference:sentence-transformers-multi-qa-MiniLM-L6-cos-v1 /usr/local/bin /app/text2vec-transformers/bin
|
| 50 |
+
|
| 51 |
+
COPY ./multi-qa-MiniLM-L6-cos-v1 /app/app/text2vec-transformers
|
| 52 |
+
|
| 53 |
+
ENV PATH="/app/text2vec-transformers:/app/text2vec-transformers/bin:${PATH}"
|
| 54 |
+
#RUN pip install -r requirements.txt
|
| 55 |
+
#RUN pip install nltk==3.8.1 optimum==1.13.2 onnxruntime==1.16.1 onnx==1.14.1
|
| 56 |
+
RUN ./custom_prerequisites.py
|
| 57 |
+
|
| 58 |
+
|
| 59 |
+
##############################
|
| 60 |
+
RUN useradd -m -u 1000 user
|
| 61 |
+
|
| 62 |
+
#############################################
|
| 63 |
+
# Specify /data volume.
|
| 64 |
+
VOLUME /data
|
| 65 |
+
|
| 66 |
+
##############################################################################
|
| 67 |
+
# Start the weaviate vector database, text2vec-transformers and the semantic search app.
|
| 68 |
+
#RUN /app/startup.sh
|
| 69 |
+
#RUN --mount=type=cache,target=/data,mode=777 /app/startup.sh
|
| 70 |
+
#RUN --mount=type=cache,target=/data,mode=777 echo "### Mounting /data"
|
| 71 |
+
CMD ["/app/startup.sh"]
|
docker-compose.yml
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
version: '3.4'
|
| 3 |
+
services:
|
| 4 |
+
weaviate:
|
| 5 |
+
command:
|
| 6 |
+
- --host
|
| 7 |
+
- 0.0.0.0
|
| 8 |
+
- --port
|
| 9 |
+
- '8080'
|
| 10 |
+
- --scheme
|
| 11 |
+
- http
|
| 12 |
+
image: semitechnologies/weaviate:1.23.8
|
| 13 |
+
ports:
|
| 14 |
+
- 8080:8080
|
| 15 |
+
- 50051:50051
|
| 16 |
+
volumes:
|
| 17 |
+
- weaviate_data:/var/lib/weaviate
|
| 18 |
+
restart: on-failure:0
|
| 19 |
+
environment:
|
| 20 |
+
QUERY_DEFAULTS_LIMIT: 25
|
| 21 |
+
AUTHENTICATION_ANONYMOUS_ACCESS_ENABLED: 'true'
|
| 22 |
+
PERSISTENCE_DATA_PATH: '/var/lib/weaviate'
|
| 23 |
+
DEFAULT_VECTORIZER_MODULE: 'text2vec-transformers'
|
| 24 |
+
TRANSFORMERS_INFERENCE_API: http://t2v-transformers:8080
|
| 25 |
+
ENABLE_MODULES: 'text2vec-transformers'
|
| 26 |
+
#ENABLE_MODULES: 'text2vec-cohere,text2vec-huggingface,text2vec-palm,text2vec-openai,generative-openai,generative-cohere,generative-palm,ref2vec-centroid,reranker-cohere,qna-openai'
|
| 27 |
+
#ENABLE_MODULES: 'text2vec-gpt4all'
|
| 28 |
+
#GPT4ALL_INFERENCE_API: "http://localhost:4891"
|
| 29 |
+
CLUSTER_HOSTNAME: 'node1'
|
| 30 |
+
t2v-transformers:
|
| 31 |
+
image: semitechnologies/transformers-inference:sentence-transformers-multi-qa-MiniLM-L6-cos-v1
|
| 32 |
+
environment:
|
| 33 |
+
ENABLE_CUDA: 0 # set to 1 to enable
|
| 34 |
+
volumes:
|
| 35 |
+
weaviate_data:
|
| 36 |
+
...
|
requirements.txt
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
weaviate-client==4.*
|
| 2 |
+
sentence-transformers
|
| 3 |
+
langchain
|
| 4 |
+
langchain_community
|
| 5 |
+
lxml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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import weaviate
|
| 2 |
+
from weaviate.connect import ConnectionParams
|
| 3 |
+
from weaviate.classes.init import AdditionalConfig, Timeout
|
| 4 |
+
|
| 5 |
+
from sentence_transformers import SentenceTransformer
|
| 6 |
+
from langchain_community.document_loaders import BSHTMLLoader
|
| 7 |
+
from pathlib import Path
|
| 8 |
+
from lxml import html
|
| 9 |
+
import logging
|
| 10 |
+
from semantic_text_splitter import HuggingFaceTextSplitter
|
| 11 |
+
from tokenizers import Tokenizer
|
| 12 |
+
import json
|
| 13 |
+
import os
|
| 14 |
+
import re
|
| 15 |
+
import logging
|
| 16 |
+
|
| 17 |
+
import llama_cpp
|
| 18 |
+
from llama_cpp import Llama
|
| 19 |
+
import ipywidgets as widgets
|
| 20 |
+
from IPython.display import display, clear_output
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
weaviate_logger = logging.getLogger("httpx")
|
| 24 |
+
weaviate_logger.setLevel(logging.DEBUG)
|
| 25 |
+
|
| 26 |
+
logger = logging.getLogger(__name__)
|
| 27 |
+
logging.basicConfig(level=logging.INFO)
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
######################################################################
|
| 32 |
+
# MAINLINE
|
| 33 |
+
#
|
| 34 |
+
logger.info("#### MAINLINE ENTERED.")
|
| 35 |
+
|
| 36 |
+
#pathString = "/Users/660565/KPSAllInOne/ProgramFilesX86/WebCopy/DownloadedWebSites/LLMPOC_HTML"
|
| 37 |
+
pathString = "/app/inputDocs"
|
| 38 |
+
chunks = []
|
| 39 |
+
webpageDocNames = []
|
| 40 |
+
page_contentArray = []
|
| 41 |
+
webpageChunks = []
|
| 42 |
+
webpageTitles = []
|
| 43 |
+
webpageChunksDocNames = []
|
| 44 |
+
|
| 45 |
+
#####################################################################
|
| 46 |
+
# Create UI widgets.
|
| 47 |
+
output_widget = widgets.Output()
|
| 48 |
+
with output_widget:
|
| 49 |
+
print("### Create widgets entered.")
|
| 50 |
+
|
| 51 |
+
systemTextArea = widgets.Textarea(
|
| 52 |
+
value='',
|
| 53 |
+
placeholder='Enter System Prompt.',
|
| 54 |
+
description='Sys Prompt: ',
|
| 55 |
+
disabled=False,
|
| 56 |
+
layout=widgets.Layout(width='300px', height='80px')
|
| 57 |
+
)
|
| 58 |
+
|
| 59 |
+
userTextArea = widgets.Textarea(
|
| 60 |
+
value='',
|
| 61 |
+
placeholder='Enter User Prompt.',
|
| 62 |
+
description='User Prompt: ',
|
| 63 |
+
disabled=False,
|
| 64 |
+
layout=widgets.Layout(width='435px', height='110px')
|
| 65 |
+
)
|
| 66 |
+
|
| 67 |
+
ragPromptTextArea = widgets.Textarea(
|
| 68 |
+
value='',
|
| 69 |
+
placeholder='App generated prompt with RAG information.',
|
| 70 |
+
description='RAG Prompt: ',
|
| 71 |
+
disabled=False,
|
| 72 |
+
layout=widgets.Layout(width='580px', height='180px')
|
| 73 |
+
)
|
| 74 |
+
|
| 75 |
+
responseTextArea = widgets.Textarea(
|
| 76 |
+
value='',
|
| 77 |
+
placeholder='LLM generated response.',
|
| 78 |
+
description='LLM Resp: ',
|
| 79 |
+
disabled=False,
|
| 80 |
+
layout=widgets.Layout(width='780px', height='200px')
|
| 81 |
+
)
|
| 82 |
+
|
| 83 |
+
selectRag = widgets.Checkbox(
|
| 84 |
+
value=False,
|
| 85 |
+
description='Use RAG',
|
| 86 |
+
disabled=False
|
| 87 |
+
)
|
| 88 |
+
|
| 89 |
+
submitButton = widgets.Button(
|
| 90 |
+
description='Run Model.',
|
| 91 |
+
disabled=False,
|
| 92 |
+
button_style='', # 'success', 'info', 'warning', 'danger' or ''
|
| 93 |
+
tooltip='Click',
|
| 94 |
+
icon='check' # (FontAwesome names without the `fa-` prefix)
|
| 95 |
+
)
|
| 96 |
+
|
| 97 |
+
|
| 98 |
+
#######################################################
|
| 99 |
+
# Read each text input file, parse it into a document,
|
| 100 |
+
# chunk it, collect chunks and document name.
|
| 101 |
+
logger.info("#### Read and chunk input text files.")
|
| 102 |
+
for filename in os.listdir(pathString):
|
| 103 |
+
logger.info(filename)
|
| 104 |
+
path = Path(pathString + "/" + filename)
|
| 105 |
+
filename = filename.rstrip(".html")
|
| 106 |
+
webpageDocNames.append(filename)
|
| 107 |
+
htmlLoader = BSHTMLLoader(path,"utf-8")
|
| 108 |
+
htmlData = htmlLoader.load()
|
| 109 |
+
|
| 110 |
+
title = htmlData[0].metadata['title']
|
| 111 |
+
page_content = htmlData[0].page_content
|
| 112 |
+
|
| 113 |
+
# Clean data. Remove multiple newlines, etc.
|
| 114 |
+
page_content = re.sub(r'\n+', '\n',page_content)
|
| 115 |
+
|
| 116 |
+
page_contentArray.append(page_content);
|
| 117 |
+
webpageTitles.append(title)
|
| 118 |
+
max_tokens = 1000
|
| 119 |
+
tokenizer = Tokenizer.from_pretrained("bert-base-uncased")
|
| 120 |
+
logger.debug(f"### tokenizer: {tokenizer}")
|
| 121 |
+
splitter = HuggingFaceTextSplitter(tokenizer, trim_chunks=True)
|
| 122 |
+
chunksOnePage = splitter.chunks(page_content, chunk_capacity=50)
|
| 123 |
+
|
| 124 |
+
chunks = []
|
| 125 |
+
for chnk in chunksOnePage:
|
| 126 |
+
logger.debug(f"#### chnk in file: {chnk}")
|
| 127 |
+
chunks.append(chnk)
|
| 128 |
+
logger.debug(f"chunks: {chunks}")
|
| 129 |
+
webpageChunks.append(chunks)
|
| 130 |
+
webpageChunksDocNames.append(filename + "Chunks")
|
| 131 |
+
|
| 132 |
+
logger.debug(f"### filename, title: {filename}, {title}")
|
| 133 |
+
|
| 134 |
+
logger.debug(f"### webpageDocNames: {webpageDocNames}")
|
| 135 |
+
|
| 136 |
+
|
| 137 |
+
######################################################
|
| 138 |
+
# Connect to the Weaviate vector database.
|
| 139 |
+
logger.info("#### Create Weaviate db client connection.")
|
| 140 |
+
#client = weaviate.connect_to_custom(
|
| 141 |
+
# http_host="127.0.0.1",
|
| 142 |
+
# http_port=8080,
|
| 143 |
+
# http_secure=False,
|
| 144 |
+
# grpc_host="127.0.0.1",
|
| 145 |
+
# grpc_port=50051,
|
| 146 |
+
# grpc_secure=False,
|
| 147 |
+
# timeout=[600,600]
|
| 148 |
+
# #read_timeout=600,
|
| 149 |
+
# #write_timeout=90
|
| 150 |
+
#)
|
| 151 |
+
|
| 152 |
+
client = weaviate.WeaviateClient(
|
| 153 |
+
connection_params=ConnectionParams.from_params(
|
| 154 |
+
http_host="localhost",
|
| 155 |
+
http_port="8080",
|
| 156 |
+
http_secure=False,
|
| 157 |
+
grpc_host="localhost",
|
| 158 |
+
grpc_port="50051",
|
| 159 |
+
grpc_secure=False,
|
| 160 |
+
),
|
| 161 |
+
# auth_client_secret=weaviate.auth.AuthApiKey("secr3tk3y"),
|
| 162 |
+
# additional_headers={
|
| 163 |
+
# "X-OpenAI-Api-Key": os.getenv("OPENAI_APIKEY")
|
| 164 |
+
# },
|
| 165 |
+
additional_config=AdditionalConfig(
|
| 166 |
+
timeout=Timeout(init=60, query=1800, insert=1800), # Values in seconds
|
| 167 |
+
)
|
| 168 |
+
)
|
| 169 |
+
client.connect()
|
| 170 |
+
|
| 171 |
+
|
| 172 |
+
######################################################
|
| 173 |
+
# Create database webpage and chunks collections.
|
| 174 |
+
#wpCollection = createWebpageCollection()
|
| 175 |
+
#wpChunkCollection = createChunksCollection()
|
| 176 |
+
logger.info("#### createWebpageCollection() entered.")
|
| 177 |
+
if client.collections.exists("Documents"):
|
| 178 |
+
client.collections.delete("Documents")
|
| 179 |
+
|
| 180 |
+
class_obj = {
|
| 181 |
+
"class": "Documents",
|
| 182 |
+
"description": "For first attempt at loading a Weviate database.",
|
| 183 |
+
"vectorizer": "text2vec-transformers",
|
| 184 |
+
"moduleConfig": {
|
| 185 |
+
"text2vec-transformers": {
|
| 186 |
+
"vectorizeClassName": False
|
| 187 |
+
}
|
| 188 |
+
},
|
| 189 |
+
"vectorIndexType": "hnsw",
|
| 190 |
+
"vectorIndexConfig": {
|
| 191 |
+
"distance": "cosine",
|
| 192 |
+
},
|
| 193 |
+
"properties": [
|
| 194 |
+
{
|
| 195 |
+
"name": "title",
|
| 196 |
+
"dataType": ["text"],
|
| 197 |
+
"description": "HTML doc title.",
|
| 198 |
+
"vectorizer": "text2vec-transformers",
|
| 199 |
+
"moduleConfig": {
|
| 200 |
+
"text2vec-transformers": {
|
| 201 |
+
"vectorizePropertyName": True,
|
| 202 |
+
"skip": False,
|
| 203 |
+
"tokenization": "lowercase"
|
| 204 |
+
}
|
| 205 |
+
},
|
| 206 |
+
"invertedIndexConfig": {
|
| 207 |
+
"bm25": {
|
| 208 |
+
"b": 0.75,
|
| 209 |
+
"k1": 1.2
|
| 210 |
+
},
|
| 211 |
+
}
|
| 212 |
+
},
|
| 213 |
+
{
|
| 214 |
+
"name": "content",
|
| 215 |
+
"dataType": ["text"],
|
| 216 |
+
"description": "HTML page content.",
|
| 217 |
+
"moduleConfig": {
|
| 218 |
+
"text2vec-transformers": {
|
| 219 |
+
"vectorizePropertyName": True,
|
| 220 |
+
"tokenization": "whitespace"
|
| 221 |
+
}
|
| 222 |
+
}
|
| 223 |
+
}
|
| 224 |
+
]
|
| 225 |
+
}
|
| 226 |
+
wpCollection = client.collections.create_from_dict(class_obj)
|
| 227 |
+
|
| 228 |
+
logger.info("#### createChunksCollection() entered.")
|
| 229 |
+
if client.collections.exists("Chunks"):
|
| 230 |
+
client.collections.delete("Chunks")
|
| 231 |
+
|
| 232 |
+
class_obj = {
|
| 233 |
+
"class": "Chunks",
|
| 234 |
+
"description": "Collection for document chunks.",
|
| 235 |
+
"vectorizer": "text2vec-transformers",
|
| 236 |
+
"moduleConfig": {
|
| 237 |
+
"text2vec-transformers": {
|
| 238 |
+
"vectorizeClassName": True
|
| 239 |
+
}
|
| 240 |
+
},
|
| 241 |
+
"vectorIndexType": "hnsw",
|
| 242 |
+
"vectorIndexConfig": {
|
| 243 |
+
"distance": "cosine",
|
| 244 |
+
},
|
| 245 |
+
"properties": [
|
| 246 |
+
{
|
| 247 |
+
"name": "chunk",
|
| 248 |
+
"dataType": ["text"],
|
| 249 |
+
"description": "Single webpage chunk.",
|
| 250 |
+
"vectorizer": "text2vec-transformers",
|
| 251 |
+
"moduleConfig": {
|
| 252 |
+
"text2vec-transformers": {
|
| 253 |
+
"vectorizePropertyName": False,
|
| 254 |
+
"skip": False,
|
| 255 |
+
"tokenization": "lowercase"
|
| 256 |
+
}
|
| 257 |
+
}
|
| 258 |
+
},
|
| 259 |
+
{
|
| 260 |
+
"name": "chunk_index",
|
| 261 |
+
"dataType": ["int"]
|
| 262 |
+
},
|
| 263 |
+
{
|
| 264 |
+
"name": "webpage",
|
| 265 |
+
"dataType": ["Documents"],
|
| 266 |
+
"description": "Webpage content chunks.",
|
| 267 |
+
|
| 268 |
+
"invertedIndexConfig": {
|
| 269 |
+
"bm25": {
|
| 270 |
+
"b": 0.75,
|
| 271 |
+
"k1": 1.2
|
| 272 |
+
}
|
| 273 |
+
}
|
| 274 |
+
}
|
| 275 |
+
]
|
| 276 |
+
}
|
| 277 |
+
wpChunkCollection = client.collections.create_from_dict(class_obj)
|
| 278 |
+
|
| 279 |
+
|
| 280 |
+
###########################################################
|
| 281 |
+
# Create document and chunks objects in the database.
|
| 282 |
+
logger.info("#### Create page/doc db objects.")
|
| 283 |
+
for i, className in enumerate(webpageDocNames):
|
| 284 |
+
title = webpageTitles[i]
|
| 285 |
+
logger.debug(f"## className, title: {className}, {title}")
|
| 286 |
+
# Create Webpage Object
|
| 287 |
+
page_content = page_contentArray[i]
|
| 288 |
+
# Insert the document.
|
| 289 |
+
wpCollectionObj_uuid = wpCollection.data.insert(
|
| 290 |
+
{
|
| 291 |
+
"name": className,
|
| 292 |
+
"title": title,
|
| 293 |
+
"content": page_content
|
| 294 |
+
}
|
| 295 |
+
)
|
| 296 |
+
logger.info("#### Create chunk db objects.")
|
| 297 |
+
# Insert the chunks for the document.
|
| 298 |
+
for i2, chunk in enumerate(webpageChunks[i]):
|
| 299 |
+
chunk_uuid = wpChunkCollection.data.insert(
|
| 300 |
+
{
|
| 301 |
+
"title": title,
|
| 302 |
+
"chunk": chunk,
|
| 303 |
+
"chunk_index": i2,
|
| 304 |
+
"references":
|
| 305 |
+
{
|
| 306 |
+
"webpage": wpCollectionObj_uuid
|
| 307 |
+
}
|
| 308 |
+
}
|
| 309 |
+
)
|
| 310 |
+
|
| 311 |
+
###############################################################################
|
| 312 |
+
# text contains prompt for vector DB.
|
| 313 |
+
text = "human-made computer cognitive ability"
|
| 314 |
+
|
| 315 |
+
|
| 316 |
+
###############################################################################
|
| 317 |
+
# Initial the the sentence transformer and encode the query prompt.
|
| 318 |
+
logger.info(f"#### Encode text query prompt to create vectors. {text}")
|
| 319 |
+
model = SentenceTransformer('/app/multi-qa-MiniLM-L6-cos-v1')
|
| 320 |
+
|
| 321 |
+
vector = model.encode(text)
|
| 322 |
+
vectorList = []
|
| 323 |
+
|
| 324 |
+
logger.debug("#### Print vectors.")
|
| 325 |
+
for vec in vector:
|
| 326 |
+
vectorList.append(vec)
|
| 327 |
+
logger.debug(f"vectorList: {vectorList[2]}")
|
| 328 |
+
|
| 329 |
+
# Fetch chunks and print chunks.
|
| 330 |
+
logger.info("#### Retrieve semchunks from db using vectors from prompt.")
|
| 331 |
+
semChunks = wpChunkCollection.query.near_vector(
|
| 332 |
+
near_vector=vectorList,
|
| 333 |
+
distance=0.7,
|
| 334 |
+
limit=3
|
| 335 |
+
)
|
| 336 |
+
logger.debug(f"### semChunks[0]: {semChunks}")
|
| 337 |
+
|
| 338 |
+
# Print chunks, corresponding document and document title.
|
| 339 |
+
logger.info("#### Print individual retrieved chunks.")
|
| 340 |
+
for chunk in enumerate(semChunks.objects):
|
| 341 |
+
logger.info(f"#### chunk: {chunk}")
|
| 342 |
+
webpage_uuid = chunk[1].properties['references']['webpage']
|
| 343 |
+
logger.info(f"webpage_uuid: {webpage_uuid}")
|
| 344 |
+
wpFromChunk = wpCollection.query.fetch_object_by_id(webpage_uuid)
|
| 345 |
+
logger.info(f"### wpFromChunk title: {wpFromChunk.properties['title']}")
|
| 346 |
+
|
| 347 |
+
|
| 348 |
+
|
| 349 |
+
####################################################################
|
| 350 |
+
#
|
| 351 |
+
collection = client.collections.get("Chunks")
|
| 352 |
+
#model = SentenceTransformer('../multi-qa-MiniLM-L6-cos-v1')
|
| 353 |
+
|
| 354 |
+
#################################################################
|
| 355 |
+
# Initialize the LLM.
|
| 356 |
+
model_path = "/app/llama-2-7b-chat.Q4_0.gguf"
|
| 357 |
+
llm = Llama(model_path,
|
| 358 |
+
#*,
|
| 359 |
+
n_gpu_layers=0,
|
| 360 |
+
split_mode=llama_cpp.LLAMA_SPLIT_MODE_LAYER,
|
| 361 |
+
main_gpu=0,
|
| 362 |
+
tensor_split=None,
|
| 363 |
+
vocab_only=False,
|
| 364 |
+
use_mmap=True,
|
| 365 |
+
use_mlock=False,
|
| 366 |
+
kv_overrides=None,
|
| 367 |
+
seed=llama_cpp.LLAMA_DEFAULT_SEED,
|
| 368 |
+
n_ctx=512,
|
| 369 |
+
n_batch=512,
|
| 370 |
+
n_threads=8,
|
| 371 |
+
n_threads_batch=16,
|
| 372 |
+
rope_scaling_type=llama_cpp.LLAMA_ROPE_SCALING_TYPE_UNSPECIFIED,
|
| 373 |
+
pooling_type=llama_cpp.LLAMA_POOLING_TYPE_UNSPECIFIED,
|
| 374 |
+
rope_freq_base=0.0,
|
| 375 |
+
rope_freq_scale=0.0,
|
| 376 |
+
yarn_ext_factor=-1.0,
|
| 377 |
+
yarn_attn_factor=1.0,
|
| 378 |
+
yarn_beta_fast=32.0,
|
| 379 |
+
yarn_beta_slow=1.0,
|
| 380 |
+
yarn_orig_ctx=0,
|
| 381 |
+
logits_all=False,
|
| 382 |
+
embedding=False,
|
| 383 |
+
offload_kqv=True,
|
| 384 |
+
last_n_tokens_size=64,
|
| 385 |
+
lora_base=None,
|
| 386 |
+
lora_scale=1.0,
|
| 387 |
+
lora_path=None,
|
| 388 |
+
numa=False,
|
| 389 |
+
chat_format=None,
|
| 390 |
+
chat_handler=None,
|
| 391 |
+
draft_model=None,
|
| 392 |
+
tokenizer=None,
|
| 393 |
+
type_k=None,
|
| 394 |
+
type_v=None,
|
| 395 |
+
verbose=True
|
| 396 |
+
)
|
| 397 |
+
|
| 398 |
+
|
| 399 |
+
display(systemTextArea)
|
| 400 |
+
display(userTextArea)
|
| 401 |
+
display(ragPromptTextArea)
|
| 402 |
+
display(responseTextArea)
|
| 403 |
+
display(selectRag)
|
| 404 |
+
display(submitButton)
|
| 405 |
+
|
| 406 |
+
def setPrompt(pprompt,ragFlag):
|
| 407 |
+
print("\n### setPrompt() entered. ragFlag: ",ragFlag)
|
| 408 |
+
if ragFlag:
|
| 409 |
+
ragPrompt = setRagPrompt(pprompt)
|
| 410 |
+
userPrompt = pprompt + "\n" + ragPrompt
|
| 411 |
+
prompt = userPrompt
|
| 412 |
+
else:
|
| 413 |
+
userPrompt = pprompt
|
| 414 |
+
prompt = f""" <s> [INST] <<SYS>> {systemTextArea.value} </SYS>> Q: {userPrompt} A: [/INST]"""
|
| 415 |
+
return prompt
|
| 416 |
+
|
| 417 |
+
def runModel(prompt):
|
| 418 |
+
output = llm.create_completion(
|
| 419 |
+
prompt, # Prompt
|
| 420 |
+
max_tokens=4096, # Generate up to 32 tokens
|
| 421 |
+
#stop = ["Q:", "\n"], # Stop generating just before the model would generate a new question
|
| 422 |
+
echo = False # Echo the prompt back in the output
|
| 423 |
+
)
|
| 424 |
+
responseTextArea.value = output["choices"][0]["text"]
|
| 425 |
+
|
| 426 |
+
def on_submitButton_clicked(b):
|
| 427 |
+
with output_widget:
|
| 428 |
+
clear_output(wait=True)
|
| 429 |
+
ragPromptTextArea.value = ""
|
| 430 |
+
responseTextArea.value = ""
|
| 431 |
+
log.debug(f"### selectRag: {selectRag.value}")
|
| 432 |
+
prompt = setPrompt(userTextArea.value,selectRag.value)
|
| 433 |
+
log.debug("### prompt: " + prompt)
|
| 434 |
+
runModel(prompt)
|
| 435 |
+
|
| 436 |
+
submitButton.on_click(on_submitButton_clicked)
|
| 437 |
+
display(output_widget)
|
| 438 |
+
|
| 439 |
+
|
| 440 |
+
logger.info("#### Closing client db connection.")
|
| 441 |
+
client.close()
|
| 442 |
+
|
| 443 |
+
logger.info("#### Program terminating.")
|
startup.sh
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#! /bin/bash
|
| 2 |
+
|
| 3 |
+
echo "#### startup.sh entered."
|
| 4 |
+
echo "### df -h"; df -h
|
| 5 |
+
#echo "### ls -l /app"; ls -l /app
|
| 6 |
+
#echo "### ls -l /app/weaviate"; ls -l /app/weaviate
|
| 7 |
+
#echo "### ls -l /app/text2vec-transformers"; ls -l /app/text2vec-transformers
|
| 8 |
+
#echo "### ls -l /data"; ls -l /data
|
| 9 |
+
|
| 10 |
+
mkdir -p /data/var/lib/weaviate
|
| 11 |
+
chmod -R 777 /data/var/lib/weaviate
|
| 12 |
+
echo "### ls -al /data/var/lib/weaviate"; ls -al /data/var/lib/weaviate
|
| 13 |
+
|
| 14 |
+
################################################
|
| 15 |
+
# Start tex2vec-transformers
|
| 16 |
+
echo "#### Before /app/text2vec-transformers"
|
| 17 |
+
/app/text2vec-transformers/bin/uvicorn app:app --host 0.0.0.0 --port 8081 --log-level info --timeout-keep-alive 1440 2>& 1 | tee /data/var/lib/weaviate/t2v.log &
|
| 18 |
+
|
| 19 |
+
#sleep 5
|
| 20 |
+
#echo "\n######## curl t2 "
|
| 21 |
+
#for (( ; ; )) do curl localhost:8081/vectors -H 'Content-Type: application/json' -d '{"text": "foo bar"}'; sleep 61; done &
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
###############################################
|
| 25 |
+
# Start the weaviate vector database server.
|
| 26 |
+
echo "#### Before /app/weaviate"
|
| 27 |
+
|
| 28 |
+
#echo "### pwd"; pwd
|
| 29 |
+
#echo "### ls -al ~"; ls -al ~
|
| 30 |
+
|
| 31 |
+
#echo "### ls -al ~"; ls -al ~
|
| 32 |
+
#ln -s ~/var/lib/weaviate /var/lib/weaviate
|
| 33 |
+
|
| 34 |
+
#echo "### ls -l /var/lib/weaviate"; ls -l /var/lib/weaviate
|
| 35 |
+
#echo "### ls -l /data"; ls -l /data
|
| 36 |
+
#echo "### ls -l /data/var/lib/weaviate"; ls -l /data/var/lib/weaviate
|
| 37 |
+
|
| 38 |
+
export AUTHENTICATION_ANONYMOUS_ACCESS_ENABLED=true \
|
| 39 |
+
PERSISTENCE_DATA_PATH=/data/var/lib/weaviate \
|
| 40 |
+
DEFAULT_VECTORIZER_MODULE=text2vec-transformers \
|
| 41 |
+
ENABLE_MODULES=text2vec-transformers \
|
| 42 |
+
TRANSFORMERS_INFERENCE_API=http://127.0.0.1:8081 \
|
| 43 |
+
LOG_LEVEL=info \
|
| 44 |
+
MODULES_CLIENT_TIMEOUT=600s
|
| 45 |
+
env
|
| 46 |
+
/app/weaviate/weaviate --host 127.0.0.1 --port 8080 --scheme http --write-timeout 600s 2>& 1 | tee /data/var/lib/weaviate/ws.log &
|
| 47 |
+
|
| 48 |
+
echo "#### Before sleep."
|
| 49 |
+
sleep 60
|
| 50 |
+
|
| 51 |
+
echo "#### Before /app/semsearch.py"
|
| 52 |
+
python /app/semsearch.py 2>& 1 | tee /data/var/lib/weaviate/ss.log &
|
| 53 |
+
|
| 54 |
+
# Display timestamps.
|
| 55 |
+
for (( ; ; )) do date; sleep 60; done &
|
| 56 |
+
|
| 57 |
+
sleep 10
|
| 58 |
+
echo "#############################"
|
| 59 |
+
df -h
|
| 60 |
+
ls -al /data/var/lib/weaviate
|
| 61 |
+
ls -al /data/var/lib/weaviate/*
|
| 62 |
+
|
| 63 |
+
wait
|
| 64 |
+
|
| 65 |
+
|