Java で開発する OpenAI 連携アプリファーストステップ

2.5K Views

May 30, 24

スライド概要

【オンライン】 JJUGナイトセミナー「Spring AI特集」5/30(木) 開催
https://jjug.doorkeeper.jp/events/173507

profile-image

ヴイエムウェア株式会社 ソリューションアーキテクト本部 プリンシパルエンタープライズアーキテクト。 Microsoft で13年間、テクニカルエバンジェリストとして .NET、Visual Studio、Windows、iOS、Android、Microsoft Azure 等の開発者向け最新技術啓発活動を実施。その後、Dell、Accenture、Elastic で開発者向け技術啓発活動等を経て現職。 モダンアプリケーション開発、マルチクラウド対応、アーキテクチャ策定等を中心に、技術者向けに最新技術の啓発活動を実施中。 2019年4月〜2021年8月迄、内閣官房 IT 総合戦略室 政府 CIO 補佐官を兼務、2021年9月〜2024年3月迄、デジタル庁 PjM ユニット ソリューションアーキテクトを兼務。

シェア

またはPlayer版

埋め込む »CMSなどでJSが使えない場合

関連スライド

各ページのテキスト
1.

Java で開発する OpenAI 連携アプリファーストステップ 鈴⽊ 章太郎 プリンシパルエンタープライズアーキテクト ソリューションアーキテクト本部 ヴイエムウェア株式会社 2024/05/30 All Rights Reserved. The term “Broadcom” refers to Broadcom Inc. and/or its subsidiaries.

2.

鈴⽊ 章太郎 X (Twitter) : @shosuz ヴイエムウェア株式会社 プリンシパルエンタープライズアーキテクト Microsoft で13年間、テクニカルエバンジェリストとして .NET、Visual Studio、Windows、iOS、Android、 Microsoft Azure 等の開発者向け技術啓発活動を実施。 その後、Dell、Accenture、Elastic での開発者向け技術 啓発活動等を経て現職。モダンアプリケーション開発、マルチ クラウド対応、アーキテクチャ策定等を中⼼に、開発者向け 最新技術啓発活動中。 2019年4⽉〜2021年8⽉、内閣官房 IT 総合戦略室 政府 CIO 補佐官、2021年9⽉〜2024年3⽉、 デジタル 庁 ソリューションアーキテクトを兼務。 https://www.docswell.com/user/shosuz All Rights Reserved. The term “Broadcom” refers to Broadcom Inc. and/or its subsidiaries.

3.

プラットフォームの認知度を⾼め、開発者の準備態勢を整える さまざまな接点とそれらを増やす機会を探りつなげていく マルチクラウド対応の 訴求 AI プラットフォームとしての 認知向上 Spring の 最新技術啓発 デベロッパーアドボカシー All Rights Reserved. The term “Broadcom” refers to Broadcom Inc. and/or its subsidiaries. 3

4.

Agenda l Spring AI 概要 l 最初のプロジェクト構築 l まとめ l Appendix All Rights Reserved. The term “Broadcom” refers to Broadcom Inc. and/or its subsidiaries. 4

5.

Spring AI 概要 All Rights Reserved. The term “Broadcom” refers to Broadcom Inc. and/or its subsidiaries. 5

6.

Spring AI - AI と Java コミュニティの連携 オープンソース Java である Spring と AI を連携する 実験プロジェクトの発⾜ GitHub の紹介⽂より AI には⻑い歴史があるにもかかわらず、Java の役割は⽐較的⼩さいものでした。(中略) Generative AI の最近の進歩により、HTTP を介した事前トレーニングされたモデルとの対話 が普及しました。これにより、 ... Java などのプログラミング⾔語の使⽤への道が開かれます。 (中略) https://github.com/springprojects-experimental/springai Spring AI プロジェクトは、AI ドメインの Spring 開発者に同様のエクスペリエンスを提供 することを⽬的としています。 All Rights Reserved. The term “Broadcom” refers to Broadcom Inc. and/or its subsidiaries. 6

7.

https://blogs.vmware.com/vmware-japan/product/spring All Rights Reserved. The term “Broadcom” refers to Broadcom Inc. and/or its subsidiaries. 7

