-
Notifications
You must be signed in to change notification settings - Fork 5.8k
/
Copy pathInvokeModel.cs
64 lines (51 loc) · 1.98 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
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
// snippet-start:[BedrockRuntime.dotnetv4.InvokeModel_Mistral]
// Use the native inference API to send a text message to Mistral.
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., Mistral Large.
var modelId = "mistral.mistral-large-2402-v1:0";
// Define the prompt for the model.
var prompt = "Describe the purpose of a 'hello world' program in one line.";
// Embed the prompt in Mistral's instruction format.
var formattedPrompt = $"<s>[INST] {prompt} [/INST]";
//Format the request payload using the model's native structure.
var nativeRequest = JsonSerializer.Serialize(new
{
prompt = formattedPrompt,
max_tokens = 512,
temperature = 0.5
});
// 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["outputs"]?[0]?["text"] ?? "";
Console.WriteLine(responseText);
}
catch (AmazonBedrockRuntimeException e)
{
Console.WriteLine($"ERROR: Can't invoke '{modelId}'. Reason: {e.Message}");
throw;
}
// snippet-end:[BedrockRuntime.dotnetv4.InvokeModel_Mistral]
// Create a partial class to make the top-level script testable.
namespace Mistral { public partial class InvokeModel { } }