Files
mealie/mealie/schema/openai/_base.py
Michael Genson 8a15f400e1 feat: Import + Translate recipe images with OpenAI (#3974)
Co-authored-by: Johan Lindell <johan@lindell.me>
Co-authored-by: boc-the-git <3479092+boc-the-git@users.noreply.github.com>
2024-08-18 08:07:01 +10:00

28 lines
793 B
Python

from pydantic import BaseModel
from mealie.core.root_logger import get_logger
logger = get_logger()
class OpenAIBase(BaseModel):
"""
This class defines the JSON schema sent to OpenAI. Its schema is
injected directly into the OpenAI prompt.
"""
__doc__ = "" # we don't want to include the docstring in the JSON schema
@classmethod
def parse_openai_response(cls, response: str | None):
"""
This method should be implemented in the child class. It should
parse the JSON response from OpenAI and return a dictionary.
"""
try:
return cls.model_validate_json(response or "")
except Exception:
logger.debug(f"Failed to parse OpenAI response as {cls}. Response: {response}")
raise