|
1 |
| -# 1. Bedrock Anthropic |
| 1 | +# Bedrock Anthropic |
2 | 2 |
|
3 | 3 | Provides Bedrock Anthropic Chat API and Spring-AI chat clients.
|
4 | 4 |
|
5 |
| -## 1.1 AnthropicChatBedrockApi |
| 5 | +## BedrockAnthropicChatClient |
| 6 | + |
| 7 | +The [BedrockAnthropicChatClient](./src/main/java/org/springframework/ai/bedrock/anthropic/BedrockAnthropicChatClient.java) implements the `ChatClient` and `StreamingChatClient` and uses the `AnthropicChatBedrockApi` library to connect to the Bedrock Anthropic service. |
| 8 | + |
| 9 | +Add the `spring-ai-` dependency to your project's Maven `pom.xml` file: |
| 10 | + |
| 11 | +```xml |
| 12 | +<dependency> |
| 13 | + <groupId>org.springframework.ai</groupId> |
| 14 | + <artifactId>spring-ai-bedrock</artifactId> |
| 15 | + <version>0.8.0-SNAPSHOT</version> |
| 16 | +</dependency> |
| 17 | +``` |
| 18 | + |
| 19 | +or to your Gradle `build.gradle` build file. |
| 20 | + |
| 21 | +```gradle |
| 22 | +dependencies { |
| 23 | + implementation 'org.springframework.ai:spring-ai-bedrock:0.8.0-SNAPSHOT' |
| 24 | +} |
| 25 | +``` |
| 26 | + |
| 27 | +Next, create an `BedrockAnthropicChatClient` instance and use it to text generations requests: |
| 28 | + |
| 29 | +```java |
| 30 | + AnthropicChatBedrockApi anthropicApi = new AnthropicChatBedrockApi( |
| 31 | + AnthropicChatBedrockApi.AnthropicModel.CLAUDE_V2.id(), |
| 32 | + EnvironmentVariableCredentialsProvider.create(), |
| 33 | + Region.EU_CENTRAL_1.id(), |
| 34 | + new ObjectMapper()); |
| 35 | + |
| 36 | + BedrockAnthropicChatClient chatClient = new BedrockAnthropicChatClient(anthropicApi, |
| 37 | + AnthropicChatOptions.builder() |
| 38 | + .withTemperature(0.6f) |
| 39 | + .withTopK(10) |
| 40 | + .withTopP(0.8f) |
| 41 | + .withMaxTokensToSample(100) |
| 42 | + .withAnthropicVersion(AnthropicChatBedrockApi.DEFAULT_ANTHROPIC_VERSION) |
| 43 | + .build()); |
| 44 | + |
| 45 | +ChatResponse response = chatClient.call( |
| 46 | + new Prompt("Generate the names of 5 famous pirates.")); |
| 47 | + |
| 48 | +// Or with streaming responses |
| 49 | +Flux<ChatResponse> response = chatClient.stream( |
| 50 | + new Prompt("Generate the names of 5 famous pirates.")); |
| 51 | +``` |
| 52 | + |
| 53 | +or you can leverage the `spring-ai-bedrock-ai-spring-boot-starter` Spring Boot starter: |
| 54 | + |
| 55 | +```xml |
| 56 | +<dependency> |
| 57 | + <groupId>org.springframework.ai</groupId> |
| 58 | + <artifactId>spring-ai-bedrock-ai-spring-boot-starter</artifactId> |
| 59 | + <version>0.8.0-SNAPSHOT</version> |
| 60 | +</dependency> |
| 61 | +``` |
| 62 | + |
| 63 | +And set `spring.ai.bedrock.anthropic.chat.enabled=true`. |
| 64 | +By default the client is disabled. |
| 65 | + |
| 66 | +Use the `BedrockAnthropicChatProperties` to configure the Bedrock Anthropic Chat client: |
| 67 | + |
| 68 | +| Property | Description | Default | |
| 69 | +| ------------- | ------------- | ------------- | |
| 70 | +| spring.ai.bedrock.aws.region | AWS region to use. | us-east-1 | |
| 71 | +| spring.ai.bedrock.aws.accessKey | AWS credentials access key. | | |
| 72 | +| spring.ai.bedrock.aws.secretKey | AWS credentials secret key. | | |
| 73 | +| spring.ai.bedrock.anthropic.chat.enable | Enable Bedrock Anthropic chat client. Disabled by default | false | |
| 74 | +| spring.ai.bedrock.anthropic.chat.model | The model id to use. See the `AnthropicChatModel` for the supported models. | anthropic.claude-v2 | |
| 75 | +| spring.ai.bedrock.anthropic.chat.options.temperature | Controls the randomness of the output. Values can range over [0.0,1.0] | 0.8 | |
| 76 | +| spring.ai.bedrock.anthropic.chat.options.topP | The maximum cumulative probability of tokens to consider when sampling. | AWS Bedrock default | |
| 77 | +| spring.ai.bedrock.anthropic.chat.options.topK | Specify the number of token choices the generative uses to generate the next token. | AWS Bedrock default | |
| 78 | +| spring.ai.bedrock.anthropic.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. | 10 | |
| 79 | +| spring.ai.bedrock.anthropic.chat.options.anthropicVersion | The version of the generative to use. | bedrock-2023-05-31 | |
| 80 | +| spring.ai.bedrock.anthropic.chat.options.maxTokensToSample | 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. | 500 | |
| 81 | + |
| 82 | +## Appendices |
| 83 | + |
| 84 | +## Using low-level AnthropicChatBedrockApi Library |
6 | 85 |
|
7 | 86 | [AnthropicChatBedrockApi](./src/main/java/org/springframework/ai/bedrock/anthropic/api/AnthropicChatBedrockApi.java) provides is lightweight Java client on top of AWS Bedrock [Anthropic Claude models](https://docs.aws.amazon.com/bedrock/latest/userguide/model-parameters-claude.html).
|
8 | 87 |
|
9 |
| -Following class diagram illustrates the Llama2ChatBedrockApi interface and building blocks: |
| 88 | +Following class diagram illustrates the AnthropicChatBedrockApi interface and building blocks: |
10 | 89 |
|
11 | 90 | 
|
12 | 91 |
|
@@ -42,51 +121,3 @@ System.out.println(responses);
|
42 | 121 | ```
|
43 | 122 |
|
44 | 123 | Follow the [AnthropicChatBedrockApi.java](./src/main/java/org/springframework/ai/bedrock/anthropic/api/AnthropicChatBedrockApi.java)'s JavaDoc for further information.
|
45 |
| - |
46 |
| -## 1.2 BedrockAnthropicChatClient |
47 |
| - |
48 |
| -[BedrockAnthropicChatClient](./src/main/java/org/springframework/ai/bedrock/anthropic/BedrockAnthropicChatClient.java) implements the Spring-Ai `ChatClient` and `StreamingChatClient` on top of the `AnthropicChatBedrockApi`. |
49 |
| - |
50 |
| -You can use like this: |
51 |
| - |
52 |
| -```java |
53 |
| -@Bean |
54 |
| -public AnthropicChatBedrockApi anthropicApi() { |
55 |
| - return new AnthropicChatBedrockApi( |
56 |
| - AnthropicChatBedrockApi.AnthropicModel.CLAUDE_V2.id(), |
57 |
| - EnvironmentVariableCredentialsProvider.create(), |
58 |
| - Region.EU_CENTRAL_1.id(), |
59 |
| - new ObjectMapper()); |
60 |
| -} |
61 |
| - |
62 |
| -@Bean |
63 |
| -public BedrockAnthropicChatClient anthropicChatClient(AnthropicChatBedrockApi anthropicApi) { |
64 |
| - return new BedrockAnthropicChatClient(anthropicApi); |
65 |
| -} |
66 |
| -``` |
67 |
| - |
68 |
| -or you can leverage the `spring-ai-bedrock-ai-spring-boot-starter` Spring Boot starter: |
69 |
| - |
70 |
| -```xml |
71 |
| -<dependency> |
72 |
| - <artifactId>spring-ai-bedrock-ai-spring-boot-starter</artifactId> |
73 |
| - <groupId>org.springframework.ai</groupId> |
74 |
| - <version>0.8.0-SNAPSHOT</version> |
75 |
| -</dependency> |
76 |
| -``` |
77 |
| - |
78 |
| -And set `spring.ai.bedrock.anthropic.chat.enabled=true`. |
79 |
| -By default the client is disabled. |
80 |
| - |
81 |
| -Use the `BedrockAnthropicChatProperties` to configure the Bedrock Llama2 Chat client: |
82 |
| - |
83 |
| -| Property | Description | Default | |
84 |
| -| ------------- | ------------- | ------------- | |
85 |
| -| spring.ai.bedrock.aws.region | AWS region to use. | us-east-1 | |
86 |
| -| spring.ai.bedrock.aws.accessKey | AWS credentials access key. | | |
87 |
| -| spring.ai.bedrock.aws.secretKey | AWS credentials secret key. | | |
88 |
| -| spring.ai.bedrock.anthropic.chat.enable | Enable Bedrock Llama2 chat client. Disabled by default | false | |
89 |
| -| spring.ai.bedrock.anthropic.chat.temperature | Controls the randomness of the output. Values can range over [0.0,1.0] | 0.8 | |
90 |
| -| spring.ai.bedrock.anthropic.chat.topP | The maximum cumulative probability of tokens to consider when sampling. | AWS Bedrock default | |
91 |
| -| spring.ai.bedrock.anthropic.chat.maxGenLen | Specify the maximum number of tokens to use in the generated response. | 300 | |
92 |
| -| spring.ai.bedrock.anthropic.chat.model | The model id to use. See the `Llama2ChatModel` for the supported models. | meta.llama2-70b-chat-v1 | |
|
0 commit comments