Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Port to Quart #27

Merged
merged 7 commits into from
Sep 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .github/workflows/python-check.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,6 @@ jobs:
- name: Lint with ruff
run: ruff .
- name: Check formatting with black
run: black . --check --verbose
run: black . --check --verbose
- name: Run tests with pytest
run: python3 -m pytest
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
.env
.azure
.pyc
__pycache__/
__pycache__/
.coverage
15 changes: 15 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.4.0
hooks:
- id: check-yaml
- id: end-of-file-fixer
- id: trailing-whitespace
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.0.288
hooks:
- id: ruff
- repo: https://github.com/psf/black
rev: 23.9.1
hooks:
- id: black
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
# Build your own ChatGPT app

This repository includes a simple Python Flask app that streams responses from ChatGPT
to an HTML/JS frontend using [NDJSON](http://ndjson.org/) over a [ReadableStream](https://developer.mozilla.org/en-US/docs/Web/API/ReadableStream).
This repository includes a simple Python [Quart](https://quart.palletsprojects.com/en/latest/)
app that streams responses from ChatGPT to an HTML/JS frontend using [NDJSON](http://ndjson.org/)
over a [ReadableStream](https://developer.mozilla.org/en-US/docs/Web/API/ReadableStream).

The repository is designed for use with [Docker containers](https://www.docker.com/), both for local development and deployment, and includes infrastructure files for deployment to [Azure Container Apps](https://learn.microsoft.com/azure/container-apps/overview). 🐳

Expand Down Expand Up @@ -53,7 +54,7 @@ This repo is set up for deployment on Azure Container Apps using the configurati
```shell
azd up
```
It will prompt you to provide an `azd` environment name (like "flask-app"), select a subscription from your Azure account, and select a [location where OpenAI is available](https://azure.microsoft.com/explore/global-infrastructure/products-by-region/?products=cognitive-services&regions=all) (like "francecentral"). Then it will provision the resources in your account and deploy the latest code. If you get an error or timeout with deployment, changing the location can help, as there may be availability constraints for the OpenAI resource.
It will prompt you to provide an `azd` environment name (like "chat-app"), select a subscription from your Azure account, and select a [location where OpenAI is available](https://azure.microsoft.com/explore/global-infrastructure/products-by-region/?products=cognitive-services&regions=all) (like "francecentral"). Then it will provision the resources in your account and deploy the latest code. If you get an error or timeout with deployment, changing the location can help, as there may be availability constraints for the OpenAI resource.

3. When `azd` has finished deploying, you'll see an endpoint URI in the command output. Visit that URI, and you should see the chat app! 🎉
4. When you've made any changes to the app code, you can just run:
Expand Down
10 changes: 10 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,16 @@ target-version = "py311"
line-length = 120
src = ["src"]

[tool.ruff.isort]
known-local-folder = ["src"]

[tool.black]
target-version = ["py311"]
line-length = 120

[tool.pytest.ini_options]
addopts = "-ra --cov"
pythonpath = ["src"]

[tool.coverage.report]
show_missing = true
3 changes: 3 additions & 0 deletions requirements-dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,6 @@
black
ruff
pre-commit
pytest
pytest-asyncio
pytest-cov
2 changes: 1 addition & 1 deletion src/app.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
from flaskapp import create_app
from quartapp import create_app

app = create_app()
49 changes: 0 additions & 49 deletions src/flaskapp/chat.py

This file was deleted.

6 changes: 4 additions & 2 deletions src/gunicorn.conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
if not os.getenv("RUNNING_IN_PRODUCTION"):
reload = True

workers = (multiprocessing.cpu_count() * 2) + 1
threads = workers
num_cpus = multiprocessing.cpu_count()
workers = (num_cpus * 2) + 1
worker_class = "uvicorn.workers.UvicornWorker"

timeout = 120
4 changes: 2 additions & 2 deletions src/flaskapp/__init__.py → src/quartapp/__init__.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import logging
import os

from flask import Flask
from quart import Quart


def create_app():
if not os.getenv("RUNNING_IN_PRODUCTION"):
logging.basicConfig(level=logging.DEBUG)

app = Flask(__name__)
app = Quart(__name__)

from . import chat # noqa

Expand Down
51 changes: 51 additions & 0 deletions src/quartapp/chat.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import json
import os

import azure.identity.aio
import openai
from quart import Blueprint, Response, current_app, render_template, request, stream_with_context

bp = Blueprint("chat", __name__, template_folder="templates", static_folder="static")


@bp.before_app_serving
async def configure_openai():
openai.api_base = os.getenv("AZURE_OPENAI_ENDPOINT")
openai.api_version = "2023-03-15-preview"
if os.getenv("AZURE_OPENAI_KEY"):
openai.api_type = "azure"
openai.api_key = os.getenv("AZURE_OPENAI_KEY")
else:
openai.api_type = "azure_ad"
if client_id := os.getenv("AZURE_OPENAI_CLIENT_ID"):
default_credential = azure.identity.aio.ManagedIdentityCredential(client_id=client_id)
else:
default_credential = azure.identity.aio.DefaultAzureCredential(exclude_shared_token_cache_credential=True)
token = await default_credential.get_token("https://cognitiveservices.azure.com/.default")
openai.api_key = token.token


@bp.get("/")
async def index():
return await render_template("index.html")


@bp.post("/chat")
async def chat_handler():
request_message = (await request.get_json())["message"]

@stream_with_context
async def response_stream():
chat_coroutine = openai.ChatCompletion.acreate(
engine=os.getenv("AZURE_OPENAI_CHATGPT_DEPLOYMENT", "chatgpt"),
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": request_message},
],
stream=True,
)
async for event in await chat_coroutine:
current_app.logger.info(event)
yield json.dumps(event, ensure_ascii=False) + "\n"

return Response(response_stream())
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,14 @@
body: JSON.stringify({message: message})
});

messageDiv.innerHTML = "";

let answer = "";
for await (const event of readNDJSONStream(response.body)) {
if (event["choices"][0]["delta"]["content"]) {
// Clear out the DIV if its the first answer chunk we've received
if (answer == "") {
messageDiv.innerHTML = "";
}
answer += event["choices"][0]["delta"]["content"];
messageDiv.innerHTML = converter.makeHtml(answer);
messageDiv.scrollIntoView();
Expand Down
3 changes: 2 additions & 1 deletion src/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
Flask==2.3.3
quart==0.18.4
gunicorn==21.2.0
uvicorn[standard]==0.23.2
openai==0.28.0
azure-identity==1.14.0
python-dotenv==1.0.0 # Pinned due to docker-in-docker issue: https://github.com/devcontainers/features/issues/616
Empty file added tests/__init__.py
Empty file.
48 changes: 48 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import openai
import pytest
import pytest_asyncio

from . import mock_cred
from src import quartapp


@pytest.fixture
def mock_openai_chatcompletion(monkeypatch):
class AsyncChatCompletionIterator:
def __init__(self, answer: str):
self.answer_index = 0
self.answer_deltas = answer.split(" ")

def __aiter__(self):
return self

async def __anext__(self):
if self.answer_index < len(self.answer_deltas):
answer_chunk = self.answer_deltas[self.answer_index]
self.answer_index += 1
return openai.util.convert_to_openai_object({"choices": [{"delta": {"content": answer_chunk}}]})
else:
raise StopAsyncIteration

async def mock_acreate(*args, **kwargs):
return AsyncChatCompletionIterator("The capital of France is Paris.")

monkeypatch.setattr(openai.ChatCompletion, "acreate", mock_acreate)


@pytest.fixture
def mock_defaultazurecredential(monkeypatch):
monkeypatch.setattr("azure.identity.aio.DefaultAzureCredential", mock_cred.MockAzureCredential)


@pytest_asyncio.fixture
async def client(monkeypatch, mock_openai_chatcompletion, mock_defaultazurecredential):
monkeypatch.setenv("AZURE_OPENAI_ENDPOINT", "test-openai-service.openai.azure.com")
monkeypatch.setenv("AZURE_OPENAI_CHATGPT_DEPLOYMENT", "test-chatgpt")

quart_app = quartapp.create_app()

async with quart_app.test_app() as test_app:
quart_app.config.update({"TESTING": True})

yield test_app.test_client()
11 changes: 11 additions & 0 deletions tests/mock_cred.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from collections import namedtuple

MockToken = namedtuple("MockToken", ["token", "expires_on"])


class MockAzureCredential:
def __init__(self, *args, **kwargs):
pass

async def get_token(self, uri):
return MockToken("mock_token", 9999999999)
51 changes: 51 additions & 0 deletions tests/test_app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import openai
import pytest

from . import mock_cred
from src import quartapp


@pytest.mark.asyncio
async def test_index(client):
response = await client.get("/")
assert response.status_code == 200


@pytest.mark.asyncio
async def test_chat_stream_text(client):
response = await client.post(
"/chat",
json={"message": "What is the capital of France?"},
)
assert response.status_code == 200
result = await response.get_data()
assert (
result
== b'{"choices": [{"delta": {"content": "The"}}]}\n{"choices": [{"delta": {"content": "capital"}}]}\n{"choices": [{"delta": {"content": "of"}}]}\n{"choices": [{"delta": {"content": "France"}}]}\n{"choices": [{"delta": {"content": "is"}}]}\n{"choices": [{"delta": {"content": "Paris."}}]}\n' # noqa
)


@pytest.mark.asyncio
async def test_openai_key(monkeypatch):
monkeypatch.setenv("AZURE_OPENAI_KEY", "test-key")
monkeypatch.setenv("AZURE_OPENAI_ENDPOINT", "test-openai-service.openai.azure.com")
monkeypatch.setenv("AZURE_OPENAI_CHATGPT_DEPLOYMENT", "test-chatgpt")

quart_app = quartapp.create_app()

async with quart_app.test_app():
assert openai.api_type == "azure"


@pytest.mark.asyncio
async def test_openai_managedidentity(monkeypatch):
monkeypatch.setenv("AZURE_OPENAI_CLIENT_ID", "test-client-id")
monkeypatch.setenv("AZURE_OPENAI_ENDPOINT", "test-openai-service.openai.azure.com")
monkeypatch.setenv("AZURE_OPENAI_CHATGPT_DEPLOYMENT", "test-chatgpt")

monkeypatch.setattr("azure.identity.aio.ManagedIdentityCredential", mock_cred.MockAzureCredential)

quart_app = quartapp.create_app()

async with quart_app.test_app():
assert openai.api_type == "azure_ad"
Loading