Add phase 4: AI cleanup and photo/text import via OpenAI

- recipes/ai.py: Chat Completions with strict JSON schema (Structured
  Outputs), Pydantic re-validation, plausibility clamping, prompt treats
  input as data only
- "Mit KI aufbereiten" on create/edit form returns an editable draft
- Import page: photo/screenshot (vision, downscaled JPEG, not stored) and
  pasted text; drafts pre-fill the recipe form with a preview
- Clear UI messages for timeout, rate limit, quota, auth, model and
  connection errors; without OPENAI_API_KEY AI features show a notice
- Optional per-user and global daily AI limits (default unlimited)
- Tests for response parsing, schema strictness, error mapping, views

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
Kyle Dammann
2026-10-30 21:43:30 +01:00
co-authored by Claude Opus 5.5
parent 9256e94286
commit 1bf8ef8b65
17 changed files with 856 additions and 13 deletions
+11
View File
@@ -92,6 +92,7 @@ TEMPLATES = [
"django.template.context_processors.request",
"django.contrib.auth.context_processors.auth",
"django.contrib.messages.context_processors.messages",
"recipes.context_processors.ai_status",
],
},
},
@@ -142,6 +143,16 @@ COMMENT_RATE_LIMIT_PER_HOUR = env_int("COMMENT_RATE_LIMIT_PER_HOUR", 30)
# Request body limit for non-file fields; files are limited per image above.
DATA_UPLOAD_MAX_MEMORY_SIZE = 5 * 1024 * 1024
# --- AI (OpenAI) -------------------------------------------------------------------
OPENAI_API_KEY = env("OPENAI_API_KEY")
OPENAI_MODEL = env("OPENAI_MODEL", "gpt-5-mini")
# For reasoning models (gpt-5*, o*): minimal|low|medium|high. Leave empty for other models.
OPENAI_REASONING_EFFORT = env("OPENAI_REASONING_EFFORT", "low")
OPENAI_TIMEOUT = env_int("OPENAI_TIMEOUT", 90)
# 0 = unlimited (default). Counted per call, including failed ones.
AI_RATE_LIMIT_PER_USER_PER_DAY = env_int("AI_RATE_LIMIT_PER_USER_PER_DAY", 0)
AI_RATE_LIMIT_GLOBAL_PER_DAY = env_int("AI_RATE_LIMIT_GLOBAL_PER_DAY", 0)
INVITATION_VALID_DAYS = 7
PASSWORD_RESET_VALID_HOURS = 24