|
2 | 2 |
|
3 | 3 | //JAVA 25+ |
4 | 4 | //REPOS mavencentral,spring-milestones=https://repo.spring.io/milestone |
5 | | -//DEPS org.springframework.ai:spring-ai-bedrock-converse:2.0.0-M4 |
| 5 | +//SOURCES ../config/BedrockChatModelConfig.java |
6 | 6 | //DEPS org.springframework.ai:spring-ai-client-chat:2.0.0-M4 |
7 | | -//DEPS software.amazon.awssdk:bedrockruntime:2.41.34 |
8 | | -//DEPS software.amazon.awssdk:auth:2.41.34 |
9 | 7 | //DEPS org.slf4j:slf4j-api:2.0.17 |
10 | 8 | //DEPS org.slf4j:slf4j-simple:2.0.17 |
11 | 9 |
|
12 | 10 | import org.slf4j.Logger; |
13 | 11 | import org.slf4j.LoggerFactory; |
14 | | -import org.springframework.ai.bedrock.converse.BedrockProxyChatModel; |
15 | | -import org.springframework.ai.bedrock.converse.BedrockChatOptions; |
16 | | -import software.amazon.awssdk.auth.credentials.AnonymousCredentialsProvider; |
17 | | -import software.amazon.awssdk.regions.Region; |
18 | | -import software.amazon.awssdk.services.bedrockruntime.BedrockRuntimeClient; |
19 | | - |
20 | 12 | import org.springframework.ai.chat.client.ChatClient; |
21 | 13 |
|
22 | 14 | private static final Logger log = LoggerFactory.getLogger("DungeonMasterSimple"); |
23 | 15 |
|
24 | 16 | void main() { |
25 | | - log.info("=== Starting Dungeon Master AI Agent ==="); |
26 | | - |
27 | | - // Step 1: Read the Bedrock API key from environment |
28 | | - var bearerToken = System.getenv("AWS_BEARER_TOKEN_BEDROCK"); |
29 | | - if (bearerToken == null || bearerToken.isBlank()) { |
30 | | - log.error("Set AWS_BEARER_TOKEN_BEDROCK first — get your key from the Amazon Bedrock Console → API keys → Short-term API keys"); |
31 | | - return; |
32 | | - } |
33 | | - |
34 | | - // Step 2: Create AWS Bedrock Runtime Client with API key (bearer token auth) |
35 | | - var bedrockClient = BedrockRuntimeClient.builder() |
36 | | - .region(Region.US_WEST_2) |
37 | | - .credentialsProvider(AnonymousCredentialsProvider.create()) |
38 | | - .overrideConfiguration(c -> c.putHeader("Authorization", "Bearer " + bearerToken)) |
39 | | - .build(); |
40 | 17 |
|
41 | | - // Step 3: Configure model options (which Claude model to use) |
42 | | - var modelId = "us.anthropic.claude-haiku-4-5-20251001-v1:0"; |
43 | | - var options = BedrockChatOptions.builder() |
44 | | - .model(modelId) |
45 | | - .build(); |
| 18 | + log.info("=== Starting Dungeon Master AI Agent ==="); |
46 | 19 |
|
47 | | - // Step 4: Create Spring AI ChatModel (wraps Bedrock client) |
48 | | - var chatModel = BedrockProxyChatModel.builder() |
49 | | - .bedrockRuntimeClient(bedrockClient) |
50 | | - .defaultOptions(options) |
51 | | - .build(); |
| 20 | + var chatModel = BedrockChatModelConfig.createChatModel(); |
52 | 21 |
|
53 | 22 | // Step 5: Build ChatClient with system prompt (defines AI personality) |
54 | 23 | var agent = ChatClient.builder(chatModel) |
55 | | - .defaultSystem("You are a game master for a Dungeon & Dragon game") |
| 24 | + .defaultSystem(""" |
| 25 | + You are a dungeon master for a D&D game. |
| 26 | + You describe the environment, creatures, and challenges. |
| 27 | + You respond in a terse and humorous way. |
| 28 | + """) |
56 | 29 | .build(); |
57 | 30 |
|
58 | 31 | // Step 6: Invoke the AI agent |
59 | 32 | var playerMessage = "Hi, I am an adventurer ready for adventure!"; |
60 | 33 | log.info("Player: " + playerMessage + "\n"); |
61 | 34 |
|
62 | 35 | try { |
63 | | - var response = agent |
64 | | - .prompt() |
| 36 | + var response = agent.prompt() |
65 | 37 | .user(playerMessage) |
66 | 38 | .call() |
67 | 39 | .content(); |
68 | | - |
69 | | - log.info("Dungeon Master says:"); |
70 | 40 | log.info(response); |
71 | | - |
72 | 41 | } catch (Exception e) { |
73 | 42 | log.error("Error invoking AI agent: {}", e.getMessage()); |
74 | 43 | } finally { |
|
0 commit comments