Spaces:
Sleeping
Sleeping
llm-wizard
commited on
Commit
•
d030b37
1
Parent(s):
b663f5c
Adding Instructions
Browse files- BuildingAChainlitApp.md +111 -0
- README.md +58 -1
BuildingAChainlitApp.md
ADDED
@@ -0,0 +1,111 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Building a Chainlit App
|
2 |
+
|
3 |
+
What if we want to take our Week 1 Day 2 assignment - [Pythonic RAG](https://github.com/AI-Maker-Space/AIE4/tree/main/Week%201/Day%202) - and bring it out of the notebook?
|
4 |
+
|
5 |
+
Well - we'll cover exactly that here!
|
6 |
+
|
7 |
+
## Anatomy of a Chainlit Application
|
8 |
+
|
9 |
+
[Chainlit](https://docs.chainlit.io/get-started/overview) is a Python package similar to Streamlit that lets users write a backend and a front end in a single (or multiple) Python file(s). It is mainly used for prototyping LLM-based Chat Style Applications - though it is used in production in some settings with 1,000,000s of MAUs (Monthly Active Users).
|
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 |
+
|
17 |
+
1. On application start - when we start the Chainlit application with a command like `chainlit run app.py`
|
18 |
+
2. On chat start - when a chat session starts (a user opens the web browser to the address hosting the application)
|
19 |
+
3. On message - when the users sends a message through the input text box in the Chainlit UI
|
20 |
+
|
21 |
+
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
|
29 |
+
from typing import List
|
30 |
+
from chainlit.types import AskFileResponse
|
31 |
+
from aimakerspace.text_utils import CharacterTextSplitter, TextFileLoader
|
32 |
+
from aimakerspace.openai_utils.prompts import (
|
33 |
+
UserRolePrompt,
|
34 |
+
SystemRolePrompt,
|
35 |
+
AssistantRolePrompt,
|
36 |
+
)
|
37 |
+
from aimakerspace.openai_utils.embedding import EmbeddingModel
|
38 |
+
from aimakerspace.vectordatabase import VectorDatabase
|
39 |
+
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 = """\
|
47 |
+
Use the following context to answer a users question. If you cannot find the answer in the context, say you don't know the answer."""
|
48 |
+
system_role_prompt = SystemRolePrompt(system_template)
|
49 |
+
|
50 |
+
user_prompt_template = """\
|
51 |
+
Context:
|
52 |
+
{context}
|
53 |
+
|
54 |
+
Question:
|
55 |
+
{question}
|
56 |
+
"""
|
57 |
+
user_role_prompt = UserRolePrompt(user_prompt_template)
|
58 |
+
```
|
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 |
+
|
66 |
+
```python
|
67 |
+
class RetrievalAugmentedQAPipeline:
|
68 |
+
def __init__(self, llm: ChatOpenAI(), vector_db_retriever: VectorDatabase) -> None:
|
69 |
+
self.llm = llm
|
70 |
+
self.vector_db_retriever = vector_db_retriever
|
71 |
+
|
72 |
+
async def arun_pipeline(self, user_query: str):
|
73 |
+
### RETRIEVAL
|
74 |
+
context_list = self.vector_db_retriever.search_by_text(user_query, k=4)
|
75 |
+
|
76 |
+
context_prompt = ""
|
77 |
+
for context in context_list:
|
78 |
+
context_prompt += context[0] + "\n"
|
79 |
+
|
80 |
+
### AUGMENTED
|
81 |
+
formatted_system_prompt = system_role_prompt.create_message()
|
82 |
+
|
83 |
+
formatted_user_prompt = user_role_prompt.create_message(question=user_query, context=context_prompt)
|
84 |
+
|
85 |
+
|
86 |
+
### GENERATION
|
87 |
+
async def generate_response():
|
88 |
+
async for chunk in self.llm.astream([formatted_system_prompt, formatted_user_prompt]):
|
89 |
+
yield chunk
|
90 |
+
|
91 |
+
return {"response": generate_response(), "context": context_list}
|
92 |
+
```
|
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 |
+
|
README.md
CHANGED
@@ -16,10 +16,16 @@ Today, we will repeat the same process - but powered by our Pythonic RAG impleme
|
|
16 |
|
17 |
You'll notice a few differences in the `app.py` logic - as well as a few changes to the `aimakerspace` package to get things working smoothly with Chainlit.
|
18 |
|
|
|
|
|
|
|
|
|
19 |
## Deploying the Application to Hugging Face Space
|
20 |
|
21 |
Due to the way the repository is created - it should be straightforward to deploy this to a Hugging Face Space!
|
22 |
|
|
|
|
|
23 |
<details>
|
24 |
<summary>Creating a Hugging Face Space</summary>
|
25 |
|
@@ -58,4 +64,55 @@ git remote add hf HF_SPACE_SSH_ADDRESS_HERE
|
|
58 |
git pull hf main --no-rebase --allow-unrelated-histories -X ours
|
59 |
```
|
60 |
|
61 |
-
4.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
16 |
|
17 |
You'll notice a few differences in the `app.py` logic - as well as a few changes to the `aimakerspace` package to get things working smoothly with Chainlit.
|
18 |
|
19 |
+
## Reference Diagram (It's Busy, but it works)
|
20 |
+
|
21 |
+
![image](https://i.imgur.com/IaEVZG2.png)
|
22 |
+
|
23 |
## Deploying the Application to Hugging Face Space
|
24 |
|
25 |
Due to the way the repository is created - it should be straightforward to deploy this to a Hugging Face Space!
|
26 |
|
27 |
+
> NOTE: If you wish to go through the local deployments using `chainlit run app.py` and Docker - please feel free to do so!
|
28 |
+
|
29 |
<details>
|
30 |
<summary>Creating a Hugging Face Space</summary>
|
31 |
|
|
|
64 |
git pull hf main --no-rebase --allow-unrelated-histories -X ours
|
65 |
```
|
66 |
|
67 |
+
4. Use the command:
|
68 |
+
|
69 |
+
```bash
|
70 |
+
git add .
|
71 |
+
```
|
72 |
+
|
73 |
+
5. Use the command:
|
74 |
+
|
75 |
+
```bash
|
76 |
+
git commit -m "Deploying Pythonic RAG"
|
77 |
+
```
|
78 |
+
|
79 |
+
6. Use the command:
|
80 |
+
|
81 |
+
```bash
|
82 |
+
git push hf main
|
83 |
+
```
|
84 |
+
|
85 |
+
7. The Space should automatically build as soon as the push is completed!
|
86 |
+
|
87 |
+
> NOTE: The build will fail before you complete the following steps!
|
88 |
+
|
89 |
+
</details>
|
90 |
+
|
91 |
+
<details>
|
92 |
+
<summary>Adding OpenAI Secrets to the Space</summary>
|
93 |
+
|
94 |
+
1. Navigate to your Space settings.
|
95 |
+
|
96 |
+
![image](https://i.imgur.com/zh0a2By.png)
|
97 |
+
|
98 |
+
2. Navigate to `Variables and secrets` on the Settings page and click `New secret`:
|
99 |
+
|
100 |
+
![image](https://i.imgur.com/g2KlZdz.png)
|
101 |
+
|
102 |
+
3. In the `Name` field - input `OPENAI_API_KEY` in the `Value (private)` field, put your OpenAI API Key.
|
103 |
+
|
104 |
+
![image](https://i.imgur.com/eFcZ8U3.png)
|
105 |
+
|
106 |
+
4. The Space will begin rebuilding!
|
107 |
+
|
108 |
+
</details>
|
109 |
+
|
110 |
+
## 🎉
|
111 |
+
|
112 |
+
You just deployed Pythonic RAG!
|
113 |
+
|
114 |
+
Try uploading a text file and asking some questions!
|
115 |
+
|
116 |
+
## 🚧CHALLENGE MODE 🚧
|
117 |
+
|
118 |
+
For more of a challenge, please reference [Building a Chainlit App](./BuildingAChainlitApp.md)!
|