From 24641d8264773fdf577cf594662fd6249bf7725c Mon Sep 17 00:00:00 2001 From: yetone Date: Sat, 11 Jan 2025 23:09:55 +0800 Subject: [PATCH] refactor: new prompts structure (#1071) --- lua/avante/suggestion.lua | 33 +++++++++++++ lua/avante/templates/base.avanterules | 14 ++++++ lua/avante/templates/editing.avanterules | 4 +- lua/avante/templates/planning.avanterules | 17 ++----- lua/avante/templates/suggesting.avanterules | 55 ++++++++++----------- 5 files changed, 78 insertions(+), 45 deletions(-) create mode 100644 lua/avante/templates/base.avanterules diff --git a/lua/avante/suggestion.lua b/lua/avante/suggestion.lua index 8fd37ab..a23a163 100644 --- a/lua/avante/suggestion.lua +++ b/lua/avante/suggestion.lua @@ -74,11 +74,43 @@ function Suggestion:suggest() local provider = Providers[Config.auto_suggestions_provider] + ---@type AvanteLLMMessage[] + local history_messages = { + { + role = "user", + content = [[ +a.py +def fib + ]], + }, + { + role = "assistant", + content = "ok", + }, + { + role = "user", + content = '{ "indentSize": 4, "position": { "row": 1, "col": 2 } }', + }, + { + role = "assistant", + content = [[ +[ + { + "row": 1, + "col": 8, + "content": "(n):\n if n < 2:\n return n\n return fib(n - 1) + fib(n - 2)" + } +] + ]], + }, + } + Llm.stream({ provider = provider, ask = true, selected_files = { { content = code_content, file_type = filetype, path = "" } }, code_lang = filetype, + history_messages = history_messages, instructions = vim.json.encode(doc), mode = "suggesting", on_chunk = function(chunk) full_response = full_response .. chunk end, @@ -93,6 +125,7 @@ function Suggestion:suggest() if cursor_row ~= doc.position.row or cursor_col ~= doc.position.col then return end -- Clean up markdown code blocks full_response = full_response:gsub("^```%w*\n(.-)\n```$", "%1") + full_response = full_response:gsub("(.-)\n```\n?$", "%1") -- Remove everything before the first '[' to ensure we get just the JSON array full_response = full_response:gsub("^.-(%[.*)", "%1") local ok, suggestions = pcall(vim.json.decode, full_response) diff --git a/lua/avante/templates/base.avanterules b/lua/avante/templates/base.avanterules new file mode 100644 index 0000000..ce2a6c3 --- /dev/null +++ b/lua/avante/templates/base.avanterules @@ -0,0 +1,14 @@ +{# Uses https://mitsuhiko.github.io/minijinja-playground/ for testing: +{ + "ask": true, + "use_xml_format": true, + "question": "Refactor to include tab flow", + "code_lang": "lua", + "file_content": "local Config = require('avante.config')" +} +#} +Act as an expert software developer. +Always use best practices when coding. +Respect and use existing conventions, libraries, etc that are already present in the code base. +{% block extra_prompt %} +{% endblock %} diff --git a/lua/avante/templates/editing.avanterules b/lua/avante/templates/editing.avanterules index a02b2e7..f0f90cf 100644 --- a/lua/avante/templates/editing.avanterules +++ b/lua/avante/templates/editing.avanterules @@ -1,5 +1,5 @@ -{% extends "planning.avanterules" %} -{% block user_prompt %} +{% extends "base.avanterules" %} +{% block extra_prompt %} Your task is to modify the provided code according to the user's request. Follow these instructions precisely: 1. Return *ONLY* the complete modified code. diff --git a/lua/avante/templates/planning.avanterules b/lua/avante/templates/planning.avanterules index 9e76666..e887e30 100644 --- a/lua/avante/templates/planning.avanterules +++ b/lua/avante/templates/planning.avanterules @@ -1,18 +1,6 @@ -{# Uses https://mitsuhiko.github.io/minijinja-playground/ for testing: -{ - "ask": true, - "use_xml_format": true, - "question": "Refactor to include tab flow", - "code_lang": "lua", - "file_content": "local Config = require('avante.config')" -} -#} -Act as an expert software developer. -Always use best practices when coding. -Respect and use existing conventions, libraries, etc that are already present in the code base. - +{% extends "base.avanterules" %} {%- if ask %} -{% block user_prompt %} +{% block extra_prompt %} Take requests for changes to the supplied code. If the request is ambiguous, ask questions. @@ -106,6 +94,7 @@ def hello(): from hello import hello + # *SEARCH/REPLACE block* Rules: Every *SEARCH/REPLACE block* must use this format: diff --git a/lua/avante/templates/suggesting.avanterules b/lua/avante/templates/suggesting.avanterules index 8eada61..44236ff 100644 --- a/lua/avante/templates/suggesting.avanterules +++ b/lua/avante/templates/suggesting.avanterules @@ -1,32 +1,29 @@ -{% extends "planning.avanterules" %} -{% block user_prompt %} +{% extends "base.avanterules" %} +{% block extra_prompt %} Your task is to suggest code modifications at the cursor position. Follow these instructions meticulously: + 1. Carefully analyze the original code, paying close attention to its structure and the cursor position. + 2. You must follow this JSON format when suggesting modifications: + {% raw %} + [ + { + "row": ${row}, + "col": ${column}, + "content": "Your suggested code here" + } + ] + {% endraw %} -1. Carefully analyze the original code, paying close attention to its structure and the cursor position. - -2. You must follow this json format when suggesting modifications: - -{% raw %} -[ - { - "row": ${row}, - "col": ${column}, - "content": "Your suggested code here" - } -] -{% endraw %} - -3. When suggesting suggested code: - - DO NOT include three backticks: {%raw%}```{%endraw%} in your suggestion. Treat the suggested code AS IS. - - Each element in the returned list is a COMPLETE and INDEPENDENT code snippet. - - MUST be a valid json format. Don't be lazy! - - Only return the new code to be inserted. - - Your returned code should not overlap with the original code in any way. Don't be lazy! - - Please strictly check the code around the position and ensure that the complete code after insertion is correct. Don't be lazy! - - Do not return the entire file content or any surrounding code. - - Do not include any explanations, comments, or line numbers in your response. - - Ensure the suggested code fits seamlessly with the existing code structure and indentation. - - If there are no recommended modifications, return an empty list. - -Remember to ONLY RETURN the suggested code snippet, without any additional formatting or explanation. +Guidelines: + 1. Make sure you have maintained the user's existing whitespace and indentation. This is REALLY IMPORTANT! + 2. DO NOT include three backticks: {%raw%}```{%endraw%} in your suggestion. Treat the suggested code AS IS. + 3. Each element in the returned list is a COMPLETE and INDEPENDENT code snippet. + 4. MUST be a valid JSON format. DON NOT be lazy! + 5. Only return the new code to be inserted. + 6. Your returned code should not overlap with the original code in any way. Don't be lazy! + 7. Please strictly check the code around the position and ensure that the complete code after insertion is correct. Don't be lazy! + 8. Do not return the entire file content or any surrounding code. + 9. Do not include any explanations, comments, or line numbers in your response. + 10. Ensure the suggested code fits seamlessly with the existing code structure and indentation. + 11. If there are no recommended modifications, return an empty list. + 12. Remember to ONLY RETURN the suggested code snippet, without any additional formatting or explanation. {% endblock %}