Spaces:
Runtime error
Runtime error
dsingh71
commited on
Commit
β’
7d8cfd6
1
Parent(s):
c2d0dd9
added project files
Browse files- Dockerfile +12 -0
- README.md +170 -6
- __pycache__/app.cpython-311.pyc +0 -0
- app.py +80 -0
- chainlit.md +3 -0
- requirements.txt +5 -0
- test.txt +1 -0
Dockerfile
ADDED
@@ -0,0 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
FROM python:3.9-slim-bullseye
|
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 config --user set global.progress_bar off
|
10 |
+
RUN pip install -r requirements.txt --progress-bar off
|
11 |
+
COPY . .
|
12 |
+
CMD ["chainlit", "run", "app.py", "--port", "7860"]
|
README.md
CHANGED
@@ -1,11 +1,175 @@
|
|
1 |
---
|
2 |
-
title:
|
3 |
-
emoji:
|
4 |
-
colorFrom:
|
5 |
-
colorTo:
|
6 |
sdk: docker
|
7 |
pinned: false
|
8 |
-
license: openrail
|
9 |
---
|
10 |
|
11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
---
|
2 |
+
title: BeyondChatGPT Demo
|
3 |
+
emoji: π
|
4 |
+
colorFrom: pink
|
5 |
+
colorTo: yellow
|
6 |
sdk: docker
|
7 |
pinned: false
|
|
|
8 |
---
|
9 |
|
10 |
+
> If you need an introduction to `git`, or information on how to set up API keys for the tools we'll be using in this repository - check out our [Interactive Dev Environment for LLM Development](https://github.com/AI-Maker-Space/Interactive-Dev-Environment-for-LLM-Development/tree/main) which has everything you'd need to get started in this repository!
|
11 |
+
|
12 |
+
In this repository, we'll walk you through the steps to create a Large Language Model (LLM) application using Chainlit, then containerize it using Docker, and finally deploy it on Huggingface Spaces.
|
13 |
+
|
14 |
+
Are you ready? Let's get started!
|
15 |
+
|
16 |
+
<details>
|
17 |
+
<summary>π₯οΈ Accessing "gpt-3.5-turbo" (ChatGPT) like a developer</summary>
|
18 |
+
|
19 |
+
1. Head to [this notebook](https://colab.research.google.com/drive/1mOzbgf4a2SP5qQj33ZxTz2a01-5eXqk2?usp=sharing) and follow along with the instructions!
|
20 |
+
|
21 |
+
2. Complete the notebook and try out your own system/assistant messages!
|
22 |
+
|
23 |
+
That's it! Head to the next step and start building your application!
|
24 |
+
|
25 |
+
</details>
|
26 |
+
|
27 |
+
|
28 |
+
<details>
|
29 |
+
<summary>ποΈ Building Your First LLM App</summary>
|
30 |
+
|
31 |
+
1. Clone [this](https://github.com/AI-Maker-Space/Beyond-ChatGPT/tree/main) repo.
|
32 |
+
|
33 |
+
``` bash
|
34 |
+
git clone https://github.com/AI-Maker-Space/Beyond-ChatGPT.git
|
35 |
+
```
|
36 |
+
|
37 |
+
2. Navigate inside this repo
|
38 |
+
``` bash
|
39 |
+
cd Beyond-ChatGPT
|
40 |
+
```
|
41 |
+
|
42 |
+
3. Install the packages required for this python envirnoment in `requirements.txt`.
|
43 |
+
``` bash
|
44 |
+
pip install -r requirements.txt
|
45 |
+
```
|
46 |
+
|
47 |
+
4. Open your `.env` file. Replace the `###` in your `.env` file with your OpenAI Key and save the file.
|
48 |
+
``` bash
|
49 |
+
OPENAI_API_KEY=sk-###
|
50 |
+
```
|
51 |
+
|
52 |
+
5. Let's try deploying it locally. Make sure you're in the python environment where you installed Chainlit and OpenAI. Run the app using Chainlit. This may take a minute to run.
|
53 |
+
``` bash
|
54 |
+
chainlit run app.py -w
|
55 |
+
```
|
56 |
+
|
57 |
+
<p align = "center" draggable=βfalseβ>
|
58 |
+
<img src="https://github.com/AI-Maker-Space/LLMOps-Dev-101/assets/37101144/54bcccf9-12e2-4cef-ab53-585c1e2b0fb5">
|
59 |
+
</p>
|
60 |
+
|
61 |
+
Great work! Let's see if we can interact with our chatbot.
|
62 |
+
|
63 |
+
<p align = "center" draggable=βfalseβ>
|
64 |
+
<img src="https://github.com/AI-Maker-Space/LLMOps-Dev-101/assets/37101144/854e4435-1dee-438a-9146-7174b39f7c61">
|
65 |
+
</p>
|
66 |
+
|
67 |
+
Awesome! Time to throw it into a docker container and prepare it for shipping!
|
68 |
+
</details>
|
69 |
+
|
70 |
+
|
71 |
+
|
72 |
+
<details>
|
73 |
+
<summary>π³ Containerizing our App</summary>
|
74 |
+
|
75 |
+
1. Let's build the Docker image. We'll tag our image as `llm-app` using the `-t` parameter. The `.` at the end means we want all of the files in our current directory to be added to our image.
|
76 |
+
|
77 |
+
``` bash
|
78 |
+
docker build -t llm-app .
|
79 |
+
```
|
80 |
+
|
81 |
+
2. Run and test the Docker image locally using the `run` command. The `-p`parameter connects our **host port #** to the left of the `:` to our **container port #** on the right.
|
82 |
+
|
83 |
+
``` bash
|
84 |
+
docker run -p 7860:7860 llm-app
|
85 |
+
```
|
86 |
+
|
87 |
+
3. Visit http://localhost:7860 in your browser to see if the app runs correctly.
|
88 |
+
|
89 |
+
<p align = "center" draggable=βfalseβ>
|
90 |
+
<img src="https://github.com/AI-Maker-Space/LLMOps-Dev-101/assets/37101144/2c764f25-09a0-431b-8d28-32246e0ca1b7">
|
91 |
+
</p>
|
92 |
+
|
93 |
+
Great! Time to ship!
|
94 |
+
</details>
|
95 |
+
|
96 |
+
|
97 |
+
<details>
|
98 |
+
<summary>π Deploying Your First LLM App</summary>
|
99 |
+
|
100 |
+
1. Let's create a new Huggingface Space. Navigate to [Huggingface](https://huggingface.co) and click on your profile picture on the top right. Then click on `New Space`.
|
101 |
+
|
102 |
+
<p align = "center" draggable=βfalseβ>
|
103 |
+
<img src="https://github.com/AI-Maker-Space/LLMOps-Dev-101/assets/37101144/f0656408-28b8-4876-9887-8f0c4b882bae">
|
104 |
+
</p>
|
105 |
+
|
106 |
+
2. Setup your space as shown below:
|
107 |
+
|
108 |
+
- Owner: Your username
|
109 |
+
- Space Name: `llm-app`
|
110 |
+
- License: `Openrail`
|
111 |
+
- Select the Space SDK: `Docker`
|
112 |
+
- Docker Template: `Blank`
|
113 |
+
- Space Hardware: `CPU basic - 2 vCPU - 16 GB - Free`
|
114 |
+
- Repo type: `Public`
|
115 |
+
|
116 |
+
<p align = "center" draggable=βfalseβ>
|
117 |
+
<img src="https://github.com/AI-Maker-Space/LLMOps-Dev-101/assets/37101144/8f16afd1-6b46-4d9f-b642-8fefe355c5c9">
|
118 |
+
</p>
|
119 |
+
|
120 |
+
3. You should see something like this. We're now ready to send our files to our Huggingface Space. After cloning, move your files to this repo and push it along with your docker file. You DO NOT need to create a Dockerfile. Make sure NOT TO push your `.env` file. This should automatically be ignored.
|
121 |
+
|
122 |
+
<p align = "center" draggable=βfalseβ>
|
123 |
+
<img src="https://github.com/AI-Maker-Space/LLMOps-Dev-101/assets/37101144/cbf366e2-7613-4223-932a-72c67a73f9c6">
|
124 |
+
</p>
|
125 |
+
|
126 |
+
4. After pushing all files, navigate to the settings in the top right to add your OpenAI API key.
|
127 |
+
|
128 |
+
<p align = "center" draggable=βfalseβ>
|
129 |
+
<img src="https://github.com/AI-Maker-Space/LLMOps-Dev-101/assets/37101144/a1123a6f-abdd-4f76-bea4-39acf9928762">
|
130 |
+
</p>
|
131 |
+
|
132 |
+
5. Scroll down to `Variables and secrets` and click on `New secret` on the top right.
|
133 |
+
|
134 |
+
<p align = "center" draggable=βfalseβ>
|
135 |
+
<img src="https://github.com/AI-Maker-Space/LLMOps-Dev-101/assets/37101144/a8a4a25d-752b-4036-b572-93381370c2db">
|
136 |
+
</p>
|
137 |
+
|
138 |
+
6. Set the name to `OPENAI_API_KEY` and add your OpenAI key under `Value`. Click save.
|
139 |
+
|
140 |
+
<p align = "center" draggable=βfalseβ>
|
141 |
+
<img src="https://github.com/AI-Maker-Space/LLMOps-Dev-101/assets/37101144/0a897538-1779-48ff-bcb4-486af30f7a14">
|
142 |
+
</p>
|
143 |
+
|
144 |
+
7. To ensure your key is being used, we recommend you `Restart this Space`.
|
145 |
+
|
146 |
+
<p align = "center" draggable=βfalseβ>
|
147 |
+
<img src="https://github.com/AI-Maker-Space/LLMOps-Dev-101/assets/37101144/fb1d83af-6ebe-4676-8bf5-b6d88f07c583">
|
148 |
+
</p>
|
149 |
+
|
150 |
+
8. Congratulations! You just deployed your first LLM Application! πππ Get on linkedin and post your results and experience! Make sure to tag us at #AIMakerspace !
|
151 |
+
|
152 |
+
Here's a template to get your post started!
|
153 |
+
|
154 |
+
```
|
155 |
+
ππ Exciting News! ππ
|
156 |
+
|
157 |
+
ποΈΒ Today, I'm thrilled to announce that I've successfully built and shipped my first-ever LLM using the powerful combination of Chainlit, Docker, and the OpenAI API! π₯οΈ
|
158 |
+
|
159 |
+
Check it out π
|
160 |
+
[LINK TO APP]
|
161 |
+
|
162 |
+
A big shoutout to the @**AI Makerspace** for all making this possible. Couldn't have done it without the incredible community there. π€π
|
163 |
+
|
164 |
+
Looking forward to building with the community! πβ¨Β Here's to many more creations ahead! π₯π
|
165 |
+
|
166 |
+
Who else is diving into the world of AI? Let's connect! ππ‘
|
167 |
+
|
168 |
+
#FirstLLM #Chainlit #Docker #OpenAI #AIMakerspace
|
169 |
+
```
|
170 |
+
|
171 |
+
</details>
|
172 |
+
|
173 |
+
<p></p>
|
174 |
+
|
175 |
+
### That's it for now! And so it begins.... :)
|
__pycache__/app.cpython-311.pyc
ADDED
Binary file (3.41 kB). View file
|
|
app.py
ADDED
@@ -0,0 +1,80 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# You can find this code for Chainlit python streaming here (https://docs.chainlit.io/concepts/streaming/python)
|
2 |
+
|
3 |
+
# OpenAI Chat completion
|
4 |
+
import os
|
5 |
+
from openai import AsyncOpenAI # importing openai for API usage
|
6 |
+
import chainlit as cl # importing chainlit for our app
|
7 |
+
from chainlit.prompt import Prompt, PromptMessage # importing prompt tools
|
8 |
+
from chainlit.playground.providers import ChatOpenAI # importing ChatOpenAI tools
|
9 |
+
from dotenv import load_dotenv
|
10 |
+
|
11 |
+
load_dotenv()
|
12 |
+
|
13 |
+
# ChatOpenAI Templates
|
14 |
+
system_template = """You are a helpful assistant who always speaks in a pleasant tone!
|
15 |
+
"""
|
16 |
+
|
17 |
+
user_template = """{input}
|
18 |
+
Think through your response step by step.
|
19 |
+
"""
|
20 |
+
|
21 |
+
|
22 |
+
@cl.on_chat_start # marks a function that will be executed at the start of a user session
|
23 |
+
async def start_chat():
|
24 |
+
settings = {
|
25 |
+
"model": "gpt-3.5-turbo",
|
26 |
+
"temperature": 0,
|
27 |
+
"max_tokens": 500,
|
28 |
+
"top_p": 1,
|
29 |
+
"frequency_penalty": 0,
|
30 |
+
"presence_penalty": 0,
|
31 |
+
}
|
32 |
+
|
33 |
+
cl.user_session.set("settings", settings)
|
34 |
+
|
35 |
+
|
36 |
+
@cl.on_message # marks a function that should be run each time the chatbot receives a message from a user
|
37 |
+
async def main(message: cl.Message):
|
38 |
+
settings = cl.user_session.get("settings")
|
39 |
+
|
40 |
+
client = AsyncOpenAI()
|
41 |
+
|
42 |
+
print(message.content)
|
43 |
+
|
44 |
+
prompt = Prompt(
|
45 |
+
provider=ChatOpenAI.id,
|
46 |
+
messages=[
|
47 |
+
PromptMessage(
|
48 |
+
role="system",
|
49 |
+
template=system_template,
|
50 |
+
formatted=system_template,
|
51 |
+
),
|
52 |
+
PromptMessage(
|
53 |
+
role="user",
|
54 |
+
template=user_template,
|
55 |
+
formatted=user_template.format(input=message.content),
|
56 |
+
),
|
57 |
+
],
|
58 |
+
inputs={"input": message.content},
|
59 |
+
settings=settings,
|
60 |
+
)
|
61 |
+
|
62 |
+
print([m.to_openai() for m in prompt.messages])
|
63 |
+
|
64 |
+
msg = cl.Message(content="")
|
65 |
+
|
66 |
+
# Call OpenAI
|
67 |
+
async for stream_resp in await client.chat.completions.create(
|
68 |
+
messages=[m.to_openai() for m in prompt.messages], stream=True, **settings
|
69 |
+
):
|
70 |
+
token = stream_resp.choices[0].delta.content
|
71 |
+
if not token:
|
72 |
+
token = ""
|
73 |
+
await msg.stream_token(token)
|
74 |
+
|
75 |
+
# Update the prompt object with the completion
|
76 |
+
prompt.completion = msg.content
|
77 |
+
msg.prompt = prompt
|
78 |
+
|
79 |
+
# Send and close the message stream
|
80 |
+
await msg.send()
|
chainlit.md
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
# Beyond ChatGPT
|
2 |
+
|
3 |
+
This Chainlit app was created following instructions from [this repository!](https://github.com/AI-Maker-Space/Beyond-ChatGPT)
|
requirements.txt
ADDED
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
chainlit==0.7.700
|
2 |
+
cohere==4.37
|
3 |
+
openai==1.3.5
|
4 |
+
tiktoken==0.5.1
|
5 |
+
python-dotenv==1.0.0
|
test.txt
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
This is a test file!
|