AI assistant API not responding in the correct language

Hi everyone, here is the issue:

My AI assistant always responds me in the query language when the request is submitted in the playground.
But when it comes to use the API, it always answers in english.

Here are the instructions:
Always reply in the same language used predominantly in the received request with. The language of response should not vary based on the sender’s name or other details. If the query is predominantly in English, reply in English; if it’s in French, reply in French, and so on.

Why does the use in the threads/playground differs from the use through the API?

Hope you have some ideas :wink:

Try using a less complex prompt such as “Respond in the language that the user talks to you in.”

It should be the same as the playground just uses the API, check your script to make sure you are creating and using threads properly, this may be your issue.

Yeah, I understand, except I see no problem in my code. But thank you anyway!

// Endpoint for free generation
app.post(‘/api/get-ai-response-free’, async (req, res) => {
const { inputs, startDate } = req.body;

console.log(`Received request with inputs: ${inputs}, startDate: ${startDate}`);

try {
const assistantId = FREE_GENERATION_ASSISTANT_ID;

// Step 1: Create a new thread
const threadResponse = await axios.post(https://api.openai.com/v1/threads, {}, {
headers: {
‘Authorization’: Bearer ${OPENAI_API_KEY},
‘Content-Type’: ‘application/json’,
‘OpenAI-Beta’: ‘assistants=v2’
}
});

const threadId = threadResponse.data.id;
console.log(‘Created new thread with ID:’, threadId);

// Step 2: Add a message to the thread
await axios.post(https://api.openai.com/v1/threads/${threadId}/messages, {
role: ‘user’,
content: User's inputs: ${inputs}. They started on: ${startDate}. Please provide advice based on this information.
}, {
headers: {
‘Authorization’: Bearer ${OPENAI_API_KEY},
‘Content-Type’: ‘application/json’,
‘OpenAI-Beta’: ‘assistants=v2’
}
});

// Step 3: Run the assistant
let run_res = await axios.post(https://api.openai.com/v1/threads/${threadId}/runs, { assistant_id: assistantId }, {
headers: {
‘Authorization’: Bearer ${OPENAI_API_KEY},
‘Content-Type’: ‘application/json’,
‘OpenAI-Beta’: ‘assistants=v2’
}
});
console.log(Running assistant with ID: ${assistantId}, run ID:, run_res.data.id);

// Wait for the assistant to finish
while (run_res.data.status !== ‘completed’) {
await new Promise(resolve => setTimeout(resolve, 1000));
run_res = await axios.get(https://api.openai.com/v1/threads/${threadId}/runs/${run_res.data.id}, {
headers: {
‘Authorization’: Bearer ${OPENAI_API_KEY},
‘Content-Type’: ‘application/json’,
‘OpenAI-Beta’: ‘assistants=v2’
}
});
console.log(‘Assistant run status:’, run_res.data.status);
}

// Step 4: Retrieve the response
const finalResponse = await axios.get(https://api.openai.com/v1/threads/${threadId}/messages, {
headers: {
‘Authorization’: Bearer ${OPENAI_API_KEY},
‘Content-Type’: ‘application/json’,
‘OpenAI-Beta’: ‘assistants=v2’
}
});

const assistantMessage = finalResponse.data.data.find(msg => msg.role === ‘assistant’).content[0];
console.log(‘Received response from assistant:’, assistantMessage.text.value);

res.json({ response: assistantMessage.text.value });

1 Like

THIS SAVED ME!!!

I have been bashing my head trying to figure out why my openai assistant wasn’t responding in my chatbot’s interface for the past MONTH!!! Granted, I am not a coder and have been relying solely on ChatGPT for this (please don’t judge lol). So when ChatGPT’s code wasn’t working, i went online night after night after night, looking and learning and then I came across this lifejacket in the seas I have been tossed around in! And you know what? I really love this!

This thread was a lifesaver…

THANK YOU!!!