Skip to content

Commit fb0c10a

Browse files
committed
Refactor Bedrock model initialization to include inference profile prefixes with region support
1 parent 8010809 commit fb0c10a

File tree

7 files changed

+320
-194
lines changed

7 files changed

+320
-194
lines changed

agents/agents-features/agents-features-memory/src/jvmMain/kotlin/ai/koog/agents/memory/storage/Aes256GCMStorageEncryptor.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package ai.koog.agents.memory.storage
22

33
import java.security.SecureRandom
4-
import java.util.*
4+
import java.util.Base64
55
import javax.crypto.Cipher
66
import javax.crypto.KeyGenerator
77
import javax.crypto.SecretKey
@@ -75,8 +75,8 @@ public class Aes256GCMEncryptor(secretKey: String) : Encryption {
7575
return String(plaintext)
7676
}
7777

78-
override fun encrypt(text: String): String {
79-
val (nonce, ciphertext) = encryptImpl(text)
78+
override fun encrypt(value: String): String {
79+
val (nonce, ciphertext) = encryptImpl(value)
8080
return Base64.getEncoder().encodeToString(nonce + ciphertext)
8181
}
8282

examples/src/main/kotlin/ai/koog/agents/example/client/BedrockAgent.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import ai.koog.agents.example.simpleapi.Switch
88
import ai.koog.agents.example.simpleapi.SwitchTools
99
import ai.koog.prompt.executor.clients.bedrock.BedrockClientSettings
1010
import ai.koog.prompt.executor.clients.bedrock.BedrockModels
11+
import ai.koog.prompt.executor.clients.bedrock.BedrockRegions
1112
import ai.koog.prompt.executor.llms.all.simpleBedrockExecutor
1213
import kotlinx.coroutines.runBlocking
1314

@@ -20,7 +21,7 @@ fun main(): Unit = runBlocking {
2021

2122
// Create Bedrock client settings
2223
val bedrockSettings = BedrockClientSettings(
23-
region = "us-east-1", // Change this to your preferred region
24+
region = BedrockRegions.US_WEST_2.regionCode, // Change this to your preferred region
2425
maxRetries = 3
2526
)
2627

integration-tests/src/jvmTest/kotlin/ai/koog/integration/tests/utils/RetryUtils.kt

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package ai.koog.integration.tests.utils
22

33
import kotlinx.coroutines.delay
4+
import kotlinx.coroutines.runBlocking
45
import org.junit.jupiter.api.Assumptions
56

67
/*
@@ -36,20 +37,18 @@ object RetryUtils {
3637
}
3738
}
3839

39-
suspend fun <T> withRetry(
40+
fun <T> withRetry(
4041
times: Int = 3,
4142
delayMs: Long = 1000,
4243
testName: String = "test",
4344
action: suspend () -> T
44-
): T {
45+
): T = runBlocking {
4546
var lastException: Throwable? = null
4647

4748
for (attempt in 1..times) {
4849
try {
49-
println("[DEBUG_LOG] Test '$testName' - attempt $attempt of $times")
5050
val result = action()
51-
println("[DEBUG_LOG] Test '$testName' succeeded on attempt $attempt")
52-
return result
51+
return@runBlocking result
5352
} catch (throwable: Throwable) {
5453
lastException = throwable
5554

@@ -59,14 +58,10 @@ object RetryUtils {
5958
false,
6059
"Skipping test due to third-party service error: ${throwable.message}"
6160
)
62-
return action()
61+
return@runBlocking action()
6362
}
6463

65-
println("[DEBUG_LOG] Test '$testName' failed on attempt $attempt: ${throwable.message}")
66-
6764
if (attempt < times) {
68-
println("[DEBUG_LOG] Retrying test '$testName' (attempt ${attempt + 1} of $times)")
69-
7065
if (delayMs > 0) {
7166
delay(delayMs)
7267
}

prompt/prompt-executor/prompt-executor-clients/prompt-executor-anthropic-client/src/commonMain/kotlin/ai/koog/prompt/executor/clients/anthropic/AnthropicModels.kt

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -196,11 +196,11 @@ public object AnthropicModels : LLModelDefinitions {
196196
}
197197

198198
internal val DEFAULT_ANTHROPIC_MODEL_VERSIONS_MAP: Map<LLModel, String> = mapOf(
199-
AnthropicModels.Opus_3 to "claude-3-opus-20240229",
200-
AnthropicModels.Haiku_3 to "claude-3-haiku-20240307",
201-
AnthropicModels.Sonnet_3_5 to "claude-3-5-sonnet-20241022",
202-
AnthropicModels.Haiku_3_5 to "claude-3-5-haiku-20241022",
203-
AnthropicModels.Sonnet_3_7 to "claude-3-7-sonnet-20250219",
204-
AnthropicModels.Sonnet_4 to "claude-sonnet-4-20250514",
205-
AnthropicModels.Opus_4 to "claude-opus-4-20250514"
199+
Opus_3 to "claude-3-opus-20240229",
200+
Haiku_3 to "claude-3-haiku-20240307",
201+
Sonnet_3_5 to "claude-3-5-sonnet-20241022",
202+
Haiku_3_5 to "claude-3-5-haiku-20241022",
203+
Sonnet_3_7 to "claude-3-7-sonnet-20250219",
204+
Sonnet_4 to "claude-sonnet-4-20250514",
205+
Opus_4 to "claude-opus-4-20250514"
206206
)

prompt/prompt-executor/prompt-executor-clients/prompt-executor-bedrock-client/src/jvmMain/kotlin/ai/koog/prompt/executor/clients/bedrock/BedrockLLMClient.kt

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ import kotlinx.serialization.json.Json
6262
* @property moderationGuardrailsSettings Optional settings of the AWS bedrock Guardrails (see [AWS documentation](https://docs.aws.amazon.com/bedrock/latest/userguide/guardrails-use-independent-api.html) ) that would be used for the [LLMClient.moderate] request
6363
*/
6464
public class BedrockClientSettings(
65-
internal val region: String = "us-west-2",
65+
internal val region: String = BedrockRegions.US_WEST_2.regionCode,
6666
internal val timeoutConfig: ConnectionTimeoutConfig = ConnectionTimeoutConfig(),
6767
internal val endpointUrl: String? = null,
6868
internal val maxRetries: Int = 3,
@@ -124,7 +124,7 @@ public class BedrockLLMClient(
124124
awsSessionToken?.let { this.sessionToken = it }
125125
}
126126

127-
// Configure custom endpoint if provided
127+
// Configure a custom endpoint if provided
128128
settings.endpointUrl?.let { url ->
129129
this.endpointUrl = Url.parse(url)
130130
}
@@ -147,10 +147,10 @@ public class BedrockLLMClient(
147147
internal fun getBedrockModelFamily(model: LLModel): BedrockModelFamilies {
148148
require(model.provider == LLMProvider.Bedrock) { "Model ${model.id} is not a Bedrock model" }
149149
return when {
150-
model.id.startsWith("us.anthropic.claude") -> BedrockModelFamilies.AnthropicClaude
151-
model.id.startsWith("us.amazon.nova") -> BedrockModelFamilies.AmazonNova
152-
model.id.startsWith("us.ai21.jamba") -> BedrockModelFamilies.AI21Jamba
153-
model.id.startsWith("us.meta.llama") -> BedrockModelFamilies.Meta
150+
model.id.contains("anthropic.claude") -> BedrockModelFamilies.AnthropicClaude
151+
model.id.contains("amazon.nova") -> BedrockModelFamilies.AmazonNova
152+
model.id.contains("ai21.jamba") -> BedrockModelFamilies.AI21Jamba
153+
model.id.contains("meta.llama") -> BedrockModelFamilies.Meta
154154
else -> throw IllegalArgumentException("Model ${model.id} is not a supported Bedrock model")
155155
}
156156
}
@@ -368,14 +368,14 @@ public class BedrockLLMClient(
368368
)
369369

370370
val inputIsHarmful = inputGuardrailResponse.action is GuardrailAction.GuardrailIntervened
371-
val outputputIsHarmful = inputGuardrailResponse.action is GuardrailAction.GuardrailIntervened
371+
val outputIsHarmful = inputGuardrailResponse.action is GuardrailAction.GuardrailIntervened
372372

373373
val categories = buildMap {
374374
fillCategoriesMap(inputGuardrailResponse)
375375
fillCategoriesMap(outputGuardrailResponse)
376376
}
377377

378-
return ModerationResult(inputIsHarmful || outputputIsHarmful, categories)
378+
return ModerationResult(inputIsHarmful || outputIsHarmful, categories)
379379
}
380380

381381
private fun MutableMap<ModerationCategory, ModerationCategoryResult>.fillCategoriesMap(

0 commit comments

Comments
 (0)