Hello everyone,
@imesse I apologize, but in my answer I took it for granted that the problem could be in the prompt, so I made some changes, based on my experience. This time I took it for granted only the problem, but not the fact that the problem can be your prompt.
I did a series of tests, using your prompt as a system prompt, and your settings, such as temperature 0.3, frequency penalty 1.8, and so on, as is. With ChatGPT I generated 20 different prompts of around 700/1000 tokens each, in 5 different European languages, including Italian. So 4 different prompts for each language.
Believe it or not, everything worked perfectly. Iām not talking about the quality of the summaries, but each summary corresponded to the language of the text to be summarized.
If youāre interested, I created a spreadsheet with the results: OpenAI API languages tests - Google Sheets
The problem must be looked for elsewhere. Between my tests and your code I see only one difference, but it could be important.
Let me start by saying that I donāt know Javascript, but I notice that the constant ācontextMessagesā is an array of strings. I canāt interpret the rest of the code, but if my guess is correct, then the AI receives a system message broken into many strings. In my opinion the json payload that arrives at the AI is as the following example:
{
"model": "gpt-3.5-turbo",
"messages": [
{
"role": "system",
"content": "Your task is to:"
},
{
"role": "system",
"content": "Understand the language of the text."
},
{
"role": "system",
"content": "Create a summary in the same language that act as a trailer."
},
.
.
{
"role": "user",
"content": prompt
}
],
"temperature": 0.3,
.
.
}
As you can see, it receive different system messages in the same time. Even though the API allows this, I suspect it may be confusing to the AI model.
I did my tests with the system message in a single string:
{
"model": "gpt-3.5-turbo",
"messages": [
{
"role": "system",
"content": "Your task is to:\nUnderstand the language of the text.\nCreate a summary in the same language that act as a trailer.\nFollow theese two rules:\nThe digest must be short, less than 200 words\nThe digest must be in the same language of the text"
},
{
"role": "user",
"content": prompt
}
],
"temperature": 0.3,
.
.
}
You could do a console.log() of your JSON payload, to check if your system message is actually being broken into many different strings. Or, more simply, do some testing by passing the modified system message directly, and see what happens. Basically try declaring your ācontextMessagesā constant as an array containing only one item of type string:
const contextMessages = [
`Your task is to:
Understand the language of the text.
Create a summary in the same language that act as a trailer.
Follow theese two rules:
The digest must be short, less than 200 words
The digest must be in the same language of the text`,
];
Again, with a console.log() try to check if the payload is what you expect.
Ciao