Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
yoomlam committed Jun 3, 2024
1 parent f1464cd commit ce6a1ec
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 20 deletions.
102 changes: 84 additions & 18 deletions .github/workflows/push-image.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ on:
- ''
- 'chatbot-chainlit-svc'
- 'secure-chatbot-svc'
- 'container-service-2'
- 'container-service-3'
build_image:
description: "Build and push image"
required: true
Expand All @@ -28,8 +30,19 @@ on:
required: true
type: boolean
default: 'false'
image_tag:
description: 'Tag/Version of the image to push'
required: true
type: string
default: '0.06'
delete_images:
description: 'Delete previous images in AWS Lightsail'
required: true
type: boolean
default: 'false'

env:
AWS_REGION: us-east-1
IMAGE_NAME: localimage

jobs:
Expand All @@ -49,25 +62,58 @@ jobs:
fi
echo "service_name=$service_name" >> $GITHUB_OUTPUT
case "${service_name}" in
# The image_tag is specific to the `*-svc` service
'secure-chatbot-svc') image_tag='0.01';;
'chatbot-chainlit-svc') image_tag='chatbot-chainlit';;
*) echo "Unknown service_name: '${service_name}'"; exit 3;;
esac
image_tag="${{ inputs.image_tag }}"
if [ "${image_tag}" = "" ]; then
case "${service_name}" in
# The image_tag is specific to the `*-svc` service
'secure-chatbot-svc') image_tag='0.01';;
'chatbot-chainlit-svc') image_tag='chatbot-chainlit';;
*) echo "Unknown service_name: '${service_name}'"; exit 3;;
esac
fi
echo "image_tag=$image_tag" >> $GITHUB_OUTPUT
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v4
with:
aws-region: us-east-1
aws-region: ${{ env.AWS_REGION }}
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
mask-aws-account-id: true
# TODO: secure credentials: https://github.com/aws-actions/amazon-ecr-login?tab=readme-ov-file#ecr-private
# https://github.com/docker/login-action?tab=readme-ov-file#aws-elastic-container-registry-ecr
# https://medium.com/@lukhee/automating-aws-lightsail-deployments-with-github-actions-53c73c9a1c1f


- name: Login to Amazon ECR
- name: "Upgrade AWS CLI version and setup lightsailctl"
run: |
# aws --version
# curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
# unzip awscliv2.zip
# sudo ./aws/install --bin-dir /usr/local/bin --install-dir /usr/local/aws-cli --update
# which aws
aws --version
sudo curl "https://s3.us-west-2.amazonaws.com/lightsailctl/latest/linux-amd64/lightsailctl" -o "/usr/local/bin/lightsailctl"
sudo chmod +x /usr/local/bin/lightsailctl
aws lightsail push-container-image help
- name: "Delete previous container images"
if: inputs.delete_images
env:
SERVICE_NAME: ${{ steps.check_inputs.outputs.service_name }}
run: |
AWS_IMAGES=$(aws lightsail get-container-images --region "$AWS_REGION" --service-name "$SERVICE_NAME" --output text)
IMAGE_NAMES=$(echo $AWS_IMAGES | grep -Eo ':"$SERVICE_NAME"\.${{ inputs.image-name }}\.[0-9]+')
echo $IMAGE_NAMES
FIRST=0
while read LINE; do
if [ "$FIRST" -ne 0 ]; then
aws lightsail delete-container-image --region "$AWS_REGION" --service-name "$SERVICE_NAME" --image $LINE;
fi
FIRST=1;
done <<< $IMAGE_NAMES
- name: "Login to Amazon ECR"
id: login-ecr
uses: aws-actions/amazon-ecr-login@v2
with:
Expand All @@ -84,23 +130,43 @@ jobs:
# TODO: make this more easily editable and secure
# The DOT_ENV_FILE_CONTENTS contains LITERAL_API_KEY, OPENAI_API_KEY, RETRIEVE_K, LLM_MODEL_NAME, SUMMARIZER_LLM_MODEL_NAME
echo "${{secrets.DOT_ENV_FILE_CONTENTS}}" > .env
echo "GIT_SHA=${{ github.sha }}" >> .env
docker build -t "$IMAGE_NAME" --build-arg GURU_CARDS_URL="https://docs.google.com/uc?export=download&id=${{ secrets.GURU_CARDS_URL_ID }}" .
- name: "Publish image to AWS ECR'"
id: publish_image
- name: "Publish image to Lightsail"
if: inputs.build_image
env:
ECR_PATH: ${{ steps.login-ecr.outputs.registry }}/${{ secrets.ECR_REPO }}
ECR_PATH: ${{ steps.login-ecr.outputs.registry }}/${{ secrets.ECR_REPO }}
SERVICE_NAME: ${{ steps.check_inputs.outputs.service_name }}
IMAGE_TAG: ${{ steps.check_inputs.outputs.image_tag }}
# LABEL must match regex ^(?:[a-z0-9]{1,2}|[a-z0-9][a-z0-9-]+[a-z0-9])$
LABEL: $IMAGE_NAME
IMAGE_SHA_TAG: ${{ github.sha }}
run: |
image_tag="${{ steps.check_inputs.outputs.image_tag }}"
echo "# Publishing image ${image_tag} to $ECR_PATH"
echo "# Publishing image $SERVICE_NAME $IMAGE_TAG to $ECR_PATH"
echo docker tag "$IMAGE_NAME" "$ECR_PATH:latest"
# Do we need to tag?
docker tag "$IMAGE_NAME" "$ECR_PATH:latest"
docker tag "$IMAGE_NAME" "$ECR_PATH:$IMAGE_TAG"
docker tag "$IMAGE_NAME" "$ECR_PATH:$IMAGE_SHA_TAG"
docker tag "$IMAGE_NAME" "$ECR_PATH:${image_tag}"
docker push "$ECR_PATH:${image_tag}"
# docker push "$ECR_PATH:latest"
# docker push "$ECR_PATH:$IMAGE_TAG"
# docker push "$ECR_PATH:$IMAGE_SHA_TAG"
aws lightsail push-container-image --region $AWS_REGION --service-name "$SERVICE_NAME" --label "$LABEL" --image "$IMAGE_NAME"
- name: "Update AWS Service"
- name: Deploy container on AWS Lightsail
if: inputs.deploy_image
env:
CLUSTER_NAME: genai-experiments
SERVICE_NAME: ${{ steps.check_inputs.outputs.service_name }}
run: |
aws ecs update-service --force-new-deployment --cluster "$CLUSTER_NAME" --service "${{ steps.check_inputs.outputs.service_name }}"
# aws lightsail create-container-service-deployment --region ${{ inputs.aws-region }} --cli-input-json '${{ inputs.aws-lightsail-service-config }}' > /dev/null
aws lightsail update-container-service --service-name "$SERVICE_NAME" --no-is-disabled
# - name: "Update AWS Service"
# if: inputs.deploy_image
# env:
# CLUSTER_NAME: genai-experiments
# run: |
# aws ecs update-service --force-new-deployment --cluster "$CLUSTER_NAME" --service "${{ steps.check_inputs.outputs.service_name }}"
11 changes: 11 additions & 0 deletions 05-assistive-chatbot/.env-DEV
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@

