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 2e6e655 commit 73deaae
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 5 deletions.
28 changes: 23 additions & 5 deletions app/hello_world.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
# -*- coding: utf-8 -*-
"""
A simple Python example of using the OpenAI API to translate "Hello World" to a popular language.
run: python3 -m app.hello_world "Chinese"
to run in dev:
python3 -m app.hello_world "Chinese"
to run in prod:
CONTAINER_NAME=hello_world
docker run -it -e OPENAI_API_KEY=${OPENAI_API_KEY} -e ENVIRONMENT=prod ${CONTAINER_NAME}
"""
import random

Expand All @@ -13,15 +19,23 @@
def hello_world(language: str = None):
"""Translate 'Hello World' to a popular language."""

# Set the OpenAI API key
# -------------------------------------------------------------------------
openai.api_key = settings.OPENAI_API_KEY

# Set the language to translate to
# -------------------------------------------------------------------------
if language is None:
random_index = random.randint(0, len(settings.LANGUAGES) - 1)
language = settings.LANGUAGES[random_index]
languages_str = ", ".join(settings.LANGUAGES)

# setup our text completion prompt
# -------------------------------------------------------------------------
prompt = f"Translate 'Hello World' to {language}"
model = "gpt-3.5-turbo"
temperature = 0.0
max_tokens = 64
model = settings.OPENAI_API_MODEL
temperature = settings.OPENAI_API_TEMPERATURE
max_tokens = settings.OPENAI_API_MAX_TOKENS
messages = [
{
"role": "system",
Expand All @@ -31,15 +45,19 @@ def hello_world(language: str = None):
},
{"role": "user", "content": prompt},
]

# Call the OpenAI API
# -------------------------------------------------------------------------
response = openai.chat.completions.create(
model=model,
messages=messages,
temperature=temperature,
max_tokens=max_tokens,
)

# Print the response
# -------------------------------------------------------------------------
return_dict = response.model_dump()

print(return_dict["choices"][0]["message"]["content"])


Expand Down
4 changes: 4 additions & 0 deletions app/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
if OPENAI_API_KEY in (None, "PLEASE-ADD-ME"):
raise ConfigurationException("No OpenAI API key found. Please add it to your .env file.")

OPENAI_API_MODEL = "gpt-3.5-turbo"
OPENAI_API_TEMPERATURE = 0.0
OPENAI_API_MAX_TOKENS = 64

LANGUAGES = [
"English",
"Spanish",
Expand Down

0 comments on commit 73deaae

Please sign in to comment.