8.

最初のプロジェクト構築 All Rights Reserved. The term “Broadcom” refers to Broadcom Inc. and/or its subsidiaries. 8

9.

OpenAI サイトで API キーを取得して .sh で結果を取得する OpenAI API サイトに登録しキーを取得する https://platform.openai.com/api-keys All Rights Reserved. The term “Broadcom” refers to Broadcom Inc. and/or its subsidiaries. 9

10.

Spring の Web サイトで Project ⼀覧を参照 https://spring.io/projects All Rights Reserved. The term “Broadcom” refers to Broadcom Inc. and/or its subsidiaries. 10

11.

Project ⼀覧から Spring AI ページに遷移 https://spring.io/projects/spring-ai All Rights Reserved. The term “Broadcom” refers to Broadcom Inc. and/or its subsidiaries. 11

12.

Spring AI ドキュメントを参照 https://spring.io/projects/spring-ai#learn All Rights Reserved. The term “Broadcom” refers to Broadcom Inc. and/or its subsidiaries. 12

13.

Spring AI ドキュメントを参照 https://docs.spring.io/spring-ai/reference/ All Rights Reserved. The term “Broadcom” refers to Broadcom Inc. and/or its subsidiaries. 13

14.

いくつかの簡単なステップでスタート https://spring.io/projects/spring-ai 1. Spring CLI をインストールし、シェルでコマンドを実⾏ spring boot new --from ai --name myai ChatGPT との基本的なやり取りを開始するためのアプリケーションを作成 ⽣成されたREADMEファイルの指⽰に従ってAPI KEYを取得する 2. アプリケーションを実⾏ ./mvw spring-boot:run 3. エンドポイントを curl ︓ ChatGPTとの基本的なやり取りを開始するためのアプリケーションを作成します。 curl localhost:8080/ai/simple All Rights Reserved. The term “Broadcom” refers to Broadcom Inc. and/or its subsidiaries. 14

15.

Spring Initializer でスタート https://docs.spring.io/spring-ai/reference/ • Language • • Project • • • • All Rights Reserved. The term “Broadcom” refers to Broadcom Inc. and/or its subsidiaries. Java Maven Project Metadata • 適当な名前を設定 • Java 21 Dependencies • Spring Web • OpenAI ダウンロードして IntelliJ 等で開く 15

16.

Spring Initializer Template でスタート - 1 https://docs.spring.io/spring-ai/reference/ • Language • Java • Project • Maven • Project Metadata All Rights Reserved. The term “Broadcom” refers to Broadcom Inc. and/or its subsidiaries. • 適当な名前を設定 • Java 21 16

17.

Spring Initializer Template でスタート - 2 https://docs.spring.io/spring-ai/reference/ • Version • 3.2.5 • Dependency • Web – • Spring Web にチェック AI – OpenAI にチェック • そのまま IntelliJ で開く All Rights Reserved. The term “Broadcom” refers to Broadcom Inc. and/or its subsidiaries. 17

18.

IntelliJ でそのまま実⾏するとエラー OpenAI API キーとモデルが application.properties に設定されていないため All Rights Reserved. The term “Broadcom” refers to Broadcom Inc. and/or its subsidiaries. 18

19.

OpenAI API キーの環境変数への保存によるセキュリティ配慮 構成の編集 → 実⾏オプションの追加リンク → 環境変数のクリック → OPENAI_API_KEY= に設定 All Rights Reserved. The term “Broadcom” refers to Broadcom Inc. and/or its subsidiaries. 19

20.

OpenAI API モデルの指定 application.properties への追加 All Rights Reserved. The term “Broadcom” refers to Broadcom Inc. and/or its subsidiaries. 20

21.

IntelliJ で改めて実⾏ OpenAI application.properties に設定追加完了 All Rights Reserved. The term “Broadcom” refers to Broadcom Inc. and/or its subsidiaries. 21

22.

ChatController の構築 Controller クラスの作成と設定 All Rights Reserved. The term “Broadcom” refers to Broadcom Inc. and/or its subsidiaries. 22

