AIビューポイント生成

説明

AIビューポイント生成機能は、OpenAIの強力な言語モデルを活用して、一時ウィッシュリストカテゴリのカテゴリ詳細と特定のビューポイントを自動生成します。このモジュールは、特定のカテゴリで商品を分析する際に、ユーザーが商品の特性、強み、弱みを理解するのに役立つAI搭載の分析視点の作成を可能にします。生成されたビューポイントは3層階層で整理されます:カテゴリビューポイント、カテゴリビューポイント詳細、特定ビューポイント。

アクティビティ図

---
config:
  theme: base
  flowchart:
    curve: linear
    htmlLabels: true
  themeVariables:
    edgeLabelBackground: "transparent"
---
flowchart TD
    Start([ユーザーがビューポイントにアクセス])
    ViewpointTables[(ビューポイントテーブル)]
    OpenAI[(OpenAI API)]
    
    subgraph Controllers
        ViewpointController[TempViewpointController]
    end
    
    subgraph Services
        ViewpointService(TempViewpointService)
        OpenAIService(OpenAIService)
    end
    
    subgraph Models
        CategoryViewpointModel[[TempWishlistCategoryViewpoint]]
        ViewpointDetailModel[[TempWishlistCategoryViewpointDetail]]
        SpecificViewpointModel[[TempWishlistSpecificViewpoint]]
    end
    
    Start --> ViewpointController
    
    ViewpointController --- ListStep[
        <div style='text-align: center'>
            <span style='display: inline-block; background-color: #6699cc !important; color:white; width: 28px; height: 28px; line-height: 28px; border-radius: 50%; font-weight: bold'>1</span>
            <p style='margin-top: 8px'>ビューポイント一覧</p>
        </div>
    ]
    ListStep --> ViewpointService
    
    ViewpointController --- GenCategoryStep[
        <div style='text-align: center'>
            <span style='display: inline-block; background-color: #6699cc !important; color:white; width: 28px; height: 28px; line-height: 28px; border-radius: 50%; font-weight: bold'>2</span>
            <p style='margin-top: 8px'>カテゴリ詳細生成</p>
        </div>
    ]
    GenCategoryStep --> ViewpointService
    
    ViewpointController --- GenSpecificStep[
        <div style='text-align: center'>
            <span style='display: inline-block; background-color: #6699cc !important; color:white; width: 28px; height: 28px; line-height: 28px; border-radius: 50%; font-weight: bold'>3</span>
            <p style='margin-top: 8px'>特定ビューポイント生成</p>
        </div>
    ]
    GenSpecificStep --> ViewpointService
    
    ViewpointService --> CategoryViewpointModel
    ViewpointService --> ViewpointDetailModel
    ViewpointService --> SpecificViewpointModel
    ViewpointService --> OpenAIService
    OpenAIService --> OpenAI
    
    CategoryViewpointModel --> ViewpointTables
    ViewpointDetailModel --> ViewpointTables
    SpecificViewpointModel --> ViewpointTables
    
    %% Styling
    style ViewpointController fill:#e6f3ff,stroke:#0066cc,stroke-width:2px
    style ViewpointService fill:#f0f8e6,stroke:#339933,stroke-width:2px
    style OpenAIService fill:#f0f8e6,stroke:#339933,stroke-width:2px
    style CategoryViewpointModel fill:#fff0f5,stroke:#cc6699,stroke-width:2px
    style ViewpointDetailModel fill:#fff0f5,stroke:#cc6699,stroke-width:2px
    style SpecificViewpointModel fill:#fff0f5,stroke:#cc6699,stroke-width:2px
    style ViewpointTables fill:#ffe6cc,stroke:#ff9900,stroke-width:2px
    style OpenAI fill:#ffe6cc,stroke:#ff9900,stroke-width:2px
    style ListStep fill:transparent,stroke:transparent,stroke-width:1px
    style GenCategoryStep fill:transparent,stroke:transparent,stroke-width:1px
    style GenSpecificStep fill:transparent,stroke:transparent,stroke-width:1px

