refactor: new prompts structure (#1071)

This commit is contained in:
yetone 2025-01-11 23:09:55 +08:00 committed by GitHub
parent c3e7ba0be0
commit 24641d8264
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 78 additions and 45 deletions

View File

@ -74,11 +74,43 @@ function Suggestion:suggest()
local provider = Providers[Config.auto_suggestions_provider] local provider = Providers[Config.auto_suggestions_provider]
---@type AvanteLLMMessage[]
local history_messages = {
{
role = "user",
content = [[
<filepath>a.py</filepath>
<code>def fib</code>
]],
},
{
role = "assistant",
content = "ok",
},
{
role = "user",
content = '<question>{ "indentSize": 4, "position": { "row": 1, "col": 2 } }</question>',
},
{
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({ Llm.stream({
provider = provider, provider = provider,
ask = true, ask = true,
selected_files = { { content = code_content, file_type = filetype, path = "" } }, selected_files = { { content = code_content, file_type = filetype, path = "" } },
code_lang = filetype, code_lang = filetype,
history_messages = history_messages,
instructions = vim.json.encode(doc), instructions = vim.json.encode(doc),
mode = "suggesting", mode = "suggesting",
on_chunk = function(chunk) full_response = full_response .. chunk end, 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 if cursor_row ~= doc.position.row or cursor_col ~= doc.position.col then return end
-- Clean up markdown code blocks -- Clean up markdown code blocks
full_response = full_response:gsub("^```%w*\n(.-)\n```$", "%1") 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 -- Remove everything before the first '[' to ensure we get just the JSON array
full_response = full_response:gsub("^.-(%[.*)", "%1") full_response = full_response:gsub("^.-(%[.*)", "%1")
local ok, suggestions = pcall(vim.json.decode, full_response) local ok, suggestions = pcall(vim.json.decode, full_response)

View File

@ -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 %}

View File

@ -1,5 +1,5 @@
{% extends "planning.avanterules" %} {% extends "base.avanterules" %}
{% block user_prompt %} {% block extra_prompt %}
Your task is to modify the provided code according to the user's request. Follow these instructions precisely: 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. 1. Return *ONLY* the complete modified code.

View File

@ -1,18 +1,6 @@
{# Uses https://mitsuhiko.github.io/minijinja-playground/ for testing: {% extends "base.avanterules" %}
{
"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.
{%- if ask %} {%- if ask %}
{% block user_prompt %} {% block extra_prompt %}
Take requests for changes to the supplied code. Take requests for changes to the supplied code.
If the request is ambiguous, ask questions. If the request is ambiguous, ask questions.
@ -106,6 +94,7 @@ def hello():
<REPLACE> <REPLACE>
from hello import hello from hello import hello
</REPLACE> </REPLACE>
# *SEARCH/REPLACE block* Rules: # *SEARCH/REPLACE block* Rules:
Every *SEARCH/REPLACE block* must use this format: Every *SEARCH/REPLACE block* must use this format:

View File

@ -1,32 +1,29 @@
{% extends "planning.avanterules" %} {% extends "base.avanterules" %}
{% block user_prompt %} {% block extra_prompt %}
Your task is to suggest code modifications at the cursor position. Follow these instructions meticulously: 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. Guidelines:
1. Make sure you have maintained the user's existing whitespace and indentation. This is REALLY IMPORTANT!
2. You must follow this json format when suggesting modifications: 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.
{% raw %} 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!
"row": ${row}, 7. Please strictly check the code around the position and ensure that the complete code after insertion is correct. Don't be lazy!
"col": ${column}, 8. Do not return the entire file content or any surrounding code.
"content": "Your suggested code here" 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.
{% endraw %} 12. Remember to ONLY RETURN the suggested code snippet, without any additional formatting or explanation.
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.
{% endblock %} {% endblock %}