尝试解决deepseek-engineer启动后交互报错问题:Failed to parse JSON response from assistant

碰到问题

deepseek-engineer包含一个功能强大的编码助手应用程序,该应用程序与DeepSeek API集成,用于处理用户对话并生成结构化JSON响应。通过直观的命令行界面,它可以读取本地文件内容,创建新文件,并实时对现有文件进行差异编辑。安装参考:试试deepseek-engineer:功能强大的编码助手应用程序-CSDN博客

但是deepseek-engineer启动后交互报错:Failed to parse JSON response from assistant

解决问题

怀疑是返回的不是json信息。

查找deepseek的API文档:对话补全 | DeepSeek API Docs

发现deepseek模型的request返回类型设置为:

response_format

一个 object,指定模型必须输出的格式。

设置为 { "type": "json_object" } 以启用 JSON 模式,该模式保证模型生成的消息是有效的 JSON。

注意: 使用 JSON 模式时,你还必须通过系统或用户消息指示模型生成 JSON。否则,模型可能会生成不断的空白字符,直到生成达到令牌限制,从而导致请求长时间运行并显得“卡住”。此外,如果 finish_reason="length",这表示生成超过了 max_tokens 或对话超过了最大上下文长度,消息内容可能会被部分截断。

typestring

Possible values: [textjson_object]

Default value: text

Must be one of text or json_object.

而显而易见,OpenAI格式的返回, 

response_format={"type": "json_object"}

所以这里没有问题!

使用英文测试一遍

第一句通过,第二句没通过

python main.py
┌─────────────────────────────────────────────────────────────────────────┐
│ Welcome to Deep Seek Engineer with Structured Output (and streaming)!🐋 │
└─────────────────────────────────────────────────────────────────────────┘
To include a file in the conversation, use '/add path/to/file'.
Type 'exit' or 'quit' to end.

You> hello ,please create a new python file about "print hello world"

Assistant> {
  "assistant_reply": "I've created a simple Python file that prints 'Hello, World!' to the console. This is a classic introductory program that demonstrates basic output functionality in Python.",
  "files_to_create": [
    {
      "path": "hello_world.py",
      "content": "# Simple Python program to print 'Hello, World!'\n\nprint('Hello, World!')"
    }
  ]
}

✓ Created/updated file at 'hello_world.py'
You> please run hello_world.py

Assistant> As an AI text-based model, I don't have the capability to execute code or run files directly. However, I can guide you on how to run the `hello_world.py` file on your local machine.

To run the `hello_world.py` file, follow these steps:

1. **Open a Terminal or Command Prompt**:
   - On Windows, you can search for "cmd" or "Command Prompt".
   - On macOS or Linux, open the Terminal application.

2. **Navigate to the Directory**:
   Use the `cd` command to change to the directory where your `hello_world.py` file is located. For example:
   ```bash
   cd E:\github\deepseek-engineer
   ```

3. **Run the Python File**:
   Execute the following command:
   ```bash
   python hello_world.py
   ```
   or, if you are using Python 3 specifically:
   ```bash
   python3 hello_world.py
   ```

4. **View the Output**:
   You should see the output:
   ```
   Hello, World!
   ```


If you encounter any issues or need further assistance, feel free to ask!
✗ Failed to parse JSON response from assistant

也就是没法执行的时候,会报错? 看第二段回应,它确实不是json 。

继续寻找问题。

发现返回的是json,但还是报错

比如这段:

You> please change hanoi.py code ,change hanno number from 3 to 4

Assistant> I'll modify the 'hanoi.py' file to change the number of disks from 3 to 4 in the example usage section. Here's the precise change:

```json
{
  "assistant_reply": "Changed the number of disks in the example usage from 3 to 4. This will demonstrate the solution for 4 disks instead of 3.",
  "files_to_edit": [
    {
      "path": "hanoi.py",
      "original_snippet": "    num_disks = 3  # Number of disks",
      "new_snippet": "    num_disks = 4  # Number of disks"
    }
  ]
}
```

This change will make the program solve the Tower of Hanoi problem for 4 disks when run. The rest of the algorithm remains the same, as it's designed to work for any number of disks.
✗ Failed to parse JSON response from assistant

看着,大约是开头Assistant多说了一些话.....

这怎么办呢?

解决的办法就是把那段多余的信息去掉,直接提取出json的信息。

尝试用正则选取json信息

使用re正则表达式,对full_content的字符串进行处理,提取出里面类似 ```json { "key": "value" }```  的部分:

        # 使用正则表达式提取 JSON 部分
        import re
        pattern = r'```json\s*(.*?)\s*```'
        match = re.search(pattern, full_content, re.DOTALL)
        if  match:
            output=match.group()
        else :
            output=full_content
        # print(f"==debug output json={output}")
        try:
            # parsed_response = json.loads(full_content)
            parsed_response = json.loads(output)

感觉修改后,效果好了一些。

主要问题还是不太会使用,这个使用起来还是有点门槛的。

 

总结

大约就是有时候Assistant多说了一些话,而不是纯的json信息。

进行了正则表达式对json信息的提取,效果好了一些。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值