11/* eslint-disable no-console */
2- import {
3- BedrockRuntimeClient ,
4- ConversationRole ,
5- ConverseCommand ,
6- Message ,
7- } from '@aws-sdk/client-bedrock-runtime' ;
2+ import { BedrockRuntimeClient , ConverseCommand , Message } from '@aws-sdk/client-bedrock-runtime' ;
83
94import { initAi , LDAIConfig } from '@launchdarkly/ai' ;
105import { init } from '@launchdarkly/node-server-sdk' ;
@@ -29,9 +24,12 @@ const context = {
2924
3025console . log ( '*** SDK successfully initialized' ) ;
3126
32- function mapPromptToConversation ( prompt : { role : ConversationRole ; content : string } [ ] ) : Message [ ] {
27+ function mapPromptToConversation (
28+ prompt : { role : 'user' | 'assistant' | 'system' ; content : string } [ ] ,
29+ ) : Message [ ] {
3330 return prompt . map ( ( item ) => ( {
34- role : item . role ,
31+ // Bedrock doesn't support systems in the converse command.
32+ role : item . role !== 'system' ? item . role : 'user' ,
3533 content : [ { text : item . content } ] ,
3634 } ) ) ;
3735}
@@ -44,31 +42,35 @@ async function main() {
4442 await ldClient . waitForInitialization ( { timeout : 10 } ) ;
4543 const aiClient = initAi ( ldClient ) ;
4644
47- configValue = await aiClient . modelConfig ( aiConfigKey , context , false , {
48- myVariable : 'My User Defined Variable' ,
49- } ) ;
50- if ( configValue === false ) {
51- console . log ( 'got default value for config' ) ;
52- process . exit ( 1 ) ;
53- } else {
54- tracker = configValue . tracker ;
55- }
45+ configValue = await aiClient . modelConfig (
46+ aiConfigKey ,
47+ context ,
48+ {
49+ model : {
50+ modelId : 'my-default-model' ,
51+ } ,
52+ } ,
53+ {
54+ myVariable : 'My User Defined Variable' ,
55+ } ,
56+ ) ;
57+ tracker = configValue . tracker ;
5658 } catch ( error ) {
5759 console . log ( `*** SDK failed to initialize: ${ error } ` ) ;
5860 process . exit ( 1 ) ;
5961 }
6062
6163 if ( tracker ) {
62- const completion = await tracker . trackBedrockConverse (
64+ const completion = tracker . trackBedrockConverse (
6365 await awsClient . send (
6466 new ConverseCommand ( {
65- modelId : configValue . config ?. model ?. modelId ?? 'default model' ,
66- messages : mapPromptToConversation ( modelConfig ?. prompt ?? 'default prompt' ) ,
67+ modelId : configValue . config ?. model ?. modelId ?? 'no- model' ,
68+ messages : mapPromptToConversation ( configValue . config ?. prompt ?? [ ] ) ,
6769 } ) ,
6870 ) ,
6971 ) ;
7072
71- console . log ( 'AI Response:' , completion . output . message . content [ 0 ] . text ) ;
73+ console . log ( 'AI Response:' , completion . output ? .message ? .content ?. [ 0 ] ? .text ?? 'no-response' ) ;
7274 console . log ( 'Success.' ) ;
7375 }
7476}
0 commit comments