Spring AI 第二讲 之 Chat Model API 第九节 watsonx.ai Chat

通过 watsonx.ai,你可以在本地运行各种大型语言模型(LLM),并从中生成文本。Spring AI 通过 WatsonxAiChatModel 支持 watsonx.ai 文本生成。

您首先需要拥有一个 watsonx.ai 的 SaaS 实例(以及一个 IBM 云帐户)。

请参阅免费试用,免费试用 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。

PropertyDescriptionDefault

spring.ai.watsonx.ai.base-url

要连接的 URL

us-south.ml.cloud.ibm.com

spring.ai.watsonx.ai.stream-endpoint

流媒体端点

generation/stream?version=2023-05-29

spring.ai.watsonx.ai.text-endpoint

文本终端

generation/text?version=2023-05-29

spring.ai.watsonx.ai.project-id

项目 ID

-

spring.ai.watsonx.ai.iam-token

IBM 云账户 IAM 令牌

-

配置属性

spring.ai.watsonx.ai.chat 前缀是让你配置 Watsonx.AI 聊天模型实现的属性前缀。 

PropertyDescriptionDefault

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

解码是模型在生成的输出中选择标记的过程。

greedy

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 产生三个连续的换行符时就会终止。在生成 Min tokens 参数中指定的标记数之前,停止序列将被忽略。

-

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

设置对重复的惩罚力度。数值越大(如 1.8),对重复的惩罚力度就越大,而数值越小(如 1.1),惩罚力度就越宽松。

1.0

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

产生可重复的结果,每次设置相同的随机种子值。

randomly generated

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

模型是要使用的 LLM 模型的标识符。

google/flan-ul2

运行时选项

WatsonxAiChatOptions.java 提供了模型配置,如使用的模型、温度、频率惩罚等。

启动时,可使用 WatsonxAiChatModel(api, options) 构造函数或 spring.ai.watsonxai.chat.options.* 属性配置默认选项。

在运行时,你可以通过向提示调用添加新的、针对特定请求的选项来覆盖默认选项。例如,覆盖特定请求的默认模型和温度:

ChatResponse response = chatModel.call(
    new Prompt(
        "Generate the names of 5 famous pirates.",
        WatsonxAiChatOptions.builder()
            .withTemperature(0.4)
        .build()
    ));

除了特定于模型的 WatsonxAiChatOptions.java 之外,你还可以使用通过 ChatOptionsBuilder#builder() 创建的便携式 ChatOptions实例。

 使用示例

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;
    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值