Skip to content

Conversation

@bashtoni
Copy link
Contributor

Description

Claude 3.7 adds DeepSeek R1-like 'extended thinking' - see https://www.anthropic.com/news/visible-extended-thinking
This PR adds support for this, and also extended output.
Like the DeepSeek plugin, it outputs the 'thinking' text when streaming.

One thing I'd appreciate feedback on. I've added parameters that don't directly map to API parameters, which I then set to nil later once we've built the actual params/header required. Is there a better way to do this?

Checklist

  • I've read the contributing guidelines and have adhered to them in this PR
  • I've updated the README and/or relevant docs pages
  • I've run make docs to update the vimdoc pages

@olimorris
Copy link
Owner

olimorris commented Feb 24, 2025

This looks great, thank you.

A couple of suggestions:

  • In schema.model, we should change "claude-3-7-sonnet-20250219" to ["claude-3-7-sonnet-20250219"] = { opts = { can_reason = true } } (I'm suggesting can_reason as it's consistent across OpenAI and DeepSeek).

  • Then we can apply conditionals to items in the schema via a condition function:

extended_output = {
   order = 2,
   mapping = "parameters",
   type = "boolean",
   optional = true,
   default = false,
   condition = function(schema)
     local model = schema.model.default
     if schema.model.choices[model] and schema.model.choices[model].opts then
       return schema.model.choices[model].opts.can_reason
     end
   end,
   desc = "Enable larger output context (128k tokens). Only available with claude-3-7-sonnet-20250219.",
},
  • I'll leave it to your discretion regarding adding tests for this. The changes in chat_output are so minimal.

One thing I'd appreciate feedback on. I've added parameters that don't directly map to API parameters, which I then set to nil later once we've built the actual params/header required. Is there a better way to do this

You could set mapping = "temp". It won't get sent to the LLM but will be available on self.schema.

@olimorris olimorris changed the title Add Extended Thinking and Extended Output for Cladue 3.7 Add Extended Thinking and Extended Output for Claude 3.7 Feb 24, 2025
@bashtoni
Copy link
Contributor Author

Added can_reason = true as suggested. I couldn't get mapping = "temp" to work for me - possibly a skill issue, but the params weren't available.

@olimorris
Copy link
Owner

olimorris commented Feb 25, 2025

@bashtoni I've just pushed a fix to the main branch (and updated this PR in the process). This has allowed me to do some clever manipulation of max_tokens should the user enable extended thinking. The temp schema mappings should now be working, leading to a body request of:

{
    "system": [
        {
            "text": "You are an AI programming assistant named \"CodeCompanion\". You are currently plugged into the Neovim text editor on a user's machine.\n\nYour core tasks include:\n- Answering general programming questions.\n- Explaining how the code in a Neovim buffer works.\n- Reviewing the selected code in a Neovim buffer.\n- Generating unit tests for the selected code.\n- Proposing fixes for problems in the selected code.\n- Scaffolding code for a new workspace.\n- Finding relevant code to the user's query.\n- Proposing fixes for test failures.\n- Answering questions about Neovim.\n- Running tools.\n\nYou must:\n- Follow the user's requirements carefully and to the letter.\n- Keep your answers short and impersonal, especially if the user's context is outside your core tasks.\n- Minimize additional prose unless clarification is needed.\n- Use Markdown formatting in your answers.\n- Include the programming language name at the start of each Markdown code block.\n- Avoid including line numbers in code blocks.\n- Avoid wrapping the whole response in triple backticks.\n- Only return code that's directly relevant to the task at hand. You may omit code that isn’t necessary for the solution.\n- Use actual line breaks in your responses; only use \"\\n\" when you want a literal backslash followed by 'n'.\n- All non-code text responses must be written in the English language indicated.\n\nWhen given a task:\n1. Think step-by-step and, unless the user requests otherwise or the task is very simple, describe your plan in detailed pseudocode.\n2. Output the final code in a single code block, ensuring that only relevant code is included.\n3. End your response with a short suggestion for the next user turn that directly supports continuing the conversation.\n4. Provide exactly one complete reply per conversation turn.",
            "cache_control": {
                "type": "ephemeral"
            },
            "type": "text"
        }
    ],
    "messages": [
        {
            "role": "user",
            "content": "Can you show me some Ruby code?"
        }
    ],
    "stream": true,
    "temperature": 1,
    "max_tokens": 17000,
    "thinking": {
        "budget_tokens": 16000,
        "type": "enabled"
    },
    "model": "claude-3-7-sonnet-20250219"
}

@olimorris
Copy link
Owner

And added one final fix. If you fix the reasoning header and give this a test, I think we're good to merge!

@bashtoni
Copy link
Contributor Author

Thanks!

I've tested and everything looks good. What's up with the reasoning header? It seems to be working fine for me:

image

@olimorris
Copy link
Owner

All seems to be good now. Fantastic job on this @bashtoni, a great addition to the plugin.

@olimorris olimorris merged commit a497adb into olimorris:main Feb 25, 2025
4 checks passed
@gstokkink
Copy link

gstokkink commented Feb 26, 2025

@olimorris can this also be enabled for the copilot adapter somehow? Is adding can_reason = true enough?

@olimorris
Copy link
Owner

@gstokkink see #1001

cleong14 pushed a commit to cleong14/codecompanion.nvim that referenced this pull request May 21, 2025
…3.7 (olimorris#998)

* Add Extended Thinking and Extended Output for Cladue 3.7

* Add `can_reason` to model as suggested

* Claude 3.7 supports 128k tokens with extended output

* Update anthropic.lua for use of temp table and max tokens error

* fix: do not overwrite headers

---------

Co-authored-by: Oli <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants