Skip to content

Commit 62569fc

Browse files
authored
Merge pull request #2 from FullStackWithLawrence/next
create docker-based command-line application
2 parents f6c7bb6 + 378080f commit 62569fc

20 files changed

+149
-109
lines changed

.github/actions/tests/pre-commit/action.yml

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,40 +22,40 @@ runs:
2222
- name: Check for pre-commit in requirements
2323
shell: bash
2424
run: |
25-
if ! grep -q "pre-commit" ./requirements.txt; then
26-
echo "pre-commit not found in requirements.txt" >&2
25+
if ! grep -q "pre-commit" ./requirements/dev.txt; then
26+
echo "pre-commit not found in requirements/dev.txt" >&2
2727
exit 1
2828
fi
2929
3030
- name: Check for black in requirements
3131
shell: bash
3232
run: |
33-
if ! grep -q "black" ./requirements.txt; then
34-
echo "black not found in requirements.txt" >&2
33+
if ! grep -q "black" ./requirements/dev.txt; then
34+
echo "black not found in requirements/dev.txt" >&2
3535
exit 1
3636
fi
3737
3838
- name: Check for flake8 in requirements
3939
shell: bash
4040
run: |
41-
if ! grep -q "flake8" ./requirements.txt; then
42-
echo "flake8 not found in requirements.txt" >&2
41+
if ! grep -q "flake8" ./requirements/dev.txt; then
42+
echo "flake8 not found in requirements/dev.txt" >&2
4343
exit 1
4444
fi
4545
4646
- name: Check for flake8-coding in requirements
4747
shell: bash
4848
run: |
49-
if ! grep -q "flake8-coding" ./requirements.txt; then
50-
echo "flake8-coding not found in requirements.txt" >&2
49+
if ! grep -q "flake8-coding" ./requirements/dev.txt; then
50+
echo "flake8-coding not found in requirements/dev.txt" >&2
5151
exit 1
5252
fi
5353
5454
- name: Cache Python dependencies
5555
uses: actions/cache@v3
5656
with:
5757
path: ~/.cache/pip
58-
key: ${{ runner.os }}-pip-${{ hashFiles('**/requirements.txt') }}
58+
key: ${{ runner.os }}-pip-${{ hashFiles('**/requirements/dev.txt') }}
5959
restore-keys: |
6060
${{ runner.os }}-pip
6161
@@ -68,7 +68,7 @@ runs:
6868
shell: bash
6969
run: |
7070
python -m pip install --upgrade pip
71-
pip install -r ./requirements.txt
71+
pip install -r ./requirements/dev.txt
7272
7373
# see: https://pre-commit.ci/lite.html
7474
- name: pre-commit ci

.github/actions/tests/python/action.yml

Lines changed: 5 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -30,40 +30,16 @@ runs:
3030
- name: Check for openai in requirements
3131
shell: bash
3232
run: |
33-
if ! grep -q "openai" ./requirements.txt; then
34-
echo "openai not found in requirements.txt" >&2
35-
exit 1
36-
fi
37-
38-
- name: Check for langchain in requirements
39-
shell: bash
40-
run: |
41-
if ! grep -q "langchain" ./requirements.txt; then
42-
echo "langchain not found in requirements.txt" >&2
43-
exit 1
44-
fi
45-
46-
- name: Check for langchain-experimental in requirements
47-
shell: bash
48-
run: |
49-
if ! grep -q "langchain-experimental" ./requirements.txt; then
50-
echo "langchain-experimental not found in requirements.txt" >&2
51-
exit 1
52-
fi
53-
54-
- name: Check for pinecone-client in requirements
55-
shell: bash
56-
run: |
57-
if ! grep -q "pinecone-client" ./requirements.txt; then
58-
echo "pinecone-client not found in requirements.txt" >&2
33+
if ! grep -q "openai" requirements/prod.txt; then
34+
echo "openai not found in requirements/prod.txt" >&2
5935
exit 1
6036
fi
6137
6238
- name: Cache Python dependencies
6339
uses: actions/cache@v3
6440
with:
6541
path: ~/.cache/pip
66-
key: ${{ runner.os }}-pip-${{ hashFiles('**/requirements.txt') }}
42+
key: ${{ runner.os }}-pip-${{ hashFiles('**/requirements/prod.txt') }}
6743
restore-keys: |
6844
${{ runner.os }}-pip
6945
@@ -85,7 +61,7 @@ runs:
8561
- name: Install dependencies
8662
shell: bash
8763
run: |
88-
pip install -r ./requirements.txt
64+
pip install -r requirements/prod.txt
8965
env:
9066
SITE_PACKAGES_PATH: ${{ env.SITE_PACKAGES_PATH }}
9167

