此版本仍在开发中,尚未被视为稳定版本。最新的快照版本请使用 Spring AI 1.0.0-SNAPSHOT!spring-doc.cadn.net.cn

基岩版 Converse API

Amazon Bedrock Converse API 为对话式 AI 模型提供了一个统一的接口,具有增强功能,包括函数/工具调用、多模态输入和流式响应。spring-doc.cadn.net.cn

Bedrock Converse API 具有以下高级功能:spring-doc.cadn.net.cn

Bedrock Converse API 提供跨多个模型提供商的统一接口,同时处理特定于 AWS 的身份验证和基础设施问题。

根据 Bedrock 的建议,Spring AI 正在过渡到使用 Amazon Bedrock 的 Converse API 来实现 Spring AI 中的所有聊天对话。 虽然现有的InvokeModel API支持对话应用程序,我们强烈建议采用 Converse API 以获得以下几个主要好处:spring-doc.cadn.net.cn

Converse API 不支持嵌入作,因此这些作将保留在当前 API 中,而嵌入模型功能将保留在现有的InvokeModel API将得到维护spring-doc.cadn.net.cn

先决条件

请参阅 Amazon Bedrock 入门以设置 API 访问spring-doc.cadn.net.cn

自动配置

添加spring-ai-bedrock-converse-spring-boot-starter依赖项添加到项目的 Mavenpom.xml或 Gradlebuild.gradlebuild 文件:spring-doc.cadn.net.cn

<dependency>
    <groupId>org.springframework.ai</groupId>
    <artifactId>spring-ai-bedrock-converse-spring-boot-starter</artifactId>
</dependency>
dependencies {
    implementation 'org.springframework.ai:spring-ai-bedrock-converse-spring-boot-starter'
}
请参阅 Dependency Management 部分,将 Spring AI BOM 添加到您的构建文件中。

聊天属性

前缀spring.ai.bedrock.aws是用于配置与 AWS Bedrock 的连接的属性前缀。spring-doc.cadn.net.cn

财产 描述 违约

spring.ai.bedrock.aws.regionspring-doc.cadn.net.cn

要使用的 AWS 区域。spring-doc.cadn.net.cn

us-east-1 (美国东部-1)spring-doc.cadn.net.cn

spring.ai.bedrock.aws.timeoutspring-doc.cadn.net.cn

要使用的 AWS 超时。spring-doc.cadn.net.cn

5 分钟spring-doc.cadn.net.cn

spring.ai.bedrock.aws.access-keyspring-doc.cadn.net.cn

AWS 访问密钥。spring-doc.cadn.net.cn

-spring-doc.cadn.net.cn

spring.ai.bedrock.aws.secret-keyspring-doc.cadn.net.cn

AWS 密钥。spring-doc.cadn.net.cn

-spring-doc.cadn.net.cn

spring.ai.bedrock.aws.session-tokenspring-doc.cadn.net.cn

用于临时凭证的 AWS 会话令牌。spring-doc.cadn.net.cn

-spring-doc.cadn.net.cn

前缀spring.ai.bedrock.converse.chat是为 Converse API 配置聊天模型实现的属性前缀。spring-doc.cadn.net.cn

财产 描述 违约

spring.ai.bedrock.converse.chat.enabledspring-doc.cadn.net.cn

启用 Bedrock Converse 聊天模型。spring-doc.cadn.net.cn

spring-doc.cadn.net.cn

spring.ai.bedrock.converse.chat.options.modelspring-doc.cadn.net.cn

要使用的模型 ID。您可以使用 Supported models (支持的模型) 和 model (模型) 功能spring-doc.cadn.net.cn

没有。从 AWS Bedrock 控制台中选择您的 modelIdspring-doc.cadn.net.cn

spring.ai.bedrock.converse.chat.options.temperaturespring-doc.cadn.net.cn

控制输出的随机性。值的范围可以超过 [0.0,1.0]spring-doc.cadn.net.cn

