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

JAWS PANKRATION前哨戦 The Ultimate RAG Showdown

Sponsored · Your Podcast. Everywhere. Effortlessly. Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
Avatar for moritalous moritalous
August 24, 2024
41

JAWS PANKRATION前哨戦 The Ultimate RAG Showdown

以下のイベントで登壇した資料です。

KyotoLT 第31回 (テーマはAWS!)
https://kyotolt.connpass.com/event/325478/

Avatar for moritalous

moritalous

August 24, 2024
Tweet

More Decks by moritalous

Transcript

  1. 自己紹介 森田 和明 富士ソフト株式会社 主任 / フェロー(アーキテクト・エバンジェリスト) AWS Ambassador(2023~) AWS Top

    Engineer(2020~) AWS All Certifications Engineer(2024) AWS Community Builder(2024) 生成AIに限らず、AWS関係のアーキテクトとエバンジェリストをやってます Java Webアプリ開発出身 新しいもの好き X / Qiita / GitHub : @moritalous 2 「Jumping deer with japanese temple」 Amazon Titan Image Generatorにて生成
  2. Knowledge bases for Amazon Bedrockと は 10 • RAGを構築するためのBedrockの機能 •

    マネージメントコンソールの操作のみで構築可能 • 機能アップデートも活発
  3. Knowledge bases for Amazon Bedrock Knowledge bases for Amazon Bedrockのアーキテクチャ

    11 埋め込み 埋め込み 回答生成 テキスト 抽出 チャンク 分割 OpenSear ch Serverles s 検索 S3 質問 回答
  4. 1回のAPI呼び出しで検索と回答生成が可能 def retrieve_and_generate(question: str): response = client.retrieve_and_generate( input={"text": question}, retrieveAndGenerateConfiguration={

    "knowledgeBaseConfiguration": { "knowledgeBaseId": knowledgeBaseId, "modelArn": modelArn, "orchestrationConfiguration": { "queryTransformationConfiguration": {"type": "QUERY_DECOMPOSITION"} }, "retrievalConfiguration": { "vectorSearchConfiguration": {"overrideSearchType": "HYBRID"} }, }, "type": "KNOWLEDGE_BASE", }, ) return response 12 APIを一つ呼び出すだけで検索と回答生成が同時に処理される (検索だけを実行するAPIもあります)
  5. ナレベベの評価 • 難易度:☆☆☆ マネジメントコンソールの操作だけで構築可能 OpenSearch Serverlessを自動で作成するクイック作成もある • 機能の豊富さ:☆☆ 機能アップデートが頻繁にあり、最近はAdvanced RAGを構築する機能が追加

    RAGの最適化手法の中から厳選されたものを簡単に適用できる • 拡張性:☆ 新しい手法や新しいLLMが登場してもすぐに使えるわけではない • 日本語対応:☆ クイック作成で作成されるOpenSearch Serverlessのインデックスには、日本語向けの 設定が入っていない 13
  6. 処理1)検索クエリ生成関数 def generate_search_query(question: str): result = bedrock_runtime.converse( modelId="cohere.command-r-plus-v1:0", additionalModelRequestFields={"search_queries_only": True},

    additionalModelResponseFieldPaths=["/search_queries"], messages=[ { "role": "user", "content": [{"text": question}], } ], ) return list( map( lambda x: x["text"], result["additionalModelResponseFields"]["search_queries"], ) ) 18 検索前にユーザーの質問からク エリを作成する処理。 Cohere Command R/R+で 用意されている機能 例:「Kendraが提供されてい て、BedrockでClaude 3.5 が使えるリージョンは?」 ・Kendraが提供されている リージョン ・BedrockでClaude 3.5が 提供されているr-ジョン
  7. 処理2)Kendra検索関数 def fetching_relevant_documents(queries: list[str]): items = [] for query in

    queries: response = kendra.retrieve( IndexId=kendra_index_id, QueryText=query, AttributeFilter={ "EqualsTo": {"Key": "_language_code", "Value": {"StringValue": "ja"}} }, ) items.extend( list( map( lambda x: {k: v for k, v in x.items() if k in ["Id", "DocumentId", "DocumentTitle", "Content", "DocumentURI"]}, response["ResultItems"], ) ) ) return items 19 Kendraで検索する処理
  8. 処理3)回答生成関数 def generating_response(question: str, documents: list[str]): result = bedrock_runtime.converse( modelId="cohere.command-r-plus-v1:0",

    additionalModelRequestFields={"documents": documents}, messages=[ { "role": "user", "content": [{"text": question}], } ], ) return result["output"]["message"]["content"][0]["text"] 20 Bedrockで回答を生成する処理 Cohere Command RのAPIと相性がよい
  9. The Ultimate RAG Showdown 22 ナレベベ KendRAG 難易度 ☆☆☆ ☆☆

    機能の豊富さ ☆☆ ☆ 拡張性 ☆ ☆☆☆ 日本語対応 ☆ ☆☆
  10. Search pipeline OpenSearch Service Ingest pipeline OpenSearchRAGのアーキテクチャ 25 Bedrock テキスト

    抽出 データソー ス 質問 回答 Bedrock SageMaker Bedrock 埋め込み 検索 リランク 回答生成 チャンク分割 埋め込み
  11. OpenSearchの検索API def search(query: str): response = client.search( index=index_name, body={ "_source":

    {"exclude": ["body_chunk_embedding"]}, "query": { "hybrid": { "queries": [ {"match": {"body_chunk": {"query": query,}}}, {"nested": { "score_mode": "max", "path": "body_chunk_embedding", "query": { "neural": { "body_chunk_embedding.knn": { "query_text": query, "model_id": titan_model_id, }}},}},],}}, "ext": { "rerank": {"query_context": {"query_text": query,},}, "generative_qa_parameters": { "llm_model": "litellm", "llm_question": query, "context_size": 4, },},}, params={"search_pipeline": "hybrid-rerank-search-pipeline"}, ) 26 context = list(map(lambda x: x["_source"], response["hits"]["hits"])) for tmp in context: del tmp["body_chunk"] return { "answer": response["ext"]["retrieval_augmented_generation"]["answer"], "context": context, } 検索パイプラインを定義しておくことで、検索 APIを呼び出すだけでRAGの結果を取得できる
  12. The Ultimate RAG Showdown 28 ナレベベ KendRAG OpenSearchRAG 難易度 ☆☆☆

    ☆☆ ☆ 機能の豊富さ ☆☆ ☆ ☆☆ 拡張性 ☆ ☆☆☆ ☆ 日本語対応 ☆ ☆☆ ☆☆