@@ -95,19 +71,15 @@ runs:
9571
touch ./.env
9672
echo "OPENAI_API_ORGANIZATION=${{ env.OPENAI_API_ORGANIZATION }}" >> ./.env
9773
echo "OPENAI_API_KEY=${{ env.OPENAI_API_KEY }}" >> ./.env
98-
echo "PINECONE_API_KEY=${{ env.PINECONE_API_KEY }}" >> ./.env
99-
echo "PINECONE_ENVIRONMENT=${{ env.PINECONE_ENVIRONMENT }}" >> ./.env
10074
echo "DEBUG_MODE=False" >> ./.env
10175
env:
10276
OPENAI_API_ORGANIZATION: ${{ inputs.openai-api-organization }}
10377
OPENAI_API_KEY: ${{ inputs.openai-api-key }}
104-
PINECONE_API_KEY: ${{ inputs.pinecone-api-key }}
105-
PINECONE_ENVIRONMENT: ${{ inputs.pinecone-environment }}
10678

10779
- name: Run Python unit tests
10880
shell: bash
10981
env:
11082
GITHUB_ACTIONS: "true"
11183
DEBUG_MODE: "true"
11284
run: |
113-
make api-test
85+
make test

.github/workflows/precommitVersionBumps.yml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ jobs:
4444
uses: actions/cache@v3
4545
with:
4646
path: ~/.cache/pip
47-
key: ${{ runner.os }}-pip-${{ hashFiles('**/requirements.txt') }}
47+
key: ${{ runner.os }}-pip-${{ hashFiles('**/requirements/**') }}
4848
restore-keys: |
4949
${{ runner.os }}-pip
5050
@@ -66,7 +66,8 @@ jobs:
6666
- name: Install dependencies
6767
shell: bash
6868
run: |
69-
pip install -r ./requirements.txt
69+
pip install -r ./requirements/dev.txt
70+
pip install -r ./requirements/prod.txt
7071
env:
7172
SITE_PACKAGES_PATH: ${{ env.SITE_PACKAGES_PATH }}
7273

.github/workflows/pullRequestController.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ on:
4747
paths:
4848
- "**.py"
4949
- "**.js"
50-
- "**.requirements.txt"
50+
- "**./requirements/**"
5151
- "**.package.json"
5252
- "./app/**"
5353

@@ -109,7 +109,7 @@ jobs:
109109
id: file_changes
110110
run: |
111111
echo "::set-output name=files_changed::$(git diff --name-only ${{ github.event.pull_request.base.sha }} ${{ github.sha }} | grep '\.py$' || true)"
112-
echo "::set-output name=requirements_changed::$(git diff --name-only ${{ github.event.pull_request.base.sha }} ${{ github.sha }} | grep 'requirements.txt$' || true)"
112+
echo "::set-output name=requirements_changed::$(git diff --name-only ${{ github.event.pull_request.base.sha }} ${{ github.sha }} | grep 'requirements$' || true)"
113113
114114
- name: Run Python tests
115115
if: steps.file_changes.outputs.files_changed != '' || steps.file_changes.outputs.requirements_changed != ''

.github/workflows/testsPython.yml

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,6 @@ jobs:
2323
id: checkout
2424
uses: actions/checkout@v4
2525

26-
- name: Configure AWS credentials
27-
uses: aws-actions/configure-aws-credentials@v4
28-
with:
29-
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
30-
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
31-
aws-region: ${{ secrets.AWS_REGION }}
32-
33-
- name: Get IAM user info
34-
run: |
35-
aws sts get-caller-identity | sed -E 's/(arn:aws:iam::)[0-9]+(:user\/\w+)/\1************\2/' | jq '.Account="************"'
36-
3726
- name: Run Python tests
3827
uses: ./.github/actions/tests/python
3928
with:

.pre-commit-config.yaml

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,6 @@ repos:
88
- id: codespell
99
args: ["--ignore-words=codespell.txt"]
1010
exclude: '\.svg$'
11-
- repo: local
12-
hooks:
13-
- id: eslint
14-
name: eslint
15-
entry: bash -c 'cd client && npx eslint'
16-
language: node
17-
types: [javascript]
18-
files: ^client/
1911
- repo: https://github.com/pre-commit/mirrors-prettier
2012
rev: v4.0.0-alpha.8
2113
hooks:
@@ -63,7 +55,6 @@ repos:
6355
- id: detect-aws-credentials
6456
- id: detect-private-key
6557
- id: end-of-file-fixer
66-
- id: forbid-new-submodules
6758
- id: trailing-whitespace
6859
- id: check-case-conflict
6960
- id: check-merge-conflict

Dockerfile

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,20 @@
33
FROM python:3.11
44

55
# Set the working directory to /app
6-
WORKDIR /app
6+
WORKDIR /dist
77

88
# Copy the current directory contents into the container at /app
9-
COPY python/ /app
9+
COPY app /dist/app
10+
COPY requirements/prod.txt requirements.txt
1011

1112
# Set environment variables
1213
ENV ENVIRONMENT dev
1314
ENV OPENAI_API_KEY ""
15+
ENV PYTHONPATH /dist
1416

1517
# Install any needed packages specified in requirements.txt
1618
RUN pip install --no-cache-dir -r requirements.txt
1719

1820

1921
# Run the application when the container launches
20-
CMD ["python", "console.py"]
22+
CMD ["python", "app/hello_world.py"]

