Skip to content

Commit

Permalink
rewrite and append with full file context
Browse files Browse the repository at this point in the history
  • Loading branch information
tommyengstrom committed May 23, 2024
1 parent 52938ff commit c612d06
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 38 deletions.
52 changes: 19 additions & 33 deletions lua/gp/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@ local config = {
-- agents = { { name = "ChatGPT4" }, ... },
agents = {
{
name = "ChatGPT4",
name = "ChatGPT4o",
chat = true,
command = false,
-- string with model name or table with model name and parameters
model = { model = "gpt-4-1106-preview", temperature = 1.1, top_p = 1 },
model = { model = "gpt-4o", temperature = 1.1, top_p = 1 },
-- system prompt (use this to specify the persona/role of the AI)
system_prompt = "You are a general AI assistant.\n\n"
.. "The user provided the additional info about how they would like you to respond:\n\n"
Expand All @@ -53,39 +53,11 @@ local config = {
.. "- Take a deep breath; You've got this!\n",
},
{
name = "ChatGPT3-5",
chat = true,
command = false,
-- string with model name or table with model name and parameters
model = { model = "gpt-3.5-turbo-1106", temperature = 1.1, top_p = 1 },
-- system prompt (use this to specify the persona/role of the AI)
system_prompt = "You are a general AI assistant.\n\n"
.. "The user provided the additional info about how they would like you to respond:\n\n"
.. "- If you're unsure don't guess and say you don't know instead.\n"
.. "- Ask question if you need clarification to provide better answer.\n"
.. "- Think deeply and carefully from first principles step by step.\n"
.. "- Zoom out first to see the big picture and then zoom in to details.\n"
.. "- Use Socratic method to improve your thinking and coding skills.\n"
.. "- Don't elide any code from your output if the answer requires coding.\n"
.. "- Take a deep breath; You've got this!\n",
},
{
name = "CodeGPT4",
chat = false,
command = true,
-- string with model name or table with model name and parameters
model = { model = "gpt-4-1106-preview", temperature = 0.8, top_p = 1 },
-- system prompt (use this to specify the persona/role of the AI)
system_prompt = "You are an AI working as a code editor.\n\n"
.. "Please AVOID COMMENTARY OUTSIDE OF THE SNIPPET RESPONSE.\n"
.. "START AND END YOUR ANSWER WITH:\n\n```",
},
{
name = "CodeGPT3-5",
name = "CodeGPT4o",
chat = false,
command = true,
-- string with model name or table with model name and parameters
model = { model = "gpt-3.5-turbo-1106", temperature = 0.8, top_p = 1 },
model = { model = "gpt-4o", temperature = 0.8, top_p = 1 },
-- system prompt (use this to specify the persona/role of the AI)
system_prompt = "You are an AI working as a code editor.\n\n"
.. "Please AVOID COMMENTARY OUTSIDE OF THE SNIPPET RESPONSE.\n"
Expand Down Expand Up @@ -158,12 +130,26 @@ local config = {
-- templates
template_selection = "I have the following from {{filename}}:"
.. "\n\n```{{filetype}}\n{{selection}}\n```\n\n{{command}}",
template_rewrite = "I have the following from {{filename}}:"
template_rewrite = "I have the following awesome stuff from {{filename}}:"
.. "\n\n```{{filetype}}\n{{selection}}\n```\n\n{{command}}"
.. "\n\nRespond exclusively with the snippet that should replace the selection above.",
template_rewrite_with_file = "I have the following file {{filename}}:"
.. "\n\n```{{filetype}}\n{{file_content}}\n```"
.. "\n\n I want to update the following code block"
.. "\n\n```{{filetype}}\n{{selection}}\n```"
.. "\n\nInstructions: "
.. "\n- {{command}}"
.. "\n- Respond exclusively with the snippet that should replace the selection above.",
template_append = "I have the following from {{filename}}:"
.. "\n\n```{{filetype}}\n{{selection}}\n```\n\n{{command}}"
.. "\n\nRespond exclusively with the snippet that should be appended after the selection above.",
template_append_with_file = "I have the following file {{filename}}:"
.. "\n\n```{{filetype}}\n{{file_content}}\n```"
.. "\n\n I want to append after the following code block"
.. "\n\n```{{filetype}}\n{{selection}}\n```"
.. "\n\nInstructions: "
.. "\n- {{command}}"
.. "\n- Respond exclusively with the snippet that should be appended after the selection above.",
template_prepend = "I have the following from {{filename}}:"
.. "\n\n```{{filetype}}\n{{selection}}\n```\n\n{{command}}"
.. "\n\nRespond exclusively with the snippet that should be prepended before the selection above.",
Expand Down
36 changes: 31 additions & 5 deletions lua/gp/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -603,6 +603,15 @@ _H.find_git_root = function()
return ""
end

_H.repo_instruct_file = function()
local git_root = _H.find_git_root()

if git_root == "" then
return ""
end

return git_root .. "/.gp.md"
end
-- tries to find an .gp.md file in the root of current git repo
---@return string # returns instructions from the .gp.md file
M.repo_instructions = function()
Expand All @@ -628,6 +637,7 @@ M.template_render = function(template, command, selection, filetype, filename)
["{{selection}}"] = selection,
["{{filetype}}"] = filetype,
["{{filename}}"] = filename,
["{{file_content}}"] = vim.api.nvim_buf_get_lines(0, 0, -1, false)
}
return _H.template_render(template, key_value_pairs)
end
Expand Down Expand Up @@ -916,6 +926,8 @@ M.Target = {
append = 1, -- for appending after the selection, range or the current line
prepend = 2, -- for prepending before the selection, range or the current line
popup = 3, -- for writing into the popup window
rewriteWithFile = 4, -- for replacing the selection, range or the current line with the full file as context
appendWithFile = 5, -- for appending after the selection, range or the current line with the full file as context

-- for writing into a new buffer
---@param filetype nil | string # nil = same as the original buffer
Expand Down Expand Up @@ -966,11 +978,13 @@ M.prepare_commands = function()
-- rewrite needs custom template
if target == M.Target.rewrite then
template = M.config.template_rewrite
end
if target == M.Target.append then
elseif target == M.Target.rewriteWithFile then
template = M.config.template_rewrite_with_file
elseif target == M.Target.append then
template = M.config.template_append
end
if target == M.Target.prepend then
elseif target == M.Target.appendWithFile then
template = M.config.template_append_with_file
elseif target == M.Target.prepend then
template = M.config.template_prepend
end
end
Expand Down Expand Up @@ -2494,6 +2508,7 @@ end

M.Prompt = function(params, target, prompt, model, template, system_template, whisper)
-- enew, new, vnew, tabnew should be resolved into table
print(template)
if type(target) == "function" then
target = target()
end
Expand Down Expand Up @@ -2665,7 +2680,6 @@ M.Prompt = function(params, target, prompt, model, template, system_template, wh

local user_prompt = M.template_render(template, command, selection, filetype, filename)
table.insert(messages, { role = "user", content = user_prompt })

-- cancel possible visual mode before calling the model
M._H.feedkeys("<esc>", "xn")

Expand All @@ -2680,13 +2694,25 @@ M.Prompt = function(params, target, prompt, model, template, system_template, wh
vim.api.nvim_buf_set_lines(buf, start_line - 1, end_line - 1, false, {})
-- prepare handler
handler = M.create_handler(buf, win, start_line - 1, true, prefix, cursor)
elseif target == M.Target.rewriteWithFile then
-- delete selection
vim.api.nvim_buf_set_lines(buf, start_line - 1, end_line - 1, false, {})
-- prepare handler
handler = M.create_handler(buf, win, start_line - 1, true, prefix, cursor)
elseif target == M.Target.append then
-- move cursor to the end of the selection
vim.api.nvim_win_set_cursor(0, { end_line, 0 })
-- put newline after selection
vim.api.nvim_put({ "" }, "l", true, true)
-- prepare handler
handler = M.create_handler(buf, win, end_line, true, prefix, cursor)
elseif target == M.Target.appendWithFile then
-- move cursor to the end of the selection
vim.api.nvim_win_set_cursor(0, { end_line, 0 })
-- put newline after selection
vim.api.nvim_put({ "" }, "l", true, true)
-- prepare handler
handler = M.create_handler(buf, win, end_line, true, prefix, cursor)
elseif target == M.Target.prepend then
-- move cursor to the start of the selection
vim.api.nvim_win_set_cursor(0, { start_line, 0 })
Expand Down

0 comments on commit c612d06

Please sign in to comment.