0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Spring AI入門

Posted at

Spring AI入門

はじめに

Spring AIは、AIエンジニアリングのためのアプリケーションフレームワークです。Springエコシステムの設計原則(移植性やモジュラー設計)をAIドメインに適用し、POJOをアプリケーションの基本構成要素として使用することを促進することを目標としています。

1. Spring AIの主な特徴

サポートされているAIモデルプロバイダー

  • Anthropic
  • OpenAI
  • Microsoft
  • Amazon
  • Google
  • Ollama

サポートされているモデルタイプ

  • チャット補完
  • 埋め込み
  • テキストから画像生成
  • 音声文字起こし
  • テキストから音声変換
  • モデレーション

その他の主要機能

  • 同期・非同期APIのサポート
  • 構造化出力(AIモデルの出力をPOJOにマッピング)
  • 主要なベクトルデータベースのサポート
  • ツール/関数呼び出し機能
  • 可観測性
  • ドキュメント注入ETLフレームワーク
  • AIモデル評価ユーティリティ

2. 基本的な使用方法

依存関係の追加

dependencies {
    implementation 'org.springframework.ai:spring-ai-openai-spring-boot-starter'
}

アプリケーションの設定

spring.ai.openai.api-key=<YOUR_OPENAI_KEY>

基本的な実装例

@SpringBootApplication
public class SpringAiDemoApplication {
    @Bean
    public CommandLineRunner runner(ChatClient.Builder builder) {
        return args -> {
            ChatClient chatClient = builder.build();
            String response = chatClient.prompt("ジョークを教えてください").call().content();
            System.out.println(response);
        };
    }
}

3. 主要な機能の詳細

チャットクライアントAPI

@Service
public class ChatService {
    private final ChatClient chatClient;

    public ChatService(ChatClient.Builder builder) {
        this.chatClient = builder.build();
    }

    public String getResponse(String prompt) {
        return chatClient.prompt(prompt).call().content();
    }
}

ベクトルストアの使用

@Service
public class VectorStoreService {
    private final VectorStore vectorStore;

    public void storeEmbedding(String text, float[] embedding) {
        vectorStore.add(text, embedding);
    }

    public List<String> searchSimilar(float[] queryEmbedding) {
        return vectorStore.search(queryEmbedding);
    }
}

4. 実践的な使用例

ドキュメントQ&Aシステム

@Service
public class DocumentQAService {
    private final ChatClient chatClient;
    private final VectorStore vectorStore;

    public String answerQuestion(String question) {
        // 質問の埋め込みを生成
        float[] questionEmbedding = generateEmbedding(question);
        
        // 関連ドキュメントを検索
        List<String> relevantDocs = vectorStore.search(questionEmbedding);
        
        // コンテキストを構築
        String context = String.join("\n", relevantDocs);
        
        // 質問に回答
        return chatClient.prompt(
            "以下のコンテキストに基づいて質問に答えてください:\n" +
            "コンテキスト:" + context + "\n" +
            "質問:" + question
        ).call().content();
    }
}

5. ベストプラクティス

  1. APIキーの管理

    • 環境変数や設定ファイルで安全に管理
    • 本番環境では適切な認証情報管理システムを使用
  2. エラーハンドリング

    • 適切な例外処理の実装
    • レート制限への対応
  3. パフォーマンス最適化

    • キャッシュの活用
    • 非同期処理の適切な使用
  4. セキュリティ考慮事項

    • 入力の検証
    • 出力のサニタイズ
    • 適切なアクセス制御

まとめ

Spring AIは、AI機能をSpringアプリケーションに統合するための強力なフレームワークです。その柔軟性と拡張性により、様々なAIユースケースに対応することができます。

参考資料

0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?