Makefile

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
SHELL := /bin/bash
2+
CONTAINER_NAME := hello_world
23

34
ifeq ($(OS),Windows_NT)
45
PYTHON = python.exe
@@ -15,7 +16,7 @@ else
1516
$(shell echo -e "OPENAI_API_ORGANIZATION=PLEASE-ADD-ME\nOPENAI_API_KEY=PLEASE-ADD-ME\nENVIRONMENT=dev\n" >> .env)
1617
endif
1718

18-
.PHONY: analyze pre-commit init lint clean test
19+
.PHONY: analyze pre-commit init lint clean test build release
1920

2021
# Default target executed when no arguments are given to make.
2122
all: help
@@ -44,12 +45,13 @@ init:
4445
$(PYTHON) -m venv venv && \
4546
$(ACTIVATE_VENV) && \
4647
$(PIP) install --upgrade pip && \
47-
$(PIP) install -r requirements.txt && \
48+
$(PIP) install -r requirements/dev.txt && \
49+
$(PIP) install -r requirements/prod.txt && \
4850
deactivate && \
4951
pre-commit install
5052

5153
test:
52-
echo "No tests yet"
54+
python -m unittest discover -s app/
5355

5456
lint:
5557
isort .
@@ -61,6 +63,13 @@ lint:
6163
clean:
6264
rm -rf venv
6365

66+
build:
67+
docker build -t ${CONTAINER_NAME} .
68+
69+
run:
70+
source .env && \
71+
docker run -it -e OPENAI_API_KEY=${OPENAI_API_KEY} -e ENVIRONMENT=prod ${CONTAINER_NAME}
72+
6473

6574
######################
6675
# HELP

README.md

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
[![FullStackWithLawrence](https://a11ybadges.com/badge?text=FullStackWithLawrence&badgeColor=orange&logo=youtube&logoColor=282828)](https://www.youtube.com/@FullStackWithLawrence)<br>
44
[![OpenAI](https://a11ybadges.com/badge?logo=openai)](https://platform.openai.com/)
5-
[![LangChain](https://a11ybadges.com/badge?text=LangChain&badgeColor=0834ac)](https://www.langchain.com/)
65
[![Amazon AWS](https://a11ybadges.com/badge?logo=amazonaws)](https://aws.amazon.com/)
76
[![ReactJS](https://a11ybadges.com/badge?logo=react)](https://react.dev/)
87
[![Python](https://a11ybadges.com/badge?logo=python)](https://www.python.org/)
@@ -15,7 +14,33 @@
1514
[![License: AGPL v3](https://img.shields.io/badge/License-AGPL_v3-blue.svg)](https://www.gnu.org/licenses/agpl-3.0)
1615
[![hack.d Lawrence McDaniel](https://img.shields.io/badge/hack.d-Lawrence%20McDaniel-orange.svg)](https://lawrencemcdaniel.com)
1716

18-
A Python "Hello World" application that also demonstrates the OpenAI API and usage of Docker.
17+
A Python "Hello World" application that demonstrates the OpenAI API and usage of Docker Compose to containerize your project. This is a command line utility that returns the value "Hello World" in the written language of your choice.
18+
19+
## Quick Start
20+
21+
```console
22+
git clone https://github.com/FullStackWithLawrence/openai-hello-world.git
23+
cd openai-hello-world
24+
make init # create a virtual environment
25+
make build # run Docker compose to containerize your application
26+
make run # run the application as a Docker container
27+
```
28+
29+
To run from the command line:
30+
31+
```console
32+
source venv/bin/activate
33+
python3 -m app.hello_world "Chinese"
34+
```
35+
36+
Sample output:
37+
38+
```console
39+
% make run
40+
Hello World
41+
Your language: Mandarin
42+
你好,世界 -- (Mandarin)
43+
```
1944

2045
## Requirements
2146

@@ -25,7 +50,8 @@ A Python "Hello World" application that also demonstrates the OpenAI API and usa
2550
_If you're new to OpenAI API then see [How to Get an OpenAI API Key](./doc/OPENAI_API_GETTING_STARTED_GUIDE.md)_
2651
- [Python 3.11](https://www.python.org/downloads/): for creating virtual environment used for building AWS Lambda Layer, and locally by pre-commit linters and code formatters.
2752
- [NodeJS](https://nodejs.org/en/download): used with NPM for local ReactJS developer environment, and for configuring/testing Semantic Release.
28-
- [Docker Compose](https://docs.docker.com/compose/install/): used by an automated Terraform process to create the AWS Lambda Layer for OpenAI and LangChain.
53+
- [Docker](https://docs.docker.com/): the Docker run-time environment for your operating system. Can be installed as a desktop application or as a service (daemon)
54+
- [Docker Compose](https://docs.docker.com/compose/install/): used to create your production container.
2955

3056
## Documentation
3157

app/__version__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
# -*- coding: utf-8 -*-
22
# DO NOT EDIT.
33
# Managed via automated CI/CD in .github/workflows/semanticVersionBump.yml.
4-
__version__ = "0.0.1"
4+
__version__ = "0.1.0"

0 commit comments

Comments
 (0)