Skip to content

Commit

Permalink
feat: create docker-based openai hello world
Browse files Browse the repository at this point in the history
  • Loading branch information
lpm0073 committed Jan 8, 2024
1 parent f6c7bb6 commit cdb5bcc
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 37 deletions.
9 changes: 0 additions & 9 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,6 @@ repos:
- id: codespell
args: ["--ignore-words=codespell.txt"]
exclude: '\.svg$'
- repo: local
hooks:
- id: eslint
name: eslint
entry: bash -c 'cd client && npx eslint'
language: node
types: [javascript]
files: ^client/
- repo: https://github.com/pre-commit/mirrors-prettier
rev: v4.0.0-alpha.8
hooks:
Expand Down Expand Up @@ -63,7 +55,6 @@ repos:
- id: detect-aws-credentials
- id: detect-private-key
- id: end-of-file-fixer
- id: forbid-new-submodules
- id: trailing-whitespace
- id: check-case-conflict
- id: check-merge-conflict
Expand Down
8 changes: 5 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,20 @@
FROM python:3.11

# Set the working directory to /app
WORKDIR /app
WORKDIR /dist

# Copy the current directory contents into the container at /app
COPY python/ /app
COPY app /dist/app
COPY requirements/prod.txt requirements.txt

# Set environment variables
ENV ENVIRONMENT dev
ENV OPENAI_API_KEY ""
ENV PYTHONPATH /dist

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


# Run the application when the container launches
CMD ["python", "console.py"]
CMD ["python", "app/hello_world.py"]
13 changes: 11 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
SHELL := /bin/bash
CONTAINER_NAME := hello_world

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

.PHONY: analyze pre-commit init lint clean test
.PHONY: analyze pre-commit init lint clean test build release

# Default target executed when no arguments are given to make.
all: help
Expand Down Expand Up @@ -44,7 +45,8 @@ init:
$(PYTHON) -m venv venv && \
$(ACTIVATE_VENV) && \
$(PIP) install --upgrade pip && \
$(PIP) install -r requirements.txt && \
$(PIP) install -r requirements/dev.txt && \
$(PIP) install -r requirements/prod.txt && \
deactivate && \
pre-commit install

Expand All @@ -61,6 +63,13 @@ lint:
clean:
rm -rf venv

build:
docker build -t ${CONTAINER_NAME} .

run:
source .env && \
docker run -it -e OPENAI_API_KEY=${OPENAI_API_KEY} -e ENVIRONMENT=prod ${CONTAINER_NAME}


######################
# HELP
Expand Down
16 changes: 5 additions & 11 deletions app/hello_world.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
run: python3 -m app.hello_world "Chinese"
"""
import argparse
import random

import openai
Expand All @@ -18,16 +17,15 @@ def hello_world(language: str = None):
if language is None:
random_index = random.randint(0, len(settings.LANGUAGES) - 1)
language = settings.LANGUAGES[random_index]
languages_str = ", ".join(settings.LANGUAGES)
prompt = f"Translate 'Hello World' to {language}"
model = "gpt-3.5-turbo"
temperature = 0.0
max_tokens = 64
messages = [
{
"role": "system",
"content": """You are a helpful assistant who speaks English, Spanish,
Italian, French, Gereman, Polish, Hebrew, Chinese, Mandarin, Japanese, Hindi, Russian,
Portuguese, Bengali, Urdu, Indonesian, Nigerian Pidgin, Marathi, Telugu, Turkish and Tamil.
"content": f"""You are a helpful assistant who speaks {languages_str}
You should respond with the correct, closest translation of "hello world" based on the language that the user
requests. Your response should be in the format: '[hello world translation] -- ([language])'.""",
},
Expand All @@ -46,10 +44,6 @@ def hello_world(language: str = None):


if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Hello World")
parser.add_argument(
"--language", type=str, help="Your language", nargs="?", default=None, choices=settings.LANGUAGES
)
args = parser.parse_args()

hello_world(language=args.language)
print("Hello World")
input_language = input("Your language: ")
hello_world(language=input_language)
1 change: 0 additions & 1 deletion doc/GOOD_CODING_PRACTICE.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ Linters and formatters are tools used in programming to analyze and improve the

### Code Analysis

- [ESLint](https://eslint.org/): an open source project that helps you find and fix problems with your JavaScript and JSX code.
- [Flake8](https://flake8.pycqa.org/en/latest/): provides Python syntax checking, naming style enforcement, code style enforcement, and [cyclomatic complexity](https://en.wikipedia.org/wiki/Cyclomatic_complexity) analysis.
- [pylint](https://pypi.org/project/pylint/): a static code analyser for Python. It analyses your code without actually running it. It checks for errors, enforces a coding standard, looks for code smells, and can make suggestions about how the code could be refactored.
- [bandit](https://github.com/PyCQA/bandit): a tool designed to find common security issues in Python code.
Expand Down
12 changes: 1 addition & 11 deletions requirements.txt → requirements/dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@
# written by: Lawrence McDaniel
# https://lawrencemcdaniel.com
#
# usage: Python requirements for AWS Lambda functions. Create a virtual
# environment in the root of this repository named `venv`. Terraform
# modules will look for and include these requirements in the zip
# packages for each Python-based Lambda function.
# usage: Python local development requirements
# -----------------------------------------------------------------------------


Expand All @@ -22,10 +19,3 @@ bandit==1.7.6
pydocstringformatter==0.7.3
tox==4.11.4
codespell==2.2.6

# production
# ------------
python-dotenv==1.0.0
openai==1.6.1
langchain
langchain-experimental
10 changes: 10 additions & 0 deletions requirements/prod.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# -----------------------------------------------------------------------------
# written by: Lawrence McDaniel
# https://lawrencemcdaniel.com
#
# usage: Python run-time requirements for the OpenAI API
# -----------------------------------------------------------------------------


python-dotenv==1.0.0
openai==1.6.1

0 comments on commit cdb5bcc

Please sign in to comment.