2
0

導入

こんにちは。エンジニアのYamaです。
今回はSpringAIというものを使ってみたので、その紹介をしていきます。

SpringAIを利用すると、Spring BootアプリケーションにAI機能を簡単に追加することができます。
GeminiやHaggingFaceなどのモデルを利用することができるものですが、今回はOpenAIのチャットモデルをSpring上で使ってみました。

SpringAIとは

こちらが公式の情報です。

原文より抜粋

Spring AI provides the following features:

  • Support for all major Model providers such as OpenAI, Microsoft, Amazon, Google, and Hugging Face.
  • Supported Model types are Chat, Text to Image, Audio Transcription, Text to Speech, and more on the way.
  • Portable API across AI providers for all models. Both synchronous and stream API options are supported. Dropping down to access model specific features is also supported.
  • Mapping of AI Model output to POJOs.
  • Support for all major Vector Database providers such as Apache Cassandra, Azure Vector Search, Chroma, Milvus, Neo4j, Oracle, PostgreSQL/PGVector, PineCone, Qdrant, Redis, and Weaviate.
  • Portable API across Vector Store providers, including a novel SQL-like metadata filter API that is also portable.
  • Function calling.
  • Spring Boot Auto Configuration and Starters for AI Models and Vector Stores.
  • ETL framework for Data Engineering.
Google翻訳版

Spring AI は次の機能を提供します。

  • OpenAI、Microsoft、Amazon、Google、Hugging Face などの主要なモデル プロバイダーをすべてサポートします。
  • サポートされているモデル タイプは、チャット、テキストから画像、オーディオ トランスクリプション、テキストから音声への変換などであり、今後さらに追加される予定です。
  • すべてのモデル向けの AI プロバイダー間でのポータブル API。同期 API オプションとストリーム API オプションの両方がサポートされています。モデル固有の機能にアクセスするためのドロップダウンもサポートされています。
  • AI モデル出力の POJO へのマッピング。
  • Apache Cassandra、Azure Vector Search、Chroma、Milvus、Neo4j、Oracle、PostgreSQL/PGVector、PineCone、Qdrant、Redis、Weaviate など、すべての主要なベクター データベース プロバイダーをサポートします。
  • Vector Store プロバイダー間でのポータブル API。これには、同様にポータブルな新しい SQL のようなメタデータ フィルター API が含まれます。
  • 関数呼び出し。
  • AI モデルとベクター ストア用の Spring Boot 自動構成とスターター。
  • データ エンジニアリング用の ETL フレームワーク。

並みな感想ですが、いろいろなことができそうです。

セットアップ

Spring CLIのインストール

Spring AIを利用する前にSpring CLIをインストールします。

git clone https://github.com/spring-projects/spring-cli $HOME/temp/spring-cli

cd $HOME/temp/spring-cli

./gradlew clean build -x test

以下のエイリアスを設定することで、springコマンドでSpring CLIを実行できるようになります。

alias spring='java -jar $HOME/temp/spring-cli/build/libs/spring-cli-0.10.0-SNAPSHOT.jar'

必要に応じて、.bashrcなどに追加しておきましょう。

プロジェクトの作成(プロジェクトへのSpring AI追加)

spring-aiという名前のプロジェクトを作成します。

spring boot new --from ai --name spring-ai
cd spring-ai

既にプロジェクトを作成済みの場合は、以下のコマンドで、Spring AI機能をできるみたいです。

spring boot add ai

OpenAIのAPIの設定

OpenAIのユーザページからAPI keyを発行し、Spring AIで利用できるように環境変数として設定します。

export SPRING_AI_OPENAI_API_KEY=<発行したAPIキー>

プロジェクトの起動

以下のコマンドでspring-aiプロジェクトを起動します。

./mvnw spring-boot:run

動作確認

コマンドで新規プロジェクトを作成した場合、/aiというエンドポイントが作成されており、こちらを叩くと、英語でジョークを生成してくれます。
私は英語がよくわからないので、日本語でジョークを言うように少し変更しています。

生成AIが生成したジョーク

...:thinking:

ジョーク自体の良し悪しはよくわかりませんが、実行するたび異なる文が返却されていることがわかりますね。

終わりに

一般的に生成AIをプログラムで書く場合は、PythonやJavaScriptでの情報が多くヒットしますが、Javaに慣れている人はSpringAIで生成AIに触れてみるのもいいかも知れません。
特に既にSpringで動いているプロジェクトに対してAIを導入したいと考えている型の参考になれば、と思います。

2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0