From 28c8808ce389d4fcad6f889c7fb4022ecc89aaa8 Mon Sep 17 00:00:00 2001 From: Huu Le <39040748+leehuwuj@users.noreply.github.com> Date: Fri, 22 Nov 2024 16:34:37 +0700 Subject: [PATCH] feat: Add fly.io deployment (#443) --------- Co-authored-by: Marcus Schiesser <mail@marcusschiesser.de> --- .changeset/olive-comics-grab.md | 5 ++ helpers/python.ts | 5 -- helpers/typescript.ts | 5 -- .../agents/python/blog/README-template.md | 4 + .../financial_report/README-template.md | 4 + .../python/form_filling/README-template.md | 4 + .../streaming/express}/Dockerfile | 0 templates/types/streaming/fastapi/DEPLOY.md | 73 +++++++++++++++++++ .../streaming/fastapi}/Dockerfile | 30 +++++++- .../streaming/fastapi/README-template.md | 34 +-------- templates/types/streaming/nextjs/Dockerfile | 16 ++++ 11 files changed, 135 insertions(+), 45 deletions(-) create mode 100644 .changeset/olive-comics-grab.md rename templates/{components/deployments/typescript => types/streaming/express}/Dockerfile (100%) create mode 100644 templates/types/streaming/fastapi/DEPLOY.md rename templates/{components/deployments/python => types/streaming/fastapi}/Dockerfile (52%) create mode 100644 templates/types/streaming/nextjs/Dockerfile diff --git a/.changeset/olive-comics-grab.md b/.changeset/olive-comics-grab.md new file mode 100644 index 00000000..0706749d --- /dev/null +++ b/.changeset/olive-comics-grab.md @@ -0,0 +1,5 @@ +--- +"create-llama": patch +--- + +Add fly.io deployment diff --git a/helpers/python.ts b/helpers/python.ts index f57f6e97..0e5ac7c7 100644 --- a/helpers/python.ts +++ b/helpers/python.ts @@ -548,9 +548,4 @@ export const installPythonTemplate = async ({ if (postInstallAction === "runApp" || postInstallAction === "dependencies") { installPythonDependencies(); } - - // Copy deployment files for python - await copy("**", root, { - cwd: path.join(compPath, "deployments", "python"), - }); }; diff --git a/helpers/typescript.ts b/helpers/typescript.ts index d0064bfd..3c57ba3b 100644 --- a/helpers/typescript.ts +++ b/helpers/typescript.ts @@ -247,11 +247,6 @@ export const installTSTemplate = async ({ ) { await installTSDependencies(packageJson, packageManager, isOnline); } - - // Copy deployment files for typescript - await copy("**", root, { - cwd: path.join(compPath, "deployments", "typescript"), - }); }; async function updatePackageJson({ diff --git a/templates/components/agents/python/blog/README-template.md b/templates/components/agents/python/blog/README-template.md index f6d928e2..730b20d8 100644 --- a/templates/components/agents/python/blog/README-template.md +++ b/templates/components/agents/python/blog/README-template.md @@ -55,6 +55,10 @@ To start the app optimized for **production**, run: poetry run prod ``` +## Deployments + +For production deployments, check the [DEPLOY.md](DEPLOY.md) file. + ## Learn More To learn more about LlamaIndex, take a look at the following resources: diff --git a/templates/components/agents/python/financial_report/README-template.md b/templates/components/agents/python/financial_report/README-template.md index 12754422..5359c81d 100644 --- a/templates/components/agents/python/financial_report/README-template.md +++ b/templates/components/agents/python/financial_report/README-template.md @@ -43,6 +43,10 @@ To start the app optimized for **production**, run: poetry run prod ``` +## Deployments + +For production deployments, check the [DEPLOY.md](DEPLOY.md) file. + ## Learn More To learn more about LlamaIndex, take a look at the following resources: diff --git a/templates/components/agents/python/form_filling/README-template.md b/templates/components/agents/python/form_filling/README-template.md index 5a8af48d..d1ab8e56 100644 --- a/templates/components/agents/python/form_filling/README-template.md +++ b/templates/components/agents/python/form_filling/README-template.md @@ -49,6 +49,10 @@ To start the app optimized for **production**, run: poetry run prod ``` +## Deployments + +For production deployments, check the [DEPLOY.md](DEPLOY.md) file. + ## Learn More To learn more about LlamaIndex, take a look at the following resources: diff --git a/templates/components/deployments/typescript/Dockerfile b/templates/types/streaming/express/Dockerfile similarity index 100% rename from templates/components/deployments/typescript/Dockerfile rename to templates/types/streaming/express/Dockerfile diff --git a/templates/types/streaming/fastapi/DEPLOY.md b/templates/types/streaming/fastapi/DEPLOY.md new file mode 100644 index 00000000..59881f7b --- /dev/null +++ b/templates/types/streaming/fastapi/DEPLOY.md @@ -0,0 +1,73 @@ +## Deployments + +### Using [Fly.io](https://fly.io/): + +First, install [flyctl](https://fly.io/docs/flyctl/install/) and then authenticate with your Fly.io account: + +```shell +fly auth login +``` + +Then, run this command and follow the prompts to deploy the app.: + +```shell +fly launch +``` + +And to open the app in your browser: + +```shell +fly apps open +``` + +> **Note**: The app will use the values from the `.env` file by default to simplify the deployment. Make sure all the needed environment variables in the [.env](.env) file are set. For production environments, you should not use the `.env` file, but [set the variables in Fly.io](https://fly.io/docs/rails/the-basics/configuration/) instead. + +#### Documents + +If you're having documents in the `./data` folder, run the following command to generate vector embeddings of the documents: + +``` +fly console --machine <machine_id> --command "poetry run generate" +``` + +Where `machine_id` is the ID of the machine where the app is running. You can show the running machines with the `fly machines` command. + +> **Note**: Using documents will make the app stateful. As Fly.io is a stateless app, you should use [LlamaCloud](https://docs.cloud.llamaindex.ai/llamacloud/getting_started) or a vector database to store the embeddings of the documents. This applies also for document uploads by the user. + +### Using Docker + +First, build an image for the app: + +``` +docker build -t <your_image_name> . +``` + +Then, start the app by running the image: + +``` +docker run \ + -v $(pwd)/.env:/app/.env \ # Use ENV variables and configuration from your file-system + -v $(pwd)/config:/app/config \ + -v $(pwd)/storage:/app/storage \ # Use your file system to store vector embeddings + -p 8000:8000 \ + <your_image_name> +``` + +Open [http://localhost:8000](http://localhost:8000) with your browser to start the app. + +#### Documents + +If you're having documents in the `./data` folder, run the following command to generate vector embeddings of the documents: + +``` +docker run \ + --rm \ + -v $(pwd)/.env:/app/.env \ # Use ENV variables and configuration from your file-system + -v $(pwd)/config:/app/config \ + -v $(pwd)/data:/app/data \ # Use your local folder to read the data + -v $(pwd)/storage:/app/storage \ # Use your file system to store the vector database + <your_image_name> \ + poetry run generate +``` + +The app will then be able to answer questions about the documents in the `./data` folder. diff --git a/templates/components/deployments/python/Dockerfile b/templates/types/streaming/fastapi/Dockerfile similarity index 52% rename from templates/components/deployments/python/Dockerfile rename to templates/types/streaming/fastapi/Dockerfile index 624364b6..7e3a0143 100644 --- a/templates/components/deployments/python/Dockerfile +++ b/templates/types/streaming/fastapi/Dockerfile @@ -1,4 +1,19 @@ -FROM python:3.11 as build +# ==================================== +# Build the frontend +# ==================================== +FROM node:20 AS frontend + +WORKDIR /app/frontend + +COPY .frontend /app/frontend + +RUN npm install && npm run build + + +# ==================================== +# Backend +# ==================================== +FROM python:3.11 AS build WORKDIR /app @@ -19,8 +34,17 @@ COPY ./pyproject.toml ./poetry.lock* /app/ RUN poetry install --no-root --no-cache --only main # ==================================== -FROM build as release +# Release +# ==================================== +FROM build AS release + +COPY --from=frontend /app/frontend/out /app/static COPY . . -CMD ["python", "main.py"] \ No newline at end of file +# Remove frontend code +RUN rm -rf .frontend + +EXPOSE 8000 + +CMD ["poetry", "run", "prod"] \ No newline at end of file diff --git a/templates/types/streaming/fastapi/README-template.md b/templates/types/streaming/fastapi/README-template.md index ceb6804b..00a522e7 100644 --- a/templates/types/streaming/fastapi/README-template.md +++ b/templates/types/streaming/fastapi/README-template.md @@ -58,39 +58,9 @@ To start the app optimized for **production**, run: poetry run prod ``` -## Using Docker +## Deployments -1. Build an image for the FastAPI app: - -``` -docker build -t <your_backend_image_name> . -``` - -2. Generate embeddings: - -Parse the data and generate the vector embeddings if the `./data` folder exists - otherwise, skip this step: - -``` -docker run \ - --rm \ - -v $(pwd)/.env:/app/.env \ # Use ENV variables and configuration from your file-system - -v $(pwd)/config:/app/config \ - -v $(pwd)/data:/app/data \ # Use your local folder to read the data - -v $(pwd)/storage:/app/storage \ # Use your file system to store the vector database - <your_backend_image_name> \ - poetry run generate -``` - -3. Start the API: - -``` -docker run \ - -v $(pwd)/.env:/app/.env \ # Use ENV variables and configuration from your file-system - -v $(pwd)/config:/app/config \ - -v $(pwd)/storage:/app/storage \ # Use your file system to store gea vector database - -p 8000:8000 \ - <your_backend_image_name> -``` +For production deployments, check the [DEPLOY.md](DEPLOY.md) file. ## Learn More diff --git a/templates/types/streaming/nextjs/Dockerfile b/templates/types/streaming/nextjs/Dockerfile new file mode 100644 index 00000000..5c738ab4 --- /dev/null +++ b/templates/types/streaming/nextjs/Dockerfile @@ -0,0 +1,16 @@ +FROM node:20-alpine as build + +WORKDIR /app + +# Install dependencies +COPY package.json package-lock.* ./ +RUN npm install + +# Build the application +COPY . . +RUN npm run build + +# ==================================== +FROM build as release + +CMD ["npm", "run", "start"] \ No newline at end of file -- GitLab