Upgrade to Pro — share decks privately, control downloads, hide ads and more …

LLMと大規模RAGによるAIサービス開発—本番運用に耐える基盤設計とサービス間連携手法

Sadayuki Furuhashi
April 24, 2024
440

 LLMと大規模RAGによるAIサービス開発—本番運用に耐える基盤設計とサービス間連携手法

At Treasure Data Tech Talk Apr 24 2024
https://techplay.jp/event/938776

Sadayuki Furuhashi

April 24, 2024
Tweet

Transcript

  1. Sadayuki Furuhashi Treasure Data, Chief Architect • 2011年渡米 • Treasure

    Data 共同創業メンバー • 大規模分散DB PlazmaDB 他、Fluentd等を設計・開発 • 2023年〜 東京在住
  2. RAGと ? Retrieval-Augmented Generation - ドメイン知識・最新知識を考慮した回答を生成 プロンプト LLM 回答 LLM

    学習結果 検索エンジン 検索結果を使ってLLMへ 入 力を拡張 検索クエリ
  3. 従来 検索(キーワード検索) 問題 • 質問に検索キーワードが含まれない • 質問者 検索キーワードを考えない • キーワード

    出現頻度によるランキング手法が使えない “結婚を前に指輪を買いに来たお客様。 価格 40万円以下。お勧めするべき商品 ? ” “5日以内に購買記録 ある F1層をターゲット としたセグメントを作成して ” 質問 回答に使用したい知識 商品名 カテゴリ 価格 商品説明 トゥルー エンゲージメント リン グ ダイヤモンド プラチナ リング 350,000 … トゥルー マリッジ リング ダイ ヤモンド ホワイトゴールド リング 410,000 … 顧客ID 性別 生年月日 123 女性 1999 456 女性 2001 顧客ID 購買日時 123 女性 123 女性
  4. ベクトル類似度検索 - 1/2 “トゥルー エンゲージメント リング ダイヤモンド プラチナ” 文章: “トゥ”

    “ルー ” “エン” “ゲージ” “メン” “ト ” “リン” … 1. トークナイズ [0.61, 0.48, 0.08] [0.71, 0.65, 0.24] [0.51, 0.60, 0.93] [0.82, 0.02, 0.27] [0.88, 0.08, 0.35] [0.24, 0.64, 0.64] [0.76, 0.00, 0.62] … LLMから意味を表す ベクトルを抽出 [0.02, 0.09, 0.73] 3. 集約(pooling) 2. 手順:
  5. ベクトル類似度検索 - 2/2 “トゥルー エンゲージメント リング ダイヤモンド プラチナ” “結婚を前に指輪を買いに来た お客様”

    文章: “トゥ” “ルー ” “エン” “ゲージ” “メン” “ト ” “リン” … “結婚” “を前” “に” “指” “輪” “を” … 1. トークナイズ [0.61, 0.48, 0.08] [0.71, 0.65, 0.24] [0.51, 0.60, 0.93] [0.82, 0.02, 0.27] [0.88, 0.08, 0.35] [0.24, 0.64, 0.64] [0.76, 0.00, 0.62] … [0.99, 0.31, 0.99] [0.10, 0.19, 0.92] [0.17, 0.23, 0.78] [0.87, 0.62, 0.87] [0.06, 0.71, 0.71] [0.87, 0.64, 0.44] … LLMから意味を表す ベクトルを抽出 [0.02, 0.09, 0.73] [0.07, 0.11, 0.64] クエリ: コサイン類似度を計 算 スコア:0.995 2. 4. 手順: 3. 集約(pooling)
  6. ベクトル類似度検索 実装例 • トークナイズとベクトル化(ステップ1〜3) a. OpenAI等 Embeddings APIを使用 b. 推論ランタイムを使ってローカル実行(MTEB等

    ベンチマークを参考にオープンモデルを選択) ▪ ONNX Runtime ▪ HuggingFace Transformers, Optimum, text-embeddings-inference • 検索(ステップ4以降) a. 検索エンジン ANN (Approximate Nearest Neighbors) インデックスを使用 ▪ Elasticsearch ▪ Azure Cognitive Search b. ベクトル検索ライブラリを使用 ▪ Voyager, Qdrant, Weaviate ▪ USearch, ScaNN, FAISS c. SQLで実装 ▪ DuckDB (list_cosine_similarity) ▪ BigQuery (ML.DISTANCE)
  7. LLMと 通信プロトコル - 1/3 Human: What's … Assistant: Certainly! …

    LLM input LLM output Generate System prompt User prompt LLM output Instruction to the LLM. How should the LLM work? User question. What should the LLM do? System: You're …
  8. LLMと 通信プロトコル - 2/3 (multi-turn) Human: What's … Assistant: Certainly!

    … LLM output Human: Continue Assistant: And it is … Generate LLM input System: You're … User prompt LLM output System prompt User prompt LLM output
  9. LLMと 通信プロトコル - 3/3 (multi-turn) System: You're … Human: What's

    … Assistant: Certainly! … LLM output Human: Continue Assistant: And it is … Human: Good Assistant: Thank you! LLM input Generate User prompt LLM output User prompt LLM output System prompt User prompt LLM output
  10. LLM コスト構造 System: You're … Human: What's … Assistant: Certainly!

    … LLM output Human: Continue Assistant: And it is … Human: Good Assistant: Thank you! LLM input Generate Max 128k input tokens $10.00 / 1M tokens (GPT-4 Turbo) Max 4k output tokens $30.00 / 1M tokens (GPT-4 Turbo) User prompt LLM output User prompt LLM output System prompt User prompt LLM output
  11. Function Calling - 1/3 System: Use [invoke $NAME] $PARAMETERS [/invoke]

    syntax to call a function. Available functions: retrieve_products(keywords: string) Human: Show me some recommended products Assistant: [invoke retrieve_products] recommend products [/invoke] Parse LLM output and invoke a function > retrieve_products("recommended products")
  12. Function Calling - 2/3 System: Use [invoke $NAME] $PARAMETERS [/invoke]

    syntax to call a function. Available functions: retrieve_products(keywords: string) Human: Show me some recommended products Assistant: [invoke retrieve_products] recommend products [/invoke] Tool: [result retrieve_products] Fluentd, MessagePack, Embulk [/result] Assistant: We recommend Fluentd, MessagePack, and Embulk. Enjoy! > retrieve_products("recommended products") < { "Fluentd": 0.93, "MessagePack": 0.43, "Embulk": 0.39 } Parse LLM output - show the contents because output doesn't contain function calls
  13. Function Calling - 3/3 System: Use [invoke $NAME] $PARAMETERS [/invoke]

    syntax to call a function. Available functions: retrieve_products(keywords: string) Human: Show me some recommended products Assistant: [invoke retrieve_products] recommend products [/invoke] Tool: [result retrieve_products] Fluentd, MessagePack, Embulk [/result] Assistant: We recommend Fluentd, MessagePack, and Embulk. Enjoy! Human: Thank you Assistant: You're welcome. Ask me more questions if you have > retrieve_products("recommended products") < { "Fluentd": 0.93, "MessagePack": 0.43, "Embulk": 0.39 } Parse LLM output - show the contents because output doesn't contain function calls Parse LLM output - show the contents because output doesn't contain function calls
  14. Function Calling 使い道(例) • 検索 ◦ ウェブ検索(LLMがクエリ生成) ◦ ウィキペディア検索 ◦

    ドキュメント検索 • 分析 ◦ SQL実行(LLMがクエリ生成) ◦ 別ウィンドウでグラフを表示 • サービス連携 ◦ 外部APIに通知(例: レストラン予約) ◦ 外部APIからデータ取得(例: 予約一覧表示) • 役割分担 ◦ 別 LLMに候補一覧を渡し、もっとも良いも を選んで返してもらう