23.
[beta]
ChatController.java の実装 - 1
package dev.shosuz.springaijjug;
import org.springframework.ai.chat.ChatClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class ChatController {
private final ChatClient chatClient;
public ChatController(ChatClient chatClient) {
this.chatClient = chatClient;
}
@GetMapping("/springaijjug")
public String generate(@RequestParam(value = "message",
defaultValue = "Tell me a American Joke!")
String message) {
return null;
}
}
All Rights Reserved. The term “Broadcom” refers to Broadcom Inc. and/or its subsidiaries.

23

24.

アプリケーションの停⽌と再実⾏ All Rights Reserved. The term “Broadcom” refers to Broadcom Inc. and/or its subsidiaries. 24

25.

ChatClient の実装の中⾝ public interface ChatClient All Rights Reserved. The term “Broadcom” refers to Broadcom Inc. and/or its subsidiaries. 25

26.

OpenAIChatClient の実装の中⾝ public class OpenAiChatClient All Rights Reserved. The term “Broadcom” refers to Broadcom Inc. and/or its subsidiaries. 26

27.
[beta]
ChatController.java の実装 – 2
package dev.shosuz.springaijjug;
import org.springframework.ai.chat.ChatClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class ChatController {
private final ChatClient chatClient;
public ChatController(ChatClient chatClient) {
this.chatClient = chatClient;
}
@GetMapping("/springaijjug")
public String generate(@RequestParam(value = "message",
defaultValue = "Tell me a American Joke!")
String message) {
return chatClient.call(message);
}
}

All Rights Reserved. The term “Broadcom” refers to Broadcom Inc. and/or its subsidiaries.

27

28.

実⾏結果 - 1 All Rights Reserved. The term “Broadcom” refers to Broadcom Inc. and/or its subsidiaries. 28

29.

実⾏結果 - 2 All Rights Reserved. The term “Broadcom” refers to Broadcom Inc. and/or its subsidiaries. 29

30.

まとめ All Rights Reserved. The term “Broadcom” refers to Broadcom Inc. and/or its subsidiaries. 30

31.

Agenda まとめ l Spring AI 概要 l 最初のプロジェクト構築 l まとめ All Rights Reserved. The term “Broadcom” refers to Broadcom Inc. and/or its subsidiaries. 31

32.

参考資料 NVIDIA Fine-Tuned Models NVIDIA Foundation Models INTRODUCING VMware Private AI Foundation with NVIDIA Third party & Community Models NVIDIA RAG LLM Operator NVIDIA GPU Operator NVIDIA NIM NVIDIA NeMo Retriever Deep Learning VMs Vector Database Postgres + pgvector Catalog Setup Wizard GPU Monitoring AI Enterprise Generally Available 選択可能な LLM 3 2 All Rights Reserved. The term “Broadcom” refers to Broadcom Inc. and/or its subsidiaries. ベアメタル並の パフォーマンス 迅速な Time-To-Value 32

33.

参考資料 Improved RAG Starter Pack: Path to Building Productiongrade Apps on VMware Private AI Foundation https://core.vmware.com/blog/improved-rag-starter-pack-path-building-production-grade-apps-vmware-private-ai-foundation https://github.com/vmware-private-ai/VMware-generative-ai-reference-architecture/tree/main/Starter-Packs/Improved_RAG All Rights Reserved. The term “Broadcom” refers to Broadcom Inc. and/or its subsidiaries. 33

34.

参考資料 Interop Tokyo 2024 LLM を活⽤して独⾃の⽣成 AI アプリを効率よく安全に開発する⽅法 https://forest.f2ff.jp/introduction/9033?project_id=20240601 ⽣成 AI を社内業務やビジネスに活⽤する動きが急増して います。しかしユースケースに適した独⾃の⽣成 AI アプリを 開発するにはプライバシーへの配慮や機密データの取り扱い など解決すべき課題に直⾯します。 本セッションでは有名な LLM を活⽤しつつ安全なプライベート 環境で効率よく⽣成 AI アプリを開発する⽅法と、それを実現 する基盤についてお話しします。 NVIDIA 社と VMware が共同開発したプライベート AI 基盤 「VMware Private AI Foundation with NVIDIA」 の最新情報と活⽤メリットをわかりやすくお伝えします。 All Rights Reserved. The term “Broadcom” refers to Broadcom Inc. and/or its subsidiaries. <VMware Booth 展⽰内容> 今年の最⼤の⽬⽟は、VMware Private AI Foundation with NVIDIA 初展⽰です。 ご期待ください。その他展⽰内容については以下のサイトをご覧ください。 34 https://f2ff.jp/2024/interop/exhibitor/show.php?id=2204&lang=ja

