Christine Mahler commited on
Commit
b1c6b79
·
1 Parent(s): df9705d
Files changed (4) hide show
  1. Dockerfile +29 -0
  2. app.py +196 -0
  3. chainlit.md +1 -0
  4. pyproject.toml +22 -0
Dockerfile ADDED
@@ -0,0 +1,29 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Get a distribution that has uv already installed
2
+ FROM ghcr.io/astral-sh/uv:python3.13-bookworm-slim
3
+
4
+ # Add user - this is the user that will run the app
5
+ # If you do not set user, the app will run as root (undesirable)
6
+ RUN useradd -m -u 1000 user
7
+ USER user
8
+
9
+ # Set the home directory and path
10
+ ENV HOME=/home/user \
11
+ PATH=/home/user/.local/bin:$PATH
12
+
13
+ ENV UVICORN_WS_PROTOCOL=websockets
14
+
15
+ # Set the working directory
16
+ WORKDIR $HOME/app
17
+
18
+ # Copy the app to the container
19
+ COPY --chown=user . $HOME/app
20
+
21
+ # Install the dependencies
22
+ # RUN uv sync --frozen
23
+ RUN uv sync
24
+
25
+ # Expose the port
26
+ EXPOSE 7860
27
+
28
+ # Run the app
29
+ CMD ["uv", "run", "chainlit", "run", "solution_app.py", "--host", "0.0.0.0", "--port", "7860"]
app.py ADDED
@@ -0,0 +1,196 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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("./paul-graham-to-kindle/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=os.environ["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
+ print("Indexing Files")
73
+
74
+ vectorstore = None
75
+ batch_size = 32
76
+
77
+ batches = [split_documents[i:i+batch_size] for i in range(0, len(split_documents), batch_size)]
78
+
79
+ async def process_all_batches():
80
+ nonlocal vectorstore
81
+ tasks = []
82
+ pbars = []
83
+
84
+ for i, batch in enumerate(batches):
85
+ pbar = tqdm(total=len(batch), desc=f"Batch {i+1}/{len(batches)}", position=i)
86
+ pbars.append(pbar)
87
+
88
+ if i == 0:
89
+ vectorstore = await process_batch(None, batch, True, pbar)
90
+ else:
91
+ tasks.append(process_batch(vectorstore, batch, False, pbar))
92
+
93
+ if tasks:
94
+ await asyncio.gather(*tasks)
95
+
96
+ for pbar in pbars:
97
+ pbar.close()
98
+
99
+ await process_all_batches()
100
+
101
+ hf_retriever = vectorstore.as_retriever()
102
+ print("\nIndexing complete. Vectorstore is ready for use.")
103
+ return hf_retriever
104
+
105
+ async def run():
106
+ retriever = await main()
107
+ return retriever
108
+
109
+ hf_retriever = asyncio.run(run())
110
+
111
+ # -- AUGMENTED -- #
112
+ """
113
+ 1. Define a String Template
114
+ 2. Create a Prompt Template from the String Template
115
+ """
116
+ ### 1. DEFINE STRING TEMPLATE
117
+ RAG_PROMPT_TEMPLATE = """\
118
+ <|start_header_id|>system<|end_header_id|>
119
+ 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|>
120
+
121
+ <|start_header_id|>user<|end_header_id|>
122
+ User Query:
123
+ {query}
124
+
125
+ Context:
126
+ {context}<|eot_id|>
127
+
128
+ <|start_header_id|>assistant<|end_header_id|>
129
+ """
130
+
131
+ ### 2. CREATE PROMPT TEMPLATE
132
+ rag_prompt = PromptTemplate.from_template(RAG_PROMPT_TEMPLATE)
133
+
134
+ # -- GENERATION -- #
135
+ """
136
+ 1. Create a HuggingFaceEndpoint for the LLM
137
+ """
138
+ ### 1. CREATE HUGGINGFACE ENDPOINT FOR LLM
139
+ hf_llm = HuggingFaceEndpoint(
140
+ endpoint_url=f"{HF_LLM_ENDPOINT}",
141
+ task="text-generation",
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
+ )
149
+
150
+ @cl.author_rename
151
+ def rename(original_author: str):
152
+ """
153
+ This function can be used to rename the 'author' of a message.
154
+
155
+ In this case, we're overriding the 'Assistant' author to be 'Paul Graham Essay Bot'.
156
+ """
157
+ rename_dict = {
158
+ "Assistant" : "Paul Graham Essay Bot"
159
+ }
160
+ return rename_dict.get(original_author, original_author)
161
+
162
+ @cl.on_chat_start
163
+ async def start_chat():
164
+ """
165
+ This function will be called at the start of every user session.
166
+
167
+ We will build our LCEL RAG chain here, and store it in the user session.
168
+
169
+ The user session is a dictionary that is unique to each user session, and is stored in the memory of the server.
170
+ """
171
+
172
+ ### BUILD LCEL RAG CHAIN THAT ONLY RETURNS TEXT
173
+ lcel_rag_chain =
174
+
175
+ cl.user_session.set("lcel_rag_chain", lcel_rag_chain)
176
+
177
+ @cl.on_message
178
+ async def main(message: cl.Message):
179
+ """
180
+ This function will be called every time a message is recieved from a session.
181
+
182
+ We will use the LCEL RAG chain to generate a response to the user query.
183
+
184
+ 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.
185
+ """
186
+ lcel_rag_chain = cl.user_session.get("lcel_rag_chain")
187
+
188
+ msg = cl.Message(content="")
189
+
190
+ async for chunk in lcel_rag_chain.astream(
191
+ {"query": message.content},
192
+ config=RunnableConfig(callbacks=[cl.LangchainCallbackHandler()]),
193
+ ):
194
+ await msg.stream_token(chunk)
195
+
196
+ await msg.send()
chainlit.md ADDED
@@ -0,0 +1 @@
 
 
1
+ # FILL OUT YOUR CHAINLIT MD HERE WITH A DESCRIPTION OF YOUR APPLICATION
pyproject.toml ADDED
@@ -0,0 +1,22 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ [project]
2
+ name = "15-app"
3
+ version = "0.1.0"
4
+ description = "Session 15 - Open Source Endpoints"
5
+ readme = "README.md"
6
+ requires-python = ">=3.09"
7
+ dependencies = [
8
+ "asyncio===3.4.3",
9
+ "chainlit==2.2.1",
10
+ "huggingface-hub==0.27.0",
11
+ "langchain-huggingface==0.1.2",
12
+ "langchain==0.3.19",
13
+ "langchain-community==0.3.18",
14
+ "langsmith==0.3.11",
15
+ "python-dotenv==1.0.1",
16
+ "tqdm==4.67.1",
17
+ "langchain-openai==0.3.7",
18
+ "langchain-text-splitters==0.3.6",
19
+ "jupyter>=1.1.1",
20
+ "faiss-cpu>=1.10.0",
21
+ "websockets>=15.0",
22
+ ]