app:
  description: サプライヤ評価コメントや監査記録を統合して、総合スコアと改善点を提示するワークフローです。評価・コメント・監査メモを1つの入力欄（query）にまとめて入力してください。
  icon: 🧾
  icon_background: '#FFEAD5'
  mode: workflow
  name: 食品 原料サプライヤ評価サマリ
  use_icon_as_answer_icon: false
kind: app
version: 0.6.0
workflow:
  conversation_variables: []
  environment_variables: []
  features:
    file_upload:
      image:
        enabled: false
    opening_statement: ''
    suggested_questions: []
    suggested_questions_after_answer:
      enabled: false
    text_to_speech:
      enabled: false
  graph:
    edges:
    - data:
        sourceType: start
        targetType: llm
      id: start-to-llm1
      source: start
      target: llm1
    - data:
        sourceType: llm
        targetType: code
      id: llm1-to-code1
      source: llm1
      target: code1
    - data:
        sourceType: code
        targetType: llm
      id: code1-to-llm2
      source: code1
      target: llm2
    - data:
        sourceType: llm
        targetType: end
      id: llm2-to-end
      source: llm2
      target: end
    nodes:
    - data:
        title: 開始
        type: start
        variables:
        - label: query
          max_length: 2048
          options: []
          required: true
          type: paragraph
          variable: query
      height: 90
      id: start
      position:
        x: 80
        y: 282
      type: custom
      width: 244
    - data:
        title: 情報構造化（JSON出力）
        type: llm
        model:
          provider: openai
          name: gpt-5-mini
          mode: chat
          completion_params:
            temperature: 0.2
        prompt_template:
        - role: system
          text: 'あなたは食品原料サプライヤの評価データを構造化するアシスタントです。出力は必ず有効なJSONのみを返してください。JSONは次のキーを含めてください:
            supplier_name (string)、ratings (配列、数値、任意)、comments (配列の文字列)、audit_findings
            (配列の文字列)。余計な説明や注釈は書かないでください。'
        - role: user
          text: '入力テキストを解析して、上記のJSON形式で出力してください。


            入力:

            {{#start.query#}}'
        vision:
          enabled: false
        memory: null
        context:
          enabled: false
      height: 90
      id: llm1
      position:
        x: 380
        y: 282
      type: custom
      width: 244
    - data:
        title: スコア計算と改善候補抽出
        type: code
        code_language: python3
        code: "def _safe_str(v):\n    \"\"\"Convert any value to string safely.\"\"\
          \"\n    if v is None:\n        return \"\"\n    if isinstance(v, list):\n\
          \        return \"\\n\".join(str(i) for i in v)\n    return str(v)\n\ndef\
          \ _safe_list(v):\n    \"\"\"Ensure value is a list.\"\"\"\n    if v is None:\n\
          \        return []\n    if isinstance(v, str):\n        return [v]\n   \
          \ if isinstance(v, list):\n        return v\n    return [v]\n\ndef main(**kwargs)\
          \ -> dict:\n    inputs = kwargs\n    import json\n    import re\n    text\
          \ = inputs.get(\"text\", \"\")\n    supplier_name = \"\"\n    ratings =\
          \ []\n    comments = []\n    audit_findings = []\n    # Try parse JSON produced\
          \ by LLM\n    parsed = None\n    try:\n        parsed = json.loads(text)\n\
          \    except Exception:\n        parsed = None\n    if isinstance(parsed,\
          \ dict):\n        supplier_name = parsed.get(\"supplier_name\", \"\") or\
          \ \"\"\n        ratings = parsed.get(\"ratings\", []) or []\n        comments\
          \ = parsed.get(\"comments\", []) or []\n        audit_findings = parsed.get(\"\
          audit_findings\", []) or []\n    else:\n        # Fallback heuristics\n\
          \        m = re.search(r\"Supplier[:：]\\\\s*(.+)\", text)\n        if m:\n\
          \            supplier_name = m.group(1).strip()\n        # find numeric\
          \ tokens that look like ratings (0-100)\n        num_tokens = re.findall(r\"\
          \\\\b([0-9]{1,3}(?:\\\\.[0-9]+)?)\\\\b\", text)\n        for t in num_tokens:\n\
          \            try:\n                v = float(t)\n                if 0 <=\
          \ v <= 100:\n                    ratings.append(v)\n            except Exception:\n\
          \                continue\n        # comments: split sentences\n       \
          \ parts = [s.strip() for s in re.split(r\"[\\\\.。]\\\\s*\", text) if s.strip()]\n\
          \        comments = parts\n        audit_findings = []\n    # Compute overall\
          \ score\n    score = None\n    if ratings:\n        try:\n            score\
          \ = sum(ratings) / max(1, len(ratings))\n        except Exception:\n   \
          \         score = None\n    if score is None:\n        # heuristic scoring\
          \ based on keywords\n        low_keywords = [\"major nonconformance\", \"\
          critical\", \"重大\", \"不適合\", \"是正\"]\n        lower_text = text.lower()\n\
          \        base = 85.0\n        penalty = 0.0\n        for kw in low_keywords:\n\
          \            if kw in lower_text:\n                penalty += 25.0\n   \
          \     score = base - penalty\n    if score < 0:\n        score = 0.0\n \
          \   if score > 100:\n        score = 100.0\n    # Prepare improvement candidates\
          \ (top up to 5 short items)\n    candidates = []\n    combined = []\n  \
          \  combined.extend(audit_findings)\n    combined.extend(comments)\n    for\
          \ c in combined:\n        if c and len(candidates) < 5:\n            s =\
          \ str(c).strip()\n            if s:\n                candidates.append(s)\n\
          \    # Short summary text\n    summary_parts = []\n    if supplier_name:\n\
          \        summary_parts.append(\"サプライヤ: \" + supplier_name)\n    summary_parts.append(\"\
          推定総合スコア: \" + str(round(score, 1)))\n    if candidates:\n        summary_parts.append(\"\
          改善候補: \" + \"; \".join(candidates[:3]))\n    summary = \" | \".join(summary_parts)\n\
          \    result = dict(\n        supplier_name=supplier_name,\n        ratings=ratings,\n\
          \        comments=comments,\n        audit_findings=audit_findings,\n  \
          \      overall_score=round(score, 1),\n        improvement_candidates=candidates,\n\
          \        summary=summary\n    )\n    return dict(output=result)\n"
        variables:
        - value_selector:
          - llm1
          - text
          variable: text
        outputs:
          output:
            type: object
            children: null
      height: 90
      id: code1
      position:
        x: 680
        y: 282
      type: custom
      width: 244
    - data:
        title: 改善提案生成
        type: llm
        model:
          provider: openai
          name: gpt-5-mini
          mode: chat
          completion_params:
            temperature: 0.6
        prompt_template:
        - role: system
          text: あなたは食品安全とサプライヤ品質改善の専門家です。提供された解析結果を基に、具体的で実行可能な改善提案（アクション）、その理由、優先度（高/中/低）を含むJSONを出力してください。さらに100文字以内の要約も含めてください。余計な説明はせず有効なJSONのみを返してください。
        - role: user
          text: "解析結果:\n{{#code1.output#}}\n\n出力例:\n{\n  \"suggestions\": [\n    {\"\
            action\": \"改善策の例\", \"reason\": \"理由\", \"priority\": \"高\"}\n  ],\n\
            \  \"summary\": \"短い要約\"\n}\n\n上記フォーマットでJSONを返してください。"
        vision:
          enabled: false
        memory: null
        context:
          enabled: false
      height: 90
      id: llm2
      position:
        x: 980
        y: 282
      type: custom
      width: 244
    - data:
        title: 終了
        type: end
        outputs:
        - value_selector:
          - llm2
          - text
          variable: result
      id: end
      position:
        x: 1280
        y: 282
      type: custom
      height: 90
      width: 244
  hash: null
