I have been spending the last two days trying to make a support chat bot for my company website, but I am struggling on all fronts.
- I built an assistant, but can’t seem to link it by ID from my application
- I can’t get the assistant to understand the thread context; every question the assistant is asked seems like a totally new question to it
Code:
import OpenAI from "openai";
const openai = new OpenAI({
apiKey: process.env.OPENAI_API_KEY,
});
export async function POST(req) {
const { messages } = await req.json();
console.log(messages);
messages.forEach((message) => {
console.log(message.role);
console.log(message.content);
});
try {
const response = await openai.chat.completions.create({
model: "gpt-4o",
// model: "asst_ROx9RsOPl0DpbJdu6sEP5B6h",
messages: [
{ role: "user", content: messages[messages.length - 1].content },
],
});
const message = response.choices[0].message;
return new Response(JSON.stringify({ message }), { status: 200 });
} catch (error) {
console.error("Error communicating with OpenAI:", error);
return new Response(JSON.stringify({ error: "Internal server error" }), {
status: 500,
});
}
}
export async function PUT(req) {
const { messages } = await req.json();
const assistant = await openai.beta.assistants.create({
name: "Support Agent",
description:
"You are a customer support agent. Your job is to answer questions specific to both broadcast and products. You should always assume you are talking to a customer.",
model: "gpt-4o",
});
const thread = await openai.beta.threads.create();
const message = await openai.beta.threads.messages.create(thread.id, {
role: "user",
content: messages[messages.length - 1].content,
});
// We use the stream SDK helper to create a run with
// streaming. The SDK provides helpful event listeners to handle
// the streamed response.
let run = await openai.beta.threads.runs.createAndPoll(thread.id, {
// assistant_id: assistant.id,
assistant_id: "asst_ROx9RsOPl0DpbJdu6sEP5B6h",
instructions: "Please address the user as Master.",
});
let currentMessage = "internal error, appologies";
if (run.status === "completed") {
const messages = await openai.beta.threads.messages.list(run.thread_id);
currentMessage = messages.data[0].content[0].text.value;
} else {
console.log(run.status);
}
// this is something for when you are using the console I think
// const run = openai.beta.threads.runs
// .stream(thread.id, {
// assistant_id: assistant.id,
// })
// .on("textCreated", (text) =>
// process.stdout.write("\nassistant > " + text.value + "\n"),
// )
// .on("textDelta", (textDelta, snapshot) => {
// process.stdout.write(textDelta.value);
// })
// .on("toolCallCreated", (toolCall) =>
// process.stdout.write(`\nassistant > ${toolCall.type}\n\n`),
// );
return new Response(
JSON.stringify({ message: { role: "Bot", content: currentMessage } }),
{
status: 200,
},
);
}