Titan Chat
Amazon Titan 基础模型 (FM) 通过一个完全托管的 API 为客户提供广泛的高性能图像、多模态嵌入和文本模型选择。Amazon Titan 模型由 AWS 创建,并在大型数据集上预先训练,使它们成为强大的通用模型,旨在支持各种用例,同时还支持负责任地使用 AI。按原样使用它们或使用你自己的数据私下进行自定义。
Amazon Titan foundation models (FMs) provide customers with a breadth of high-performing image, multimodal embeddings, and text model choices, via a fully managed API. Amazon Titan models are created by AWS and pretrained on large datasets, making them powerful, general-purpose models built to support a variety of use cases, while also supporting the responsible use of AI. Use them as is or privately customize them with your own data.
AWS Bedrock Titan Model Page 和 Amazon Bedrock User Guide 包含有关如何使用 AWS 托管模型的详细信息。
The AWS Bedrock Titan Model Page and Amazon Bedrock User Guide contains detailed information on how to use the AWS hosted model.
Prerequisites
请参阅 Spring AI documentation on Amazon Bedrock 以设置 API 访问。
Refer to the Spring AI documentation on Amazon Bedrock for setting up API access.
Add Repositories and BOM
Spring AI 工件发布在 Spring Milestone 和 Snapshot 存储库中。有关将这些存储库添加到你的构建系统的说明,请参阅 Repositories 部分。
Spring AI artifacts are published in Spring Milestone and Snapshot repositories. Refer to the Repositories section to add these repositories to your build system.
为了帮助进行依赖项管理,Spring AI 提供了一个 BOM(物料清单)以确保在整个项目中使用一致版本的 Spring AI。有关将 Spring AI BOM 添加到你的构建系统的说明,请参阅 Dependency Management 部分。
To help with dependency management, Spring AI provides a BOM (bill of materials) to ensure that a consistent version of Spring AI is used throughout the entire project. Refer to the Dependency Management section to add the Spring AI BOM to your build system.
Auto-configuration
将 spring-ai-bedrock-ai-spring-boot-starter
依赖项添加到项目 Maven 的 pom.xml
文件:
Add the spring-ai-bedrock-ai-spring-boot-starter
dependency to your project’s Maven pom.xml
file:
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-bedrock-ai-spring-boot-starter</artifactId>
</dependency>
或添加到 Gradle build.gradle
构建文件中。
or to your Gradle build.gradle
build file.
dependencies {
implementation 'org.springframework.ai:spring-ai-bedrock-ai-spring-boot-starter'
}
|
Refer to the Dependency Management section to add the Spring AI BOM to your build file. |
Enable Titan Chat
默认情况下,Titan 模型处于禁用状态。要启用它,请将 spring.ai.bedrock.titan.chat.enabled
属性设置为 true
。导出环境变量是一种设置此配置属性的方法:
By default the Titan model is disabled.
To enable it set the spring.ai.bedrock.titan.chat.enabled
property to true
.
Exporting environment variable is one way to set this configuration property:
export SPRING_AI_BEDROCK_TITAN_CHAT_ENABLED=true
Chat Properties
spring.ai.bedrock.aws
前缀是配置与 AWS Bedrock 的连接的属性前缀。
The prefix spring.ai.bedrock.aws
is the property prefix to configure the connection to AWS Bedrock.
Property | Description | Default |
---|---|---|
spring.ai.bedrock.aws.region |
AWS region to use. |
us-east-1 |
spring.ai.bedrock.aws.access-key |
AWS access key. |
- |
spring.ai.bedrock.aws.secret-key |
AWS secret key. |
- |
前缀 spring.ai.bedrock.titan.chat
是一个属性前缀,它可以配置 Titan 的聊天客户端实现。
The prefix spring.ai.bedrock.titan.chat
is the property prefix that configures the chat client implementation for Titan.
Property | Description | Default |
---|---|---|
spring.ai.bedrock.titan.chat.enable |
Enable Bedrock Titan chat client. Disabled by default |
false |
spring.ai.bedrock.titan.chat.model |
The model id to use. See the TitanChatBedrockApi#TitanChatModel for the supported models. |
amazon.titan-text-lite-v1 |
spring.ai.bedrock.titan.chat.options.temperature |
Controls the randomness of the output. Values can range over [0.0,1.0] |
0.7 |
spring.ai.bedrock.titan.chat.options.topP |
The maximum cumulative probability of tokens to consider when sampling. |
AWS Bedrock default |
spring.ai.bedrock.titan.chat.options.stopSequences |
Configure up to four sequences that the generative recognizes. After a stop sequence, the generative stops generating further tokens. The returned text doesn’t contain the stop sequence. |
AWS Bedrock default |
spring.ai.bedrock.titan.chat.options.maxTokenCount |
Specify the maximum number of tokens to use in the generated response. Note that the models may stop before reaching this maximum. This parameter only specifies the absolute maximum number of tokens to generate. We recommend a limit of 4,000 tokens for optimal performance. |
AWS Bedrock default |
查看 TitanChatBedrockApi#TitanChatModel 了解其他模型 ID。支持的值为: amazon.titan-text-lite-v1
和 amazon.titan-text-express-v1
。模型 ID 值也可在 AWS Bedrock documentation for base model IDs 中找到。
Look at the TitanChatBedrockApi#TitanChatModel for other model IDs.
Supported values are: amazon.titan-text-lite-v1
and amazon.titan-text-express-v1
.
Model ID values can also be found in the AWS Bedrock documentation for base model IDs.
所有带有 |
All properties prefixed with |
Chat Options
BedrockTitanChatOptions.java 提供模型配置,如温度、topP 等。
The BedrockTitanChatOptions.java provides model configurations, such as temperature, topP, etc.
在启动时,可以使用 BedrockTitanChatClient(api, options)
构造函数或 spring.ai.bedrock.titan.chat.options.*
属性来配置默认选项。
On start-up, the default options can be configured with the BedrockTitanChatClient(api, options)
constructor or the spring.ai.bedrock.titan.chat.options.*
properties.
在运行时,你可以通过向 Prompt
调用添加新的请求特定选项来覆盖默认选项。例如,覆盖特定请求的默认温度:
At run-time you can override the default options by adding new, request specific, options to the Prompt
call.
For example to override the default temperature for a specific request:
ChatResponse response = chatClient.call(
new Prompt(
"Generate the names of 5 famous pirates.",
BedrockTitanChatOptions.builder()
.withTemperature(0.4)
.build()
));
|
In addition to the model specific BedrockTitanChatOptions you can use a portable ChatOptions instance, created with the ChatOptionsBuilder#builder(). |
Sample Controller (Auto-configuration)
Create一个新的 Spring Boot 项目,并将 `spring-ai-bedrock-ai-spring-boot-starter`添加到您的 pom(或 gradle)依赖项。
Create a new Spring Boot project and add the spring-ai-bedrock-ai-spring-boot-starter
to your pom (or gradle) dependencies.
向 src/main/resources
目录中添加一个 application.properties
文件,以启用并配置 Titan 聊天客户端:
Add a application.properties
file, under the src/main/resources
directory, to enable and configure the Titan Chat client:
spring.ai.bedrock.aws.region=eu-central-1
spring.ai.bedrock.aws.access-key=${AWS_ACCESS_KEY_ID}
spring.ai.bedrock.aws.secret-key=${AWS_SECRET_ACCESS_KEY}
spring.ai.bedrock.titan.chat.enabled=true
spring.ai.bedrock.titan.chat.options.temperature=0.8
将 |
replace the |
这将创建一个 BedrockTitanChatClient
实现,您可以在将该实现注入自己的类中。下面是一个简单的 @Controller
类的示例,它使用了聊天客户端来生成文本。
This will create a BedrockTitanChatClient
implementation that you can inject into your class.
Here is an example of a simple @Controller
class that uses the chat client for text generations.
@RestController
public class ChatController {
private final BedrockTitanChatClient chatClient;
@Autowired
public ChatController(BedrockTitanChatClient chatClient) {
this.chatClient = chatClient;
}
@GetMapping("/ai/generate")
public Map generate(@RequestParam(value = "message", defaultValue = "Tell me a joke") String message) {
return Map.of("generation", chatClient.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 chatClient.stream(prompt);
}
}
Manual Configuration
BedrockTitanChatClient实现 ChatClient`和 `StreamingChatClient
,并使用 Low-level TitanChatBedrockApi Client连接到 Bedrock Titanic 服务。
The BedrockTitanChatClient implements the ChatClient
and StreamingChatClient
and uses the Low-level TitanChatBedrockApi Client to connect to the Bedrock Titanic service.
将 spring-ai-bedrock
依赖项添加到项目的 Maven pom.xml
文件:
Add the spring-ai-bedrock
dependency to your project’s Maven pom.xml
file:
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-bedrock</artifactId>
</dependency>
或添加到 Gradle build.gradle
构建文件中。
or to your Gradle build.gradle
build file.
dependencies {
implementation 'org.springframework.ai:spring-ai-bedrock'
}
|
Refer to the Dependency Management section to add the Spring AI BOM to your build file. |
接下来,创建一个 BedrockTitanChatClient 并将其用于文本生成:
Next, create an BedrockTitanChatClient and use it for text generations:
TitanChatBedrockApi titanApi = new TitanChatBedrockApi(
TitanChatModel.TITAN_TEXT_EXPRESS_V1.id(),
EnvironmentVariableCredentialsProvider.create(),
Region.US_EAST_1.id(), new ObjectMapper());
BedrockTitanChatClient chatClient = new BedrockTitanChatClient(titanApi,
BedrockTitanChatOptions.builder()
.withTemperature(0.6f)
.withTopP(0.8f)
.withMaxTokenCount(100)
.build());
ChatResponse response = chatClient.call(
new Prompt("Generate the names of 5 famous pirates."));
// Or with streaming responses
Flux<ChatResponse> response = chatClient.stream(
new Prompt("Generate the names of 5 famous pirates."));
Low-level TitanChatBedrockApi Client
TitanChatBedrockApi 在 AWS Bedrock Bedrock Titan models 之上提供轻量级 Java 客户端。
The TitanChatBedrockApi provides is lightweight Java client on top of AWS Bedrock Bedrock Titan models.
下面的类图说明了 TitanChatBedrockApi 接口和构建模块:
Following class diagram illustrates the TitanChatBedrockApi interface and building blocks:
客户端支持 amazon.titan-text-lite-v1
和 amazon.titan-text-express-v1
模型,用于同步(例如 chatCompletion()
)和流式(例如 chatCompletionStream()
)响应。
Client supports the amazon.titan-text-lite-v1
and amazon.titan-text-express-v1
models for both synchronous (e.g. chatCompletion()
) and streaming (e.g. chatCompletionStream()
) responses.
下面是一个简单的片段,说明如何以编程方式使用 API:
Here is a simple snippet how to use the api programmatically:
TitanChatBedrockApi titanBedrockApi = new TitanChatBedrockApi(TitanChatCompletionModel.TITAN_TEXT_EXPRESS_V1.id(),
Region.EU_CENTRAL_1.id());
TitanChatRequest titanChatRequest = TitanChatRequest.builder("Give me the names of 3 famous pirates?")
.withTemperature(0.5f)
.withTopP(0.9f)
.withMaxTokenCount(100)
.withStopSequences(List.of("|"))
.build();
TitanChatResponse response = titanBedrockApi.chatCompletion(titanChatRequest);
Flux<TitanChatResponseChunk> response = titanBedrockApi.chatCompletionStream(titanChatRequest);
List<TitanChatResponseChunk> results = response.collectList().block();
关注 TitanChatBedrockApi 的 JavaDoc 了解详细信息。
Follow the TitanChatBedrockApi's JavaDoc for further information.