Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ChatGPTBot对错误的解析偶尔报错[BUG] #177

Closed
qcgm1978 opened this issue Jun 4, 2023 · 1 comment
Closed

ChatGPTBot对错误的解析偶尔报错[BUG] #177

qcgm1978 opened this issue Jun 4, 2023 · 1 comment
Labels
bug Something isn't working

Comments

@qcgm1978
Copy link
Contributor

qcgm1978 commented Jun 4, 2023

Describe the bug / 描述问题
使用ChatGPT bot时有时候返回的错误数据不是json,而是html。导致JSON.parse(error.data)报错。

To Reproduce / 复现步骤
打开应用选择GPT-3.5或GPT(WEB浏览),切换到香港代理。发送prompt

Expected behavior / 期望行为
能够正确解析数据。比如这样(src/bots/openai/ChatGPTBot.js):

if (error.data) {
  try {
    const data = JSON.parse(error.data);
    message = data.detail;
  } catch (e) {
    // 创建一个虚拟的 HTML 元素
    const div = document.createElement("div");

    // 将文档字符串赋值给虚拟元素的 innerHTML 属性
    div.innerHTML = error.data;

    // 使用 DOM 操作获取目标标签的文本内容
    const p = div.querySelector("p").textContent;
    const span=div.querySelector("span").textContent
    message = `${p}. ${span}`;
  }
}

这时能够显示恰当的内容:

Screenshot 2023-06-04 at 18 39 55
@qcgm1978 qcgm1978 added the bug Something isn't working label Jun 4, 2023
@sunner
Copy link
Owner

sunner commented Jun 4, 2023

嗯,这种情况是没处理,多谢发现了它。不过,最好别用 catch 来处理正常逻辑。可以先通过简单的格式判断来分辨是 JSON 还是 html

@sunner sunner closed this as completed in 69a08d6 Jun 5, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants