Skip to content

Commit ed16715

Browse files
committed
refactor: tweaks
1 parent a4954aa commit ed16715

4 files changed

Lines changed: 24 additions & 11 deletions

File tree

app/const.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# -*- coding: utf-8 -*-
2+
"""Constants for the Stackademy application."""
3+
4+
MISSING = "MISSING"

app/prompt.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,22 +19,24 @@
1919
)
2020

2121
from app import settings
22+
from app.const import MISSING
2223
from app.logging_config import get_logger, setup_logging
2324
from app.stackademy import stackademy_app
2425

2526

2627
setup_logging()
2728
logger = get_logger(__name__)
2829

29-
30-
messages: list[
30+
MessagesType = list[
3131
Union[
3232
ChatCompletionSystemMessageParam,
3333
ChatCompletionUserMessageParam,
3434
ChatCompletionAssistantMessageParam,
3535
ChatCompletionToolMessageParam,
3636
]
37-
] = [
37+
]
38+
39+
messages: MessagesType = [
3840
ChatCompletionSystemMessageParam(
3941
role="system",
4042
content="""You are a helpful assistant for the Stackademy online learning platform.
@@ -68,9 +70,9 @@ def handle_function_call(function_name: str, arguments: dict) -> str:
6870
return json.dumps(courses, default=str, indent=2)
6971

7072
if function_name == "register_course":
71-
course_code = arguments.get("course_code", "MISSING COURSE CODE")
72-
email = arguments.get("email", "MISSING EMAIL")
73-
full_name = arguments.get("full_name", "MISSING NAME")
73+
course_code = arguments.get("course_code", MISSING)
74+
email = arguments.get("email", MISSING)
75+
full_name = arguments.get("full_name", MISSING)
7476

7577
# Call the actual function
7678
success = stackademy_app.register_course(course_code=course_code, email=email, full_name=full_name)
@@ -123,7 +125,6 @@ def process_tool_calls(message: ChatCompletionMessage) -> list[str]:
123125
return functions_called
124126

125127

126-
# pylint: disable=too-many-locals
127128
def completion(prompt: str) -> tuple[ChatCompletion, list[str]]:
128129
"""LLM text completion"""
129130

app/settings.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@
1212
load_dotenv()
1313

1414
# OpenAI API settings
15-
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY", None)
16-
OPENAI_API_MODEL = "gpt-4o-mini"
17-
OPENAI_API_TEMPERATURE = 0.0
18-
OPENAI_API_MAX_TOKENS = 4096
15+
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY", "SET-ME-PLEASE")
16+
OPENAI_API_MODEL = os.getenv("OPENAI_API_MODEL", "gpt-4o-mini")
17+
OPENAI_API_TEMPERATURE = float(os.getenv("OPENAI_API_TEMPERATURE", "0.0"))
18+
OPENAI_API_MAX_TOKENS = int(os.getenv("OPENAI_API_MAX_TOKENS", "4096"))
1919

2020

2121
# MySQL database settings

app/stackademy.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from openai.types.chat import ChatCompletionFunctionToolParam
88
from pydantic import BaseModel, Field
99

10+
from app.const import MISSING
1011
from app.database import db
1112
from app.exceptions import ConfigurationException
1213
from app.logging_config import get_logger, setup_logging
@@ -182,6 +183,13 @@ def register_course(self, course_code: str, email: str, full_name: str) -> bool:
182183
Returns:
183184
bool: True if registration is successful, False otherwise
184185
"""
186+
if MISSING in (course_code, email, full_name):
187+
raise ConfigurationException("Missing required registration parameters.")
188+
189+
full_name = full_name.title().strip()
190+
email = email.lower().strip()
191+
course_code = course_code.upper().strip()
192+
185193
logger.info("Registering %s (%s) for course %s...", full_name, email, course_code)
186194
if not self.verify_course(course_code):
187195
logger.error("Course code %s does not exist.", course_code)

0 commit comments

Comments
 (0)