@@ -30,19 +30,23 @@ export default async function chatResponse(req: NextApiRequest, res: NextApiResp
30
30
secretAccessKey : process . env . AWS_SECRET_ACCESS_KEY ?? "" ,
31
31
} ,
32
32
} ) ;
33
- const messages = req . body ;
34
-
33
+ const messages = req . body ;
34
+
35
35
const ldClient = await getServerClient ( process . env . LD_SDK_KEY || "" ) ;
36
36
// console.log("ldClient",ldClient)
37
- const context : any = getCookie ( "ld-context" ) || { "kind" : "user" , "name" : "anonymous" , "key" : "abc-123" } ;
37
+ const context : any = getCookie ( "ld-context" ) || {
38
+ kind : "user" ,
39
+ name : "anonymous" ,
40
+ key : "abc-123" ,
41
+ } ;
38
42
39
43
const model = await ldClient . variation ( "ai-chatbot" , context , {
40
- modelId : ' anthropic.claude-instant-v1' ,
44
+ modelId : " anthropic.claude-instant-v1" ,
41
45
temperature : 0.9 ,
42
46
top_k : 250 ,
43
47
top_p : 1 ,
44
- max_tokens_to_sample : 500
45
- } )
48
+ max_tokens_to_sample : 500 ,
49
+ } ) ;
46
50
47
51
// Ask Claude for a streaming chat completion given the prompt
48
52
// const claudeMessage = [
@@ -51,42 +55,42 @@ export default async function chatResponse(req: NextApiRequest, res: NextApiResp
51
55
// content: "Where is a good vacation place for under $1000? Limit to 100 characters.",
52
56
// },
53
57
// ];
54
-
58
+
59
+ const modelWithoutModelId = Object . keys ( model )
60
+ . filter ( ( objKey ) => objKey !== "modelId" )
61
+ . reduce ( ( newObj : any , key ) => {
62
+ newObj [ key ] = model [ key ] ;
63
+ return newObj ;
64
+ } , { } ) ;
65
+
55
66
const chatBotModelInput = new InvokeModelCommand ( {
56
- modelId : "cohere.command-text-v14" ,
67
+ modelId : model . modelId ,
57
68
contentType : "application/json" ,
58
69
accept : "application/json" ,
59
70
body : JSON . stringify ( {
60
71
prompt : `\n\nHuman:${ messages } \n\nAssistant:` ,
61
- temperature : model . temperature ,
62
- max_tokens_to_sample : model ?. max_tokens_to_sample ,
63
- max_tokens : 500 ,
64
- p : 1 ,
65
- // max_gen_len: model?.max_gen_len,
66
- // top_p: model?.top_p,
72
+ ...modelWithoutModelId ,
67
73
} ) ,
68
74
} ) ;
69
75
70
76
try {
71
77
const bedrockResponse = await bedrockClient . send ( chatBotModelInput ) ;
72
78
const decoder = new TextDecoder ( ) ;
73
79
const jsontext = JSON . parse ( decoder . decode ( bedrockResponse . body ) ) ;
74
- console . log ( jsontext )
75
80
res . status ( 200 ) . json ( jsontext ) ;
76
81
} catch ( error : any ) {
77
82
throw new Error ( error . message ) ;
78
83
}
79
84
80
85
// const cohere = new InvokeModelWithResponseStreamCommand({
81
- // modelId: "cohere.command-text-v14",
82
- // contentType: "application/json",
83
- // accept: "application/json",
84
- // body: JSON.stringify({
85
- // prompt: experimental_buildLlama2Prompt(cohereMessage),
86
- // temperature: 0.9,
87
- // max_tokens: 500,
88
- // p: 1,
89
- // }),
90
- // });
91
-
86
+ // modelId: "cohere.command-text-v14",
87
+ // contentType: "application/json",
88
+ // accept: "application/json",
89
+ // body: JSON.stringify({
90
+ // prompt: experimental_buildLlama2Prompt(cohereMessage),
91
+ // temperature: 0.9,
92
+ // max_tokens: 500,
93
+ // p: 1,
94
+ // }),
95
+ // });
92
96
}
0 commit comments