forked from aws/agentcore-cli
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbedrock.ts
More file actions
71 lines (61 loc) · 1.95 KB
/
Copy pathbedrock.ts
File metadata and controls
71 lines (61 loc) · 1.95 KB
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
66
67
68
69
70
71
import { getCredentialProvider } from './account';
import { BedrockRuntimeClient, InvokeModelCommand } from '@aws-sdk/client-bedrock-runtime';
/**
* Options for invoking a Bedrock model synchronously.
*/
export interface BedrockInvokeOptions {
region: string;
modelId: string;
body: Record<string, unknown>;
}
/**
* Invoke a Bedrock model synchronously and return the raw response body.
*/
export async function invokeBedrockSync(options: BedrockInvokeOptions): Promise<Record<string, unknown>> {
const client = new BedrockRuntimeClient({
region: options.region,
credentials: getCredentialProvider(),
});
const command = new InvokeModelCommand({
modelId: options.modelId,
contentType: 'application/json',
accept: 'application/json',
body: JSON.stringify(options.body),
});
const response = await client.send(command);
return JSON.parse(new TextDecoder().decode(response.body)) as Record<string, unknown>;
}
/**
* Claude-specific model configuration.
*/
const CLAUDE_MODEL_ID = 'global.anthropic.claude-opus-4-5-20251101-v1:0';
const CLAUDE_ANTHROPIC_VERSION = 'bedrock-2023-05-31';
export interface ClaudeInvokeOptions {
region: string;
prompt: string;
maxTokens?: number;
}
export interface ClaudeResponse {
content: string;
}
/**
* Invoke Claude on Bedrock with a prompt and return the response text.
*/
export async function invokeClaude(options: ClaudeInvokeOptions): Promise<ClaudeResponse> {
const body = {
anthropic_version: CLAUDE_ANTHROPIC_VERSION,
max_tokens: options.maxTokens ?? 8192,
messages: [{ role: 'user', content: options.prompt }],
};
const response = await invokeBedrockSync({
region: options.region,
modelId: CLAUDE_MODEL_ID,
body,
});
const content = response.content as { type: string; text: string }[];
const firstContent = content[0];
if (!firstContent) {
throw new Error('No content returned from Bedrock');
}
return { content: firstContent.text };
}