35.

Thank You https://blogs.vmware.com/vmware-japan/product/spring All Rights Reserved. The term “Broadcom” refers to Broadcom Inc. and/or its subsidiaries.

36.

Agenda Appendix l Azure Spring Apps 概要 l OpenAI/Azure Spring Apps Standard AI ショッピングカートアプリ l Spring AI x Azure Spring Apps Enterprise 新フィットネスストアアプリ l VMware Private AI Foundation with NVIDIA All Rights Reserved. The term “Broadcom” refers to Broadcom Inc. and/or its subsidiaries. 36

37.

Azure Spring Apps 概要 All Rights Reserved. The term “Broadcom” refers to Broadcom Inc. and/or its subsidiaries. 37

38.

Azure Spring Apps Spring Boot マイクロサービス向けの完全なマネージドサービス Azure のエコシステムとサービスの多くの選択肢と完全な統合 Azure Active Directory Azure Monitor DIY with Spring Boot 責務 Azure Spring Apps Service アプリケーションの統合、デバッグ Azure Database for MySQL Azure Cosmos DB CI/CD クラスタのビルドと管理 メトリクス ログ トレーシング サービス バインディング Managed Identities Service Principals Azure Spring Apps Service Runtime User Environment Azure Cache for Redis Spring Cloud ミドルウェアのホスト モニタリングとロギング Git リポジトリ Config Source App 1 App 2 App N Config Server Service Registry Lifecycle Mgmt. App Resiliency Log Stream Data Encryption Custom Domain SelfDiagnostics スケーリング パッチ Azure DevOps CI/CD サポート Azure Spring Apps agents GitHub お客様 VMware Microsoft Jenkins All Rights Reserved. The term “Broadcom” refers to Broadcom Inc. and/or its subsidiaries. Azure Kubernetes Service 38

39.

Spring Boot マイクロサービス向けの完全なマネージドサービス Azure エコシステムの⼀部としてストレージ、データベース、監視など他の Azure サービスに簡単にバインドできる • • • • • ユーザーはインフラストラクチャの管理に煩わされることなく、アプリの構築と実⾏に集中できる Spring Boot アプリに JAR やコード、または Steeltoe アプリに ZIP ファイルをデプロイすると、Spring サービスランタイムと 組み込みのアプリ ライフサイクル サポートにアプリが⾃動的に接続される 監視が容易であり、デプロイ後、アプリのパフォーマンスを監視し、エラーを修正して、アプリケーションを迅速に改善できる Azure のエコシステムとサービスへの完全な統合 フルマネージド インフラストラクチャと組み込みのライフサイクル管理を備えたエンタープライズ対応性あり All Rights Reserved. The term “Broadcom” refers to Broadcom Inc. and/or its subsidiaries. 39

40.

Azure Spring Apps を使⽤する理由 アプリケーションを Azure Spring Apps にデプロイして得られる利点 l 既存の Spring アプリを効率的に移⾏し、クラウドのスケーリングとコストを管理 l Spring Cloud のパターンを使⽤したアプリの最新化により、俊敏性とデリバリーのスピードを向上 l クラウド規模で Java を実⾏し複雑なインフラストラクチャなしで使⽤率向上 l コンテナー化の依存関係なしで迅速に開発およびデプロイ l 運⽤ワークロードを効率的かつ簡単に監視 ※ Azure Spring Apps では、Java Spring Boot と ASP.NET Core Steeltoe の両⽅のアプリがサポート All Rights Reserved. The term “Broadcom” refers to Broadcom Inc. and/or its subsidiaries. 40

41.

