watsonx.ai 聊天
通过 watsonx.ai,您可以在本地运行各种大型语言模型(LLMs)并从中生成文本。Spring AI 通过 WatsonxAiChatModel 支持 watsonx.ai 的文本生成功能。
先决条件
你首先需要拥有一个 watsonx.ai 的 SaaS 实例(以及一个 IBM Cloud 账户)。
请参考 免费试用 免费试用 watsonx.ai。
更多信息可以在这里找到。
自动配置
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 | 流式端点 | ml/v1/text/generation_stream?version=2023-05-29 | 
| spring.ai.watsonx.ai.text-endpoint | 文本端点 | ml/v1/text/generation?version=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 | 解码是模型用于选择生成输出中的 token 的过程。 | greedy | 
| spring.ai.watsonx.ai.chat.options.max-new-tokens | 设置 LLM 跟随的 token 限制。 | 20 | 
| spring.ai.watsonx.ai.chat.options.min-new-tokens | 设置 LLM 必须生成的 token 数量。 | 0 | 
| spring.ai.watsonx.ai.chat.options.stop-sequences | 设置 LLM 何时停止。(例如,["\n\n\n"])当 LLM 生成三个连续换行符时,它将终止。在生成 Min tokens 参数指定的 token 数量之前,停止序列将被忽略。 | - | 
| 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 | 模型是要使用的 LLM 模型的标识符。 | google/flan-ul2 | 
运行时选项
WatsonxAiChatOptions.java 提供了模型的配置选项,例如使用的模型、温度(temperature)、频率惩罚(frequency penalty)等。
在启动时,默认选项可以通过 WatsonxAiChatModel(api, options) 构造函数或 spring.ai.watsonxai.chat.options.* 属性进行配置。
在运行时,你可以通过向 Prompt 调用添加新的、特定于请求的选项来覆盖默认选项。例如,要为特定请求覆盖默认的模型和温度:
ChatResponse response = chatModel.call(
    new Prompt(
        "Generate the names of 5 famous pirates.",
        WatsonxAiChatOptions.builder()
            .temperature(0.4)
        .build()
    ));
除了特定于模型的 WatsonxAiChatOptions.java 外,你还可以使用可移植的 ChatOptions 实例,该实例可以通过 ChatOptionsBuilder#builder() 创建。
欲了解更多信息,请访问 watsonx-parameters-info
使用示例
public class MyClass {
    private static final String MODEL = "google/flan-ul2";
    private final WatsonxAiChatModel chatModel;
    @Autowired
    MyClass(WatsonxAiChatModel chatModel) {
        this.chatModel = chatModel;
    }
    public String generate(String userInput) {
        WatsonxAiChatOptions options = WatsonxAiChatOptions.builder()
            .model(MODEL)
            .decodingMethod("sample")
            .randomSeed(1)
            .build();
        Prompt prompt = new Prompt(new SystemMessage(userInput), options);
        var results = this.chatModel.call(prompt);
        var generatedText = results.getResult().getOutput().getContent();
        return generatedText;
    }
    public String generateStream(String userInput) {
        WatsonxAiChatOptions options = WatsonxAiChatOptions.builder()
            .model(MODEL)
            .decodingMethod("greedy")
            .randomSeed(2)
            .build();
        Prompt prompt = new Prompt(new SystemMessage(userInput), options);
        var results = this.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;
    }
}