0.8spring-doc.cadn.net.cn

spring.ai.bedrock.converse.chat.options.top-pspring-doc.cadn.net.cn

采样时要考虑的 token 的最大累积概率。spring-doc.cadn.net.cn

AWS Bedrock 默认spring-doc.cadn.net.cn

spring.ai.bedrock.converse.chat.options.top-kspring-doc.cadn.net.cn

用于生成下一个令牌的令牌选项数。spring-doc.cadn.net.cn

AWS Bedrock 默认spring-doc.cadn.net.cn

spring.ai.bedrock.converse.chat.options.max 代币spring-doc.cadn.net.cn

生成的响应中的最大令牌数。spring-doc.cadn.net.cn

500spring-doc.cadn.net.cn

运行时选项

使用便携式ChatOptionsFunctionCallingOptions用于创建模型配置的便携式构建器,例如 temperature、maxToken、topP 等。spring-doc.cadn.net.cn

启动时,可以使用BedrockConverseProxyChatModel(api, options)constructor 或spring.ai.bedrock.converse.chat.options.*性能。spring-doc.cadn.net.cn

在运行时,您可以通过向Prompt叫:spring-doc.cadn.net.cn

var options = FunctionCallingOptions.builder()
        .withModel("anthropic.claude-3-5-sonnet-20240620-v1:0")
        .withTemperature(0.6)
        .withMaxTokens(300)
        .withFunctionCallbacks(List.of(FunctionCallback.builder()
            .description("Get the weather in location. Return temperature in 36°F or 36°C format. Use multi-turn if needed.")
            .function("getCurrentWeather", new WeatherService())
            .inputType(WeatherService.Request.class)
            .build()))
        .build();

ChatResponse response = chatModel.call(new Prompt("What is current weather in Amsterdam?", options));

工具/函数调用

Bedrock Converse API 支持函数调用功能,允许模型在对话期间使用工具。下面是如何定义和使用函数的示例:spring-doc.cadn.net.cn

@Bean
@Description("Get the weather in location. Return temperature in 36°F or 36°C format.")
public Function<Request, Response> weatherFunction() {
    return new MockWeatherService();
}

String response = ChatClient.create(this.chatModel)
        .prompt("What's the weather like in Boston?")
        .function("weatherFunction")
        .call()
        .content();

Samples控制器

创建一个新的 Spring Boot 项目并添加spring-ai-bedrock-converse-spring-boot-starter添加到您的依赖项中。spring-doc.cadn.net.cn

添加application.properties文件src/main/resources:spring-doc.cadn.net.cn

spring.ai.bedrock.aws.region=eu-central-1
spring.ai.bedrock.aws.timeout=10m
spring.ai.bedrock.aws.access-key=${AWS_ACCESS_KEY_ID}
spring.ai.bedrock.aws.secret-key=${AWS_SECRET_ACCESS_KEY}
# session token is only required for temporary credentials
spring.ai.bedrock.aws.session-token=${AWS_SESSION_TOKEN}

spring.ai.bedrock.converse.chat.options.temperature=0.8
spring.ai.bedrock.converse.chat.options.top-k=15

下面是一个使用 chat 模型的示例控制器:spring-doc.cadn.net.cn

@RestController
public class ChatController {

    private final ChatClient chatClient;

    @Autowired
    public ChatController(ChatClient.Builder builder) {
        this.chatClient = builder.build();
    }

    @GetMapping("/ai/generate")
    public Map generate(@RequestParam(value = "message", defaultValue = "Tell me a joke") String message) {
        return Map.of("generation", this.chatClient.prompt(message).call().content());
    }

    @GetMapping("/ai/generateStream")
    public Flux<ChatResponse> generateStream(@RequestParam(value = "message", defaultValue = "Tell me a joke") String message) {
        return this.chatClient.prompt(message).stream().content();
    }
}