Skip to content

Commit 6e086f5

Browse files
committed
got cohere to work
1 parent d341eda commit 6e086f5

File tree

2 files changed

+32
-28
lines changed

2 files changed

+32
-28
lines changed

components/chatbot/ChatBot.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ export default function Chatbot() {
3434
};
3535

3636
setMessages2([...messages2, userMessage]);
37-
37+
3838
const response = await fetch("/api/chat", {
3939
method: "POST",
4040
body: JSON.stringify(`${userInput}. Limit response to 100 characters.`),
@@ -45,7 +45,7 @@ export default function Chatbot() {
4545

4646
if (data?.generation) {
4747
aiAnswer = data?.generation; //llama
48-
} else if (data?.generations.length > 0) {
48+
} else if (data?.generations?.length > 0) {
4949
aiAnswer = data?.generations[0]?.text; //cohere
5050
} else {
5151
aiAnswer = data?.completion; //claude

pages/api/chat.ts

+30-26
Original file line numberDiff line numberDiff line change
@@ -30,19 +30,23 @@ export default async function chatResponse(req: NextApiRequest, res: NextApiResp
3030
secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY ?? "",
3131
},
3232
});
33-
const messages = req.body;
34-
33+
const messages = req.body;
34+
3535
const ldClient = await getServerClient(process.env.LD_SDK_KEY || "");
3636
// 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+
};
3842

3943
const model = await ldClient.variation("ai-chatbot", context, {
40-
modelId: 'anthropic.claude-instant-v1',
44+
modelId: "anthropic.claude-instant-v1",
4145
temperature: 0.9,
4246
top_k: 250,
4347
top_p: 1,
44-
max_tokens_to_sample: 500
45-
})
48+
max_tokens_to_sample: 500,
49+
});
4650

4751
// Ask Claude for a streaming chat completion given the prompt
4852
// const claudeMessage = [
@@ -51,42 +55,42 @@ export default async function chatResponse(req: NextApiRequest, res: NextApiResp
5155
// content: "Where is a good vacation place for under $1000? Limit to 100 characters.",
5256
// },
5357
// ];
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+
5566
const chatBotModelInput = new InvokeModelCommand({
56-
modelId: "cohere.command-text-v14",
67+
modelId: model.modelId,
5768
contentType: "application/json",
5869
accept: "application/json",
5970
body: JSON.stringify({
6071
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,
6773
}),
6874
});
6975

7076
try {
7177
const bedrockResponse = await bedrockClient.send(chatBotModelInput);
7278
const decoder = new TextDecoder();
7379
const jsontext = JSON.parse(decoder.decode(bedrockResponse.body));
74-
console.log(jsontext)
7580
res.status(200).json(jsontext);
7681
} catch (error: any) {
7782
throw new Error(error.message);
7883
}
7984

8085
// 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+
// });
9296
}

0 commit comments

Comments
 (0)