jackMort / ChatGPT.nvim

ChatGPT Neovim Plugin: Effortless Natural Language Generation with OpenAI's ChatGPT API
Apache License 2.0
3.56k stars 307 forks source link

:ChatGPTEditWithInstructions Not Returning Full Code Block #447

Open cparram opened 2 weeks ago

cparram commented 2 weeks ago

Description I am encountering an issue with the :ChatGPTEditWithInstructions command. When I use this command, it does not return the full code block but only the new lines. I have tried several configurations, but the problem persists.

Steps to Reproduce

  1. Open Neovim.
  2. Select a block of text in visual mode.
  3. Execute the command pe (mapped to :ChatGPTEditWithInstructions).
  4. Adds the instruction

Expected Behavior The full code block should be returned with the modifications.

Actual Behavior Only the new lines are returned, not the full code block.

Additional Information Neovim version: v0.10.0 Operating System: macOS

Any help to resolve this issue would be greatly appreciated. Thank you!

cparram commented 2 weeks ago

Solution To ensure the API response maintains the code, the instruction sent to the API needs to be modified. The instruction should specifically request that the entire code be outputted with the applied changes and that the indentation and formatting be preserved.

Updated Function Here is the updated function build_edit_messages which includes the modified instruction:

local build_edit_messages = function(input, instructions, use_functions_for_edits)
  local system_message_content
  if use_functions_for_edits then
    system_message_content =
      "Apply the changes requested by the user to the code. Output the entire code with the applied changes and a brief description of the edits. Ensure that the indentation and formatting are preserved. DO NOT wrap the code in a formatting block. DO NOT provide other text or explanation."
  else
    system_message_content =
      "Apply the changes requested by the user to the code. Output the entire code with the applied changes. Ensure that the indentation and formatting are preserved. DO NOT wrap the code in a formatting block. DO NOT provide other text or explanation."
  end
  local messages = {
    {
      role = "system",
      content = system_message_content,
    },
    {
      role = "user",
      content = input,
    },
    {
      role = "user",
      content = instructions,
    },
  }
  return messages
end