詳細データフロー依存関係

ステップバイステッププロセス

ステップ1: カテゴリビューポイント取得

  • 説明: システムがユーザーまたはグループの既存カテゴリビューポイントを取得
  • アクション: ユーザーまたはグループに関連付けられたカテゴリビューポイントをデータベースからクエリ
  • 入力: ユーザーIDまたはグループID
  • 出力: 詳細を含むカテゴリビューポイントのリスト
  • 依存関係: ユーザーが認証され、有効なグループメンバーシップを持つ必要がある

ステップ2: カテゴリ詳細生成

  • 説明: システムがOpenAIを使用してカテゴリの詳細説明を生成
  • アクション: 特定のプロンプトテンプレートでカテゴリ名をOpenAI APIに送信
  • 入力: カテゴリ名とモールID
  • 出力: AI生成カテゴリ詳細テキスト
  • 依存関係: OpenAI API統合、有効なAPI認証情報、適切に設定されたプロンプトテンプレート
  • 外部サービス: OpenAI API

ステップ3: カテゴリビューポイント保存

  • 説明: システムが生成されたカテゴリ詳細をデータベースに保存
  • アクション: temp_wl_cat_vpsテーブルにレコードを挿入または更新
  • 入力: カテゴリID、ユーザーID、グループID、生成されたカテゴリ詳細
  • 出力: 保存されたカテゴリビューポイントレコード
  • 依存関係: 有効なデータベース接続、適切な認可

ステップ4: 特定ビューポイント生成

  • 説明: システムがOpenAIを使用してカテゴリの特定ビューポイントを生成
  • アクション: カテゴリ名、カテゴリ詳細、要求数量をOpenAI APIに送信
  • 入力: カテゴリ名、カテゴリ詳細、生成するビューポイント数量
  • 出力: 名前と説明を含むAI生成特定ビューポイントの配列
  • 依存関係: OpenAI API統合、有効なAPI認証情報、適切に設定されたプロンプトテンプレート
  • 外部サービス: OpenAI API

ステップ5: 特定ビューポイント保存

  • 説明: システムが生成された特定ビューポイントをデータベースに保存
  • アクション: temp_wl_spec_vpsテーブルにレコードを挿入
  • 入力: カテゴリビューポイントID、特定ビューポイントの配列
  • 出力: 保存された特定ビューポイントレコード
  • 依存関係: カテゴリビューポイントがデータベースに存在する必要がある

ステップ6: エラーハンドリングと再試行

  • 説明: システムがOpenAI APIエラーを処理し、再試行メカニズムを実装
  • アクション: APIエラーを検出し、詳細をログに記録し、指数バックオフで再試行
  • 入力: APIエラーレスポンス
  • 出力: 再試行後の成功または最終エラーメッセージ
  • 依存関係: 適切なエラー検出とログインフラストラクチャ
  • 外部サービス: ログサービス

データベース関連テーブル・フィールド

データベース: gb_console

