-
Notifications
You must be signed in to change notification settings - Fork 5.8k
/
Copy pathInvokeModel.cs
65 lines (53 loc) · 2.01 KB
/
InvokeModel.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
// snippet-start:[BedrockRuntime.dotnetv4.InvokeModel_AnthropicClaude]
// Use the native inference API to send a text message to Anthropic Claude.
using System;
using System.IO;
using System.Text.Json;
using System.Text.Json.Nodes;
using Amazon;
using Amazon.BedrockRuntime;
using Amazon.BedrockRuntime.Model;
// Create a Bedrock Runtime client in the AWS Region you want to use.
var client = new AmazonBedrockRuntimeClient(RegionEndpoint.USEast1);
// Set the model ID, e.g., Claude 3 Haiku.
var modelId = "anthropic.claude-3-haiku-20240307-v1:0";
// Define the user message.
var userMessage = "Describe the purpose of a 'hello world' program in one line.";
//Format the request payload using the model's native structure.
var nativeRequest = JsonSerializer.Serialize(new
{
anthropic_version = "bedrock-2023-05-31",
max_tokens = 512,
temperature = 0.5,
messages = new[]
{
new { role = "user", content = userMessage }
}
});
// Create a request with the model ID and the model's native request payload.
var request = new InvokeModelRequest()
{
ModelId = modelId,
Body = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(nativeRequest)),
ContentType = "application/json"
};
try
{
// Send the request to the Bedrock Runtime and wait for the response.
var response = await client.InvokeModelAsync(request);
// Decode the response body.
var modelResponse = await JsonNode.ParseAsync(response.Body);
// Extract and print the response text.
var responseText = modelResponse["content"]?[0]?["text"] ?? "";
Console.WriteLine(responseText);
}
catch (AmazonBedrockRuntimeException e)
{
Console.WriteLine($"ERROR: Can't invoke '{modelId}'. Reason: {e.Message}");
throw;
}
// snippet-end:[BedrockRuntime.dotnetv4.InvokeModel_AnthropicClaude]
// Create a partial class to make the top-level script testable.
namespace AnthropicClaude { public partial class InvokeModel { } }