| 此版本仍在开发中,尚未被视为稳定版本。最新的快照版本请使用 Spring AI 1.0.0-SNAPSHOT! | 
watsonx.ai 聊天
借助 watsonx.ai 您可以在本地运行各种大型语言模型 (LLM) 并从中生成文本。
Spring AI watsonx.ai 支持使用WatsonxAiChatModel.
自动配置
Spring AI 为 watsonx.ai Chat 客户端提供 Spring Boot 自动配置。
要启用它,请将以下依赖项添加到项目的 Maven 中pom.xml文件:
<dependency>
   <groupId>org.springframework.ai</groupId>
   <artifactId>spring-ai-watsonx-ai-spring-boot-starter</artifactId>
</dependency>或发送到您的 Gradlebuild.gradlebuild 文件。
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 | |
| 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 聊天模型。 | 真 | 
| 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 新令牌 | 设置 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 生成三个连续的换行符时,它将终止。在生成 Min tokens 参数中指定的令牌数之前,将忽略停止序列。 | - | 
| 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 是要使用的 LLM 模型的标识符。 | 谷歌/flan-ul2 | 
运行时选项
WatsonxAiChatOptions.java 提供模型配置,例如要使用的模型、温度、频率损失等。
启动时,可以使用WatsonxAiChatModel(api, options)constructor 或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您还可以使用使用 ChatOptionsBuilder#builder() 创建的可移植 ChatOptions 实例。 | 
| 有关更多信息,请访问 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;
    }
}