Spaces:
Sleeping
Sleeping
llm-wizard
commited on
Commit
β’
9f1b514
1
Parent(s):
d030b37
Final Versions
Browse files- BuildingAChainlitApp.md +103 -0
BuildingAChainlitApp.md
CHANGED
@@ -101,10 +101,113 @@ Notice a few things:
|
|
101 |
4. We chain the response of the LLM call to the user
|
102 |
3. We are using a lot of `async` again!
|
103 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
104 |
#### QUESTION #1:
|
105 |
|
106 |
Why do we want to support streaming? What about streaming is important, or useful?
|
107 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
108 |
|
109 |
|
110 |
|
|
|
101 |
4. We chain the response of the LLM call to the user
|
102 |
3. We are using a lot of `async` again!
|
103 |
|
104 |
+
Now, we're going to create a helper function for processing uploaded text files.
|
105 |
+
|
106 |
+
First, we'll instantiate a shared `CharacterTextSplitter`.
|
107 |
+
|
108 |
+
```python
|
109 |
+
text_splitter = CharacterTextSplitter()
|
110 |
+
```
|
111 |
+
|
112 |
+
Now we can define our helper.
|
113 |
+
|
114 |
+
```python
|
115 |
+
def process_text_file(file: AskFileResponse):
|
116 |
+
import tempfile
|
117 |
+
|
118 |
+
with tempfile.NamedTemporaryFile(mode="w", delete=False, suffix=".txt") as temp_file:
|
119 |
+
temp_file_path = temp_file.name
|
120 |
+
|
121 |
+
with open(temp_file_path, "wb") as f:
|
122 |
+
f.write(file.content)
|
123 |
+
|
124 |
+
text_loader = TextFileLoader(temp_file_path)
|
125 |
+
documents = text_loader.load_documents()
|
126 |
+
texts = text_splitter.split_texts(documents)
|
127 |
+
return texts
|
128 |
+
```
|
129 |
+
|
130 |
+
Simply put, this downloads the file as a temp file, we load it in with `TextFileLoader` and then split it with our `TextSplitter`, and returns that list of strings!
|
131 |
+
|
132 |
#### QUESTION #1:
|
133 |
|
134 |
Why do we want to support streaming? What about streaming is important, or useful?
|
135 |
|
136 |
+
## On Chat Start:
|
137 |
+
|
138 |
+
The next scope is where "the magic happens". On Chat Start is when a user begins a chat session. This will happen whenever a user opens a new chat window, or refreshes an existing chat window.
|
139 |
+
|
140 |
+
You'll see that our code is set-up to immediately show the user a chat box requesting them to upload a file.
|
141 |
+
|
142 |
+
```python
|
143 |
+
while files == None:
|
144 |
+
files = await cl.AskFileMessage(
|
145 |
+
content="Please upload a Text File file to begin!",
|
146 |
+
accept=["text/plain"],
|
147 |
+
max_size_mb=2,
|
148 |
+
timeout=180,
|
149 |
+
).send()
|
150 |
+
```
|
151 |
+
|
152 |
+
Once we've obtained the text file - we'll use our processing helper function to process our text!
|
153 |
+
|
154 |
+
After we have processed our text file - we'll need to create a `VectorDatabase` and populate it with our processed chunks and their related embeddings!
|
155 |
+
|
156 |
+
```python
|
157 |
+
vector_db = VectorDatabase()
|
158 |
+
vector_db = await vector_db.abuild_from_list(texts)
|
159 |
+
```
|
160 |
+
|
161 |
+
Once we have that piece completed - we can create the chain we'll be using to respond to user queries!
|
162 |
+
|
163 |
+
```python
|
164 |
+
retrieval_augmented_qa_pipeline = RetrievalAugmentedQAPipeline(
|
165 |
+
vector_db_retriever=vector_db,
|
166 |
+
llm=chat_openai
|
167 |
+
)
|
168 |
+
```
|
169 |
+
|
170 |
+
Now, we'll save that into our user session!
|
171 |
+
|
172 |
+
> NOTE: Chainlit has some great documentation about [User Session](https://docs.chainlit.io/concepts/user-session).
|
173 |
+
|
174 |
+
### QUESTION #2:
|
175 |
+
|
176 |
+
Why are we using User Session here? What about Python makes us need to use this? Why not just store everything in a global variable?
|
177 |
+
|
178 |
+
## On Message
|
179 |
+
|
180 |
+
First, we load our chain from the user session:
|
181 |
+
|
182 |
+
```python
|
183 |
+
chain = cl.user_session.get("chain")
|
184 |
+
```
|
185 |
+
|
186 |
+
Then, we run the chain on the content of the message - and stream it to the front end - that's it!
|
187 |
+
|
188 |
+
```python
|
189 |
+
msg = cl.Message(content="")
|
190 |
+
result = await chain.arun_pipeline(message.content)
|
191 |
+
|
192 |
+
async for stream_resp in result["response"]:
|
193 |
+
await msg.stream_token(stream_resp)
|
194 |
+
```
|
195 |
+
|
196 |
+
## π
|
197 |
+
|
198 |
+
With that - you've created a Chainlit application that moves our Pythonic RAG notebook to a Chainlit application!
|
199 |
+
|
200 |
+
## π§ CHALLENGE MODE π§
|
201 |
+
|
202 |
+
For an extra challenge - modify the behaviour of your applciation by integrating changes you made to your Pythonic RAG notebook (using new retrieval methods, etc.)
|
203 |
+
|
204 |
+
If you're still looking for a challenge, or didn't make any modifications to your Pythonic RAG notebook:
|
205 |
+
|
206 |
+
1) Allow users to upload PDFs (this will require you to build a PDF parser as well)
|
207 |
+
2) Modify the VectorStore to leverage [Qdrant](https://python-client.qdrant.tech/)
|
208 |
+
|
209 |
+
> NOTE: The motivation for these challenges is simple - the beginning of the course is extremely information dense, and people come from all kinds of different technical backgrounds. In order to ensure that all learners are able to engage with the content confidently and comfortably, we want to focus on the basic units of technical competency required. This leads to a situation where some learners, who came in with more robust technical skills, find the introductory material to be too simple - and these open-ended challenges help us do this!
|
210 |
+
|
211 |
|
212 |
|
213 |
|