watsonx.ai チャット

watsonx.ai (英語) を使用すると、さまざまな大規模言語モデル (LLM) をローカルで実行し、そこからテキストを生成できます。Spring AI は、WatsonxAiChatModel を使用した watsonx.ai テキスト生成をサポートしています。

前提条件

まず、watsonx.ai の SaaS インスタンス (および IBM Cloud アカウント) が必要です。

Refer to free-trial (英語) to try watsonx.ai for free

More info can be found here (英語)

自動構成

Spring AI は、watsonx.ai チャットクライアントの Spring Boot 自動構成を提供します。これを有効にするには、プロジェクトの Maven pom.xml ファイルに次の依存関係を追加します。

<dependency>
   <groupId>org.springframework.ai</groupId>
   <artifactId>spring-ai-watsonx-ai-spring-boot-starter</artifactId>
</dependency>

または、Gradle build.gradle ビルドファイルに保存します。

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

チャットのプロパティ

接続プロパティ

プレフィックス spring.ai.watsonx.ai は、watsonx.ai に接続するためのプロパティプレフィックスとして使用されます。

プロパティ 説明 デフォルト

spring.ai.watsonx.ai.base-url

接続先の URL

us-south.ml.cloud.ibm.com (英語)

spring.ai.watsonx.ai.stream-endpoint

ストリーミングエンドポイント

世代 / ストリーム ? バージョン =2023-05-29

spring.ai.watsonx.ai.text-endpoint

テキストエンドポイント

生成 / テキスト ? バージョン =2023-05-29

spring.ai.watsonx.ai.project-id

プロジェクト ID

-

spring.ai.watsonx.ai.iam-token

IBM Cloud アカウントの IAM トークン

-

プロパティの構成

プレフィックス spring.ai.watsonx.ai.chat は、Watsonx.AI のチャットモデルの実装を構成できるプロパティプレフィックスです。

プロパティ 説明 デフォルト

spring.ai.watsonx.ai.chat.enabled

Watsonx.AI チャットモデルを有効にします。

true

spring.ai.watsonx.ai.chat.options.temperature

モデルの温度。温度を上げると、モデルはより創造的に答えられるようになります。

0.7

spring.ai.watsonx.ai.chat.options.top-p

top-k と連携します。値が大きいほど (0.95 など)、より多様なテキストが生成され、値が小さいほど (0.2 など)、より焦点が絞られた控えめなテキストが生成されます。

1.0

spring.ai.watsonx.ai.chat.options.top-k

意味のない回答が生成される可能性を減らします。値が高いほど (例: 100) 回答の多様性が増し、値が低いほど (例: 10) 回答の保守性が増します。

50

spring.ai.watsonx.ai.chat.options.decoding-method

デコードは、生成された出力内のトークンを選択するためにモデルが使用するプロセスです。

よく深い

spring.ai.watsonx.ai.chat.options.max-new-tokens

LLM が追跡するトークンの制限を設定します。

20

spring.ai.watsonx.ai.chat.options.min-new-tokens

LLM が生成する必要があるトークンの数を設定します。

0

spring.ai.watsonx.ai.chat.options.stop-sequences

LLM を停止するタイミングを設定します。(例: ["\n\n\n" ]) LLM が 3 つの連続した改行を生成すると、LLM は終了します。停止シーケンスは、最小トークンパラメーターで指定された数のトークンが生成されるまで無視されます。

-

spring.ai.watsonx.ai.chat.options.repetition-penalty

繰り返しをどの程度強くペナルティするかを設定します。値が大きいほど (1.8 など)、繰り返しに対するペナルティがより強くなり、値が小さいほど (1.1 など) ペナルティがより緩やかになります。

1.0

spring.ai.watsonx.ai.chat.options.random-seed

繰り返し可能な結果を生成するには、毎回同じランダムシード値を設定します。

ランダムに生成された

spring.ai.watsonx.ai.chat.options.model

Model is the identifier of the LLM Model to be used.

google/flan-ul2

ランタイムオプション

WatsonxAiChatOptions.java [GitHub] (英語) は、使用するモデル、温度、周波数ペナルティなどのモデル構成を提供します。

起動時に、WatsonxAiChatModel(api, options) コンストラクターまたは spring.ai.watsonxai.chat.options.* プロパティを使用してデフォルトのオプションを構成できます。

実行時に、新しいリクエスト固有のオプションを Prompt 呼び出しに追加することで、デフォルトのオプションをオーバーライドできます。たとえば、特定のリクエストのデフォルトのモデルと温度をオーバーライドするには、次のようにします。

ChatResponse response = chatModel.call(
    new Prompt(
        "Generate the names of 5 famous pirates.",
        WatsonxAiChatOptions.builder()
            .withTemperature(0.4)
        .build()
    ));
モデル固有の WatsonxAiChatOptions.java [GitHub] (英語) に加えて、ChatOptionsBuilder#builder() [GitHub] (英語) で作成されたポータブル ChatOptions [GitHub] (英語) インスタンスを使用できます。
For more information go to watsonx-parameters-info (英語)

使用例

public class MyClass {

    private final static String MODEL = "google/flan-ul2";
    private final WatsonxAiChatModel chatModel;

    @Autowired
    MyClass(WatsonxAiChatModel chatModel) {
        this.chatModel = chatModel;
    }

    public String generate(String userInput) {

        WatsonxAiOptions options = WatsonxAiOptions.create()
            .withModel(MODEL)
            .withDecodingMethod("sample")
            .withRandomSeed(1);

        Prompt prompt = new Prompt(new SystemMessage(userInput), options);

        var results = chatModel.call(prompt);

        var generatedText = results.getResult().getOutput().getContent();

        return generatedText;
    }

    public String generateStream(String userInput) {

        WatsonxAiOptions options = WatsonxAiOptions.create()
            .withModel(MODEL)
            .withDecodingMethod("greedy")
            .withRandomSeed(2);

        Prompt prompt = new Prompt(new SystemMessage(userInput), options);

        var results = chatModel.stream(prompt).collectList().block(); // wait till the stream is resolved (completed)

        var generatedText = results.stream()
            .map(generation -> generation.getResult().getOutput().getContent())
            .collect(Collectors.joining());

        return generatedText;
    }

}