Hi everyone,
I’ve followed the documentation to the letter of the law. Yet I’m encountering the following error: “An error occurred: 400 Unknown parameter: ‘assistant_id’.” Despite reviewing tutorials and repeatedly reading the documentation, I’m still unsure how to resolve this issue.
const openai = new OpenAI({
apiKey: process.env.openaiAPI,
defaultHeaders: {
"OpenAI-Beta": "assistants=v2",
},
});
app.get("/openai", async (req, res) => {
const {
recipe_country_of_origin,
is_lactose_intolerant,
is_vegan,
what_are_user_other_dietary_requirements,
} = req.query;
try {
console.log(
{ recipe_country_of_origin },
{ is_lactose_intolerant },
{ is_vegan },
{ what_are_user_other_dietary_requirements }
);
const prompt = `Provide a recipe for a dish from ${recipe_country_of_origin}, taking into account the fact that I'm ${
is_lactose_intolerant === "true"
? "lactose intolerant"
: "not lactose intolerant"
} ${is_vegan === "true" ? "vegan" : "not vegan"} and ${
what_are_user_other_dietary_requirements === ""
? "I have no other dietary requirements"
: what_are_user_other_dietary_requirements
} `;
console.log(prompt);
const completion = await openai.chat.completions.create({
messages: [
{
role: "user",
content: `${prompt}`,
},
],
model: "gpt-3.5-turbo",
max_tokens: 2000,
});
const recipeImagePrompt = completion.choices[0].message.content;
const imageResponse = await openai.images.generate({
model: "dall-e-3",
prompt: `${recipeImagePrompt}`,
n: 1,
size: "1024x1024",
});
const assistant = await openai.beta.assistants.create({
name: "Cuisine expert",
instructions:
"You are one of the best chefs in the world. Write and run code to help user with their question.",
tools: [{ type: "code_interpreter" }],
model: "gpt-4o",
});
console.log(assistant)
const thread = await openai.beta.threads.create({
assistant_id: assistant.id,
});
console.log(thread)
const message = await openai.beta.threads.messages.create(thread.id, {
role: "user",
content:
"I've got some tomatoes, onions, and some beef. What can I cook?",
});
// We use the stream SDK helper to create a run with
// streaming. The SDK provides helpful event listeners to handle
// the streamed response.
const run = openai.beta.threads.runs
.stream(thread.id, {
assistant_id: assistant.id,
})
.on("textCreated", (text) => process.stdout.write("\nassistant > "))
.on("textDelta", (textDelta, snapshot) =>
process.stdout.write(textDelta.value)
)
.on("toolCallCreated", (toolCall) =>
process.stdout.write(`\nassistant > ${toolCall.type}\n\n`)
)
.on("toolCallDelta", (toolCallDelta, snapshot) => {
if (toolCallDelta.type === "code_interpreter") {
if (toolCallDelta.code_interpreter.input) {
process.stdout.write(toolCallDelta.code_interpreter.input);
}
if (toolCallDelta.code_interpreter.outputs) {
process.stdout.write("\noutput >\n");
toolCallDelta.code_interpreter.outputs.forEach((output) => {
if (output.type === "logs") {
process.stdout.write(`\n${output.logs}\n`);
}
});
}
}
});
console.log({ run }, message);
doubleResponse = {
text: completion,
image: imageResponse,
};
res.json(doubleResponse);
} catch (error) {
console.error("An error occurred:", error.message);
res.status(500).json({ error: error.message });
}
});
app.use(express.static(path.join(__dirname, "public")));
const port = process.env.PORT || 3000;
app.listen(port, () => {
console.log(`Server running on port ${port}`);
});