fix: use the original prompts (#397)

This commit is contained in:
yetone 2024-08-30 22:21:50 +08:00 committed by GitHub
parent 5cf85d59bd
commit 104484f17c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
9 changed files with 216 additions and 135 deletions

View File

@ -351,7 +351,6 @@ We would like to express our heartfelt gratitude to the contributors of the foll
| [ChatGPT.nvim](https://github.com/jackMort/ChatGPT.nvim) | Apache 2.0 License | Calculation of tokens count | https://github.com/yetone/avante.nvim/blob/main/lua/avante/utils/tokens.lua |
| [img-clip.nvim](https://github.com/HakonHarnes/img-clip.nvim) | MIT License | Clipboard image support | https://github.com/yetone/avante.nvim/blob/main/lua/avante/clipboard.lua |
| [copilot.lua](https://github.com/zbirenbaum/copilot.lua) | MIT License | Copilot support | https://github.com/yetone/avante.nvim/blob/main/lua/avante/providers/copilot.lua |
| [Claude Engineer](https://github.com/Doriandarko/claude-engineer) | No License | Some prompts | https://github.com/yetone/avante.nvim/blob/main/lua/avante/llm.lua |
The high quality and ingenuity of these projects' source code have been immensely beneficial throughout our development process. We extend our sincere thanks and respect to the authors and contributors of these projects. It is the selfless dedication of the open-source community that drives projects like avante.nvim forward.

View File

@ -13,97 +13,77 @@ M.CANCEL_PATTERN = "AvanteLLMEscape"
------------------------------Prompt and type------------------------------
-- Copy from: https://github.com/Doriandarko/claude-engineer/blob/15c94963cbf9d01b8ae7bbb5d42d7025aa0555d5/main.py#L276
---@alias AvanteBasePrompt string
local planning_mode_system_prompt_tpl = [[
You are an excellent programming expert and your primary task is to generate code according to the instructions. Follow these steps:
1. Review the entire file content to understand the context:
${file_content}
2. Carefully analyze the selected code:
${selected_code}
3. Carefully analyze the specific instructions:
${instructions}
4. Take into account the overall project context:
${project_context}
5. Consider the memory of previous edits:
${memory_context}
6. Consider the full context of all files in the project:
${full_file_contents_context}
7. Generate SEARCH/REPLACE blocks for each necessary change. Each block should:
- Include enough context to uniquely identify the code to be changed
- Provide the exact replacement code, maintaining correct INDENTATION and FORMATTING
- Focus on specific, targeted changes rather than large, sweeping modifications
- The content in the SEARCH tag MUST NOT contain any of your generated content. Do not be lazy!
- The content in the SEARCH tag MUST be based on the original content of the source file
- The content in the SEARCH tag needs to ensure a certain context to guarantee its UNIQUENESS
- There should be NO OVERLAP between the code of each SEARCH tag. Do not be lazy!
- Be sure to ensure that there are NO SYNTAX ERRORS OR FORMATTING ERRORS after the SEARCH part in the source code is replaced by the REPLACE part! Do not be lazy!
- DO NOT use ``` to wrap code blocks, Do not be lazy!
8. Ensure that your SEARCH/REPLACE blocks:
- Address all relevant aspects of the instructions
- Maintain or enhance code readability and efficiency
- Consider the overall structure and purpose of the code
- Follow best practices and coding standards for the language
- Maintain consistency with the project context and previous edits
- Take into account the full context of all files in the project
IMPORTANT: MUST TO ADD EXPLANATIONS BEFORE AND AFTER EACH SEARCH/REPLACE BLOCK.
USE THE FOLLOWING FORMAT FOR EACH BLOCK:
<SEARCH>
Code to be replaced
</SEARCH>
<REPLACE>
New code to insert
</REPLACE>
If no changes are needed, return an empty list.
---@alias AvanteSystemPrompt string
local system_prompt = [[
You are an excellent programming expert.
]]
local editing_mode_system_prompt_tpl = [[
You are an excellent programming expert and your primary task is to generate code according to the instructions. Follow these steps:
-- Copy from: https://github.com/Doriandarko/claude-engineer/blob/15c94963cbf9d01b8ae7bbb5d42d7025aa0555d5/main.py#L276
---@alias AvanteBasePrompt string
local planning_mode_user_prompt_tpl = [[
Your primary task is to suggest code modifications with precise line number ranges. Follow these instructions meticulously:
1. Review the entire file content to understand the context:
${file_content}
1. Carefully analyze the original code, paying close attention to its structure and line numbers. Line numbers start from 1 and include ALL lines, even empty ones.
2. Carefully analyze the selected code:
${selected_code}
2. When suggesting modifications:
a. Use the language in the question to reply. If there are non-English parts in the question, use the language of those parts.
b. Explain why the change is necessary or beneficial.
c. If an image is provided, make sure to use the image in conjunction with the code snippet.
d. Provide the exact code snippet to be replaced using this format:
3. Carefully analyze the specific instructions:
${instructions}
Replace lines: {{start_line}}-{{end_line}}
```{{language}}
{{suggested_code}}
```
4. Take into account the overall project context:
${project_context}
3. Crucial guidelines for suggested code snippets:
- Only apply the change(s) suggested by the most recent assistant message (before your generation).
- Do not make any unrelated changes to the code.
- Produce a valid full rewrite of the entire original file without skipping any lines. Do not be lazy!
- Do not arbitrarily delete pre-existing comments/empty Lines.
- Do not omit large parts of the original file for no reason.
- Do not omit any needed changes from the requisite messages/code blocks.
- If there is a clicked code block, bias towards just applying that (and applying other changes implied).
- Please keep your suggested code changes minimal, and do not include irrelevant lines in the code snippet.
- Maintain the SAME indentation in the returned code as in the source code
5. Consider the memory of previous edits:
${memory_context}
4. Crucial guidelines for line numbers:
- The content regarding line numbers MUST strictly follow the format "Replace lines: {{start_line}}-{{end_line}}". Do not be lazy!
- The range {{start_line}}-{{end_line}} is INCLUSIVE. Both start_line and end_line are included in the replacement.
- Count EVERY line, including empty lines and comments lines, comments. Do not be lazy!
- For single-line changes, use the same number for start and end lines.
- For multi-line changes, ensure the range covers ALL affected lines, from the very first to the very last.
- Double-check that your line numbers align perfectly with the original code structure.
6. Consider the full context of all files in the project:
${full_file_contents_context}
5. Final check:
- Review all suggestions, ensuring each line number is correct, especially the start_line and end_line.
- Confirm that no unrelated code is accidentally modified or deleted.
- Verify that the start_line and end_line correctly include all intended lines for replacement.
- Perform a final alignment check to ensure your line numbers haven't shifted, especially the start_line.
- Double-check that your line numbers align perfectly with the original code structure.
- DO NOT return the complete modified code with applied changes!
7. Return ONLY the complete modified code.
Remember: Accurate line numbers are CRITICAL. The range start_line to end_line must include ALL lines to be replaced, from the very first to the very last. Double-check every range before finalizing your response, paying special attention to the start_line to ensure it hasn't shifted down. Ensure that your line numbers perfectly match the original code structure without any overall shift.
]]
8. Do not include any explanations, comments, or line numbers in your response.
local editing_mode_user_prompt_tpl = [[
Your task is to modify the provided code according to the user's request. Follow these instructions precisely:
9. Ensure the returned code is complete and can be directly used as a replacement for the original code.
1. Return ONLY the complete modified code.
11. Preserve the original structure, indentation, and formatting of the code as much as possible.
2. Do not include any explanations, comments, or line numbers in your response.
12. Do not omit any parts of the code, even if they are unchanged.
3. Ensure the returned code is complete and can be directly used as a replacement for the original code.
13. Maintain the SAME indentation in the returned code as in the source code
4. Preserve the original structure, indentation, and formatting of the code as much as possible.
14. Do NOT include three backticks: ```
5. Do not omit any parts of the code, even if they are unchanged.
15. Only return code part, do NOT return the context part!
6. Maintain the SAME indentation in the returned code as in the source code
7. Do NOT include three backticks: ```
8. Only return the new code snippets to be updated, DO NOT return the entire file content.
Remember: Your response should contain nothing but ONLY the modified code, ready to be used as a direct replacement for the original file.
]]
@ -113,6 +93,7 @@ local active_job = nil
---@class StreamOptions
---@field file_content string
---@field code_lang string
---@field selected_code string | nil
---@field instructions string
---@field project_context string | nil
@ -127,9 +108,9 @@ M.stream = function(opts)
local mode = opts.mode or "planning"
local provider = Config.provider
local system_prompt_tpl = mode == "planning" and planning_mode_system_prompt_tpl or editing_mode_system_prompt_tpl
local user_prompt_tpl = mode == "planning" and planning_mode_user_prompt_tpl or editing_mode_user_prompt_tpl
-- Check if the question contains an image path
-- Check if the instructions contains an image path
local image_paths = {}
local original_instructions = opts.instructions
if opts.instructions:match("image: ") then
@ -144,14 +125,43 @@ M.stream = function(opts)
original_instructions = table.concat(lines, "\n")
end
local system_prompt =
system_prompt_tpl:gsub("%${(.-)}", vim.tbl_deep_extend("force", opts, { instructions = original_instructions }))
local user_prompts = {}
if opts.selected_code and opts.selected_code ~= "" then
table.insert(
user_prompts,
string.format("<code_context>```%s\n%s\n```</code_context>", opts.code_lang, opts.file_content)
)
table.insert(user_prompts, string.format("<code>```%s\n%s\n```</code>", opts.code_lang, opts.selected_code))
else
table.insert(user_prompts, string.format("<code>```%s\n%s\n```</code>", opts.code_lang, opts.file_content))
end
if opts.project_context then
table.insert(user_prompts, string.format("<project_context>%s</project_context>", opts.project_context))
end
if opts.memory_context then
table.insert(user_prompts, string.format("<memory_context>%s</memory_context>", opts.memory_context))
end
if opts.full_file_contents_context then
table.insert(
user_prompts,
string.format("<full_file_contents_context>%s</full_file_contents_context>", opts.full_file_contents_context)
)
end
table.insert(user_prompts, "<question>" .. original_instructions .. "</question>")
local user_prompt = user_prompt_tpl:gsub("%${(.-)}", opts)
table.insert(user_prompts, user_prompt)
---@type AvantePromptOptions
local code_opts = {
system_prompt = system_prompt,
user_prompt = opts.selected_code and "Please suggest modifications to the selected code."
or "Please suggest modifications to the file coontent.",
user_prompts = user_prompts,
image_paths = image_paths,
}

View File

@ -24,19 +24,19 @@ M.parse_message = function(prompt_opts)
end
end
local user_prompt = prompt_opts.user_prompt
for _, user_prompt in ipairs(prompt_opts.user_prompts) do
local user_prompt_obj = {
type = "text",
text = user_prompt,
}
local user_prompt_obj = {
type = "text",
text = user_prompt,
}
if Utils.tokens.calculate_tokens(user_prompt_obj.text) > 1024 then
user_prompt_obj.cache_control = { type = "ephemeral" }
end
if Utils.tokens.calculate_tokens(user_prompt_obj.text) > 1024 then
user_prompt_obj.cache_control = { type = "ephemeral" }
table.insert(message_content, user_prompt_obj)
end
table.insert(message_content, user_prompt_obj)
return {
{
role = "user",
@ -75,6 +75,8 @@ M.parse_curl_args = function(provider, prompt_opts)
headers["x-api-key"] = provider.parse_api_key()
end
local messages = M.parse_message(prompt_opts)
return {
url = Utils.trim(base.endpoint, { suffix = "/" }) .. "/v1/messages",
proxy = base.proxy,
@ -89,7 +91,7 @@ M.parse_curl_args = function(provider, prompt_opts)
cache_control = { type = "ephemeral" },
},
},
messages = M.parse_message(prompt_opts),
messages = messages,
stream = true,
}, body_opts),
}

View File

@ -31,9 +31,14 @@ local M = {}
M.api_key_name = "CO_API_KEY"
M.parse_message = function(opts)
local user_prompt = ""
for _, user_prompt_ in ipairs(opts.user_prompts) do
user_prompt = user_prompt .. "\n\n" .. user_prompt_
end
return {
preamble = opts.system_prompt,
message = opts.user_prompt,
message = user_prompt,
}
end

View File

@ -24,9 +24,11 @@ M.parse_message = function(opts)
end
-- insert a part into parts
table.insert(message_content, {
text = opts.user_prompt,
})
for _, user_prompt in ipairs(opts.user_prompts) do
table.insert(message_content, {
text = user_prompt,
})
end
return {
systemInstruction = {

View File

@ -10,7 +10,7 @@ local Dressing = require("avante.ui.dressing")
---
---@class AvantePromptOptions: table<[string], string>
---@field system_prompt string
---@field user_prompt string
---@field user_prompts string[]
---@field image_paths? string[]
---
---@class AvanteBaseMessage

View File

@ -28,6 +28,11 @@ local M = {}
M.api_key_name = "OPENAI_API_KEY"
M.parse_message = function(opts)
local user_prompt = ""
for _, user_prompt_ in ipairs(opts.user_prompts) do
user_prompt = user_prompt .. "\n\n" .. user_prompt_
end
---@type string | OpenAIMessage[]
local user_content
if Config.behaviour.support_paste_from_clipboard and opts.image_paths and #opts.image_paths > 0 then
@ -40,9 +45,9 @@ M.parse_message = function(opts)
},
})
end
table.insert(user_content, { type = "text", text = opts.user_prompt })
table.insert(user_content, { type = "text", text = user_prompt })
else
user_content = opts.user_prompt
user_content = user_prompt
end
return {

View File

@ -407,8 +407,11 @@ function Selection:create_editing_input()
end, 0)
end
local filetype = api.nvim_get_option_value("filetype", { buf = code_bufnr })
Llm.stream({
file_content = code_content,
code_lang = filetype,
selected_code = self.selection.content,
instructions = input,
mode = "editing",

View File

@ -134,6 +134,55 @@ function Sidebar:toggle()
end
end
local function realign_line_numbers(code_lines, snippet)
local snippet_lines = vim.split(snippet.content, "\n")
local snippet_lines_count = #snippet_lines
local start_line = snippet.range[1]
local correct_start
for i = start_line, math.max(1, start_line - snippet_lines_count + 1), -1 do
local matched = true
for j = 1, math.min(snippet_lines_count, start_line - i + 1) do
if code_lines[i + j - 1] ~= snippet_lines[j] then
matched = false
break
end
end
if matched then
correct_start = i
break
end
end
local end_line = snippet.range[2]
local correct_end
for i = snippet_lines_count - 1, 1, -1 do
local matched = true
for j = 1, i do
if code_lines[end_line + j - 1] ~= snippet_lines[snippet_lines_count - j] then
matched = false
break
end
end
if matched then
correct_end = end_line + i
break
end
end
if correct_start then
snippet.range[1] = correct_start
end
if correct_end then
snippet.range[2] = correct_end
end
return snippet
end
---@class AvanteReplacementResult
---@field content string
---@field is_searching boolean
@ -275,9 +324,11 @@ end
---@field start_line_in_response_buf integer
---@field end_line_in_response_buf integer
---@param code_content string
---@param response_content string
---@return AvanteCodeSnippet[]
local function extract_code_snippets(response_content)
local function extract_code_snippets(code_content, response_content)
local code_lines = vim.split(code_content, "\n")
local snippets = {}
local current_snippet = {}
local in_code_block = false
@ -301,6 +352,7 @@ local function extract_code_snippets(response_content)
start_line_in_response_buf = start_line_in_response_buf,
end_line_in_response_buf = idx,
}
snippet = realign_line_numbers(code_lines, snippet)
table.insert(snippets, snippet)
end
current_snippet = {}
@ -470,7 +522,7 @@ end
function Sidebar:apply(current_cursor)
local content = table.concat(Utils.get_buf_lines(0, -1, self.code.bufnr), "\n")
local response, response_start_line = self:get_content_between_separators()
local snippets = extract_code_snippets(response)
local snippets = extract_code_snippets(content, response)
snippets = ensure_snippets_no_overlap(content, snippets)
if current_cursor then
if self.result and self.result.winid then
@ -1001,6 +1053,17 @@ function Sidebar:update_content(content, opts)
return self
end
local function prepend_line_number(content, start_line)
start_line = start_line or 1
local lines = vim.split(content, "\n")
local result = {}
for i, line in ipairs(lines) do
i = i + start_line - 1
table.insert(result, "L" .. i .. ": " .. line)
end
return table.concat(result, "\n")
end
-- Function to get current timestamp
local function get_timestamp()
return os.date("%Y-%m-%d %H:%M:%S")
@ -1193,11 +1256,14 @@ function Sidebar:create_input()
self:update_content(content_prefix .. "🔄 **Generating response ...**\n")
local content = table.concat(Utils.get_buf_lines(0, -1, self.code.bufnr), "\n")
local content_with_line_numbers = prepend_line_number(content)
local filetype = api.nvim_get_option_value("filetype", { buf = self.code.bufnr })
local selected_code_content = nil
local selected_code_content_with_line_numbers = nil
if self.code.selection ~= nil then
selected_code_content = self.code.selection.content
selected_code_content_with_line_numbers =
prepend_line_number(self.code.selection.content, self.code.selection.range.start.line)
end
if request:sub(1, 1) == "/" then
@ -1226,8 +1292,10 @@ function Sidebar:create_input()
Utils.error("Invalid end line number", { once = true, title = "Avante" })
return
end
selected_code_content =
table.concat(api.nvim_buf_get_lines(self.code.bufnr, start_line - 1, end_line, false), "\n")
selected_code_content_with_line_numbers = prepend_line_number(
table.concat(api.nvim_buf_get_lines(self.code.bufnr, start_line - 1, end_line, false), "\n"),
start_line
)
request = question
end)
else
@ -1240,30 +1308,22 @@ function Sidebar:create_input()
end
end
local original_response = ""
local transformed_response = ""
local displayed_response = ""
local full_response = ""
local is_first_chunk = true
---@type AvanteChunkParser
local on_chunk = function(chunk)
original_response = original_response .. chunk
local transformed = transform_result_content(content, transformed_response .. chunk, filetype)
transformed_response = transformed.content
local cur_displayed_response = generate_display_content(transformed)
full_response = full_response .. chunk
if is_first_chunk then
is_first_chunk = false
self:update_content(content_prefix .. chunk, { stream = false, scroll = true })
return
end
if cur_displayed_response ~= displayed_response then
displayed_response = cur_displayed_response
self:update_content(content_prefix .. displayed_response, { stream = false, scroll = true })
vim.schedule(function()
vim.cmd("redraw")
end)
end
self:update_content(chunk, { stream = true, scroll = true })
vim.schedule(function()
vim.cmd("redraw")
end)
end
---@type AvanteCompleteParser
@ -1274,18 +1334,13 @@ function Sidebar:create_input()
end
-- Execute when the stream request is actually completed
self:update_content(
content_prefix
.. displayed_response
.. "\n\n🎉🎉🎉 **Generation complete!** Please review the code suggestions above.",
{
stream = false,
scroll = true,
callback = function()
api.nvim_exec_autocmds("User", { pattern = VIEW_BUFFER_UPDATED_PATTERN })
end,
}
)
self:update_content("\n\n🎉🎉🎉 **Generation complete!** Please review the code suggestions above.", {
stream = true,
scroll = true,
callback = function()
api.nvim_exec_autocmds("User", { pattern = VIEW_BUFFER_UPDATED_PATTERN })
end,
})
vim.defer_fn(function()
if self.result and self.result.winid and api.nvim_win_is_valid(self.result.winid) then
@ -1299,15 +1354,15 @@ function Sidebar:create_input()
provider = Config.provider,
model = model,
request = request,
response = displayed_response,
original_response = original_response,
response = full_response,
})
History.save(self.code.bufnr, chat_history)
end
Llm.stream({
file_content = content,
selected_code = selected_code_content,
file_content = content_with_line_numbers,
code_lang = filetype,
selected_code = selected_code_content_with_line_numbers,
instructions = request,
mode = "planning",
on_chunk = on_chunk,