Skip to content

Commit

Permalink
Fix bedrock example.
Browse files Browse the repository at this point in the history
  • Loading branch information
kinyoklion committed Nov 4, 2024
1 parent 95fa2bc commit 817d4f0
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 15 deletions.
35 changes: 24 additions & 11 deletions packages/sdk/ai/examples/bedrock/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ const context = {

console.log('*** SDK successfully initialized');

interface MyModelConfig {
modelId: string;
prompt: { role: ConversationRole; content: string }[];
}

function mapPromptToConversation(prompt: { role: ConversationRole; content: string }[]): Message[] {
return prompt.map((item) => ({
role: item.role,
Expand All @@ -47,23 +52,31 @@ async function main() {
configValue = await aiClient.modelConfig(aiConfigKey, context, false, {
myVariable: 'My User Defined Variable',
});
tracker = configValue.tracker;
if (configValue === false) {
console.log('got default value for config');
process.exit(1);
} else {
tracker = configValue.tracker;
}
} catch (error) {
console.log(`*** SDK failed to initialize: ${error}`);
process.exit(1);
}

const completion = await tracker.trackBedrockConverse(
await awsClient.send(
new ConverseCommand({
modelId: configValue.config.config.modelId,
messages: mapPromptToConversation(configValue.config.prompt),
}),
),
);
if (tracker) {
const modelConfig = configValue.config as MyModelConfig;
const completion = await tracker.trackBedrockConverse(
await awsClient.send(
new ConverseCommand({
modelId: modelConfig.modelId,
messages: mapPromptToConversation(modelConfig.prompt),
}),
),
);

console.log('AI Response:', completion.output.message.content[0].text);
console.log('Success.');
console.log('AI Response:', completion.output.message.content[0].text);
console.log('Success.');
}
}

main();
2 changes: 1 addition & 1 deletion packages/sdk/ai/examples/bedrock/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@
"module": "CommonJS",
"moduleResolution": "Node"
},
"include": ["src", "test"],
"include": ["src"],
"exclude": ["dist", "node_modules"]
}
5 changes: 2 additions & 3 deletions packages/sdk/ai/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
{
"compilerOptions": {
// Uses "." so it can load package.json.
"rootDir": ".",
"rootDir": "src",
"outDir": "dist",
"target": "es2017",
"lib": ["es6"],
"module": "commonjs",
"strict": true,
"noImplicitOverride": true,
// Needed for CommonJS modules: markdown-it, fs-extra
// Needed for CommonJS modules.
"allowSyntheticDefaultImports": true,
"sourceMap": true,
"declaration": true,
Expand Down

0 comments on commit 817d4f0

Please sign in to comment.