From 1cf2ed0590dd288d493ac429b6a61e957909c029 Mon Sep 17 00:00:00 2001 From: Florian Schepers Date: Wed, 10 Apr 2024 13:18:51 +0200 Subject: [PATCH] WIP: Add classificationservice FastAPI setup and Dockerfile TASK: IL-421 --- .../issue_classification_user_journey.ipynb | 125 +++--------------- .../Dockerfile | 4 +- .../requirements.txt | Bin 26825 -> 26894 bytes 3 files changed, 19 insertions(+), 110 deletions(-) diff --git a/src/examples/issue_classification_user_journey.ipynb b/src/examples/issue_classification_user_journey.ipynb index 14eb53cc2..47174d979 100644 --- a/src/examples/issue_classification_user_journey.ipynb +++ b/src/examples/issue_classification_user_journey.ipynb @@ -731,121 +731,32 @@ ] }, { - "cell_type": "code", - "execution_count": null, + "cell_type": "markdown", "metadata": {}, - "outputs": [], "source": [ - "import http\n", - "import os\n", - "from http import HTTPStatus\n", - "from typing import Annotated, Sequence\n", - "\n", - "from aleph_alpha_client import Client\n", - "from dotenv import load_dotenv\n", - "from fastapi import Depends, FastAPI, HTTPException, Request, Response\n", - "from fastapi.datastructures import URL\n", - "\n", - "from intelligence_layer.connectors import AlephAlphaClientProtocol\n", - "from intelligence_layer.core import LuminousControlModel, NoOpTracer, Task\n", - "from intelligence_layer.use_cases import (\n", - " ClassifyInput,\n", - " PromptBasedClassify,\n", - " SingleLabelClassifyOutput,\n", - ")\n", - "\n", - "# Minimal FastAPI app ##########################################################\n", - "\n", - "app = FastAPI()\n", - "\n", - "\n", - "@app.get(\"/\")\n", - "def root() -> Response:\n", - " return Response(content=\"Classification Service\", status_code=HTTPStatus.OK)\n", - "\n", - "\n", - "# Authentication ###############################################################\n", - "\n", - "\n", - "class AuthService:\n", - " def is_valid_token(self, token: str, permissions: Sequence[str], url: URL) -> bool:\n", - " # Add your authentication logic here\n", - " print(f\"Checking permission for route: {url.path}\")\n", - " return True\n", - "\n", - "\n", - "class PermissionChecker:\n", - " def __init__(self, permissions: Sequence[str] = []):\n", - " self.permissions = permissions\n", + "## Classification Service\n", "\n", - " def __call__(\n", - " self,\n", - " request: Request,\n", - " auth_service: Annotated[AuthService, Depends(AuthService)],\n", - " ) -> None:\n", - " token = request.headers.get(\"Authorization\") or \"\"\n", - " try:\n", - " if not auth_service.is_valid_token(token, self.permissions, request.url):\n", - " raise HTTPException(HTTPStatus.UNAUTHORIZED)\n", - " except RuntimeError:\n", - " raise HTTPException(HTTPStatus.INTERNAL_SERVER_ERROR)\n", + "Outline\n", + "- Setup FastAPI Service\n", + "- Setup Docker\n", + "- Setup Kubernetes\n", + "- Setup Pulumi\n", + "- Check that it is working\n", "\n", + "### Enviroment variables\n", + "TODO: Describe how to set environment variables \n", "\n", - "permission_checker_for_user = PermissionChecker([\"User\"])\n", + "### FastAPI service\n", + "TODO: Describe FastAPI setup for `ClassificationService.py` \n", "\n", + "In order to start the Classification service open a new terminal and execute the command\n", + "```shell\n", + "hypercorn .\\src\\examples\\issue_classification_user_journey\\ClassificationService:app\n", + "```\n", "\n", - "# Intelligence Layer Task ######################################################\n", - "\n", - "PROMPT = \"\"\"Identify the department that would be responsible for handling the given request.\n", - "Reply with only the department name.\"\"\"\n", - "\n", - "load_dotenv()\n", - "\n", - "\n", - "def client() -> Client:\n", - " return Client(\n", - " token=os.environ[\"AA_TOKEN\"],\n", - " host=os.getenv(\"AA_CLIENT_BASE_URL\", \"https://api.aleph-alpha.com\"),\n", - " )\n", - "\n", - "\n", - "def default_model(\n", - " app_client: Annotated[AlephAlphaClientProtocol, Depends(client)],\n", - ") -> LuminousControlModel:\n", - " return LuminousControlModel(\"luminous-supreme-control\", client=app_client)\n", - "\n", - "\n", - "def classification_task(\n", - " model: Annotated[LuminousControlModel, Depends(default_model)],\n", - ") -> PromptBasedClassify:\n", - " return PromptBasedClassify(instruction=PROMPT, model=model)\n", - "\n", - "\n", - "@app.post(\n", - " \"/classify\",\n", - " # dependencies=[Depends(PermissionChecker([\"User\"]))],\n", - " status_code=http.HTTPStatus.OK,\n", - ")\n", - "def classification_task_route(\n", - " input: ClassifyInput,\n", - " task: Annotated[\n", - " Task[ClassifyInput, SingleLabelClassifyOutput], Depends(classification_task)\n", - " ],\n", - ") -> SingleLabelClassifyOutput:\n", - " return task.run(input, NoOpTracer())" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "import nest_asyncio\n", - "import uvicorn\n", + "### Docker\n", "\n", - "nest_asyncio.apply()\n", - "uvicorn.run(app, port=8000)" + "poetry export --without-hashes --format=requirements.txt > requirements.txt\n" ] } ], diff --git a/src/examples/issue_classification_user_journey/Dockerfile b/src/examples/issue_classification_user_journey/Dockerfile index d265295a7..58505737a 100644 --- a/src/examples/issue_classification_user_journey/Dockerfile +++ b/src/examples/issue_classification_user_journey/Dockerfile @@ -9,8 +9,6 @@ COPY requirements.txt /app/requirements.txt RUN python3 -m venv /app/venv ENV PATH="/app/venv/bin:$PATH" -RUN echo $(cat /run/secrets/GITHUB_TOKEN) - RUN pip install --upgrade pip RUN --mount=type=secret,id=GITHUB_TOKEN \ GITHUB_TOKEN=$(cat /run/secrets/GITHUB_TOKEN) pip install -r /app/requirements.txt @@ -28,4 +26,4 @@ COPY --from=builder /app /app COPY main.py /app/main.py ENV PATH="/app/venv/bin:$PATH" -ENTRYPOINT [ "uvicorn", "app.main:app" ] +ENTRYPOINT [ "uvicorn", "--host", "0.0.0.0", "--port", "80", "app.main:app" ] diff --git a/src/examples/issue_classification_user_journey/requirements.txt b/src/examples/issue_classification_user_journey/requirements.txt index e1fc1667dc2fe82985a554128b16f9f1e6f56e86..00f5ff0d31ccc514c2326ecf828fea8cb7be92ef 100644 GIT binary patch delta 84 zcmX?kk+JU*