Infrastracture

Note

Use make to install all.

Makefile
  1.PHONY: all create-env install format lint clean shell
  2
  3all: install format lint
  4
  5VENV := venv
  6PYTHON := python3.11
  7VENV_PY := $(VENV)/bin/python
  8PIP := $(VENV)/bin/pip
  9TEST := $(VENV)/bin/pytest
 10BLACK := $(VENV)/bin/black
 11ISORT := $(VENV)/bin/isort
 12PYLINT := $(VENV)/bin/pylint
 13
 14create-env:
 15	@echo "Creating virtualenv with Python $(PYTHON)"
 16	@if ! command -v $(PYTHON) >/dev/null; then \
 17		echo "Python $(PYTHON) not found"; exit 1; \
 18	fi
 19	@if [ ! -d $(VENV) ]; then \
 20		$(PYTHON) -m venv $(VENV); \
 21	else \
 22		echo "Virtualenv already exists."; \
 23	fi
 24
 25install: create-env
 26	@echo "Creating virtual environment if needed"
 27	@if [ ! -d $(VENV) ]; then \
 28		python3 -m venv $(VENV); \
 29	fi
 30
 31	@echo "Upgrading pip"
 32	$(PIP) install --upgrade pip
 33
 34	@echo "Testing availability of requirements-dev.txt"
 35	@if [ ! -f requirements-dev.txt ]; then \
 36		echo "Missing requirements-dev.txt" >&2; \
 37		exit 1; \
 38	fi
 39	@echo "Installing packages for Dev"
 40	$(PIP) install -r requirements-dev.txt
 41
 42	@echo "Testing availability of requirements-lambda.txt"
 43	@if [ ! -f requirements-lambda.txt ]; then \
 44		echo "Missing requirements-lamdba.txt" >&2; \
 45		exit 1; \
 46	fi
 47	@echo "Installing packages for Lambda"
 48	$(PIP) install -r requirements-lambda.txt
 49
 50	@echo "Testing availability of requirements-docs.txt"
 51	@if [ ! -f requirements-docs.txt ]; then \
 52		echo "Missing requirements-docs.txt" >&2; \
 53		exit 1; \
 54	fi
 55	@echo "Installing packages for Sphinx"
 56	$(PIP) install -r requirements-docs.txt
 57
 58	@echo "Installing pre-commit"
 59	$(PIP) install pre-commit
 60
 61format:
 62	@echo "Correcting with black"
 63	- $(BLACK) . --extend-exclude="venv/*" --skip-magic-trailing-comma --line-length=79
 64
 65	@echo "Correcting with isort"
 66	- $(ISORT) . --skip-glob=venv --profile=black --line-length=79
 67
 68lint:
 69	@echo "Linting Gauss repo"
 70	- $(PYLINT) ./ --ignore=venv
 71	@echo "We allow make to continue despite pylint errors and threshold"
 72
 73precommit:
 74	@echo "Installing pre-commit git hooks"
 75	$(VENV)/bin/pre-commit install
 76
 77test:
 78	@echo "Running tests"
 79	- $(TEST) tests/
 80
 81check: format lint test
 82	@echo "All checks passed!"
 83
 84clean:
 85	@echo "Removing virtual environment"
 86	rm -rf $(VENV)
 87
 88shell:
 89	@echo "Starting shell with virtualenv activated"
 90	@bash --rcfile <(echo "source $(VENV)/bin/activate")
 91
 92help:
 93	@echo "Usage:"
 94	@echo "  make all         - install, format, lint"
 95	@echo "  make create-env  - create virtual environment"
 96	@echo "  make install     - install dependencies"
 97	@echo "  make format      - run black and isort"
 98	@echo "  make lint        - run pylint"
 99	@echo "  make test        - run tests"
100	@echo "  make clean       - remove virtual environment"
101	@echo "  make shell       - open shell with venv activated"
Dockerfile
 1# FROM public.ecr.aws/lambda/python:3.11
 2FROM python:3.11-slim
 3
 4# Set working directory
 5WORKDIR /app
 6
 7# Copy app files
 8COPY app.py utils.py lambda_handler.py ./
 9COPY knowledgebase.json ./
10COPY requirements-dev.txt ./
11COPY requirements-lambda.txt ./
12COPY templates ./templates
13COPY static ./static
14
15# Install dependencies
16RUN pip install --no-cache-dir -r requirements-dev.txt
17RUN pip install --no-cache-dir -r requirements-lambda.txt
18
19# Start Flask app with gunicorn
20# CMD ["lambda_handler.handler"]
21# CMD ["gunicorn", "app:app", "--bind", "0.0.0.0:8080"]
22CMD ["sh", "-c", "gunicorn app:app --bind 0.0.0.0:${PORT:-8080}"]
serverless.yml
 1service: circassiandna-chatbot
 2frameworkVersion: '3'
 3
 4provider:
 5  name: aws
 6  runtime: python3.11
 7  region: us-east-1
 8  environment:
 9    OPENAI_API_KEY: ${env:OPENAI_API_KEY}
10    PINECONE_API_KEY: ${env:PINECONE_API_KEY}
11    PINECONE_INDEX: ${env:PINECONE_INDEX}
12    PINECONE_ENVIRONMENT: ${env:PINECONE_ENVIRONMENT}
13    PINECONE_CLOUD: ${env:PINECONE_CLOUD}
14    PINECONE_REGION: ${env:PINECONE_REGION}
15    PINECONE_NAMESPACE: ${env:PINECONE_NAMESPACE}
16    PORT: ${env:PORT}
17  iamRoleStatements:
18    - Effect: Allow
19      Action:
20        - lambda:InvokeFunction
21      Resource: '*'
22
23package:
24  exclude:
25    - tests/**
26    - docs/**
27    - venv/**
28
29custom:
30  pythonRequirements:
31    fileName: requirements-lambda.txt  # requirements for Lambda
32    layer: true
33    slim: true
34    strip: true
35    dockerizePip: false   # ensures Lambda-compatible packages (<250 Mb)
36    noDeploy:             # exclude dev-only libs from Lambda
37      - black
38      - isort
39      - pylint
40      - pre-commit
41      - pytest
42      - gunicorn
43      - sphinx
44      - sphinx-rtd-theme
45
46plugins:
47  - serverless-python-requirements
48
49layers:
50  pythonDependencies:
51    path: layer
52    compatibleRuntimes:
53      - python3.11
54
55functions:
56  web:
57    handler: lambda_handler.handler
58    layers:
59      - { Ref: PythonDependenciesLambdaLayer }
60    events:
61      - httpApi:       # Use httpApi (v2) or http (REST API, v1)
62          path: /
63          method: ANY
64      - httpApi:
65          path: /api/chat
66          method: POST