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

Error parsing JSON:"Expected value but found T_END at character 1" #42

Closed sohanemon closed 1 month ago

sohanemon commented 1 month ago

This is the issue im getting recently. These was working fine before. Here is my config.

return {
  {
    "frankroeder/parrot.nvim",
    commit = '2880cc',
    config = function()
      require("parrot").setup({
        cmd_prefix = "Ai",

        providers = {
          openai = {
            api_key = require('env').openai_api_key,
          },
          ollama = {}
        },
        agents = {
          chat = {
            {
              name = "GPT3.5",
              model = { model = "gpt-3.5-turbo", temperature = 1.1, top_p = 1 },
              provider = "openai",
              system_prompt =
              "Provide the solution inside a single code block, specifying the {{filetype}}. Do not include any explanation or additional text outside the code block."
            },
            {
              name = "Llama",
              model = { model = "codellama", temperature = 1.5, top_p = 1, num_ctx = 8192, min_p = 0.05 },
              provider = "ollama",
              system_prompt =
              "Provide the solution inside a single code block, specifying the {{filetype}}. Do not include any explanation or additional text outside the code block."
            },
          },
          command = {
            {
              name = "Llama",
              model = { model = "codellama", temperature = 1.5, top_p = 1, num_ctx = 8192, min_p = 0.05 },
              provider = "ollama",
              system_prompt =
              "Provide the solution inside a single code block, specifying the {{filetype}}. Do not include any explanation or additional text outside the code block."
            },
            {
              name = "GPT4o",
              model = { model = "gpt-4o", temperature = 1.1, top_p = 1 },
              provider = "openai",
              system_prompt =
              "Provide the solution inside a single code block, specifying the {{filetype}}. Do not include any explanation or additional text outside the code block."
            },
          }
        },

        chat_user_prefix = "Sohan:",
        agent_prefix = "๐Ÿฆœ:",
        chat_confirm_delete = true,
        enable_spinner = true,
        spinner_type = "star",
        toggle_target = "popup",

        -- in case of visibility colisions with other plugins, you can increase/decrease zindex
        zindex = 1,

        command_prompt_prefix_template = "{{agent}} ~ ",
        command_auto_select_response = true,

        -- Local chat buffer shortcuts
        chat_shortcut_respond = { modes = { "n" }, shortcut = "<cr>" },
        chat_shortcut_delete = { modes = { "n", "i", "v", "x" }, shortcut = "<c-d>" },
        chat_shortcut_stop = { modes = { "n", "i", "v", "x" }, shortcut = "<c-c>" },
        chat_shortcut_new = { modes = { "n", "i", "v", "x" }, shortcut = "<c-n>" },

        hooks = require('utils.hooks').hooks
      })

      require("which-key").add({
        {
          mode = "n",
          nowait = true,
          remap = true,
          { "?a",  "<cmd>AiAppendWithCommand<cr>", desc = "Append (command)" },
          { "?A",  "<cmd>AiAppendBuffer<cr>",      desc = "Append (auto)" },
          { "??",  "<cmd>AiAsk<cr>",               desc = "Ask (ai)" },
          { "?/",  "<cmd>AiAskAboutBuffer<cr>",    desc = "Ask (buffer)" },
          -- chat window
          { "?c",  group = "New Chat" },
          { "?cc", "<cmd>AiChatNew popup<cr>",     desc = "New Chat popup" },
          { "?ct", "<cmd>AiChatNew tabnew<cr>",    desc = "New Chat tabnew" },
          { "?cv", "<cmd>AiChatNew vsplit<cr>",    desc = "New Chat vsplit" },
          { "?cs", "<cmd>AiChatNew split<cr>",     desc = "New Chat split" },
          { "?f",  "<cmd>AiChatFinder<cr>",        desc = "Chat Finder" },
          { "?w",  group = "Whisper" },
          { "?wa", "<cmd>AiWhisperAppend<cr>",     desc = "Whisper Append (after)" },
          { "?wb", "<cmd>AiWhisperPrepend<cr>",    desc = "Whisper Prepend (before)" },
          { "?we", "<cmd>AiWhisperEnew<cr>",       desc = "Whisper Enew" },
          { "?wn", "<cmd>AiWhisperNew<cr>",        desc = "Whisper New" },
          { "?wp", "<cmd>AiWhisperPopup<cr>",      desc = "Whisper Popup" },
          { "?wr", "<cmd>AiWhisperRewrite<cr>",    desc = "Whisper Inline Rewrite" },
          { "?wt", "<cmd>AiWhisperTabnew<cr>",     desc = "Whisper Tabnew" },
          { "?wv", "<cmd>AiWhisperVnew<cr>",       desc = "Whisper Vnew" },
          { "?ww", "<cmd>AiWhisper<cr>",           desc = "Whisper" },
          { "?x",  "<cmd>AiContext<cr>",           desc = "Toggle AiContext" },
        },
        {
          mode = "v",
          nowait = true,
          remap = true,
          { "?c",  group = "New Chat" },
          { "?cc", "<cmd>'<,'>AiChatNew popup<cr>",             desc = "New Chat popup" },
          { "?ct", "<cmd>'<,'>AiChatNew tabnew<cr>",            desc = "New Chat tabnew" },
          { "?cv", "<cmd>'<,'>AiChatNew vsplit<cr>",            desc = "New Chat vsplit" },
          { "?cs", "<cmd>'<,'>AiChatNew split<cr>",             desc = "New Chat split" },
          { "?e",  "<cmd>'<,'>AiEditWithCommand<cr>",           desc = "Edit" },
          { "?E",  "<cmd>'<,'>AiEditWithCommandAndContext<cr>", desc = "Edit with Context" },
          { "?r",  "<cmd>'<,'>AiCodeReview<cr>",                desc = "Review" },
        },
      })

      local k = vim.keymap.set
      require("utils.lib").actionByFileType("markdown", function()
        local opts = { noremap = true, silent = true, buffer = true }

        k('n', '<C-c>', "<cmd>AiChatStop<cr>", opts)
      end)
    end
  },
}