Steeltoe とは︖ .NET 開発者がマイクロサービスやその他のクラウドネイティブなパターンを実装できるようにするライブラリ群 l Java 向けの Spring Boot と 似ており Spring Bootにインス パイアされている l .NET エコシステム向けに特別に 設計されており、.NET アプリ ケーション内で Spring ⾃体を 直接使⽤することはできない l 構成管理、サービスディスカバ リー、サーキットブレーカーなどの 機能を提供することで、マイクロ サービスの構築を容易にし、クラ ウド環境での .NET アプリケー ションの開発、デプロイ、管理を 容易にする All Rights Reserved. The term “Broadcom” refers to Broadcom Inc. and/or its subsidiaries. 41

42.

OpenAI/Azure Spring Apps Standard AI ショッピングカートアプリ https://learn.microsoft.com/enus/samples/azure-samples/apptemplates-java-openaispringapps/app-templates-javaopenai-springapps/ All Rights Reserved. The term “Broadcom” refers to Broadcom Inc. and/or its subsidiaries. 42

43.

アプリケーションアーキテクチャ フロントエンド React アプリをコンテナアプリとしてホストする Azure Container Apps (環境) 及び Azure Spring Apps Standard コンサンプション及び専⽤プラン • AI ショッピング カート サービスを Spring アプリとして ホストする Azure Spring Apps • フロントエンドの Docker イメージをホストする Azure Container Registry • AI ショッピング カート サービスのデータを保存する Azure Database for PostgreSQL (フレキシブル サーバー) • 監視とログ記録を⾏う Azure Monitor • 栄養分析を実⾏し、トップ 3 のレシピを⽣成する Azure OpenAI • azd up コマンドを使⽤して作成される単⼀の リソース グループ (Azure OpenAI は別途デプロイ) デプロイは簡単︕ All Rights Reserved. The term “Broadcom” refers to Broadcom Inc. and/or its subsidiaries. https://learn.microsoft.com/en-us/samples/azure-samples/app-templates-java-openai-springapps/app-templates-java-openai-springapps/ 43

44.

ソースコード https://github.com/azure-samples/app-templates-java-openai-springapps/tree/main/ All Rights Reserved. The term “Broadcom” refers to Broadcom Inc. and/or its subsidiaries.

45.

Azure OpenAI Service のデプロイ Portal で先にデプロイしておき (GPT-3.5 or GPT-4)、それを azd コマンド実⾏時にセットする # Set the environment variables for Azure OpenAI • azd env set azureOpenAiApiKey <replace-with-Azure-OpenAi-API-key> • azd env set azureOpenAiEndpoint <replace-with-Azure-OpenAi-endpoint> • azd env set azureOpenAiDeploymentId <replace-with-Azure-OpenAi-deployment-id/name> # To use GPT-3.5 Turbo model set this environment variable to false • azd env set isAzureOpenAiGpt4Model true All Rights Reserved. The term “Broadcom” refers to Broadcom Inc. and/or its subsidiaries. 45

46.

Demo Java/OpenAI/Azure Spring Apps AI ショッピングカートアプリ https://learn.microsoft.com/enus/samples/azure-samples/apptemplates-java-openai-springapps/ All Rights Reserved. The term “Broadcom” refers to Broadcom Inc. and/or its subsidiaries. 46

47.

All Rights Reserved. The term “Broadcom” refers to Broadcom Inc. and/or its subsidiaries. 47

48.

Azure Spring Apps ランディングゾーンアクセラレータ 多⾔語アプリケーションのデプロイ、Tanzu コンポーネントのサポート、SLA 保証を求める場合に最適 https://github.com/Azure/azure-spring-apps-landing-zone-accelerator l Azure Spring Apps ランディング ゾーン アクセラレータ (Spring Boot アプリケーションをデプロイするために使 ⽤できるAzure Spring Apps リファレンス アーキテク チャを作成する⼿順)を参照 l 運⽤対応インフラストラクチャのプロビジョニングと、Spring Boot および Spring Cloud アプリの Azure Spring Apps へのデプロイを合理化するように設計されたアーキ テクチャガイダンスを提供 l ワークロード所有者は、ランディング ゾーン アクセラレータ で提供されるアーキテクチャ ガイダンスを使⽤して、⾃信を 持って⽬標の技術的状態を達成 All Rights Reserved. The term “Broadcom” refers to Broadcom Inc. and/or its subsidiaries.

