From d5263b13d9e66c1a4fba9cf84bf7527b24f7403e Mon Sep 17 00:00:00 2001 From: Bryan Ho Date: Thu, 4 Apr 2024 11:37:45 -0700 Subject: [PATCH 1/2] deploy-ready demo with instructions --- examples/chainlit/private-chatgpt/Dockerfile | 9 ++++ examples/chainlit/private-chatgpt/README.md | 22 ++++++++ examples/chainlit/private-chatgpt/app.py | 50 +++++++++++++++++++ examples/chainlit/private-chatgpt/chainlit.md | 5 ++ .../chainlit/private-chatgpt/requirements.txt | 2 + 5 files changed, 88 insertions(+) create mode 100644 examples/chainlit/private-chatgpt/Dockerfile create mode 100644 examples/chainlit/private-chatgpt/README.md create mode 100644 examples/chainlit/private-chatgpt/app.py create mode 100644 examples/chainlit/private-chatgpt/chainlit.md create mode 100644 examples/chainlit/private-chatgpt/requirements.txt diff --git a/examples/chainlit/private-chatgpt/Dockerfile b/examples/chainlit/private-chatgpt/Dockerfile new file mode 100644 index 00000000..fa042fa6 --- /dev/null +++ b/examples/chainlit/private-chatgpt/Dockerfile @@ -0,0 +1,9 @@ +FROM python:3.11 + +COPY app.py app.py +COPY chainlit.md chainlit.md +COPY requirements.txt requirements.txt +RUN pip install -r requirements.txt + +# do not change the arguments +ENTRYPOINT ["chainlit", "run", "app.py", "--host=0.0.0.0", "--port=80", "--headless"] \ No newline at end of file diff --git a/examples/chainlit/private-chatgpt/README.md b/examples/chainlit/private-chatgpt/README.md new file mode 100644 index 00000000..32475203 --- /dev/null +++ b/examples/chainlit/private-chatgpt/README.md @@ -0,0 +1,22 @@ +# Private ChatGPT + +A basic Chainlit chat UI to be used with an LLM hosted and served via vLLM. + +## Deployment instructions: + +Ensure your LLM has already been deployed. + +If you configured a private API key on the backend, set it here via a secret or `.env` file: + +```sh +export VLLM_KEY=your_key +``` + +Note down the host URL. In `app.py`, modify `api_base` with your host URL. + +Then, initialize and deploy: + +```sh +ploomber-cloud init +ploomber-cloud deploy --watch +``` \ No newline at end of file diff --git a/examples/chainlit/private-chatgpt/app.py b/examples/chainlit/private-chatgpt/app.py new file mode 100644 index 00000000..ae0efb45 --- /dev/null +++ b/examples/chainlit/private-chatgpt/app.py @@ -0,0 +1,50 @@ +from openai import AsyncOpenAI +import chainlit as cl +import os + +# Enter the personal API key that you set for the host, +# or leave it blank if you didn't set one +vllm_key = os.environ.get("VLLM_KEY") or "" + +# Modify this value to match your host, remember to add /v1 at the end +api_base = "https://aged-math-3623.ploomberapp.io/v1" + +client = AsyncOpenAI( + api_key=vllm_key, + base_url=api_base, +) + +# Instrument the OpenAI client +cl.instrument_openai() + +settings = { + "model": "google/gemma-2b-it", + # ... more settings +} + +messages = [] + +@cl.on_chat_start +def main(): + messages = [] + cl.user_session.set("messages", messages) + +@cl.on_message +async def on_message(message: cl.Message): + messages = cl.user_session.get("messages") + messages = messages + [ + { + "content": message.content, + "role": "user" + } + ] + response = await client.chat.completions.create( + messages=messages, + **settings + ) + messages = messages + [{ + "content": response.choices[0].message.content, + "role": "assistant", + }] + cl.user_session.set("messages", messages) + await cl.Message(content=response.choices[0].message.content).send() diff --git a/examples/chainlit/private-chatgpt/chainlit.md b/examples/chainlit/private-chatgpt/chainlit.md new file mode 100644 index 00000000..662ccddf --- /dev/null +++ b/examples/chainlit/private-chatgpt/chainlit.md @@ -0,0 +1,5 @@ +# Custom ChatGPT with vLLM 🚀🤖 + +This application is a custom ChatGPT that uses vLLM + gemma-2b-it and Chainlit for the chat interface. + +Deploy your own version using __Ploomber Cloud!__ \ No newline at end of file diff --git a/examples/chainlit/private-chatgpt/requirements.txt b/examples/chainlit/private-chatgpt/requirements.txt new file mode 100644 index 00000000..aa7104bf --- /dev/null +++ b/examples/chainlit/private-chatgpt/requirements.txt @@ -0,0 +1,2 @@ +chainlit +openai \ No newline at end of file From a1190ba83d3d7ca52f0024c86f98151ee8669534 Mon Sep 17 00:00:00 2001 From: Bryan Ho Date: Thu, 4 Apr 2024 13:36:32 -0700 Subject: [PATCH 2/2] rename api key for consistency --- examples/chainlit/private-chatgpt/README.md | 2 +- examples/chainlit/private-chatgpt/app.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/chainlit/private-chatgpt/README.md b/examples/chainlit/private-chatgpt/README.md index 32475203..a05883ca 100644 --- a/examples/chainlit/private-chatgpt/README.md +++ b/examples/chainlit/private-chatgpt/README.md @@ -9,7 +9,7 @@ Ensure your LLM has already been deployed. If you configured a private API key on the backend, set it here via a secret or `.env` file: ```sh -export VLLM_KEY=your_key +export VLLM_API_KEY=your_key ``` Note down the host URL. In `app.py`, modify `api_base` with your host URL. diff --git a/examples/chainlit/private-chatgpt/app.py b/examples/chainlit/private-chatgpt/app.py index ae0efb45..0b72387e 100644 --- a/examples/chainlit/private-chatgpt/app.py +++ b/examples/chainlit/private-chatgpt/app.py @@ -4,7 +4,7 @@ # Enter the personal API key that you set for the host, # or leave it blank if you didn't set one -vllm_key = os.environ.get("VLLM_KEY") or "" +vllm_key = os.environ.get("VLLM_API_KEY") or "" # Modify this value to match your host, remember to add /v1 at the end api_base = "https://aged-math-3623.ploomberapp.io/v1"