Skip to content

Commit e3cc650

Browse files
committed
add test for Anthropic Claude model
1 parent 4cc8bde commit e3cc650

File tree

3 files changed

+65
-8
lines changed

3 files changed

+65
-8
lines changed

test/contract-tests/images/applications/TestSimpleApp.AWSSDK.Core/BedrockTests.cs

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public GetGuardrailResponse GetGuardrailResponse()
3939
};
4040
}
4141

42-
public void InvokeModel()
42+
public void InvokeModelAmazonTitan()
4343
{
4444
bedrockRuntime.InvokeModelAsync(new InvokeModelRequest
4545
{
@@ -59,7 +59,7 @@ public void InvokeModel()
5959
return;
6060
}
6161

62-
public object InvokeModelResponse()
62+
public object InvokeModelAmazonTitanResponse()
6363
{
6464
return new
6565
{
@@ -68,14 +68,42 @@ public object InvokeModelResponse()
6868
{
6969
new
7070
{
71-
outputText = "\nsample output text\n",
71+
outputText = "sample output text",
7272
tokenCount = 789,
7373
completionReason = "finish_reason"
7474
},
7575
},
7676
};
7777
}
7878

79+
public void InvokeModelAnthropicClaude()
80+
{
81+
bedrockRuntime.InvokeModelAsync(new InvokeModelRequest
82+
{
83+
ModelId = "anthropic.claude-v2:1",
84+
Body = new MemoryStream(Encoding.UTF8.GetBytes(JsonSerializer.Serialize(new
85+
{
86+
// prompt is 72 chars long, input_tokens should be estimated as ceil(72/6) = 12
87+
prompt = "sample input text sample input text sample input text sample input text ",
88+
temperature = 0.123,
89+
top_p = 0.456,
90+
max_tokens_to_sample = 123,
91+
}))),
92+
ContentType = "application/json",
93+
});
94+
return;
95+
}
96+
97+
public object InvokeModelAnthropicClaudeResponse()
98+
{
99+
return new
100+
{
101+
// response is 56 chars long, output_tokens should be estimated as ceil(56/6) = 10
102+
completion = "sample output text sample output text sample output text",
103+
stop_reason = "finish_reason",
104+
};
105+
}
106+
79107
public Task<GetAgentResponse> GetAgent()
80108
{
81109
return bedrockAgent.GetAgentAsync(new GetAgentRequest

test/contract-tests/images/applications/TestSimpleApp.AWSSDK.Core/Program.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -125,8 +125,11 @@
125125
.WithName("get-guardrail")
126126
.WithOpenApi();
127127

128-
app.MapGet("bedrock/invokemodel/invoke-model", (BedrockTests bedrock) => bedrock.InvokeModel())
129-
.WithName("invoke-model")
128+
app.MapGet("bedrock/invokemodel/invoke-model-titan", (BedrockTests bedrock) => bedrock.InvokeModelAmazonTitan())
129+
.WithName("invoke-model-titan")
130+
.WithOpenApi();
131+
app.MapGet("bedrock/invokemodel/invoke-model-claude", (BedrockTests bedrock) => bedrock.InvokeModelAnthropicClaude())
132+
.WithName("invoke-model-claude")
130133
.WithOpenApi();
131134

132135
app.MapGet("bedrock/getagent/get-agent", (BedrockTests bedrock) => bedrock.GetAgent())
@@ -152,7 +155,8 @@
152155
// Reroute the Bedrock API calls to our mock responses in BedrockTests. While other services use localstack to handle the requests,
153156
// we write our own responses with the necessary data to mimic the expected behavior of the Bedrock services.
154157
app.MapGet("guardrails/test-guardrail", (BedrockTests bedrock) => bedrock.GetGuardrailResponse());
155-
app.MapPost("model/amazon.titan-text-express-v1/invoke", (BedrockTests bedrock) => bedrock.InvokeModelResponse());
158+
app.MapPost("model/amazon.titan-text-express-v1/invoke", (BedrockTests bedrock) => bedrock.InvokeModelAmazonTitanResponse());
159+
app.MapPost("model/anthropic.claude-v2:1/invoke", (BedrockTests bedrock) => bedrock.InvokeModelAnthropicClaudeResponse());
156160
app.MapGet("agents/test-agent", (BedrockTests bedrock) => bedrock.GetAgentResponse());
157161
app.MapGet("knowledgebases/test-knowledge-base", (BedrockTests bedrock) => bedrock.GetKnowledgeBaseResponse());
158162
app.MapGet("knowledgebases/test-knowledge-base/datasources/test-data-source", (BedrockTests bedrock) => bedrock.GetDataSourceResponse());

test/contract-tests/tests/test/amazon/awssdk/awssdk_test.py

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -323,9 +323,9 @@ def test_bedrock_get_guardrail(self):
323323
span_name="Bedrock.GetGuardrail",
324324
)
325325

326-
def test_bedrock_runtime_invoke_model(self):
326+
def test_bedrock_runtime_invoke_model_titan(self):
327327
self.do_test_requests(
328-
"bedrock/invokemodel/invoke-model",
328+
"bedrock/invokemodel/invoke-model-titan",
329329
"GET",
330330
200,
331331
0,
@@ -346,6 +346,30 @@ def test_bedrock_runtime_invoke_model(self):
346346
},
347347
span_name="Bedrock Runtime.InvokeModel",
348348
)
349+
350+
def test_bedrock_runtime_invoke_model_claude(self):
351+
self.do_test_requests(
352+
"bedrock/invokemodel/invoke-model-claude",
353+
"GET",
354+
200,
355+
0,
356+
0,
357+
rpc_service="Bedrock Runtime",
358+
remote_service="AWS::BedrockRuntime",
359+
remote_operation="InvokeModel",
360+
remote_resource_type="AWS::Bedrock::Model",
361+
remote_resource_identifier="anthropic.claude-v2:1",
362+
request_specific_attributes={
363+
_GEN_AI_REQUEST_MODEL: "anthropic.claude-v2:1",
364+
_GEN_AI_REQUEST_TEMPERATURE: 0.123,
365+
_GEN_AI_REQUEST_TOP_P: 0.456,
366+
_GEN_AI_REQUEST_MAX_TOKENS: 123,
367+
_GEN_AI_USAGE_INPUT_TOKENS: 12,
368+
_GEN_AI_USAGE_OUTPUT_TOKENS: 10,
369+
_GEN_AI_RESPONSE_FINISH_REASONS: ["finish_reason"],
370+
},
371+
span_name="Bedrock Runtime.InvokeModel",
372+
)
349373

350374
def test_bedrock_agent_runtime_invoke_agent(self):
351375
self.do_test_requests(
@@ -612,6 +636,7 @@ def _filter_bedrock_metrics(self, target_metrics: List[Metric]):
612636
"GET knowledgebases/test-knowledge-base/datasources/test-data-source",
613637
"POST agents/test-agent/agentAliases/test-agent-alias/sessions/test-session/text",
614638
"POST model/amazon.titan-text-express-v1/invoke",
639+
"POST model/anthropic.claude-v2:1/invoke",
615640
"POST knowledgebases/test-knowledge-base/retrieve"
616641
}
617642
for metric in target_metrics:

0 commit comments

Comments
 (0)