I try to send a prompt containing raw text and HTML code in order to fix spelling errors.
E.g. a prompt would be:
fix spelling and grammar in the following html code
<p>this is som txt which needs to b fixed.<p>
But when I submit this prompt to the API via Javascript (see the code below) the only thing I receive as a response is this:
```html
The same prompt works fine when using Chat-GPT’s web page. The response is:
<p>This is some text which needs to be fixed.</p>
What am I doing wrong?
Here’s the code I’m using:
const prompt = 'fix spelling and grammar in the following html codenn<p>this is som txt which needs to b fixed.<p>';
const response = await fetch(url, {
method: 'POST',
headers: {
'Authorization': bearer,
'Content-Type': 'application/json'
},
body: JSON.stringify({
temperature: 1,
top_p: 1,
n: 1,
stream: true,
logprobs: null,
stop: 'n',
model: 'gpt-4o',
messages: [
{ "role": "user", "content": prompt }
]
}),
signal: controller.signal
});
const reader = response.body.getReader();
const decoder = new TextDecoder("utf-8");
let result = '';
while (true) {
const { done, value } = await reader.read();
if (done) {
break;
}
// Massage and parse the chunk of data
const chunk = decoder.decode(value);
const lines = chunk.split("n");
const parsedLines = lines
.map((line) => line.replaceAll(/^data: /gi, "").trim()) // Remove the "data: " prefix
.filter((line) => line !== "" && line !== "[DONE]") // Remove empty lines and "[DONE]"
.map((line) => JSON.parse(line)); // Parse the JSON string
for (const parsedLine of parsedLines) {
const { choices } = parsedLine;
const { delta } = choices[0];
const { content } = delta;
// Update the UI with the new content
if (content) {
console.log("Content: " + content);
result += content;
}
}
}
console.log('Result:n' + result);
``
2
Answers
I finally found it out. The error was the
stop
param which caused the API to stop generating tokens.Furthermore, I simplified the code for better readability. This code works:
In the past I wrote some codes that are working with ChatGPT and I edited my code. Very common issue while working with ChatGPT API is that sometimes you have to write him: You are ChatGPT, a large language model trained by OpenAI. . This code is a version for Node.js so there might be problems with running with HTML.