Llama2 Chat
Meta’s Llama 2 Chat是 Llama 2 大语言模型集合的一部分。它擅长对话类应用程序,参数范围从 70 亿到 700 亿。利用公共数据集和超过 100 万个人的注释,Llama Chat 提供了情境感知对话。
Meta’s Llama 2 Chat is part of the Llama 2 collection of large language models. It excels in dialogue-based applications with a parameter scale ranging from 7 billion to 70 billion. Leveraging public datasets and over 1 million human annotations, Llama Chat offers context-aware dialogues.
在来自公共数据源的 2 万亿个语料库上接受训练,Llama-2-Chat 提供了广泛的知识,以进行深入的对话。严格的测试,包括 1000 多个小时的红队对战和注释,确保性能和安全性,使其成为 AI 驱动的对话的可靠选择。
Trained on 2 trillion tokens from public data sources, Llama-2-Chat provides extensive knowledge for insightful conversations. Rigorous testing, including over 1,000 hours of red-teaming and annotation, ensures both performance and safety, making it a reliable choice for AI-driven dialogues.
AWS Llama 2 Model Page和 Amazon Bedrock User Guide包含有关如何使用 AWS 托管模型的详细信息。
The AWS Llama 2 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 Llama2 Chat Support
默认情况下,Bedrock Llama2 模型处于禁用状态。要启用该模型,请将 spring.ai.bedrock.llama2.chat.enabled
属性设置为 true
。导出环境变量是一种设置此配置属性的方法:
By default the Bedrock Llama2 model is disabled.
To enable it set the spring.ai.bedrock.llama2.chat.enabled
property to true
.
Exporting environment variable is one way to set this configuration property:
export SPRING_AI_BEDROCK_LLAMA2_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.llama2.chat
是配置 Llama2 的聊天客户端实现的属性前缀。
The prefix spring.ai.bedrock.llama2.chat
is the property prefix that configures the chat client implementation for Llama2.
Property | Description | Default |
---|---|---|
spring.ai.bedrock.llama2.chat.enabled |
Enable or disable support for Llama2 |
false |
spring.ai.bedrock.llama2.chat.model |
The model id to use (See Below) |
meta.llama2-70b-chat-v1 |
spring.ai.bedrock.llama2.chat.options.temperature |
Controls the randomness of the output. Values can range over [0.0,1.0], inclusive. A value closer to 1.0 will produce responses that are more varied, while a value closer to 0.0 will typically result in less surprising responses from the model. This value specifies default to be used by the backend while making the call to the model. |
0.7 |
spring.ai.bedrock.llama2.chat.options.top-p |
The maximum cumulative probability of tokens to consider when sampling. The model uses combined Top-k and nucleus sampling. Nucleus sampling considers the smallest set of tokens whose probability sum is at least topP. |
AWS Bedrock default |
spring.ai.bedrock.llama2.chat.options.max-gen-len |
Specify the maximum number of tokens to use in the generated response. The model truncates the response once the generated text exceeds maxGenLen. |
300 |
查看 Llama2ChatBedrockApi#Llama2ChatModel以了解其他模型 ID。支持的另一个值是 meta.llama2-13b-chat-v1
。还可以从 AWS Bedrock documentation for base model IDs中找到模型 ID 值。
Look at Llama2ChatBedrockApi#Llama2ChatModel for other model IDs. The other value supported is meta.llama2-13b-chat-v1
.
Model ID values can also be found in the AWS Bedrock documentation for base model IDs.
所有带有 |
All properties prefixed with |
Chat Options
BedrockLlama2ChatOptions.java提供模型配置,例如 temperature、topK、topP 等。
The BedrockLlama2ChatOptions.java provides model configurations, such as temperature, topK, topP, etc.
在启动时,可以通过 BedrockLlama2ChatClient(api, options)
构造函数或 spring.ai.bedrock.llama2.chat.options.*
属性配置默认选项。
On start-up, the default options can be configured with the BedrockLlama2ChatClient(api, options)
constructor or the spring.ai.bedrock.llama2.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.",
BedrockLlama2ChatOptions.builder()
.withTemperature(0.4)
.build()
));
|
In addition to the model specific BedrockLlama2ChatOptions 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
文件,以启用和配置 Anthropic Chat 客户端:
Add a application.properties
file, under the src/main/resources
directory, to enable and configure the Anthropic 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.llama2.chat.enabled=true
spring.ai.bedrock.llama2.chat.options.temperature=0.8
将 |
replace the |
这将创建一个 BedrockLlama2ChatClient
实现,你可以将其注入到你的类中。这里是一个简单的 @Controller
类的示例,该类使用聊天客户端进行文本生成。
This will create a BedrockLlama2ChatClient
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 BedrockLlama2ChatClient chatClient;
@Autowired
public ChatController(BedrockLlama2ChatClient 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
BedrockLlama2ChatClient实现 ChatClient`和 `StreamingChatClient
,并使用 Low-level Llama2ChatBedrockApi Client连接到 Bedrock Anthropic 服务。
The BedrockLlama2ChatClient implements the ChatClient
and StreamingChatClient
and uses the Low-level Llama2ChatBedrockApi Client to connect to the Bedrock Anthropic 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. |
接下来,创建一个 BedrockLlama2ChatClient并使用它进行文本生成:
Next, create an BedrockLlama2ChatClient and use it for text generations:
Llama2ChatBedrockApi api = new Llama2ChatBedrockApi(Llama2ChatModel.LLAMA2_70B_CHAT_V1.id(),
EnvironmentVariableCredentialsProvider.create(), Region.US_EAST_1.id(), new ObjectMapper());
BedrockLlama2ChatClient chatClient = new BedrockLlama2ChatClient(api,
BedrockLlama2ChatOptions.builder()
.withTemperature(0.5f)
.withMaxGenLen(100)
.withTopP(0.9f).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 Llama2ChatBedrockApi Client
Llama2ChatBedrockApi提供轻量级 Java 客户端,基于 AWS Bedrock Meta Llama 2 and Llama 2 Chat models。
Llama2ChatBedrockApi provides is lightweight Java client on top of AWS Bedrock Meta Llama 2 and Llama 2 Chat models.
以下类图说明了 Llama2ChatBedrockApi 接口及其构建模块:
Following class diagram illustrates the Llama2ChatBedrockApi interface and building blocks:
Llama2ChatBedrockApi 支持 meta.llama2-13b-chat-v1
和 meta.llama2-70b-chat-v1
模型,用于同步(例如 chatCompletion()
)和流式(例如 chatCompletionStream()
)响应。
The Llama2ChatBedrockApi supports the meta.llama2-13b-chat-v1
and meta.llama2-70b-chat-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:
Llama2ChatBedrockApi llama2ChatApi = new Llama2ChatBedrockApi(
Llama2ChatModel.LLAMA2_70B_CHAT_V1.id(),
Region.US_EAST_1.id());
Llama2ChatRequest request = Llama2ChatRequest.builder("Hello, my name is")
.withTemperature(0.9f)
.withTopP(0.9f)
.withMaxGenLen(20)
.build();
Llama2ChatResponse response = llama2ChatApi.chatCompletion(request);
// Streaming response
Flux<Llama2ChatResponse> responseStream = llama2ChatApi.chatCompletionStream(request);
List<Llama2ChatResponse> responses = responseStream.collectList().block();
有关详细信息,请遵循 Llama2ChatBedrockApi.java的 JavaDoc。
Follow the Llama2ChatBedrockApi.java's JavaDoc for further information.