diff --git a/bin/generate_data.sh b/bin/generate_data.sh index f876ec0..49e2b02 100755 --- a/bin/generate_data.sh +++ b/bin/generate_data.sh @@ -26,7 +26,7 @@ if [ "$MODEL" = "openrouter/auto" ] && [ "${OPENROUTER_ALLOW_AUTO:-}" != "1" ]; echo "WARN: openrouter/auto is unreliable for strict JSON; using openai/gpt-4o-mini. Set OPENROUTER_MODEL to a specific vision model, or OPENROUTER_ALLOW_AUTO=1 to force auto." >&2 MODEL="openai/gpt-4o-mini" fi -OPENROUTER_MAX_RETRIES="${OPENROUTER_MAX_RETRIES:-3}" +OPENROUTER_MAX_TOKENS="${OPENROUTER_MAX_TOKENS:-1024}" OPENROUTER_SITE_URL="${OPENROUTER_SITE_URL:-https://bubulescu.org}" OPENROUTER_SITE_NAME="${OPENROUTER_SITE_NAME:-MVLog}" @@ -218,11 +218,13 @@ Content rules: - Do not be subtle. - The voice MUST BE provocative, opinionated, funny, and slightly over the top. - Highlight the funniest, strangest, most beautiful, or most ridiculous visible aspect you see on the captured frames. +- The story may be strange, surreal, ridiculous, darkly funny, or chaotic, but avoid using the words "absurd", "absurdity", or "absurdities" in the teaser or description. Show it through concrete details instead of naming it. - Be challenging, provocative, witty, and playful. - Roasting is allowed. - Profanity is allowed only if it fits the selected personality. - Do not invent specific facts that are not visible or provided. - Description MUST contain at least one of the following words: subscribe, contact. +- Funny references to ice hockey and pizza napoletana are allways welcome JSON rules: - Output JSON only. @@ -260,7 +262,7 @@ RESPONSE_FILE="$WORKDIR/response.json" printf '%s' "$PROMPT" > "$PROMPT_FILE" printf '%s\n' "${FRAMES[@]}" > "$FRAMES_LIST" -python3 - "$PROMPT_FILE" "$REQUEST_FILE" "$FRAMES_LIST" "$MODEL" <<'PY' +python3 - "$PROMPT_FILE" "$REQUEST_FILE" "$FRAMES_LIST" "$MODEL" "$OPENROUTER_MAX_TOKENS" <<'PY' import sys import json import base64 @@ -269,6 +271,12 @@ prompt_file = sys.argv[1] request_file = sys.argv[2] frames_list = sys.argv[3] model = sys.argv[4] +try: + max_tokens = int(sys.argv[5]) +except Exception: + max_tokens = 1024 +if max_tokens < 1: + max_tokens = 1024 with open(prompt_file, "r", encoding="utf-8") as f: prompt = f.read() @@ -300,6 +308,7 @@ req = { } ], "temperature": 0.7, + "max_tokens": max_tokens, "response_format": { "type": "json_object" } @@ -315,76 +324,61 @@ OUT_TMP="$OUT_JSON.tmp.$$" DEBUG_RESPONSE_FILE="$IN_DIR/.mvlog-last-openrouter-response.json" DEBUG_TEXT_FILE="$IN_DIR/.mvlog-last-openrouter-text.txt" -if [[ ! "$OPENROUTER_MAX_RETRIES" =~ ^[0-9]+$ ]] || [ "$OPENROUTER_MAX_RETRIES" -lt 1 ]; then - OPENROUTER_MAX_RETRIES=3 +echo "Calling OpenRouter API..." >&2 +echo "DEBUG_MODEL=$MODEL" >&2 +echo "DEBUG_MAX_TOKENS=$OPENROUTER_MAX_TOKENS" >&2 +echo "DEBUG_PERSONALITY_INDEX=$idx" >&2 + +if ! curl -sS \ + "https://openrouter.ai/api/v1/chat/completions" \ + -H "Authorization: Bearer ${OPENROUTER_API_KEY}" \ + -H "Content-Type: application/json" \ + -H "HTTP-Referer: ${OPENROUTER_SITE_URL}" \ + -H "X-Title: ${OPENROUTER_SITE_NAME}" \ + -X POST \ + -d @"$REQUEST_FILE" \ + > "$RESPONSE_FILE"; then + echo "OpenRouter curl request failed" >&2 + cp "$RESPONSE_FILE" "$DEBUG_RESPONSE_FILE" 2>/dev/null || true + exit 1 fi -attempt=1 -while [ "$attempt" -le "$OPENROUTER_MAX_RETRIES" ]; do - echo "Calling OpenRouter API (attempt $attempt/$OPENROUTER_MAX_RETRIES)..." >&2 - echo "DEBUG_MODEL=$MODEL" >&2 - echo "DEBUG_PERSONALITY_INDEX=$idx" >&2 +cp "$RESPONSE_FILE" "$DEBUG_RESPONSE_FILE" 2>/dev/null || true +chown www-data:www-data "$DEBUG_RESPONSE_FILE" 2>/dev/null || true +chmod 664 "$DEBUG_RESPONSE_FILE" 2>/dev/null || true - if ! curl -sS \ - "https://openrouter.ai/api/v1/chat/completions" \ - -H "Authorization: Bearer ${OPENROUTER_API_KEY}" \ - -H "Content-Type: application/json" \ - -H "HTTP-Referer: ${OPENROUTER_SITE_URL}" \ - -H "X-Title: ${OPENROUTER_SITE_NAME}" \ - -X POST \ - -d @"$REQUEST_FILE" \ - > "$RESPONSE_FILE"; then - echo "OpenRouter curl request failed on attempt $attempt" >&2 - cp "$RESPONSE_FILE" "$DEBUG_RESPONSE_FILE" 2>/dev/null || true - attempt=$((attempt + 1)) - sleep 2 - continue +TEXT=$(jq -r '.choices[0].message.content // empty' "$RESPONSE_FILE" 2>/dev/null || true) + +if [ -z "$TEXT" ]; then + api_error=$(jq -r '.error.message // empty' "$RESPONSE_FILE" 2>/dev/null || true) + if [ -n "$api_error" ]; then + echo "OpenRouter returned no text: $api_error" >&2 + else + echo "OpenRouter returned no text. Full response:" >&2 + cat "$RESPONSE_FILE" >&2 fi + exit 1 +fi - cp "$RESPONSE_FILE" "$DEBUG_RESPONSE_FILE" 2>/dev/null || true - chown www-data:www-data "$DEBUG_RESPONSE_FILE" 2>/dev/null || true - chmod 664 "$DEBUG_RESPONSE_FILE" 2>/dev/null || true +printf '%s\n' "$TEXT" > "$DEBUG_TEXT_FILE" 2>/dev/null || true +chown www-data:www-data "$DEBUG_TEXT_FILE" 2>/dev/null || true +chmod 664 "$DEBUG_TEXT_FILE" 2>/dev/null || true - TEXT=$(jq -r '.choices[0].message.content // empty' "$RESPONSE_FILE" 2>/dev/null || true) - - if [ -z "$TEXT" ]; then - api_error=$(jq -r '.error.message // empty' "$RESPONSE_FILE" 2>/dev/null || true) - if [ -n "$api_error" ]; then - echo "OpenRouter returned no text on attempt $attempt: $api_error" >&2 - else - echo "OpenRouter returned no text on attempt $attempt. Full response:" >&2 - cat "$RESPONSE_FILE" >&2 - fi - attempt=$((attempt + 1)) - sleep 2 - continue +if printf '%s' "$TEXT" | jq -e 'type == "object" and (.personality | type == "string") and (.quote_da | type == "string") and (.teaser | type == "string") and (.description | type == "string") and (.tags | type == "array")' >/dev/null \ + && printf '%s' "$TEXT" | jq . > "$OUT_TMP"; then + mv "$OUT_TMP" "$OUT_JSON" + chown www-data:www-data "$OUT_JSON" 2>/dev/null || true + chmod 664 "$OUT_JSON" 2>/dev/null || true + if [ "$FORCED_PERSONALITY" != "1" ]; then + printf '%s\n' "$idx" > "$STATE_FILE" + chown www-data:www-data "$STATE_FILE" 2>/dev/null || true + chmod 664 "$STATE_FILE" 2>/dev/null || true fi - - printf '%s\n' "$TEXT" > "$DEBUG_TEXT_FILE" 2>/dev/null || true - chown www-data:www-data "$DEBUG_TEXT_FILE" 2>/dev/null || true - chmod 664 "$DEBUG_TEXT_FILE" 2>/dev/null || true - - if printf '%s' "$TEXT" | jq -e 'type == "object" and (.personality | type == "string") and (.quote_da | type == "string") and (.teaser | type == "string") and (.description | type == "string") and (.tags | type == "array")' >/dev/null \ - && printf '%s' "$TEXT" | jq . > "$OUT_TMP"; then - mv "$OUT_TMP" "$OUT_JSON" - chown www-data:www-data "$OUT_JSON" 2>/dev/null || true - chmod 664 "$OUT_JSON" 2>/dev/null || true - if [ "$FORCED_PERSONALITY" != "1" ]; then - printf '%s\n' "$idx" > "$STATE_FILE" - chown www-data:www-data "$STATE_FILE" 2>/dev/null || true - chmod 664 "$STATE_FILE" 2>/dev/null || true - fi - echo "OpenRouter JSON saved: $OUT_JSON" >&2 - exit 0 - fi - - rm -f "$OUT_TMP" - echo "OpenRouter returned invalid JSON on attempt $attempt. Raw text saved to $DEBUG_TEXT_FILE" >&2 - printf '%s\n' "$TEXT" >&2 - attempt=$((attempt + 1)) - sleep 2 -done + echo "OpenRouter JSON saved: $OUT_JSON" >&2 + exit 0 +fi rm -f "$OUT_TMP" -echo "OpenRouter did not return valid JSON after $OPENROUTER_MAX_RETRIES attempt(s). Last raw response: $DEBUG_RESPONSE_FILE; last text: $DEBUG_TEXT_FILE" >&2 +echo "OpenRouter returned invalid JSON. Raw text saved to $DEBUG_TEXT_FILE" >&2 +printf '%s\n' "$TEXT" >&2 exit 1