XicoC commited on
Commit
b2c96a7
β€’
1 Parent(s): ea5fc16

Update BuildingAChainlitApp.md

Browse files
Files changed (1) hide show
  1. BuildingAChainlitApp.md +64 -10
BuildingAChainlitApp.md CHANGED
@@ -10,7 +10,7 @@ Well - we'll cover exactly that here!
10
 
11
  The primary method of customizing and interacting with the Chainlit UI is through a few critical [decorators](https://blog.hubspot.com/website/decorators-in-python).
12
 
13
- > NOTE: Simply put, the decorators (in Chainlit) are just ways we can "plug-in" to the functionality in Chainlit.
14
 
15
  We'll be concerning ourselves with three main scopes:
16
 
@@ -22,7 +22,7 @@ Let's dig into each scope and see what we're doing!
22
 
23
  ## On Application Start:
24
 
25
- The first thing you'll notice is that we have the traditional "wall of imports" this is to ensure we have everything we need to run our application.
26
 
27
  ```python
28
  import os
@@ -40,7 +40,7 @@ from aimakerspace.openai_utils.chatmodel import ChatOpenAI
40
  import chainlit as cl
41
  ```
42
 
43
- Next up, we have some prompt templates. As all sessions will use the same prompt templates without modification, and we don't need these templates to be specific per template - we can set them up here - at the application scope.
44
 
45
  ```python
46
  system_template = """\
@@ -59,7 +59,7 @@ user_role_prompt = UserRolePrompt(user_prompt_template)
59
 
60
  > NOTE: You'll notice that these are the exact same prompt templates we used from the Pythonic RAG Notebook in Week 1 Day 2!
61
 
62
- Following that - we can create the Python Class definition for our RAG pipeline - or *chain*, as we'll refer to it in the rest of this walkthrough.
63
 
64
  Let's look at the definition first:
65
 
@@ -93,19 +93,73 @@ class RetrievalAugmentedQAPipeline:
93
 
94
  Notice a few things:
95
 
96
- 1. We have modified this `RetrievalAugmentedQAPipeline` from the initial notebook to support streaming.
97
- 2. In essence, our pipeline is *chaining* a few events together:
98
- 1. We take our user query, and chain it into our Vector Database to collect related chunks
99
- 2. We take those contexts and our user's questions and chain them into the prompt templates
100
- 3. We take that prompt template and chain it into our LLM call
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
 
 
 
 
 
 
 
 
 
111
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10
 
11
  The primary method of customizing and interacting with the Chainlit UI is through a few critical [decorators](https://blog.hubspot.com/website/decorators-in-python).
12
 
13
+ > NOTE: Simply put, the decorators (in Chainlit) are just ways we can "plug-in" to the functionality in Chainlit.
14
 
15
  We'll be concerning ourselves with three main scopes:
16
 
 
22
 
23
  ## On Application Start:
24
 
25
+ The first thing you'll notice is that we have the traditional "wall of imports" this is to ensure we have everything we need to run our application.
26
 
27
  ```python
28
  import os
 
40
  import chainlit as cl
41
  ```
42
 
43
+ Next up, we have some prompt templates. As all sessions will use the same prompt templates without modification, and we don't need these templates to be specific per template - we can set them up here - at the application scope.
44
 
45
  ```python
46
  system_template = """\
 
59
 
60
  > NOTE: You'll notice that these are the exact same prompt templates we used from the Pythonic RAG Notebook in Week 1 Day 2!
61
 
62
+ Following that - we can create the Python Class definition for our RAG pipeline - or _chain_, as we'll refer to it in the rest of this walkthrough.
63
 
64
  Let's look at the definition first:
65
 
 
93
 
94
  Notice a few things:
95
 
96
+ 1. We have modified this `RetrievalAugmentedQAPipeline` from the initial notebook to support streaming.
97
+ 2. In essence, our pipeline is _chaining_ a few events together:
98
+ 1. We take our user query, and chain it into our Vector Database to collect related chunks
99
+ 2. We take those contexts and our user's questions and chain them into the prompt templates
100
+ 3. We take that prompt template and chain it into our LLM call
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
+ ## On Chat Start:
109
 
110
+ 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.
111
 
112
+ You'll see that our code is set-up to immediately show the user a chat box requesting them to upload a file.
113
 
114
+ while files == None:
115
+ files = await cl.AskFileMessage(
116
+ content="Please upload a Text File file to begin!",
117
+ accept=["text/plain"],
118
+ max_size_mb=2,
119
+ timeout=180,
120
+ ).send()
121
+ Once we've obtained the text file - we'll use our processing helper function to process our text!
122
 
123
+ 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!
124
+
125
+ vector_db = VectorDatabase()
126
+ vector_db = await vector_db.abuild_from_list(texts)
127
+ Once we have that piece completed - we can create the chain we'll be using to respond to user queries!
128
+
129
+ retrieval_augmented_qa_pipeline = RetrievalAugmentedQAPipeline(
130
+ vector_db_retriever=vector_db,
131
+ llm=chat_openai
132
+ )
133
+ Now, we'll save that into our user session!
134
+
135
+ NOTE: Chainlit has some great documentation about User Session.
136
+
137
+ #### QUESTION #2:
138
+
139
+ 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?
140
+
141
+ ## On Message
142
+
143
+ First, we load our chain from the user session:
144
+
145
+ chain = cl.user_session.get("chain")
146
+ Then, we run the chain on the content of the message - and stream it to the front end - that's it!
147
+
148
+ msg = cl.Message(content="")
149
+ result = await chain.arun_pipeline(message.content)
150
+
151
+ async for stream_resp in result["response"]:
152
+ await msg.stream_token(stream_resp)
153
+ πŸŽ‰
154
+ With that - you've created a Chainlit application that moves our Pythonic RAG notebook to a Chainlit application!
155
+
156
+ ### 🚧 CHALLENGE MODE 🚧
157
+
158
+ 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.)
159
+
160
+ If you're still looking for a challenge, or didn't make any modifications to your Pythonic RAG notebook:
161
+
162
+ 1. Allow users to upload PDFs (this will require you to build a PDF parser as well)
163
+ 2. Modify the VectorStore to leverage Qdrant
164
+
165
+ 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!