Skip to content

[错误报告] 使用自定义引擎时遇到响应解析错误,DeepSeek API 无法正常返回结果 #363

@cksdxz1007

Description

@cksdxz1007

基础信息

  • 操作系统: macOS
  • Calibre 版本: 7.20
  • 插件版本:2.3.5
  • 插件来源: 安装自 Calibre/Rolling Release

描述错误

描述

在使用 Ebook-Translator-Calibre-Plugin 的自定义引擎功能时,我尝试使用 DeepSeek API 进行代码翻译,但遇到了响应解析错误。错误信息显示插件无法解析 DeepSeek 返回的 JSON 响应。我怀疑问题可能出在插件的 _parse 函数中,未能正确提取响应中的 choices[0].message.content 数据。

复现步骤

  1. 在自定义引擎中配置 DeepSeek 的 API 请求,结构如下:

    {
        "name": "Deepseek",
        "languages": {
            "source": {
                "Source Language": "code"
            },
            "target": {
                "Target Language": "code"
            }
        },
        "request": {
            "url": "https://api.deepseek.com/chat/completions",
            "method": "POST",
            "headers": {
                "Content-Type": "application/json",
                "Authorization": "Bearer sk-xxx"
            },
            "data": {
                "model": "deepseek-chat",
                "messages": [
                    {"role": "system", "content": "You are a code translation assistant."},
                    {"role": "user", "content": "Translate the following code from <source> to <target>:\n\n<text>"}
                ],
                "stream": false
            }
        },
        "response": "response"
    }
  2. 执行代码翻译请求时,遇到以下错误:

    Traceback (most recent call last):
      File "calibre_plugins.ebook_translator.components.engine", line 67, in translate_text
        translation = self.translator.translate(text)
                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
      File "calibre_plugins.ebook_translator.engines.custom", line 120, in translate
        return self.get_result(
               ^^^^^^^^^^^^^^^^
      File "calibre_plugins.ebook_translator.engines.base", line 210, in get_result
        raise Exception(
    Exception: 无法解析返回的响应。原始数据:
    
    Traceback (most recent call last):
      File "calibre_plugins.ebook_translator.engines.base", line 201, in get_result
        return response if callback is None else callback(response)
                                                 ^^^^^^^^^^^^^^^^^^
      File "calibre_plugins.ebook_translator.engines.custom", line 134, in _parse
        raise Exception(_('Response was parsed incorrectly.'))
    Exception: 响应没有正确解析。
    
    {"id":"7182382a-4151-4001-97e3-20102b31912f","object":"chat.completion","created":1730721207,"model":"deepseek-chat","choices":[{"index":0,"message":{"role":"assistant","content":"¡Hola Mundo!"},"logprobs":null,"finish_reason":"stop"}],"usage":{"prompt_tokens":24,"completion_tokens":4,"total_tokens":28,"prompt_cache_hit_tokens":0,"prompt_cache_miss_tokens":24},"system_fingerprint":"fp_1c141eb703"}
    

问题描述

从错误信息看,DeepSeek API 返回的数据格式如下:

{
    "id": "7182382a-4151-4001-97e3-20102b31912f",
    "object": "chat.completion",
    "created": 1730721207,
    "model": "deepseek-chat",
    "choices": [
        {
            "index": 0,
            "message": {
                "role": "assistant",
                "content": "¡Hola Mundo!"
            },
            "logprobs": null,
            "finish_reason": "stop"
        }
    ],
    "usage": {
        "prompt_tokens": 24,
        "completion_tokens": 4,
        "total_tokens": 28,
        "prompt_cache_hit_tokens": 0,
        "prompt_cache_miss_tokens": 24
    },
    "system_fingerprint": "fp_1c141eb703"
}

错误提示插件在 calibre_plugins.ebook_translator.engines.custom 文件的 _parse 函数中,未能正确提取 choices[0].message.content 数据,导致解析失败。

期望的行为

希望插件能够识别并解析 DeepSeek API 的响应格式,将 choices[0].message.content 的内容返回为翻译结果。

其他信息

由于插件的自定义引擎没有提供直接输入 API 密钥的地方,我只能在自定义引擎 JSON 配置中通过 Authorization 字段传入 API key。

希望可以帮助解决这个解析问题!

Metadata

Metadata

Assignees

No one assigned

    Labels

    wontfixThis will not be worked on

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions