operion files
Browse files- Dockerfile +11 -0
- app.py +167 -0
- chainlit.md +1 -0
- data/paul_graham_essays.txt +0 -0
- requirements.txt +8 -0
Dockerfile
ADDED
@@ -0,0 +1,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
FROM python:3.9
|
2 |
+
RUN useradd -m -u 1000 user
|
3 |
+
USER user
|
4 |
+
ENV HOME=/home/user \
|
5 |
+
PATH=/home/user/.local/bin:$PATH
|
6 |
+
WORKDIR $HOME/app
|
7 |
+
COPY --chown=user . $HOME/app
|
8 |
+
COPY ./requirements.txt ~/app/requirements.txt
|
9 |
+
RUN pip install -r requirements.txt
|
10 |
+
COPY . .
|
11 |
+
CMD ["chainlit", "run", "app.py", "--port", "7860"]
|
app.py
ADDED
@@ -0,0 +1,167 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import chainlit as cl
|
3 |
+
from dotenv import load_dotenv
|
4 |
+
from operator import itemgetter
|
5 |
+
from langchain_huggingface import HuggingFaceEndpoint
|
6 |
+
from langchain_community.document_loaders import TextLoader
|
7 |
+
from langchain_text_splitters import RecursiveCharacterTextSplitter
|
8 |
+
from langchain_community.vectorstores import FAISS
|
9 |
+
from langchain_huggingface import HuggingFaceEndpointEmbeddings
|
10 |
+
from langchain_core.prompts import PromptTemplate
|
11 |
+
from langchain.schema.output_parser import StrOutputParser
|
12 |
+
from langchain.schema.runnable import RunnablePassthrough
|
13 |
+
from langchain.schema.runnable.config import RunnableConfig
|
14 |
+
|
15 |
+
# GLOBAL SCOPE - ENTIRE APPLICATION HAS ACCESS TO VALUES SET IN THIS SCOPE #
|
16 |
+
# ---- ENV VARIABLES ---- #
|
17 |
+
"""
|
18 |
+
This function will load our environment file (.env) if it is present.
|
19 |
+
|
20 |
+
NOTE: Make sure that .env is in your .gitignore file - it is by default, but please ensure it remains there.
|
21 |
+
"""
|
22 |
+
load_dotenv()
|
23 |
+
|
24 |
+
"""
|
25 |
+
We will load our environment variables here.
|
26 |
+
"""
|
27 |
+
HF_LLM_ENDPOINT = os.environ["HF_LLM_ENDPOINT"]
|
28 |
+
HF_EMBED_ENDPOINT = os.environ["HF_EMBED_ENDPOINT"]
|
29 |
+
HF_TOKEN = os.environ["HF_TOKEN"]
|
30 |
+
|
31 |
+
# ---- GLOBAL DECLARATIONS ---- #
|
32 |
+
|
33 |
+
# -- RETRIEVAL -- #
|
34 |
+
"""
|
35 |
+
1. Load Documents from Text File
|
36 |
+
2. Split Documents into Chunks
|
37 |
+
3. Load HuggingFace Embeddings (remember to use the URL we set above)
|
38 |
+
4. Index Files if they do not exist, otherwise load the vectorstore
|
39 |
+
"""
|
40 |
+
### 1. CREATE TEXT LOADER AND LOAD DOCUMENTS
|
41 |
+
### NOTE: PAY ATTENTION TO THE PATH THEY ARE IN.
|
42 |
+
text_loader = TextLoader("./data/paul_graham_essays.txt")
|
43 |
+
documents = text_loader.load()
|
44 |
+
|
45 |
+
### 2. CREATE TEXT SPLITTER AND SPLIT DOCUMENTS
|
46 |
+
text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=30)
|
47 |
+
split_documents = text_splitter.split_documents(documents)
|
48 |
+
|
49 |
+
### 3. LOAD HUGGINGFACE EMBEDDINGS
|
50 |
+
hf_embeddings = HuggingFaceEndpointEmbeddings(
|
51 |
+
model=HF_EMBED_ENDPOINT,
|
52 |
+
task="feature-extraction",
|
53 |
+
huggingfacehub_api_token=os.environ["HF_TOKEN"],
|
54 |
+
)
|
55 |
+
## Prevent re-indexing if vectorstores already exists
|
56 |
+
if os.path.exists("./data/vectorstore"):
|
57 |
+
vectorstore = FAISS.load_local(
|
58 |
+
"./data/vectorstore",
|
59 |
+
hf_embeddings,
|
60 |
+
allow_dangerous_deserialization=True # this is necessary to load the vectorstore from disk as it's stored as a `.pkl` file.
|
61 |
+
)
|
62 |
+
hf_retriever = vectorstore.as_retriever()
|
63 |
+
print("Loaded Vectorstore")
|
64 |
+
else:
|
65 |
+
print("Indexing Files")
|
66 |
+
os.makedirs("./data/vectorstore", exist_ok=True)
|
67 |
+
### 4. INDEX FILES
|
68 |
+
### NOTE: REMEMBER TO BATCH THE DOCUMENTS WITH MAXIMUM BATCH SIZE = 32
|
69 |
+
for i in range(0, len(split_documents), 32):
|
70 |
+
if i == 0:
|
71 |
+
vectorstore = FAISS.from_documents(split_documents[i:i+32], hf_embeddings)
|
72 |
+
continue
|
73 |
+
vectorstore.add_documents(split_documents[i:i+32])
|
74 |
+
vectorstore.save_local("./data/vectorstore")
|
75 |
+
hf_retriever = vectorstore.as_retriever()
|
76 |
+
|
77 |
+
# -- AUGMENTED -- #
|
78 |
+
"""
|
79 |
+
1. Define a String Template
|
80 |
+
2. Create a Prompt Template from the String Template
|
81 |
+
"""
|
82 |
+
### 1. DEFINE STRING TEMPLATE
|
83 |
+
RAG_PROMPT_TEMPLATE = """\
|
84 |
+
<|start_header_id|>system<|end_header_id|>
|
85 |
+
You are a helpful assistant. You answer user questions based on provided context. If you can't answer the question with the provided context,\
|
86 |
+
say you don't know.<|eot_id|>
|
87 |
+
|
88 |
+
<|start_header_id|>user<|end_header_id|>
|
89 |
+
User Query:
|
90 |
+
{query}
|
91 |
+
|
92 |
+
Context:
|
93 |
+
{context}<|eot_id|>
|
94 |
+
|
95 |
+
<|start_header_id|>assistant<|end_header_id|>
|
96 |
+
"""
|
97 |
+
#Note that we do not have the response here. We have assistent, we ONLY start, but not followed by <|eot_id> as we do not have a response YET.
|
98 |
+
|
99 |
+
### 2. CREATE PROMPT TEMPLATE
|
100 |
+
rag_prompt =PromptTemplate.from_template(RAG_PROMPT_TEMPLATE)
|
101 |
+
|
102 |
+
# -- GENERATION -- #
|
103 |
+
"""
|
104 |
+
1. Create a HuggingFaceEndpoint for the LLM
|
105 |
+
"""
|
106 |
+
### 1. CREATE HUGGINGFACE ENDPOINT FOR LLM
|
107 |
+
hf_llm = HuggingFaceEndpoint(
|
108 |
+
endpoint_url=f"{HF_LLM_ENDPOINT}",
|
109 |
+
max_new_tokens=512,
|
110 |
+
top_k=10,
|
111 |
+
top_p=0.95,
|
112 |
+
typical_p=0.95,
|
113 |
+
temperature=0.01,
|
114 |
+
repetition_penalty=1.03,
|
115 |
+
huggingfacehub_api_token=os.environ["HF_TOKEN"]
|
116 |
+
)
|
117 |
+
|
118 |
+
@cl.author_rename
|
119 |
+
def rename(original_author: str):
|
120 |
+
"""
|
121 |
+
This function can be used to rename the 'author' of a message.
|
122 |
+
|
123 |
+
In this case, we're overriding the 'Assistant' author to be 'Paul Graham Essay Bot'.
|
124 |
+
"""
|
125 |
+
rename_dict = {
|
126 |
+
"Assistant" : "Paul Graham Essay Bot"
|
127 |
+
}
|
128 |
+
return rename_dict.get(original_author, original_author)
|
129 |
+
|
130 |
+
@cl.on_chat_start
|
131 |
+
async def start_chat():
|
132 |
+
"""
|
133 |
+
This function will be called at the start of every user session.
|
134 |
+
|
135 |
+
We will build our LCEL RAG chain here, and store it in the user session.
|
136 |
+
|
137 |
+
The user session is a dictionary that is unique to each user session, and is stored in the memory of the server.
|
138 |
+
"""
|
139 |
+
|
140 |
+
### BUILD LCEL RAG CHAIN THAT ONLY RETURNS TEXT
|
141 |
+
lcel_rag_chain = ( {"context": itemgetter("query") | hf_retriever, "query": itemgetter("query")}
|
142 |
+
|
143 |
+
| rag_prompt | hf_llm
|
144 |
+
)
|
145 |
+
|
146 |
+
cl.user_session.set("lcel_rag_chain", lcel_rag_chain)
|
147 |
+
|
148 |
+
@cl.on_message
|
149 |
+
async def main(message: cl.Message):
|
150 |
+
"""
|
151 |
+
This function will be called every time a message is recieved from a session.
|
152 |
+
|
153 |
+
We will use the LCEL RAG chain to generate a response to the user query.
|
154 |
+
|
155 |
+
The LCEL RAG chain is stored in the user session, and is unique to each user session - this is why we can access it here.
|
156 |
+
"""
|
157 |
+
lcel_rag_chain = cl.user_session.get("lcel_rag_chain")
|
158 |
+
|
159 |
+
msg = cl.Message(content="")
|
160 |
+
|
161 |
+
async for chunk in lcel_rag_chain.astream(
|
162 |
+
{"query": message.content},
|
163 |
+
config=RunnableConfig(callbacks=[cl.LangchainCallbackHandler()]),
|
164 |
+
):
|
165 |
+
await msg.stream_token(chunk)
|
166 |
+
|
167 |
+
await msg.send()
|
chainlit.md
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
# Here is Paul Graham Assistent BOT! Ask me any questions from his essay.
|
data/paul_graham_essays.txt
ADDED
The diff for this file is too large to render.
See raw diff
|
|
requirements.txt
ADDED
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
chainlit==0.7.700
|
2 |
+
langchain==0.2.5
|
3 |
+
langchain_community==0.2.5
|
4 |
+
langchain_core==0.2.9
|
5 |
+
langchain_huggingface==0.0.3
|
6 |
+
langchain_text_splitters==0.2.1
|
7 |
+
python-dotenv==1.0.1
|
8 |
+
faiss-cpu
|