Cohere 聊天

提供 Bedrock Cohere 聊天模型。 将生成式 AI 功能集成到基本应用程序和工作流中,以改善业务成果。spring-doc.cadn.net.cn

AWS Bedrock Cohere 模型页面Amazon Bedrock 用户指南包含有关如何使用 AWS 托管模型的详细信息。spring-doc.cadn.net.cn

先决条件

添加存储库和 BOM

Spring AI 工件发布在 Spring Milestone 和 Snapshot 存储库中。请参阅 Repositories 部分,将这些存储库添加到您的构建系统中。spring-doc.cadn.net.cn

为了帮助进行依赖项管理,Spring AI 提供了一个 BOM(物料清单),以确保在整个项目中使用一致的 Spring AI 版本。请参阅依赖项管理部分,将 Spring AI BOM 添加到您的构建系统中。spring-doc.cadn.net.cn

自动配置

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

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

或发送到您的 Gradlebuild.gradlebuild 文件。spring-doc.cadn.net.cn

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

启用 Cohere 聊天支持

默认情况下,Cohere 模型处于禁用状态。 要启用它,请将spring.ai.bedrock.cohere.chat.enabledproperty 设置为true. 导出环境变量是设置此配置属性的一种方法:spring-doc.cadn.net.cn

export SPRING_AI_BEDROCK_COHERE_CHAT_ENABLED=true

聊天属性

前缀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.cohere.chat是为 Cohere 配置聊天模型实现的属性前缀。spring-doc.cadn.net.cn

财产 描述 违约

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

启用或禁用对 Cohere 的支持spring-doc.cadn.net.cn

spring-doc.cadn.net.cn

spring.ai.bedrock.cohere.chat.modelspring-doc.cadn.net.cn

要使用的模型 ID。有关支持的模型,请参阅 CohereChatModelspring-doc.cadn.net.cn

cohere.command-text-v14spring-doc.cadn.net.cn

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

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

0.7spring-doc.cadn.net.cn

spring.ai.bedrock.cohere.chat.options.topPspring-doc.cadn.net.cn

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

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

spring.ai.bedrock.cohere.chat.options.topKspring-doc.cadn.net.cn

指定模型用于生成下一个标记的标记选择数spring-doc.cadn.net.cn

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

spring.ai.bedrock.cohere.chat.options.maxTokensspring-doc.cadn.net.cn

指定要在生成的响应中使用的最大令牌数。spring-doc.cadn.net.cn

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

spring.ai.bedrock.cohere.chat.options.stopSequencesspring-doc.cadn.net.cn

配置模型可识别的最多 4 个序列。spring-doc.cadn.net.cn

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

spring.ai.bedrock.cohere.chat.options.returnLikelihoodsspring-doc.cadn.net.cn

标记可能性与响应一起返回。spring-doc.cadn.net.cn

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

spring.ai.bedrock.cohere.chat.options.numGenerationsspring-doc.cadn.net.cn

模型应返回的最大代数。spring-doc.cadn.net.cn

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

spring.ai.bedrock.cohere.chat.options.logitBiasspring-doc.cadn.net.cn

防止模型生成不需要的令牌或激励模型包含所需的令牌。spring-doc.cadn.net.cn

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

spring.ai.bedrock.cohere.chat.options.truncatespring-doc.cadn.net.cn

指定 API 如何处理长于最大令牌长度的输入spring-doc.cadn.net.cn

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

查看 CohereChatModel 以获取其他模型 ID。 支持的值为:cohere.command-light-text-v14cohere.command-text-v14. 模型 ID 值也可以在 AWS Bedrock 文档中找到基本模型 IDspring-doc.cadn.net.cn

所有前缀为spring.ai.bedrock.cohere.chat.options可以通过将特定于请求的运行时选项添加到Prompt叫。

运行时选项

BedrockCohereChatOptions.java 提供模型配置,如 temperature、topK、topP 等。spring-doc.cadn.net.cn

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

在运行时,您可以通过向Prompt叫。 例如,要覆盖特定请求的默认温度:spring-doc.cadn.net.cn

ChatResponse response = chatModel.call(
    new Prompt(
        "Generate the names of 5 famous pirates.",
        BedrockCohereChatOptions.builder()
            .withTemperature(0.4)
        .build()
    ));
除了特定于模型的 BedrockCohereChatOptions 之外,您还可以使用通过 ChatOptionsBuilder#builder() 创建的可移植 ChatOptions 实例。

Samples控制器

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

添加application.properties文件中的src/main/resources目录中,以启用和配置 Cohere 聊天模型:spring-doc.cadn.net.cn

