Skip to content

Commit

Permalink
fix: #23 combine user prompts for anthropic adapter
Browse files Browse the repository at this point in the history
  • Loading branch information
olimorris committed Mar 14, 2024
1 parent fdf5dbe commit 6272706
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 13 deletions.
33 changes: 22 additions & 11 deletions lua/codecompanion/actions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -341,11 +341,13 @@ M.static.actions = {
role = "user",
contains_code = true,
content = function(context)
return "This the code:\n"
.. send_code(context)
.. "\nPlease add a documentation comment to the provided code and reply with just the comment only and no explanation, no codeblocks and do not return the code either. If neccessary add parameter and return types"
return send_code(context)
end,
},
{
role = "user",
content = "Please add a documentation comment to the provided code and reply with just the comment only and no explanation, no codeblocks and do not return the code either. If neccessary add parameter and return types",
},
},
},
{
Expand All @@ -369,11 +371,13 @@ M.static.actions = {
role = "user",
contains_code = true,
content = function(context)
return "This is the code:\n"
.. send_code(context)
.. "\nPlease optimize the provided code. Please just respond with the code only and no explanation and no markdown codeblocks"
return send_code(context)
end,
},
{
role = "user",
content = "Please optimize the provided code. Please just respond with the code only and no explanation or markdown block syntax",
},
},
},
{
Expand All @@ -397,11 +401,13 @@ M.static.actions = {
role = "user",
contains_code = true,
content = function(context)
return "This is the code:\n"
.. send_code(context)
.. "\nPlease create a unit test for the provided code. Please just respond with the code only and no explanation or markdown block syntax"
return send_code(context)
end,
},
{
role = "user",
content = "Please create a unit test for the provided code. Please just respond with the code only and no explanation or markdown block syntax",
},
},
},
},
Expand Down Expand Up @@ -450,7 +456,6 @@ M.static.actions = {
},
{
role = "user",
contains_code = true,
content = function(context)
local diagnostics =
require("codecompanion.helpers.lsp").get_diagnostics(context.start_line, context.end_line, context.bufnr)
Expand All @@ -474,7 +479,13 @@ M.static.actions = {
.. context.filetype
.. ". This is a list of the diagnostic messages:\n\n"
.. concatenated_diagnostics
.. "\n\nThis is the code, for context:\n\n"
end,
},
{
role = "user",
contains_code = true,
content = function(context)
return "This is the code, for context:\n\n"
.. "```"
.. context.filetype
.. "\n"
Expand Down
23 changes: 21 additions & 2 deletions lua/codecompanion/adapters/anthropic.lua
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
local log = require("codecompanion.utils.log")

local function get_system_prompt(tbl)
for i, element in ipairs(tbl) do
if element.role == "system" then
for i = 1, #tbl do
if tbl[i].role == "system" then
return i
end
end
Expand Down Expand Up @@ -50,11 +50,30 @@ return {
---@param messages table Format is: { { role = "user", content = "Your prompt here" } }
---@return table
form_messages = function(messages)
-- Remove any system prompts from the messages array
local system_prompt_index = get_system_prompt(messages)
if system_prompt_index then
table.remove(messages, system_prompt_index)
end

-- Combine user prompts into a single prompt
local user_role_content = {}
for i = #messages, 1, -1 do
local message = messages[i]
if message.role == "user" then
table.insert(user_role_content, 1, message.content)
table.remove(messages, i)
end
end
local output = table.concat(user_role_content, " ")

if output ~= "" then
table.insert(messages, {
role = "user",
content = output,
})
end

return { messages = messages }
end,

Expand Down

0 comments on commit 6272706

Please sign in to comment.