首页
/ 解决CodeCompanion.nvim中DeepSeek R1推理模型系统提示问题

解决CodeCompanion.nvim中DeepSeek R1推理模型系统提示问题

2025-06-24 17:47:45作者:宣利权Counsellor

在CodeCompanion.nvim插件中使用DeepSeek R1推理模型时,开发者可能会遇到一个常见问题:当消息序列中包含系统提示(system prompt)时,模型会返回错误。本文将深入分析这个问题并提供解决方案。

问题背景

DeepSeek R1推理模型对消息序列有特殊要求:除了系统消息外,第一条消息必须是用户消息。如果消息序列以系统消息开头,后面跟着用户消息,模型会返回如下错误:

错误:{"error":{"message":"The first message (except the system message) of deepseek-reasoner must be a user message, but an assistant message detected.","type":"invalid_request_error","param":null,"code":"invalid_request_error"}}

技术分析

这个问题源于DeepSeek R1模型的消息序列处理机制。模型期望消息序列中用户消息和助手消息交替出现。当使用CodeCompanion.nvim的标准配置时,通常会设置一个系统提示作为第一条消息,这违反了模型的预期输入格式。

解决方案

方案一:修改适配器配置

对于直接使用DeepSeek API的情况,可以通过修改适配器配置来解决。以下是完整的适配器配置示例:

deepseek = function()
  return require("codecompanion.adapters").extend("deepseek", {
    env = {
      api_key = "DEEPSEEK_API_KEY",
    },
    schema = {
      model = {
        default = "deepseek-reasoner",
      },
      temperature = {
        default = 0,
      },
    },
    handlers = {
      chat_output = function(self, data)
        local output = {}
        if data and data ~= "" then
          local data_mod = prepare_data_for_json(data)
          local ok, json = pcall(vim.json.decode, data_mod, { luanil = { object = true } })
          if ok and json.choices and #json.choices > 0 then
            local choice = json.choices[1]
            local delta = (self.opts and self.opts.stream) and choice.delta or choice.message
            if delta then
              if delta.role then
                output.role = delta.role
              else
                output.role = nil
              end
              output.content = ""
              -- 添加推理输出
              if delta.reasoning_content then
                output.content = delta.reasoning_content
              end
              if delta.content then
                output.content = output.content .. delta.content
              end
              return {
                status = "success",
                output = output,
              }
            end
          end
        end
      end,
    },
  })
end,

方案二:通过OpenRouter使用DeepSeek R1

如果通过OpenRouter使用DeepSeek R1模型,需要注意以下几点:

  1. OpenRouter已经标准化了推理令牌的处理方式
  2. 推理内容会出现在消息的reasoning字段中
  3. 需要设置can_reason选项为true

以下是完整的OpenRouter适配器配置:

deepseekr1 = function()
  return require("codecompanion.adapters").extend("openai_compatible", {
    env = {
      url = "https://openrouter.ai/api",
      api_key = "OPENROUTER_API_KEY",
      chat_url = "/v1/chat/completions",
    },
    opts = {
      stream = false,
      can_reason = true
    },
    schema = {
      model = {
        default = "deepseek/deepseek-r1",
      },
    },
    handlers = {
      chat_output = function(self, data)
        local utils = require("codecompanion.utils.adapters")
        local output = {}
        if data and data ~= "" then
          local data_mod = utils.clean_streamed_data(data)
          local ok, json = pcall(vim.json.decode, data_mod, { luanil = { object = true } })
          if ok and json.choices and #json.choices > 0 then
            local choice = json.choices[1]
            local delta = (self.opts and self.opts.stream) and choice.delta or choice.message
            if delta then
              output.role = nil
              if delta.role then
                output.role = delta.role
              end
              if self.opts.can_reason and delta.reasoning then
                output.reasoning = delta.reasoning
              end
              if delta.content then
                output.content = (output.content or "") .. delta.content
              end
              return {
                status = "success",
                output = output,
              }
            end
          end
        end
      end,
    },
  })
end,

注意事项

  1. 流式传输(stream=true)可能会导致响应格式问题,建议先使用非流式模式
  2. OpenRouter返回的推理内容在reasoning字段而非reasoning_content字段
  3. 建议启用DEBUG或TRACE级别的日志来检查请求和响应的具体内容

总结

DeepSeek R1推理模型在CodeCompanion.nvim中的使用需要注意其特殊的消息序列要求。通过合理配置适配器,特别是正确处理系统提示和推理内容字段,可以充分发挥该模型的推理能力。对于通过OpenRouter使用的情况,还需要注意其标准化的推理令牌处理方式。

登录后查看全文
热门项目推荐
相关项目推荐

热门内容推荐

最新内容推荐

项目优选

收起
ohos_react_nativeohos_react_native
React Native鸿蒙化仓库
C++
176
260
RuoYi-Vue3RuoYi-Vue3
🎉 (RuoYi)官方仓库 基于SpringBoot,Spring Security,JWT,Vue3 & Vite、Element Plus 的前后端分离权限管理系统
Vue
854
505
openGauss-serveropenGauss-server
openGauss kernel ~ openGauss is an open source relational database management system
C++
129
182
openHiTLSopenHiTLS
旨在打造算法先进、性能卓越、高效敏捷、安全可靠的密码套件,通过轻量级、可剪裁的软件技术架构满足各行业不同场景的多样化要求,让密码技术应用更简单,同时探索后量子等先进算法创新实践,构建密码前沿技术底座!
C
254
295
ShopXO开源商城ShopXO开源商城
🔥🔥🔥ShopXO企业级免费开源商城系统,可视化DIY拖拽装修、包含PC、H5、多端小程序(微信+支付宝+百度+头条&抖音+QQ+快手)、APP、多仓库、多商户、多门店、IM客服、进销存,遵循MIT开源协议发布、基于ThinkPHP8框架研发
JavaScript
93
15
Cangjie-ExamplesCangjie-Examples
本仓将收集和展示高质量的仓颉示例代码,欢迎大家投稿,让全世界看到您的妙趣设计,也让更多人通过您的编码理解和喜爱仓颉语言。
Cangjie
331
1.08 K
HarmonyOS-ExamplesHarmonyOS-Examples
本仓将收集和展示仓颉鸿蒙应用示例代码,欢迎大家投稿,在仓颉鸿蒙社区展现你的妙趣设计!
Cangjie
397
370
note-gennote-gen
一款跨平台的 Markdown AI 笔记软件,致力于使用 AI 建立记录和写作的桥梁。
TSX
83
4
CangjieCommunityCangjieCommunity
为仓颉编程语言开发者打造活跃、开放、高质量的社区环境
Markdown
1.07 K
0
kernelkernel
deepin linux kernel
C
21
5