frankroeder / parrot.nvim

parrot.nvim 🦜 - the plugin that brings stochastic parrots to Neovim. This is a gp.nvim-fork focused on simplicity.
Other
219 stars 14 forks source link

[Bug] parrot.nvim: messages.0.role: Input should be 'user' or 'assistant' #8

Closed ryicoh closed 4 months ago

ryicoh commented 4 months ago

Today, Anthropic API has changed to return the following error.

parrot.nvim: messages.0.role: Input should be 'user' or 'assistant'
Screenshot 2024-05-06 at 9 44 14

The log of the payload is here.

parrot.nvim: M.query(): payload: {
  max_tokens = 4096,
  messages = { {
      content = "",
      role = ""
    }, {
      content = "hello",
      role = "user"
    } },
  model = "claude-3-haiku-20240307",
  stream = true,
  system = "You are a versatile AI assistant with capabilities\nextending to general knowledge and coding support. Whe
n engaging\nwith users, please adhere to the following guidelines to ensure\nthe highest quality of interaction:\n\n-
Admit when unsure by saying 'I don't know.'\n- Ask for clarification when needed.\n- Use first principles thinking to
analyze queries.\n- Start with the big picture, then focus on details.\n- Apply the Socratic method to enhance underst
anding.\n- Include all necessary code in your responses.\n- Stay calm and confident with each task.\n"
}

I make it work with the following changes.

$ git diff
diff --git a/lua/parrot/init.lua b/lua/parrot/init.lua
index cf19ce0..8248c71 100644
--- a/lua/parrot/init.lua
+++ b/lua/parrot/init.lua
@@ -1155,6 +1155,13 @@ M.chat_respond = function(params)
     message.content = message.content:gsub("^%s*(.-)%s*$", "%1")
   end

+  -- remove a message if it's role is empty
+  for i = #messages, 1, -1 do
+    if messages[i].role == "" then
+      table.remove(messages, i)
+    end
+  end
+
   -- write assistant prompt
   local last_content_line = utils.last_content_line(buf)
   vim.api.nvim_buf_set_lines(buf, last_content_line, last_content_line, false, { "", agent_prefix .. agent_suffix, "" })
frankroeder commented 4 months ago

Hi @ryicoh, thank you for your report. Which version are you currently using? Please let me know the commit or tag. With the latest version v0.2.3, I was not able to reproduce this error message. However, I did encounter a similar error when I realized I was using my wrong account with no remaining credit.

ryicoh commented 4 months ago

This is my version of parrot.nvim.

parrot.nvim % git log
commit c9db07995107fa7d4f8196126451e1760a39ab25 (grafted, HEAD -> main, origin/main, origin/HEAD)
Author: Frank Röder <fr.coding@proton.me>
Date:   Fri May 3 13:56:43 2024 +0200
ryicoh commented 4 months ago

I've tried to make minimal vimrc for reproducing the issue, but I couldn't do it. I'll reopen the issue if I encounter it again.

" This file is for reproducing the bug in the issue #8
" see: https://github.com/frankroeder/parrot.nvim/issues/8

" 1. Clone the repository
"
"   $ git clone https://github.com/frankroeder/parrot.nvim/ ~/.local/share/nvim/site/pack/debug/opt/parrot.nvim
"   $ git clone https://github.com/nvim-lua/plenary.nvim    ~/.local/share/nvim/site/pack/debug/opt/plenary.nvim

" 2. Open nvim
"
"   $ nvim -u ./minimal.vimrc

packadd plenary.nvim
packadd parrot.nvim

" Set ANTHROPIC_API_KEY environment variable
const anth_key_path = expand('~/.config/anthropic.token')
if filereadable(anth_key_path)
  let $ANTHROPIC_API_KEY = readfile(anth_key_path)[0] 
else
  echomsg "ANTHROPIC_API_KEY not found"
endif

lua << EOF
require("parrot").setup {
  providers = {
    anthropic = {
      api_key = os.getenv "ANTHROPIC_API_KEY",
    }
  }
}
EOF

call execute('PrtProvider anthropic')
call execute('PrtChatNew')
call execute('PrtAgent Claude-3-Haiku-Chat')