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