spring.ai.bedrock.aws.region=eu-central-1
spring.ai.bedrock.aws.timeout=1000ms
spring.ai.bedrock.aws.access-key=${AWS_ACCESS_KEY_ID}
spring.ai.bedrock.aws.secret-key=${AWS_SECRET_ACCESS_KEY}

spring.ai.bedrock.cohere.chat.enabled=true
spring.ai.bedrock.cohere.chat.options.temperature=0.8
regions,access-keysecret-key替换为您的 AWS 凭证。

这将创建一个BedrockCohereChatModel实现,您可以将其注入到您的类中。 下面是一个简单的示例@Controller使用 Chat 模型生成文本的类。spring-doc.cadn.net.cn

@RestController
public class ChatController {

    private final BedrockCohereChatModel chatModel;

    @Autowired
    public ChatController(BedrockCohereChatModel chatModel) {
        this.chatModel = chatModel;
    }

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

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

手动配置

BedrockCohereChatModel 实现了ChatModelStreamingChatModel并使用低级 CohereChatBedrockApi 客户端连接到 Bedrock Cohere 服务。spring-doc.cadn.net.cn

添加spring-ai-bedrock依赖项添加到项目的 Mavenpom.xml文件:spring-doc.cadn.net.cn

<dependency>
    <groupId>org.springframework.ai</groupId>
    <artifactId>spring-ai-bedrock</artifactId>
</dependency>

或发送到您的 Gradlebuild.gradlebuild 文件。spring-doc.cadn.net.cn

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

接下来,创建一个 BedrockCohereChatModel 并将其用于文本生成:spring-doc.cadn.net.cn

CohereChatBedrockApi api = new CohereChatBedrockApi(CohereChatModel.COHERE_COMMAND_V14.id(),
		EnvironmentVariableCredentialsProvider.create(),
		Region.US_EAST_1.id(),
		new ObjectMapper(),
		Duration.ofMillis(1000L));

BedrockCohereChatModel chatModel = new BedrockCohereChatModel(this.api,
	    BedrockCohereChatOptions.builder()
					.withTemperature(0.6)
					.withTopK(10)
					.withTopP(0.5)
					.withMaxTokens(678)
					.build());

ChatResponse response = this.chatModel.call(
    new Prompt("Generate the names of 5 famous pirates."));

// Or with streaming responses
Flux<ChatResponse> response = this.chatModel.stream(
    new Prompt("Generate the names of 5 famous pirates."));

低级 CohereChatBedrockApi 客户端

CohereChatBedrockApi 提供的是基于 AWS Bedrock Cohere Command 模型的轻量级 Java 客户端。spring-doc.cadn.net.cn

以下类图说明了 CohereChatBedrockApi 接口和构建块:spring-doc.cadn.net.cn

Bedrock Cohere Chat 低级 API

CohereChatBedrockApi 支持cohere.command-light-text-v14cohere.command-text-v14同步模型(例如chatCompletion()) 和流式处理(例如chatCompletionStream()) 请求。spring-doc.cadn.net.cn

以下是如何以编程方式使用 api 的简单代码段:spring-doc.cadn.net.cn

CohereChatBedrockApi cohereChatApi = new CohereChatBedrockApi(
	CohereChatModel.COHERE_COMMAND_V14.id(),
	Region.US_EAST_1.id(),
	Duration.ofMillis(1000L));

var request = CohereChatRequest
	.builder("What is the capital of Bulgaria and what is the size? What is the national anthem?")
	.withStream(false)
	.withTemperature(0.5)
	.withTopP(0.8)
	.withTopK(15)
	.withMaxTokens(100)
	.withStopSequences(List.of("END"))
	.withReturnLikelihoods(CohereChatRequest.ReturnLikelihoods.ALL)
	.withNumGenerations(3)
	.withLogitBias(null)
	.withTruncate(Truncate.NONE)
	.build();

CohereChatResponse response = this.cohereChatApi.chatCompletion(this.request);

var request = CohereChatRequest
	.builder("What is the capital of Bulgaria and what is the size? What it the national anthem?")
	.withStream(true)
	.withTemperature(0.5)
	.withTopP(0.8)
	.withTopK(15)
	.withMaxTokens(100)
	.withStopSequences(List.of("END"))
	.withReturnLikelihoods(CohereChatRequest.ReturnLikelihoods.ALL)
	.withNumGenerations(3)
	.withLogitBias(null)
	.withTruncate(Truncate.NONE)
	.build();

Flux<CohereChatResponse.Generation> responseStream = this.cohereChatApi.chatCompletionStream(this.request);
List<CohereChatResponse.Generation> responses = this.responseStream.collectList().block();