My hooks

local M = {}

M.hooks = {
  AppendBuffer = function(prt, params)
    local template = [[
          I have the following code from {{filename}}:

          ```{{filetype}}
          {{filecontent}}
      Please look at the following section specifically:
      ```{{filetype}}
      {{selection}}
      ```

      Please finish the code above carefully and logically.
      Respond just with the snippet of code that should be inserted.
      ]]
local agent = prt.get_command_agent()
prt.Prompt(params, prt.ui.Target.append, agent, nil, template)

end,

AppendWithCommand = function(prt, params) local template = [[ I have the following code from {{filename}}:

      ```{{filetype}}
      {{filecontent}}
      ```

      Please finish the code above carefully and logically.
      Respond just with the snippet of code that should be inserted.

      Your Task:{{command}}
      ]]
local agent = prt.get_command_agent()
prt.logger.info("Asking agent: " .. agent.name)
prt.Prompt(params, prt.ui.Target.append, agent, "๎ฎพ Instruction ~", template)

end,

EditWithCommand = function(prt, params) local template = [[ Here is my code snippet

          {{selection}}
      Please rewrite the code above as per the following instructions or changes mentioned.
      Respond just with the snippet of code that should be replaced with the given code snippet.

      Command: {{command}}

]] local agent = prt.get_command_agent() prt.logger.info("Asking agent: " .. agent.name) prt.Prompt(params, prt.ui.Target.rewrite, agent, "Rewrite Instructions ๐Ÿ”„ ~", template) end,

EditWithCommandAndContext = function(prt, params) local template = [[ I have the following code from {{filename}}:

      ```{{filetype}}
      {{filecontent}}
      ```

      Please rewrite the code above as per the following instructions or changes mentioned.
      Respond just with the snippet of code that should be inserted.

      Command: {{command}}

]] local agent = prt.get_command_agent() prt.logger.info("Asking agent: " .. agent.name) prt.Prompt(params, prt.ui.Target.rewrite, agent, "Rewrite Instructions ๐Ÿ”„ ~", template) end,

AskAboutBuffer = function(parrot, params) local template = [[ I have the following code from {{filename}}:

      ```{{filetype}}
      {{filecontent}}
      ```

      In light of your existing knowledge base, please generate a response that
      is succinct and directly addresses the question posed. Prioritize accuracy
      and relevance in your answer, drawing upon the most recent information
      available to you. Aim to deliver your response in a concise manner,
      focusing on the essence of the inquiry.
      Question: {{command}}
    ]]
local agent = parrot.get_command_agent()
parrot.logger.info("Asking agent: " .. agent.name)
parrot.Prompt(params, parrot.ui.Target.popup, agent, "๐Ÿค– Ask ~ ", template)

end, CodeReview = function(prt, params) local chat_prompt = [[ Your task is to analyze the provided {{filetype}} code and suggest improvements to optimize its performance. Identify areas where the code can be made more efficient, faster, or less resource-intensive. Provide specific suggestions for optimization, along with explanations of how these changes can enhance the code's performance. The optimized code should maintain the same functionality as the original code while demonstrating improved efficiency.

      Here is the code
      ```{{filetype}}
      {{selection}}
      ```
            ]]
prt.ChatNew(params, chat_prompt)

end,

Explain = function(prt, params) local template = "I have the following code from {{filename}}:\n\n" .. "{{filetype}}\n{{selection}}\n\n\n" .. "Please respond by explaining the code above." local agent = prt.get_chat_agent() prt.Prompt(params, prt.ui.Target.popup, agent, nil, template) end,

SpellCheck = function(prt, params) local chat_prompt = [[ Your task is to take the text provided and rewrite it into a clear, grammatically correct version while preserving the original meaning as closely as possible. Correct any spelling mistakes, punctuation errors, verb tense issues, word choice problems, and other grammatical mistakes. ]] prt.ChatNew(params, chat_prompt) end, Translator = function(prt, params) local chat_prompt = "You are a Translator, please translate between English and Bengali." prt.ChatNew(params, chat_prompt) end, }

return M

frankroeder commented 1 month ago

Dear @sohanemon,

Version 0.4.0 of parrot.nvim introduced some breaking changes. The concept of an agent has been removed. Based on your configuration, you should be fine by removing your agent table and instead providing the system prompt for the chat and command agent. Your hooks will also need to be updated accordingly. Everything is documented in the README, but I will provide you with the changes below.

If you would be so kind as to try version 0.4.2, does the error _Expected value but found TEND at character 1 appear when changing the model (formerly agent) or on startup?

return {
  {
    "frankroeder/parrot.nvim",
    commit = "2880cc",
    config = function()
      require("parrot").setup {
        cmd_prefix = "Ai",

        providers = {
          openai = {
            api_key = require('env').openai_api_key,
          },
          ollama = {},
        },
        chat_system_prompt = "Provide the solution inside a single code block, specifying the {{filetype}}. Do not include any explanation or additional text outside the code block.",
        command_system_prompt = "Provide the solution inside a single code block, specifying the {{filetype}}. Do not include any explanation or additional text outside the code block.",
        chat_user_prefix = "Sohan:",
        agent_prefix = "๐Ÿฆœ:",
        chat_confirm_delete = true,
        enable_spinner = true,
        spinner_type = "star",
        toggle_target = "popup",

        -- in case of visibility colisions with other plugins, you can increase/decrease zindex
        zindex = 1,

        command_prompt_prefix_template = "{{agent}} ~ ",
        command_auto_select_response = true,

        -- Local chat buffer shortcuts
        chat_shortcut_respond = { modes = { "n" }, shortcut = "<cr>" },
        chat_shortcut_delete = { modes = { "n", "i", "v", "x" }, shortcut = "<c-d>" },
        chat_shortcut_stop = { modes = { "n", "i", "v", "x" }, shortcut = "<c-c>" },
        chat_shortcut_new = { modes = { "n", "i", "v", "x" }, shortcut = "<c-n>" },

        hooks = require('utils.hooks').hooks
     })

      require("which-key").add({
        {
          mode = "n",
          nowait = true,
          remap = true,
          { "?a",  "<cmd>AiAppendWithCommand<cr>", desc = "Append (command)" },
          { "?A",  "<cmd>AiAppendBuffer<cr>",      desc = "Append (auto)" },
          { "??",  "<cmd>AiAsk<cr>",               desc = "Ask (ai)" },
          { "?/",  "<cmd>AiAskAboutBuffer<cr>",    desc = "Ask (buffer)" },
          -- chat window
          { "?c",  group = "New Chat" },
          { "?cc", "<cmd>AiChatNew popup<cr>",     desc = "New Chat popup" },
          { "?ct", "<cmd>AiChatNew tabnew<cr>",    desc = "New Chat tabnew" },
          { "?cv", "<cmd>AiChatNew vsplit<cr>",    desc = "New Chat vsplit" },
          { "?cs", "<cmd>AiChatNew split<cr>",     desc = "New Chat split" },
          { "?f",  "<cmd>AiChatFinder<cr>",        desc = "Chat Finder" },
          { "?w",  group = "Whisper" },
          { "?wa", "<cmd>AiWhisperAppend<cr>",     desc = "Whisper Append (after)" },
          { "?wb", "<cmd>AiWhisperPrepend<cr>",    desc = "Whisper Prepend (before)" },
          { "?we", "<cmd>AiWhisperEnew<cr>",       desc = "Whisper Enew" },
          { "?wn", "<cmd>AiWhisperNew<cr>",        desc = "Whisper New" },
          { "?wp", "<cmd>AiWhisperPopup<cr>",      desc = "Whisper Popup" },
          { "?wr", "<cmd>AiWhisperRewrite<cr>",    desc = "Whisper Inline Rewrite" },
          { "?wt", "<cmd>AiWhisperTabnew<cr>",     desc = "Whisper Tabnew" },
          { "?wv", "<cmd>AiWhisperVnew<cr>",       desc = "Whisper Vnew" },
          { "?ww", "<cmd>AiWhisper<cr>",           desc = "Whisper" },
          { "?x",  "<cmd>AiContext<cr>",           desc = "Toggle AiContext" },
        },
        {
          mode = "v",
          nowait = true,
          remap = true,
          { "?c",  group = "New Chat" },
          { "?cc", "<cmd>'<,'>AiChatNew popup<cr>",             desc = "New Chat popup" },
          { "?ct", "<cmd>'<,'>AiChatNew tabnew<cr>",            desc = "New Chat tabnew" },
          { "?cv", "<cmd>'<,'>AiChatNew vsplit<cr>",            desc = "New Chat vsplit" },
          { "?cs", "<cmd>'<,'>AiChatNew split<cr>",             desc = "New Chat split" },
          { "?e",  "<cmd>'<,'>AiEditWithCommand<cr>",           desc = "Edit" },
          { "?E",  "<cmd>'<,'>AiEditWithCommandAndContext<cr>", desc = "Edit with Context" },
          { "?r",  "<cmd>'<,'>AiCodeReview<cr>",                desc = "Review" },
        },
      })

      local k = vim.keymap.set
      require("utils.lib").actionByFileType("markdown", function()
        local opts = { noremap = true, silent = true, buffer = true }

        k('n', '<C-c>', "<cmd>AiChatStop<cr>", opts)
      end)
    end
  },
}

Updated hooks:

local M = {}

M.hooks = {
  AppendBuffer = function(prt, params)
    local template = [[
          I have the following code from {{filename}}:

          ```{{filetype}}
          {{filecontent}}
      Please look at the following section specifically:
      ```{{filetype}}
      {{selection}}
      ```

      Please finish the code above carefully and logically.
      Respond just with the snippet of code that should be inserted.
      ]]
local model_obj = prt.get_model "command"
prt.Prompt(params, prt.ui.Target.append, model_obj, nil, template)

end,

AppendWithCommand = function(prt, params) local template = [[ I have the following code from {{filename}}:

      ```{{filetype}}
      {{filecontent}}
      ```

      Please finish the code above carefully and logically.
      Respond just with the snippet of code that should be inserted.

      Your Task:{{command}}
      ]]
local model_obj = prt.get_model "command"
prt.logger.info("Asking model: " .. model_obj.name)
prt.Prompt(params, prt.ui.Target.append, model_obj, "๎ฎพ Instruction ~", template)

end,

EditWithCommand = function(prt, params) local template = [[ Here is my code snippet

          {{selection}}
      Please rewrite the code above as per the following instructions or changes mentioned.
      Respond just with the snippet of code that should be replaced with the given code snippet.

      Command: {{command}}

]]

local model_obj = prt.get_model "command"
prt.logger.info("Asking model: " .. model_obj.name)
prt.Prompt(params, prt.ui.Target.rewrite, model_obj, "Rewrite Instructions ๐Ÿ”„ ~", template)

end,

EditWithCommandAndContext = function(prt, params) local template = [[ I have the following code from {{filename}}:

      ```{{filetype}}
      {{filecontent}}
      ```

      Please rewrite the code above as per the following instructions or changes mentioned.
      Respond just with the snippet of code that should be inserted.

      Command: {{command}}

]] local model_obj = prt.get_model "command" prt.logger.info("Asking model: " .. model_obj.name) prt.Prompt(params, prt.ui.Target.rewrite, model_obj, "Rewrite Instructions ๐Ÿ”„ ~", template) end,

AskAboutBuffer = function(parrot, params) local template = [[ I have the following code from {{filename}}:

      ```{{filetype}}
      {{filecontent}}
      ```

      In light of your existing knowledge base, please generate a response that
      is succinct and directly addresses the question posed. Prioritize accuracy
      and relevance in your answer, drawing upon the most recent information
      available to you. Aim to deliver your response in a concise manner,
      focusing on the essence of the inquiry.
      Question: {{command}}
    ]]
local model_obj = parrot.get_model "command"
parrot.logger.info("Asking model: " .. model_obj.name)
parrot.Prompt(params, parrot.ui.Target.popup, model_obj, "๐Ÿค– Ask ~ ", template)

end, CodeReview = function(prt, params) local chat_prompt = [[ Your task is to analyze the provided {{filetype}} code and suggest improvements to optimize its performance. Identify areas where the code can be made more efficient, faster, or less resource-intensive. Provide specific suggestions for optimization, along with explanations of how these changes can enhance the code's performance. The optimized code should maintain the same functionality as the original code while demonstrating improved efficiency.

      Here is the code
      ```{{filetype}}
      {{selection}}
      ```
            ]]
prt.ChatNew(params, chat_prompt)

end,

Explain = function(prt, params) local template = "I have the following code from {{filename}}:\n\n" .. "{{filetype}}\n{{selection}}\n\n\n" .. "Please respond by explaining the code above." local model_obj = parrot.get_model "command" prt.Prompt(params, prt.ui.Target.popup, model_obj, nil, template) end,

SpellCheck = function(prt, params) local chat_prompt = [[ Your task is to take the text provided and rewrite it into a clear, grammatically correct version while preserving the original meaning as closely as possible. Correct any spelling mistakes, punctuation errors, verb tense issues, word choice problems, and other grammatical mistakes. ]] prt.ChatNew(params, chat_prompt) end, Translator = function(prt, params) local chat_prompt = "You are a Translator, please translate between English and Bengali." prt.ChatNew(params, chat_prompt) end, }

return M

sohanemon commented 1 month ago

Thanks for you response @frankroeder, i have replaced my code with your given one. But the issue is the same. I'm getting the same error

frankroeder commented 1 month ago

Is it during startup or when triggering the model selection (formerly known as agent selection)?

sohanemon commented 1 month ago

during startup

frankroeder commented 1 month ago

This should be fixed with the latest commit. Could you try it and let me know if the error is gone?

sohanemon commented 1 month ago

Yes, im on the latest commit.

image

sohanemon commented 1 month ago

the issue still persists

sohanemon commented 1 month ago
require("parrot").setup({
   cmd_prefix = "Ai",
}

I'm not sure why but after removing cmd_prefix fixed the error on startup ๐Ÿ˜€.

Thanks for your help and the awesome plugin.