erDiagram
    temp_wl_cat_vps {
        bigint id PK
        bigint temp_wishlist_to_group_id FK
        bigint user_id FK
        bigint group_id FK
        string name
        string slug
    }
    
    temp_wl_cat_vp_details {
        bigint id PK
        bigint temp_wl_cat_vp_id FK
        text detail
    }
    
    temp_wl_spec_vps {
        bigint id PK
        bigint temp_wl_cat_vp_detail_id FK
        string name
        text description
    }

    temp_wl_cat_vps ||--o{ temp_wl_cat_vp_details : "持つ"
    temp_wl_cat_vp_details ||--o{ temp_wl_spec_vps : "持つ"

ケースドキュメント

ケース1: ユーザー別カテゴリビューポイント取得

API: ユーザー別カテゴリビューポイント取得

シーケンス図

sequenceDiagram
    participant Client
    participant Controller as TempViewpointController
    participant Service as TempViewpointService
    participant Repository as TempWishlistCategoryViewpointRepository
    participant Database
    
    rect rgb(200, 255, 200)
    Note right of Client: ハッピーケース - ユーザービューポイント取得
    
    Client->>Controller: GET /api/v1/temp-viewpoint/category
    Controller->>Service: getCategoryViewpointsByUser()
    
    Service->>Repository: findWhere(user_id: currentUser.id)
    Repository->>Database: Query WHERE user_id = ?
    Database-->>Repository: ビューポイントを返却
    Repository-->>Service: ビューポイントコレクションを返却
    
    Service->>Service: ビューポイントデータをフォーマット
    Service-->>Controller: フォーマットされたビューポイントを返却
    Controller-->>Client: ビューポイント付きJSONレスポンスを返却
    end
    
    rect rgb(255, 200, 200)
    Note right of Client: エラーハンドリング
    
    rect rgb(255, 230, 230)
    alt 認証エラー
        Client->>Controller: GET /api/v1/temp-viewpoint/category (認証なし)
        Controller-->>Client: 401 Unauthorizedを返却
    end
    end
    end

ステップ

  1. クライアントが現在のユーザーのカテゴリビューポイントを取得するGETリクエストを送信
  2. コントローラーがリクエストを受信し、サービス層を呼び出し
  3. サービス層が現在のユーザーIDを使用してリポジトリからビューポイントを取得
  4. リポジトリがデータベースをクエリし、ビューポイントコレクションを返却
  5. ビューポイントがフォーマットされ、JSONレスポンスとしてクライアントに返却

エラーハンドリング

  • ユーザーが認証されていない場合、401 Unauthorizedエラーが返却される
  • システムエラーが発生した場合、エラーメッセージ付きの500 Internal Server Errorが返却される

ケース2: グループ別カテゴリビューポイント取得

API: グループ別カテゴリビューポイント取得

シーケンス図

sequenceDiagram
    participant Client
    participant Controller as TempViewpointController
    participant Service as TempViewpointService
    participant Repository as TempWishlistCategoryViewpointRepository
    participant Database
    
    rect rgb(200, 255, 200)
    Note right of Client: ハッピーケース - グループビューポイント取得
    
    Client->>Controller: GET /api/v1/temp-viewpoint/category/group
    Controller->>Service: getCategoryViewpointsByGroup()
    
    Service->>Service: 現在のユーザーのgroup_idを取得
    Service->>Repository: findWhere(group_id: currentUser.group_id)
    Repository->>Database: Query WHERE group_id = ?
    Database-->>Repository: ビューポイントを返却
    Repository-->>Service: ビューポイントコレクションを返却
    
    Service->>Service: ビューポイントデータをフォーマット
    Service-->>Controller: フォーマットされたビューポイントを返却
    Controller-->>Client: ビューポイント付きJSONレスポンスを返却
    end
    
    rect rgb(255, 200, 200)
    Note right of Client: エラーハンドリング
    
    rect rgb(255, 230, 230)
    alt グループメンバーシップなし
        Client->>Controller: GET /api/v1/temp-viewpoint/category/group
        Controller->>Service: getCategoryViewpointsByGroup()
        Service->>Service: 現在のユーザーのgroup_idを取得
        Service-->>Controller: グループなしエラーを返却
        Controller-->>Client: 400 Bad Requestを返却
    end
    end
    end

ステップ

  1. クライアントがユーザーのグループのカテゴリビューポイントを取得するGETリクエストを送信
  2. コントローラーがリクエストを受信し、サービス層を呼び出し
  3. サービス層が現在のユーザーのグループIDを取得
  4. サービスがグループIDを使用してリポジトリからビューポイントを取得
  5. リポジトリがデータベースをクエリし、ビューポイントコレクションを返却
  6. ビューポイントがフォーマットされ、JSONレスポンスとしてクライアントに返却

エラーハンドリング

  • ユーザーがグループメンバーシップを持たない場合、400 Bad Requestエラーが返却される
  • ユーザーが認証されていない場合、401 Unauthorizedエラーが返却される
  • システムエラーが発生した場合、エラーメッセージ付きの500 Internal Server Errorが返却される

ケース3: カテゴリ詳細生成

API: カテゴリ詳細生成

シーケンス図

sequenceDiagram
    participant Client
    participant Controller as TempViewpointController
    participant Service as TempViewpointService
    participant OpenAIService
    participant OpenAI as OpenAI API
    participant Repository as TempWishlistCategoryViewpointRepository
    participant Database
    
    rect rgb(200, 255, 200)
    Note right of Client: ハッピーケース - カテゴリ詳細生成
    
    Client->>Controller: POST /api/v1/temp-viewpoint/category/generate
    Controller->>Service: generateCategoryDetail(categoryName, mallId)
    
    Service->>OpenAIService: generateCategoryDetail(categoryName, promptTemplate)
    OpenAIService->>OpenAI: カテゴリ名でプロンプト送信
    OpenAI-->>OpenAIService: 生成されたカテゴリ詳細を返却
    OpenAIService-->>Service: 生成されたカテゴリ詳細を返却
    
    Service->>Repository: createOrUpdate(data)
    Repository->>Database: INSERT/UPDATE ビューポイント
    Database-->>Repository: 作成/更新を確認
    Repository-->>Service: ビューポイントを返却
    
    Service-->>Controller: カテゴリ詳細付きビューポイントを返却
    Controller-->>Client: ビューポイント付きJSONレスポンスを返却
    end
    
    rect rgb(255, 200, 200)
    Note right of Client: エラーハンドリング

    rect rgb(255, 230, 230)
    alt OpenAI APIエラー
        Client->>Controller: POST /api/v1/temp-viewpoint/category/generate
        Controller->>Service: generateCategoryDetail(categoryName, mallId)
        Service->>OpenAIService: generateCategoryDetail(categoryName, promptTemplate)
        OpenAIService->>OpenAI: カテゴリ名でプロンプト送信
        OpenAI-->>OpenAIService: APIエラーを返却
        OpenAIService-->>Service: エラーを返却
        Service-->>Controller: APIエラーを返却
        Controller-->>Client: 500 Internal Server Errorを返却
    end
    end
    end

ステップ

  1. クライアントがカテゴリ名とモールIDを含むPOSTリクエストを送信
  2. コントローラーがリクエストを受信し、サービス層を呼び出し
  3. サービスがOpenAIサービスを呼び出してカテゴリ詳細を生成
  4. OpenAIサービスがOpenAI APIにプロンプトを送信し、生成されたテキストを受信
  5. サービスがカテゴリビューポイントをデータベースに保存
  6. ビューポイントデータ付きの成功レスポンスがクライアントに返却される

エラーハンドリング

  • OpenAI APIがエラーを返す場合、500 Internal Server Errorが返却される
  • カテゴリ名が無効または欠落している場合、400 Bad Requestエラーが返却される
  • システムエラーが発生した場合、エラーメッセージ付きの500 Internal Server Errorが返却される

ケース4: 特定ビューポイント生成

API: 特定ビューポイント生成

シーケンス図

sequenceDiagram
    participant Client
    participant Controller as TempViewpointController
    participant Service as TempViewpointService
    participant OpenAIService
    participant OpenAI as OpenAI API
    participant Model as TempWishlistSpecificViewpoint
    participant Database
    
    Note over Client,Database: POST /api/v1/temp-viewpoint/specific-viewpoint/generate
    
    rect rgb(200, 255, 200)
    Note right of Client: ハッピーケース - 特定ビューポイント生成
    
    Client->>Controller: POST /specific-viewpoint/generate (categoryViewpointDetailId, quantity)
    
    rect rgb(200, 230, 255)
    Note right of Controller: 入力検証
    Controller->>Controller: 入力データを検証
    end
    
    rect rgb(200, 255, 255)
    Note right of Controller: ビジネスロジック処理
    Controller->>Service: generateSpecificViewpoints(categoryViewpointDetailId, quantity)
    Service->>Service: カテゴリビューポイント詳細を取得
    end
    
    rect rgb(255, 230, 200)
    Note right of Service: 外部API呼び出し
    Service->>OpenAIService: generateSpecificViewpoints(categoryName, categoryDetail, quantity)
    OpenAIService->>OpenAI: カテゴリ情報と数量でプロンプト送信
    OpenAI-->>OpenAIService: 生成されたビューポイントの配列を返却
    OpenAIService-->>Service: 生成されたビューポイント配列を返却
    end
    
    rect rgb(230, 200, 255)
    Note right of Service: データベース操作
    Service->>Model: createMany(viewpoints)
    Model->>Database: 複数の特定ビューポイントをINSERT
    Database-->>Model: 作成を確認
    Model-->>Service: 作成されたビューポイントを返却
    end
    
    Service-->>Controller: 特定ビューポイントコレクションを返却
    Controller-->>Client: 201 Created (ビューポイント)
    end
    
    rect rgb(255, 200, 200)
    Note right of Client: エラーハンドリング
    
    alt カテゴリビューポイント詳細が見つからない
        rect rgb(255, 230, 230)
        Controller->>Service: generateSpecificViewpoints(invalidId, quantity)
        Service->>Service: カテゴリビューポイント詳細を取得
        Service-->>Controller: 見つからないエラー
        Controller-->>Client: 404 Not Found
        end
    else 検証エラー
        rect rgb(255, 230, 230)
        Controller->>Controller: 入力データを検証
        Controller-->>Client: 422 Unprocessable Entity
        end
    else OpenAI APIエラー
        rect rgb(255, 230, 230)
        OpenAIService->>OpenAI: プロンプト送信
        OpenAI-->>OpenAIService: APIエラーレスポンス
        OpenAIService-->>Service: 外部APIエラー
        Service-->>Controller: APIエラー結果
        Controller-->>Client: 502 Bad Gateway
        end
    else データベースエラー
        rect rgb(255, 230, 230)
        Model->>Database: 複数の特定ビューポイントをINSERT
        Database-->>Model: データベースエラー
        Model-->>Service: データベースエラー
        Service-->>Controller: ストレージエラー
        Controller-->>Client: 500 Internal Server Error
        end
    end
    end

ステップ

  1. クライアントがカテゴリビューポイント詳細IDと生成するビューポイント数量を含むPOSTリクエストを送信
  2. コントローラーがリクエストを受信し、サービス層を呼び出し
  3. サービスがカテゴリビューポイント詳細を取得してカテゴリ名と説明を取得
  4. サービスがカテゴリ情報に基づいてOpenAIサービスを呼び出して特定ビューポイントを生成
  5. OpenAIサービスがOpenAI APIにプロンプトを送信し、生成されたビューポイントの配列を受信
  6. サービスが生成されたすべての特定ビューポイントをデータベースに保存
  7. ビューポイントコレクション付きの成功レスポンスがクライアントに返却される

エラーハンドリング

  • カテゴリビューポイント詳細が見つからない場合、404 Not Foundエラーが返却される
  • OpenAI APIがエラーを返す場合、500 Internal Server Errorが返却される
  • 数量パラメータが無効または欠落している場合、400 Bad Requestエラーが返却される
  • システムエラーが発生した場合、エラーメッセージ付きの500 Internal Server Errorが返却される
  • ユーザーがカテゴリのビューポイントを生成する権限がない場合、403 Forbiddenエラーが返却される