Dhritiman Sagar commited on
Commit
1cfff16
1 Parent(s): e86455d

Add app files

Browse files
Files changed (5) hide show
  1. Dockerfile +11 -0
  2. app.py +197 -0
  3. chainlit.md +3 -0
  4. data/paul_graham_essays.txt +0 -0
  5. requirements.txt +132 -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,197 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ from tqdm.asyncio import tqdm_asyncio
15
+ import asyncio
16
+ from tqdm.asyncio import tqdm
17
+
18
+ # GLOBAL SCOPE - ENTIRE APPLICATION HAS ACCESS TO VALUES SET IN THIS SCOPE #
19
+ # ---- ENV VARIABLES ---- #
20
+ """
21
+ This function will load our environment file (.env) if it is present.
22
+
23
+ NOTE: Make sure that .env is in your .gitignore file - it is by default, but please ensure it remains there.
24
+ """
25
+ load_dotenv()
26
+
27
+ """
28
+ We will load our environment variables here.
29
+ """
30
+ HF_LLM_ENDPOINT = os.environ["HF_LLM_ENDPOINT"]
31
+ HF_EMBED_ENDPOINT = os.environ["HF_EMBED_ENDPOINT"]
32
+ HF_TOKEN = os.environ["HF_TOKEN"]
33
+
34
+ # ---- GLOBAL DECLARATIONS ---- #
35
+
36
+ # -- RETRIEVAL -- #
37
+ """
38
+ 1. Load Documents from Text File
39
+ 2. Split Documents into Chunks
40
+ 3. Load HuggingFace Embeddings (remember to use the URL we set above)
41
+ 4. Index Files if they do not exist, otherwise load the vectorstore
42
+ """
43
+ ### 1. CREATE TEXT LOADER AND LOAD DOCUMENTS
44
+ ### NOTE: PAY ATTENTION TO THE PATH THEY ARE IN.
45
+ text_loader = TextLoader("./data/paul_graham_essays.txt")
46
+ documents = text_loader.load()
47
+
48
+ ### 2. CREATE TEXT SPLITTER AND SPLIT DOCUMENTS
49
+ text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=30)
50
+ split_documents = text_splitter.split_documents(documents)
51
+
52
+ ### 3. LOAD HUGGINGFACE EMBEDDINGS
53
+ hf_embeddings = HuggingFaceEndpointEmbeddings(
54
+ model=HF_EMBED_ENDPOINT,
55
+ task="feature-extraction",
56
+ huggingfacehub_api_token=HF_TOKEN,
57
+ )
58
+
59
+ async def add_documents_async(vectorstore, documents):
60
+ await vectorstore.aadd_documents(documents)
61
+
62
+ async def process_batch(vectorstore, batch, is_first_batch, pbar):
63
+ if is_first_batch:
64
+ result = await FAISS.afrom_documents(batch, hf_embeddings)
65
+ else:
66
+ await add_documents_async(vectorstore, batch)
67
+ result = vectorstore
68
+ pbar.update(len(batch))
69
+ return result
70
+
71
+ async def main():
72
+ """Runs on application start"""
73
+ print("Indexing Files")
74
+
75
+ vectorstore = None
76
+ batch_size = 32
77
+
78
+ batches = [split_documents[i:i+batch_size] for i in range(0, len(split_documents), batch_size)]
79
+
80
+ async def process_all_batches():
81
+ nonlocal vectorstore
82
+ tasks = []
83
+ pbars = []
84
+
85
+ for i, batch in enumerate(batches):
86
+ pbar = tqdm(total=len(batch), desc=f"Batch {i+1}/{len(batches)}", position=i)
87
+ pbars.append(pbar)
88
+
89
+ if i == 0:
90
+ vectorstore = await process_batch(None, batch, True, pbar)
91
+ else:
92
+ tasks.append(process_batch(vectorstore, batch, False, pbar))
93
+
94
+ if tasks:
95
+ await asyncio.gather(*tasks)
96
+
97
+ for pbar in pbars:
98
+ pbar.close()
99
+
100
+ await process_all_batches()
101
+
102
+ hf_retriever = vectorstore.as_retriever()
103
+ print("\nIndexing complete. Vectorstore is ready for use.")
104
+ return hf_retriever
105
+
106
+ async def run():
107
+ retriever = await main()
108
+ return retriever
109
+
110
+ hf_retriever = asyncio.run(run())
111
+
112
+ # -- AUGMENTED -- #
113
+ """
114
+ 1. Define a String Template
115
+ 2. Create a Prompt Template from the String Template
116
+ """
117
+ ### 1. DEFINE STRING TEMPLATE
118
+ RAG_PROMPT_TEMPLATE = """\
119
+ <|start_header_id|>system<|end_header_id|>
120
+ You are a helpful assistant. You answer user questions based on provided context. If you can't answer the question with the provided context, say you don't know.<|eot_id|>
121
+
122
+ <|start_header_id|>user<|end_header_id|>
123
+ User Query:
124
+ {query}
125
+
126
+ Context:
127
+ {context}<|eot_id|>
128
+
129
+ <|start_header_id|>assistant<|end_header_id|>
130
+ """
131
+
132
+ ### 2. CREATE PROMPT TEMPLATE
133
+ rag_prompt = PromptTemplate.from_template(RAG_PROMPT_TEMPLATE)
134
+
135
+ # -- GENERATION -- #
136
+ """
137
+ 1. Create a HuggingFaceEndpoint for the LLM
138
+ """
139
+ ### 1. CREATE HUGGINGFACE ENDPOINT FOR LLM
140
+ hf_llm = HuggingFaceEndpoint(
141
+ endpoint_url=HF_LLM_ENDPOINT,
142
+ max_new_tokens=512,
143
+ top_k=10,
144
+ top_p=0.95,
145
+ typical_p=0.95,
146
+ temperature=0.01,
147
+ repetition_penalty=1.03,
148
+ huggingfacehub_api_token=HF_TOKEN
149
+ )
150
+
151
+ @cl.author_rename
152
+ def rename(original_author: str):
153
+ """
154
+ This function can be used to rename the 'author' of a message.
155
+
156
+ In this case, we're overriding the 'Assistant' author to be 'Paul Graham Essay Bot'.
157
+ """
158
+ rename_dict = {
159
+ "Assistant" : "Paul Graham Essay Bot"
160
+ }
161
+ return rename_dict.get(original_author, original_author)
162
+
163
+ @cl.on_chat_start
164
+ async def start_chat():
165
+ """
166
+ This function will be called at the start of every user session.
167
+
168
+ We will build our LCEL RAG chain here, and store it in the user session.
169
+
170
+ The user session is a dictionary that is unique to each user session, and is stored in the memory of the server.
171
+ """
172
+
173
+ ### BUILD LCEL RAG CHAIN THAT ONLY RETURNS TEXT
174
+ lcel_rag_chain = {"context": itemgetter("query") | hf_retriever, "query": itemgetter("query")}| rag_prompt | hf_llm
175
+
176
+ cl.user_session.set("lcel_rag_chain", lcel_rag_chain)
177
+
178
+ @cl.on_message
179
+ async def handle_message(message: cl.Message):
180
+ """
181
+ This function will be called every time a message is recieved from a session.
182
+
183
+ We will use the LCEL RAG chain to generate a response to the user query.
184
+
185
+ 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.
186
+ """
187
+ lcel_rag_chain = cl.user_session.get("lcel_rag_chain")
188
+
189
+ msg = cl.Message(content="")
190
+
191
+ async for chunk in lcel_rag_chain.astream(
192
+ {"query": message.content},
193
+ config=RunnableConfig(callbacks=[cl.LangchainCallbackHandler()]),
194
+ ):
195
+ await msg.stream_token(chunk)
196
+
197
+ await msg.send()
chainlit.md ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ # Paul Graham Essay Bot
2
+
3
+ Hello! I can answer all your questions about Paul Graham's public essays. Ask away!
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,132 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ aiofiles==23.2.1
2
+ aiohappyeyeballs==2.4.3
3
+ aiohttp==3.10.8
4
+ aiosignal==1.3.1
5
+ annotated-types==0.7.0
6
+ anyio==3.7.1
7
+ async-timeout==4.0.3
8
+ asyncer==0.0.2
9
+ attrs==24.2.0
10
+ bidict==0.23.1
11
+ certifi==2024.8.30
12
+ chainlit==0.7.700
13
+ charset-normalizer==3.3.2
14
+ click==8.1.7
15
+ dataclasses-json==0.5.14
16
+ Deprecated==1.2.14
17
+ distro==1.9.0
18
+ exceptiongroup==1.2.2
19
+ faiss-cpu==1.8.0.post1
20
+ fastapi==0.100.1
21
+ fastapi-socketio==0.0.10
22
+ filelock==3.16.1
23
+ filetype==1.2.0
24
+ frozenlist==1.4.1
25
+ fsspec==2024.9.0
26
+ googleapis-common-protos==1.65.0
27
+ greenlet==3.1.1
28
+ grpcio==1.66.2
29
+ grpcio-tools==1.62.3
30
+ h11==0.14.0
31
+ h2==4.1.0
32
+ hpack==4.0.0
33
+ httpcore==0.17.3
34
+ httpx==0.24.1
35
+ huggingface-hub==0.25.1
36
+ hyperframe==6.0.1
37
+ idna==3.10
38
+ importlib_metadata==8.4.0
39
+ Jinja2==3.1.4
40
+ jiter==0.5.0
41
+ joblib==1.4.2
42
+ jsonpatch==1.33
43
+ jsonpointer==3.0.0
44
+ langchain==0.3.0
45
+ langchain-community==0.3.0
46
+ langchain-core==0.3.1
47
+ langchain-huggingface==0.1.0
48
+ langchain-openai==0.2.0
49
+ langchain-qdrant==0.1.4
50
+ langchain-text-splitters==0.3.0
51
+ langsmith==0.1.121
52
+ Lazify==0.4.0
53
+ MarkupSafe==2.1.5
54
+ marshmallow==3.22.0
55
+ mpmath==1.3.0
56
+ multidict==6.1.0
57
+ mypy-extensions==1.0.0
58
+ nest-asyncio==1.6.0
59
+ networkx==3.2.1
60
+ numpy==1.26.4
61
+ nvidia-cublas-cu12==12.1.3.1
62
+ nvidia-cuda-cupti-cu12==12.1.105
63
+ nvidia-cuda-nvrtc-cu12==12.1.105
64
+ nvidia-cuda-runtime-cu12==12.1.105
65
+ nvidia-cudnn-cu12==9.1.0.70
66
+ nvidia-cufft-cu12==11.0.2.54
67
+ nvidia-curand-cu12==10.3.2.106
68
+ nvidia-cusolver-cu12==11.4.5.107
69
+ nvidia-cusparse-cu12==12.1.0.106
70
+ nvidia-nccl-cu12==2.20.5
71
+ nvidia-nvjitlink-cu12==12.6.77
72
+ nvidia-nvtx-cu12==12.1.105
73
+ openai==1.51.0
74
+ opentelemetry-api==1.27.0
75
+ opentelemetry-exporter-otlp==1.27.0
76
+ opentelemetry-exporter-otlp-proto-common==1.27.0
77
+ opentelemetry-exporter-otlp-proto-grpc==1.27.0
78
+ opentelemetry-exporter-otlp-proto-http==1.27.0
79
+ opentelemetry-instrumentation==0.48b0
80
+ opentelemetry-proto==1.27.0
81
+ opentelemetry-sdk==1.27.0
82
+ opentelemetry-semantic-conventions==0.48b0
83
+ orjson==3.10.7
84
+ packaging==23.2
85
+ pillow==10.4.0
86
+ portalocker==2.10.1
87
+ protobuf==4.25.5
88
+ pydantic==2.9.2
89
+ pydantic-settings==2.5.2
90
+ pydantic_core==2.23.4
91
+ PyJWT==2.9.0
92
+ PyMuPDF==1.24.10
93
+ PyMuPDFb==1.24.10
94
+ python-dotenv==1.0.1
95
+ python-engineio==4.9.1
96
+ python-graphql-client==0.4.3
97
+ python-multipart==0.0.6
98
+ python-socketio==5.11.4
99
+ PyYAML==6.0.2
100
+ qdrant-client==1.11.2
101
+ regex==2024.9.11
102
+ requests==2.32.3
103
+ safetensors==0.4.5
104
+ scikit-learn==1.5.2
105
+ scipy==1.13.1
106
+ sentence-transformers==3.1.1
107
+ simple-websocket==1.0.0
108
+ sniffio==1.3.1
109
+ SQLAlchemy==2.0.35
110
+ starlette==0.27.0
111
+ sympy==1.13.3
112
+ syncer==2.0.3
113
+ tenacity==8.5.0
114
+ threadpoolctl==3.5.0
115
+ tiktoken==0.7.0
116
+ tokenizers==0.20.0
117
+ tomli==2.0.1
118
+ torch==2.4.1
119
+ tqdm==4.66.5
120
+ transformers==4.45.1
121
+ triton==3.0.0
122
+ typing-inspect==0.9.0
123
+ typing_extensions==4.12.2
124
+ uptrace==1.26.0
125
+ urllib3==2.2.3
126
+ uvicorn==0.23.2
127
+ watchfiles==0.20.0
128
+ websockets==13.1
129
+ wrapt==1.16.0
130
+ wsproto==1.2.0
131
+ yarl==1.13.1
132
+ zipp==3.20.2