Skip to content

Commit d341eda

Browse files
committed
able to spit out cohere
1 parent 138ac62 commit d341eda

File tree

2 files changed

+20
-7
lines changed

2 files changed

+20
-7
lines changed

components/chatbot/ChatBot.tsx

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

3636
setMessages2([...messages2, userMessage]);
37+
3738
const response = await fetch("/api/chat", {
3839
method: "POST",
3940
body: JSON.stringify(`${userInput}. Limit response to 100 characters.`),
4041
});
4142
const data = await response.json();
42-
console.log(data);
43+
44+
let aiAnswer;
45+
46+
if (data?.generation) {
47+
aiAnswer = data?.generation; //llama
48+
} else if (data?.generations.length > 0) {
49+
aiAnswer = data?.generations[0]?.text; //cohere
50+
} else {
51+
aiAnswer = data?.completion; //claude
52+
}
53+
4354
const assistantMessage = {
4455
role: "assistant",
45-
content: data?.completion === undefined ? data?.generation : data?.completion,
56+
content: aiAnswer,
4657
id: uuidv4().slice(0, 4),
4758
};
4859
setMessages2([...messages2, userMessage, assistantMessage]);

pages/api/chat.ts

+7-5
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ export default async function chatResponse(req: NextApiRequest, res: NextApiResp
3333
const messages = req.body;
3434

3535
const ldClient = await getServerClient(process.env.LD_SDK_KEY || "");
36-
console.log("ldClient",ldClient)
36+
// console.log("ldClient",ldClient)
3737
const context: any = getCookie("ld-context") || { "kind": "user", "name": "anonymous", "key": "abc-123" };
3838

3939
const model = await ldClient.variation("ai-chatbot", context, {
@@ -53,23 +53,25 @@ export default async function chatResponse(req: NextApiRequest, res: NextApiResp
5353
// ];
5454

5555
const chatBotModelInput = new InvokeModelCommand({
56-
modelId: model.modelId,
56+
modelId: "cohere.command-text-v14",
5757
contentType: "application/json",
5858
accept: "application/json",
5959
body: JSON.stringify({
6060
prompt: `\n\nHuman:${messages}\n\nAssistant:`,
6161
temperature: model.temperature,
6262
max_tokens_to_sample: model?.max_tokens_to_sample,
63-
max_gen_len: model?.max_gen_len,
64-
top_p: model.top_p,
63+
max_tokens: 500,
64+
p: 1,
65+
// max_gen_len: model?.max_gen_len,
66+
// top_p: model?.top_p,
6567
}),
6668
});
6769

6870
try {
6971
const bedrockResponse = await bedrockClient.send(chatBotModelInput);
7072
const decoder = new TextDecoder();
7173
const jsontext = JSON.parse(decoder.decode(bedrockResponse.body));
72-
74+
console.log(jsontext)
7375
res.status(200).json(jsontext);
7476
} catch (error: any) {
7577
throw new Error(error.message);

0 commit comments

Comments
 (0)