向OpenAI API发送含HTML代码的提示词时出现异常结果
问题:调用OpenAI API修复HTML拼写错误时响应不完整
我尝试发送包含纯文本和HTML代码的提示词来修正拼写错误,提示词如下:
fix spelling and grammar in the following html code <p>this is som txt which needs to b fixed.<p>
但通过JavaScript调用OpenAI API提交该提示词时,仅收到如下响应:
而在ChatGPT网页端使用相同提示词可正常得到修复后的结果:
<p>This is some text which needs to be fixed.</p>
附上调用代码,请问哪里操作有误?
const prompt = 'fix spelling and grammar in the following html code\n\n<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);
解决方案
错误原因
- stop参数截断了输出:你设置了
stop: '\n',这会强制模型在生成第一个换行符时停止输出。模型返回修复内容时,会先输出```html,紧接着就是换行,刚好触发停止条件,导致后续的HTML代码被截断。 - temperature参数过高:
temperature: 1会让输出随机性极强,不仅可能导致结果不稳定,还可能加剧截断问题,建议调低到0-0.3区间,保证输出精准可控。
修改后的代码
const prompt = 'fix spelling and grammar in the following html code\n\n<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: 0.2, // 调低温度保证输出稳定 top_p: 1, n: 1, stream: true, logprobs: null, // 移除stop参数,避免强制截断输出 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; } const chunk = decoder.decode(value); const lines = chunk.split("\n"); const parsedLines = lines .map((line) => line.replaceAll(/^data: /gi, "").trim()) .filter((line) => line !== "" && line !== "[DONE]") .map((line) => JSON.parse(line)); for (const parsedLine of parsedLines) { const { choices } = parsedLine; const { delta } = choices[0]; const { content } = delta; if (content) { console.log("Content: " + content); result += content; } } } console.log('Result:\n' + result);
内容的提问来源于stack exchange,提问作者Abid
相关产品推荐
相关产品推荐