49.

Spring AI x Azure Spring Apps 新フィットネスストアアプリ https://learn.microsoft.com/jajp/azure/springapps/enterprise/quickstart-fitnessstore-azure-openai All Rights Reserved. The term “Broadcom” refers to Broadcom Inc. and/or its subsidiaries. 49

50.

Azure Spring Apps Enterprise = Tanzu Application Platform as a Service Kubernetes 上の開発者体験を向上するアプリケーションプラットフォーム https://learn.microsoft.com/ja-jp/azure/spring-apps/overview#enterprise-plan デベロッパーの⽣産性向上 ⾼速なビルドと持続可能な 本番環境へのパス デベロッパーとインフラエンジニアの 仕事を調整 ↑ AKS、EKS、GKE、TKG、その他殆どの K8s クラスターで稼働 All Rights Reserved. The term “Broadcom” refers to Broadcom Inc. and/or its subsidiaries. 50

51.

開発者の⽣産性向上 標準環境の構築により、開発チーム全体の標準化を促進、チームの⽣産性を向上 l プロジェクト開始にどれくらいの期間を要していますか︖ n 開発チームの標準ポータル l 開発環境の構築にどれくらいの期間を要していますか︖ n アプリケーションの標準テンプレート l プロジェクト標準はお持ちですか︖ n アプリケーションフレームワーク (Spring) n 開発者の⽣産性を阻害しない開発環境 • アプリケーションの標準テンプレート ü 多様な⾔語によるプロジェクトの標準テンプレートにより、 プロジェクトの開始をスムーズに ü 雛形のコードを⾃動⽣成 All Rights Reserved. The term “Broadcom” refers to Broadcom Inc. and/or its subsidiaries. • 組織カタログ、API ポータル、ツールアドイン他 ü 標準 API やデータアクセスのためのポータルを提供 ü Visual Studio Code, Visual Studio, IntelliJ ⽤拡張機能 51

52.

フィットネス ストア サンプル アプリの概要 https://learn.microsoft.com/ja-jp/azure/spring-apps/enterprise/quickstart-sample-app-acmefitness-store-introduction • ドメインごとに分割された次のサービスで構成 • • • • 4 つの Java Spring Boot アプリケーション: • カタログ サービス、使⽤可能な製品をフェッチする ための API • 決済サービス、ユーザーの注⽂に対する⽀払いを 検証および処理 • ID サービスは、認証されたユーザーへの参照を提 供 • Assist Service、フィットネスストアに AI 機能を 提供 1 つの Python アプリケーション: • カートサービス、購⼊⽤に選択されたユーザーの アイテムを管理 1 つの ASP.NET Core アプリケーション: • 注⽂サービス、ユーザーのカートにある製品を購⼊ するように注⽂ 1 つの NodeJS と静的 HTML のアプリケーション: • フロントエンド、他のサービスに依存するショッピング アプリケーション All Rights Reserved. The term “Broadcom” refers to Broadcom Inc. and/or its subsidiaries. 52

53.

フィットネス ストア サンプルアプリのデプロイ⼿順 https://learn.microsoft.com/ja-jp/azure/spring-apps/enterprise/quickstart-deploy-apps-enterprise • • • • • • • • アプリのビルドとデプロイ シングル サインオンの構成 Azure Database for PostgreSQL と Azure Cache for Redis を統合 Key Vault を使⽤してアプリケーション シークレットを読み込む アプリケーションをエンドツーエンドで監視 する 要求レートの制限を設定する 配置の⾃動化 Azure Open AI を統合する All Rights Reserved. The term “Broadcom” refers to Broadcom Inc. and/or its subsidiaries. 53

54.

Demo Enterprise 向け 新フィットネスアプリご紹介 https://learn.microsoft.com/ja-jp/azure/springapps/enterprise/quickstart-deploy-apps-enterprise https://learn.microsoft.com/ja-jp/azure/springapps/enterprise/quickstart-fitness-store-azure-openai All Rights Reserved. The term “Broadcom” refers to Broadcom Inc. and/or its subsidiaries. 54

55.

All Rights Reserved. The term “Broadcom” refers to Broadcom Inc. and/or its subsidiaries. 55