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

OCI Generative AI Service 実践編 / Practice of OCI Generative AI Service

OCI Generative AI Service 実践編 / Practice of OCI Generative AI Service

2024/02/22 に行われた OCI Generative AI Service 実践編で用いた資料です

oracle4engineer

February 21, 2024
Tweet

More Decks by oracle4engineer

Other Decks in Technology

Transcript

  1. OCI Generative AI Service 実践編 Oracle AI Brown Bag Seminar

    #7 Shuhei Kawamura Cloud Architect Oracle Digital, Oracle Corporation Japan February 22, 2024
  2. Copyright © 2024, Oracle and/or its affiliates 2 • 所属

    • ⽇本オラクル株式会社 • Oracle Digital • 普段の業務 • Digital Nativeなお客様を技術⾯でご⽀援 • コミュニティ • OCHaCafe • CloudNative Days – Observability team 川村 修平 (Shuhei Kawamura) @shukawam X/GitHub/Qiita
  3. Copyright © 2024, Oracle and/or its affiliates 3 Agenda 1.

    OCI Generative AI Agents Service(Beta) 2. LangChain/LlamaIndex Integration
  4. Copyright © 2024, Oracle and/or its affiliates 4 Oracle Cloud

    Infrastructure AI/ML 製品ポートフォリオ Applications Fusion Applications Fusion Analytics Industry Applications 3rd Party Applications Oracle AI Partners ML for data platforms OCI Data Science AI Vector Search in Oracle Database MySQL HeatWave Vector Store OCI Data Labeling AI infrastructure Compute bare metal instances and VMs with NVIDIA GPUs OCI Supercluster with RDMA networking Block, object, and file storage; HPC filesystems Data NetSuite OCI Generative AI OCI Generative AI Agents Digital Assistant Speech Language Vision Document Understanding AI Services
  5. Copyright © 2024, Oracle and/or its affiliates 5 Oracle Cloud

    Infrastructure AI/ML 製品ポートフォリオ Applications Fusion Applications Fusion Analytics Industry Applications 3rd Party Applications Oracle AI Partners ML for data platforms OCI Data Science AI Vector Search in Oracle Database MySQL HeatWave Vector Store OCI Data Labeling AI infrastructure Compute bare metal instances and VMs with NVIDIA GPUs OCI Supercluster with RDMA networking Block, object, and file storage; HPC filesystems Data NetSuite Digital Assistant Speech Language Vision Document Understanding AI Services OCI Generative AI OCI Generative AI Agents
  6. Copyright © 2024, Oracle and/or its affiliates 7 LLMが学習していないデータを含めたテキスト⽣成を可能にする構成 LLMが答えられない知識をその他の知識ベース(例えば、ベクトルデータベースなど)で補完する構成

    検索拡張⽣成RAG(Retrieval-Augmented Generation) Vector Store LLM Application インターネット 企業 テキストデータ (企業内のデータ) テキストデータ (⼀般的に公開 されているデータ) prompt completion 連携 Oracleの新製品◦◦の 2024年の売り上げは︖ ◦◦の2024年の売り上げ は△△億円です。 以下のコンテキストに基づいて 「 Oracleの新製品◦◦の2024年の売り上げは︖ 」 に対する回答を作成してください。 --- ◦◦の2024年の売り上げは… Oracleの◦◦とは… … 知識ベースと連携し、LLM に渡すプロンプトを拡張 LLMと知識ベースの 合わせ技で回答
  7. Copyright © 2024, Oracle and/or its affiliates 8 検索拡張⽣成RAG(Retrieval-Augmented Generation)の構成例

    ベクトルデータベースを使ったRAGの代表的な構成要素 Application prompt completion code ①⼤規模⾔語モデル ü ドキュメントデータ、プロンプトの埋め込み ü テキスト⽣成、テキスト要約など ②ベクトルストア ü ドキュメントデータの保持 ü ベクトルの類似性検索 ③キャッシュストア ü プロンプト、⽣成テキストの保持、管理 ü プロンプトの内容に応じて参照 LLM Vector Store Cache Store PromptをLLMに⼊⼒ ベクトルデータベースの検索必要性を判別 ベクトル検索実⾏結果をLLMに連携 最終的なテキスト出⼒を⽣成 ベクトル検索実⾏ ベクトル検索実⾏結果取得 プロンプト、⽣成テキストのキャッシュ キャッシュの読み込み 各サービスの連携処理 フローを全て実装
  8. Application code Copyright © 2024, Oracle and/or its affiliates 9

    オーケストレーションツールを⽤いたRAGの代表的な構成例 prompt completion ①⼤規模⾔語モデル ü ドキュメントデータ、プロンプトの埋め込み ü テキスト⽣成、テキスト要約など ②ベクトルストア ü ドキュメントデータの保持 ü ベクトルの類似性検索 ③キャッシュストア ü プロンプト、⽣成テキストの保持、管理 ü プロンプトの内容に応じて参照 LLM Vector Store Cache Store ④オーケストレーション ü ⼊⼒プロンプトに対するナレッジベースの検索のプラン ü 各ナレッジベースの連携 Orchestration Tool プロンプトをオーケストレーション ツールに連携するコードのみ
  9. Copyright © 2024, Oracle and/or its affiliates 10 n ユースケース

    • コールセンター業務の最適化 • 法的調査の迅速化 n 特⻑ • OCI Generative AI Serviceと、その他のサービスの オーケストレーションをフルマネージドのPaaSとして提供 • ノーコード、Web UIウィザードでの検索拡張⽣成 (RAG)のプロビジョニング • チャットUI機能 n 価格 2024/02現在、ベータ版のため未定 OCI Generative AI Agents(Beta) Input OCI Generative AI Agents Output + ⾃然⾔語で問い合わせ 1. ナレッジベースを検索 2. 検索結果をリランク 3. 関連上位の⽂書と元のクエリを LLMへ送信し、回答を作成する 4. 回答をユーザーに返却する 回答とレスポンスの作成に使⽤された ⽂書への参照を受け取る
  10. Copyright © 2024, Oracle and/or its affiliates 11 OCI Generative

    AI Agents Service(Beta)を⽤いたRAG構成 Application prompt completion code LLM Vector Store Cache Store Orchestration Tool プロンプトをオーケストレーション ツールに連携するコードのみ OCI Generative AI Agents Service(Beta) OCI Generative AI Service OCI OpenSearch Service OCI Cache with Redis ※AI Vector Search(将来予定) ※MySQL HeatWave(将来予定)
  11. Copyright © 2024, Oracle and/or its affiliates 12 デモシナリオ︓ •

    LLM(Generative AI Serviceのモデル)が学習していないデータをあらかじめOpenSearchに保存 • ユーザーからのプロンプトにLLMが応答できない場合、データソースを検索する • 検索により得られた情報をLLMに連携し、最終的な応答テキストを作成 Demo: Generative AI Agents Service(Beta)を⽤いたRAG構成 Application (Streamlit) prompt completion OCI Generative AI Agents Service Generative AI Service OCI Search Service w/ OpenSearch OCI Cache with Redis Identity Domains/ IDCS Access Token OAuth 2.0 (Client Credentials Grant) OCI Serviceに関する情報 が保存済み OCI Generative AI Serviceの テキスト⽣成モデル (command)を利⽤ ⼊⼒プロンプト、応答内容など の履歴が保持できるキャッシュ・ ストア(OCI Redis Service)を設 定済み
  12. Copyright © 2024, Oracle and/or its affiliates 13 参考: デモ構成の解説

    – Identity Domains/IDCS Data sourceから提供されるAPIをOAuth 2.0で保護するためのIdentity Domains/IDCSの設定例 ※本設定例は、IDCSによるものですが、Identity Domainsでも同様に実施いただけます 許可されるOAuth 2.0のGrant Typeを指定 (認可コードGrantの場合)リダイレクト先のURLを指定
  13. Copyright © 2024, Oracle and/or its affiliates 14 参考: デモ構成の解説

    – Identity Domains/IDCS Data sourceから提供されるAPIをOAuth 2.0で保護するためのIdentity Domains/IDCSの設定例 aud(トークンの発⾏先)として、 AgentのAPI Endpointを指定 Data Storeの検索APIを 実⾏するためのscopeを作成 作成したscopeを指定
  14. Copyright © 2024, Oracle and/or its affiliates 15 PUT ${OPENSEARCH_ENDPOINT}/_plugins/_security/api/securityconfig/config

    参考: デモ構成の解説 – OCI Search Service w/ OpenSearch { "dynamic": { "authc": { "openid_auth_domain": { "http_enabled": true, "transport_enabled": true, "order": 0, "http_authenticator": { "challenge": false, "type": "openid", "config": { "subject_key": "sub", "roles_key": "scope", "openid_connect_url": "https://...oraclecloud.com/.well-known/openid-configuration" } }, "authentication_backend": { "type": "noop", "config": {} }, "description": "Authenticate using OpenId connect" }, "basic_internal_auth_domain": { "http_enabled": true, "transport_enabled": true, "order": 1, "http_authenticator": { "challenge": true, "type": "basic", "config": {} }, "authentication_backend": { "type": "intern", "config": {} }, "description": "Authenticate via HTTP Basic against internal users database" } }, "authz": null } } OpenSearchに対するセキュリティ設定 (OpenID Connect)
  15. Copyright © 2024, Oracle and/or its affiliates 16 PUT ${OPENSEARCH_ENDPOINT}/_plugins/_security/api/roles/genaiagent

    PUT ${OPENSEARCH_ENDPOINT}/_plugins/_security/api/rolesmapping/genaiagent 参考: デモ構成の解説 – OCI Search Service w/ OpenSearch { "description": "Role to be used by Generative AI Agent having read only permission to all Indexes", "cluster_permissions": [ "cluster_composite_ops_ro" ], "index_permissions": [ { "index_patterns": [ "*" ], "fls": [], "masked_fields": [], "allowed_actions": [ "read" ] } ], "tenant_permissions": [] } { "backend_roles": [ "genaiagent" ], "hosts": [], "users": [] } 全てのIndexに対するread操作が許可されたRoleを作成 作成したRoleを紐づける(マッピングさせる)対象の設定 今回は、genaiagentというスコープが含まれているAccess TokenであればOK (roles_keyにscopeを指定しているため)
  16. Copyright © 2024, Oracle and/or its affiliates 17 参考: デモ構成の解説

    – OCI Generative AI Agents Service(Beta) 使⽤するData sourceを選択 (今回は、OpenSearch v2.3.0) 使⽤するCache storeを選択 使⽤するIdentity Domains or IDCSに関する設定
  17. Copyright © 2024, Oracle and/or its affiliates 19 LangChain •

    ⾔語モデルを利⽤したアプリケーション開発のためのフレームワーク • LangChain(Python), LangChain JS(JavaScript/TypeScript) • モジュール • Model I/O: ⾔語モデルとのインタフェース • Retrieval: アプリケーション固有のデータとのインタフェース • Agents: ⾔語モデルを推論エンジンとして使⽤し、どのアクションをどの順番で実⾏するかを決定する • Chains: データの前処理や⾔語モデル、各ツールにおける呼び出しのシーケンスを構築する • Memory: チェーンの実⾏間でアプリケーションの状態を保持する • Callbacks: 様々なステージにフックできるコールバックシステムを提供 LangChain
  18. Copyright © 2024, Oracle and/or its affiliates 21 LangChain +

    OCI Generative AI Service Generate Texts from os import getenv from dotenv import load_dotenv from langchain_community.llms.oci_generative_ai import OCIGenAI load_dotenv() COMPARTMENT_ID = getenv("COMPARTMENT_ID") # https://inference.generativeai.us-chicago-1.oci.oraclecloud.com SERVICE_ENDPOINT = getenv("GEN_AI_INFERENCE_ENDPOINT") llm = OCIGenAI( service_endpoint=SERVICE_ENDPOINT, compartment_id=COMPARTMENT_ID, model_id="cohere.command", # auth_type=“API_KEY”, # デフォルトでは、API Keyが認証⽅式として使⽤される auth_profile="CHICAGO" ) response = llm.invoke("Tell me a joke", temperature=0.7) print(response) # Why was the computer cold? Because it left its Windows open! ...
  19. Copyright © 2024, Oracle and/or its affiliates 22 LangChain +

    OCI Generative AI Service Embeddings from os import getenv from dotenv import load_dotenv from langchain_community.embeddings.oci_generative_ai import OCIGenAIEmbeddings load_dotenv() COMPARTMENT_ID = getenv("COMPARTMENT_ID") # https://inference.generativeai.us-chicago-1.oci.oraclecloud.com SERVICE_ENDPOINT = getenv("GEN_AI_INFERENCE_ENDPOINT") embeddings = OCIGenAIEmbeddings( model_id="cohere.embed-english-v3.0", service_endpoint=SERVICE_ENDPOINT, compartment_id=COMPARTMENT_ID ) response = embeddings.embed_query(text="This is a query in English.") print(response) # [-0.008598328, -0.030687425, -0.00093364716, ...
  20. Copyright © 2024, Oracle and/or its affiliates 23 • LangChainには、拡張⽬的の基底クラスが存在する

    • LLM → langchain_core.language_models.llms.LLM • Embeddings → langchain_core.embeddings.Embeddings • VectorStore → langchain_core.vectorstores.VectorStore • … • これらを継承することで、あらゆるLLM, VectorStore, …をLangChainに対応させることができる • OCI Generative AI Services • community/langchain_community/llms/oci_generative_ai.py • community/langchain_community/embeddings/oci_generative_ai.py 参考: LangChainの拡張
  21. Copyright © 2024, Oracle and/or its affiliates 24 Session Token

    Instance Principal Resource Principal LangChain + OCI Generative AI Service その他の認証⽅式(Session Token, Instance Principal, Resource Principal)をサポート llm = OCIGenAI( # ... omit ... auth_type="SECURITY_TOKEN", auth_profile="langchain" ) llm = OCIGenAI( # ... omit ... auth_type="INSTANCE_PRINCIPAL", ) llm = OCIGenAI( # ... omit ... auth_type="RESOURCE_PRINCIPAL", )
  22. Copyright © 2024, Oracle and/or its affiliates 25 LangChainを⽤いた最⼩限のRAGの実装例 embeddings

    = OCIGenAIEmbeddings( # ... omit ... ) vectorstore = FAISS.from_documents( documents=documents, embedding=embeddings ) retriever = vectorstore.as_retriever() template = """Answer the question based only on the following context: {context} Question: {question} """ prompt = PromptTemplate.from_template(template=template) llm = OCIGenAI( # ... omit ... ) chain = ( {"context": retriever, "question": RunnablePassthrough()} | prompt | llm | StrOutputParser() ) print(chain.invoke("What is region?")) クエリに関連するドキュメントをFAISS*(Facebook AI Similarity Search)から返却する *: Facebook(現Meta)が開発した類似検索⽤ライブラリ LLMに最終的に渡すプロンプトの定義 どのような順番で実⾏されるのか(=チェーン)を定義︓ 1. クエリの内容を元にFAISSに問い合わせて、関連度の⾼い ドキュメントを取得 2. 取得したドキュメントと質問内容をテンプレートに埋め込み 3. LLMに作成したプロンプトを渡して回答を⽣成させる
  23. Copyright © 2024, Oracle and/or its affiliates 26 あらゆる⽣成AIをサポートする、データ・プラットフォーム AIアプリケーション開発の⽣産性向上

    • ベクトル・データを含む、あらゆるデータタイプを⼀つのデータベースで • 構造化データとベクトル・データのベクトル検索をSQLで ⾼速なベクトル検索 • パーティション化された、ベクトル索引 • グラフ・テクノロジーを活⽤した、ベクトル検索 ⾼可⽤性、スケーラビリティ、セキュリティ • データ整合性を保持し、スケールアウトを可能 • Exadata Cloudの持つ、⾼性能、⾼可⽤性、堅牢なセキュリティを ベクトル検索に Oracle Database 23c AI Vector Search Oracle Databaseが新たにベクトル・データをサポート 7 1 4 9
  24. Copyright © 2024, Oracle and/or its affiliates 27 デモシナリオ︓ •

    LLM(Generative AI Serviceのモデル)が学習していないデータをあらかじめOracle Database 23cに保存 • ユーザーからのプロンプトにLLMが応答できない場合、データソースを検索する • 検索により得られた情報をLLMに連携し、最終的な応答テキストを作成 Demo: LangChain + Generative AI Serviceを⽤いたRAG構成 Application (Streamlit) prompt completion Generative AI Service Oracle Database 23c OCI Serviceに関する情報 が保存済み OCI Generative AI Serviceの テキスト⽣成モデル (command)を利⽤
  25. Copyright © 2024, Oracle and/or its affiliates 28 • Generative

    AI Agents • https://www.oracle.com/jp/artificial-intelligence/generative-ai/agents/ • Generative AI Agents – API Reference • https://docs.public.oneportal.content.oci.oraclecloud.com/en-us/iaas/api/ - /en/generative-ai- agents/20240331/ • LangChain • https://github.com/langchain-ai/langchain • デモで使⽤したコード • https://github.com/oracle-japan/ai-bbs-genai-practice 参考情報