メインコンテンツまでスキップ

LLMを用いたWebスクレイパー

Version: 1
  • GenerativeAI
  • structured data
Download Canvas JSON
logo

LLMを活用してWebスクレイピングを行えます。対象のWebページから抽出したい情報・出力フォーマットを指定できます。

想定ユーザー

  • 複数のWebサイトから決まった情報を抽出し、表にリストアップしたいユーザ
  • SideAppとPipelineを組み合わせることで、疎結合なアプリケーションを作成したいユーザ

紹介動画

概要

テンプレート動作

  • Input

    • Webスクレイピングを行いたいURL (.json, 形式は利用方法を参照)

      • Webアプリの画面にて入力
    • LLMプロンプト (.txt)

      • Injection機能を用いてFunction内に記載
    • configファイル (.json)

      • Injection機能を用いてFunction内に記載
    • 環境変数

      環境変数内容備考
      OPEN_AI_ORGIDOpenAIの組織IDシークレットに設定
      OPEN_AI_API_KEYOpenAIのAPI Keyシークレットに設定
      CHAT_MODEL_NAMEChatGPTのモデル名gpt-4o-mini推奨
  • Output

    • 抽出済みの情報一覧
      • Webアプリの画面から確認
  • Function

    • 下記の処理を順番に実行
      • Webスクレイピング
      • コンテンツから情報を抽出(表示内容・更新日 etc..)
      • コンテンツのカテゴリ化(Energy, Materials, Industrials etc..)
      • コンテンツの日本語訳、要約

利用方法

単体での動作確認

デプロイ方法

  1. 下記のシークレットをCanvasに設定する。

    • OPEN_AI_ORGID
    • OPEN_AI_API_KEY
  2. デプロイボタンを押す。

実行方法

  1. Webアプリにアクセスし、左側のサイドバーから「app」を選択する。
    WebアプリURLは下記

    https://studio.{環境名}.studio.exabase.ai/{ネームスペース名}/ws/ui/
  2. 情報を抽出したいサイトのURLを、入力欄にJSON形式で記載する。
    フォーマットは下記

    {
    "urllist": [
    {
    "url": "xxxx1",
    "target": "yyyy1"
    },
    {
    "url": "xxxx2",
    "target": "yyyy2"
    }
    ]
    }

    もしくは、「サンプルデータを挿入」ボタンを押すことで、入力欄にサンプルのJSONが記載される。

  3. 「実行ボタン」を押す。

実行結果確認方法

  1. Webアプリの左側のサイドバーから「result」を選択する。

  2. 「解析結果を取得ボタン」を押す。

    • 必ず実行中のパイプライン動作が完了してから押してください。
    • もしくは、「サンプルの結果を読み込む」ボタンを押すことでも、結果画面にサンプルの結果が表示されます。
  3. 表示された結果のテーブルのセルをダブルクリックすると、隠れている長いテキストが表示される。

  4. テーブル左側のチェックボックスにて詳細表示したい情報を選択し、「詳細表示」ボタンを押す。

    • 各タイトルをクリックし展開すると原文と日本語訳をそれぞれ表示されます。

抽出内容の変更方法

抽出・表示する内容を変えたい場合、下記のファイルの値をそれぞれ変える必要があります。

  • Function

    • /app/config/config.json

      • 注) 各Functionで全く同じ記述になるようにしてください。
      • 変更する可能性が高いのは「input_list_keyname」〜「summary_text」あたりです。
    • /app/config/prompt.txt

      • 出力されるフォーマットをプロンプト内で定義しています。
  • SideApp

    • /app/src/pages/result.py
      • 下記定数内の値を必要に応じて変更(20行目〜40行目 あたりに記載)
        • COLUMN_NAME_LIST
        • COLUMN_NAME_DISPLAY_LIST
        • COLUMN_TITLE
        • COLUMN_CONTENT_JA
        • COLUMN_CONTENT_EN

例えば、入力・出力をそれぞれ以下のように変えたい場合を考える。

入力

{
"urls": [ <- キーの名前を変更
{
"url": "xxxx1",
<- 「target」を削除
},
{
"url": "xxxx2",
}
]
}

出力

{
"urls": [ <- キーの名前を変更
{
"url": "<https://ofsi.blog.gov.uk/2023/08/31/ofsi-uses-disclosure-power-for-first-time/>",
"title": "OFSI uses disclosure power for first time",
<- 「target」を削除
"main_content_english": "The critical role that financial ....", <- キーの名前を変更
"article_update_date": "2023/08/31",
"regulation_released_date": "2022/08/31",
"category": "Financials",
"main_content_japanese": "金融制裁がロシアのウクライナアクション...", <- キーの名前を変更
"summary_content_japanese": "- 英国は金融制裁を使って国家安全保障と金..." <- キーの名前を変更
},
{
"url": "<https://www.gov.uk/government/collections/enforcement-of-financial-sanctions>",
"title": "Enforcement of financial sanctions",
"main_content_english": "OFSI is responsible for...",
"article_update_date": "2023/08/31",
"regulation_released_date": "2019/02/25",
"category": "Financials",
"main_content_japanese": "OFSI(英国資産執行部)は、金融制裁のトラベレックスU...",
"summary_content_japanese": "- OFSIは金融制裁の遵守を監視し、疑わしい違反を..."
}
]
}

このとき、それぞれのfunctionに記載されているconfig.jsonと、function「構造化」のプロンプトは、以下のように書き換える必要がある。

/app/config/config.json

{
"input_list_keyname": "urls", <- valueを変更
"target_url": "url",
"scraped_text": "page_text",
"target_text": "main_content_english", <- valueを変更
"translate_text": "main_content_japanese", <- valueを変更
"summary_text": "summary_content_japanese", <- valueを変更
"retry_count": 3,
"timeout_sec": 600,
"paths": {
"input": "/app/data/input/input.json",
"output": "/app/data/output/output.json",
"prompts": {"structuralize": "/app/config/prompt.txt",
"categorize": "/app/config/prompt.txt",
"translate": "/app/config/translate_prompt.txt",
"summarize": "/app/config/summary_prompt.txt"}
}
}

/app/config/prompt.txt

Please extract the corresponding info from the following texts, and ouput it based on the provided output format.

# Texts (extracted from a specific Web site)
{scraped_text}

# Output Format
{{
"title" : "<Title of the article>",
"main_content_english" : "<The text information related to Compliance, Financial Sanctions and Implementation.>", <- キーの名前を変更
"article_update_date" : "<The date when the article was released or updated. YYYY/MM/DD>",
"regulation_released_date" : "<The date when the Compliance, Financial Sanctions and Implementation described in the article was notified. YYYY/MM/DD>"
}}

SideApp内の/app/src/pages/result.pyは以下のように書き換える。

## テーブルに表示する項目
COLUMN_NAME_LIST = ["title",
"category", <- 「target」を削除
"article_update_date",
"regulation_released_date",
"summary_content_japanese", <- 値を変更
"url",
"changelog",
]
## テーブルに表示する項目(日本語)
COLUMN_NAME_DISPLAY_LIST = ["表題",
"情報カテゴリ", <- 「対象HP」(targetに対応)を削除
"情報更新日",
"規制発行日",
"要約(日本語)",
"対象URL",
"従来からの変更点",
]
COLUMN_TITLE = "title"
COLUMN_CONTENT_JA = "main_content_japanese" <- 値を変更
COLUMN_CONTENT_EN = "main_content_english" <- 値を変更

他のアセットとの連携

本アセットは、単体での実行を想定しております。