ENABLE_CHATBOT_API=False

# Used by SentenceTransformerEmbeddings and HuggingFaceEmbeddings
SENTENCE_TRANSFORMERS_HOME="./.sentence-transformers-cache"

# CHAT_ENGINE
CHAT_ENGINE='Summaries'
LLM_MODEL_NAME='openai :: gpt-3.5-turbo-instruct'
RETRIEVE_K=4
SUMMARIZER_LLM_MODEL_NAME='openai :: gpt-3.5-turbo-instruct'
7 changes: 7 additions & 0 deletions 05-assistive-chatbot/.env-PROD
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

CHATBOT_LOG_LEVEL='DEBUG'

ENABLE_CHATBOT_API=True

CHAT_ENGINE='Direct'
LLM_MODEL_NAME='openai :: gpt-3.5-turbo'
4 changes: 3 additions & 1 deletion 05-assistive-chatbot/chatbot/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ def configure_logging():
logging.info("Configured logging level: %s", log_level)


env = os.environ.get("ENV", "DEV")
dotenv.load_dotenv(f".env-{env}")
dotenv.load_dotenv()
configure_logging()

Expand All @@ -46,7 +48,7 @@ def _init_settings():
# Remember to update ChatSettings in chatbot-chainlit.py when adding new settings
# and update chatbot/engines/__init.py:CHATBOT_SETTING_KEYS
return {
"env": os.environ.get("ENV", "DEV"),
"env": env,
"enable_api": is_true(os.environ.get("ENABLE_CHATBOT_API", "False")),
"chat_engine": os.environ.get("CHAT_ENGINE", "Direct"),
"model": os.environ.get("LLM_MODEL_NAME", "mock :: llm"),
Expand Down
5 changes: 4 additions & 1 deletion 05-assistive-chatbot/chatbot_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import logging
from functools import cached_property
import os
from typing import Dict

from fastapi import FastAPI, Request
Expand Down Expand Up @@ -51,7 +52,9 @@ def query(message: str | Dict):
def healthcheck(request: Request):
logger.info(request.headers)
# TODO: Add a health check - https://pypi.org/project/fastapi-healthchecks/
return HTMLResponse("Healthy")

git_sha = os.environ.get("GIT_SHA", "")
return HTMLResponse("Healthy " + git_sha)


if __name__ == "__main__":
Expand Down

0 comments on commit ce6a1ec

Please sign in to comment.