feat(templates): avanterules filetype support (closes #254) (#466)

Signed-off-by: Aaron Pham <contact@aarnphm.xyz>
This commit is contained in:
Aaron Pham
2024-09-03 04:09:13 -04:00
committed by GitHub
parent 054695cc63
commit 4ad913435c
31 changed files with 962 additions and 265 deletions

View File

@@ -7,13 +7,13 @@ local M = {}
M.api_key_name = "ANTHROPIC_API_KEY"
M.tokenizer_id = "gpt-4o"
M.use_xml_format = true
---@param prompt_opts AvantePromptOptions
M.parse_message = function(prompt_opts)
M.parse_message = function(opts)
local message_content = {}
if Clipboard.support_paste_image() and prompt_opts.image_paths then
for _, image_path in ipairs(prompt_opts.image_paths) do
if Clipboard.support_paste_image() and opts.image_paths then
for _, image_path in ipairs(opts.image_paths) do
table.insert(message_content, {
type = "image",
source = {
@@ -25,32 +25,15 @@ M.parse_message = function(prompt_opts)
end
end
local user_prompts_with_length = {}
for idx, user_prompt in ipairs(prompt_opts.user_prompts) do
table.insert(user_prompts_with_length, { idx = idx, length = Utils.tokens.calculate_tokens(user_prompt) })
local user_prompt_obj = {
type = "text",
text = opts.user_prompt,
}
if Utils.tokens.calculate_tokens(opts.user_prompt) then
user_prompt_obj.cache_control = { type = "ephemeral" }
end
table.sort(user_prompts_with_length, function(a, b)
return a.length > b.length
end)
local top_three = {}
for i = 1, math.min(3, #user_prompts_with_length) do
top_three[user_prompts_with_length[i].idx] = true
end
for idx, prompt_data in ipairs(prompt_opts.user_prompts) do
local user_prompt_obj = {
type = "text",
text = prompt_data,
}
if top_three[idx] then
user_prompt_obj.cache_control = { type = "ephemeral" }
end
table.insert(message_content, user_prompt_obj)
end
table.insert(message_content, user_prompt_obj)
return {
{

View File

@@ -32,11 +32,9 @@ M.api_key_name = "CO_API_KEY"
M.tokenizer_id = "CohereForAI/c4ai-command-r-plus-08-2024"
M.parse_message = function(opts)
local user_prompt = table.concat(opts.user_prompts, "\n\n")
return {
preamble = opts.system_prompt,
message = user_prompt,
message = opts.user_prompt,
}
end

View File

@@ -132,7 +132,7 @@ M.tokenizer_id = "gpt-4o"
M.parse_message = function(opts)
return {
{ role = "system", content = opts.system_prompt },
{ role = "user", content = table.concat(opts.user_prompts, "\n\n") },
{ role = "user", content = opts.user_prompt },
}
end

View File

@@ -25,11 +25,7 @@ M.parse_message = function(opts)
end
-- insert a part into parts
for _, user_prompt in ipairs(opts.user_prompts) do
table.insert(message_content, {
text = user_prompt,
})
end
table.insert(message_content, { text = opts.user_prompt })
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_prompts string[]
---@field user_prompt string
---@field image_paths? string[]
---
---@class AvanteBaseMessage
@@ -70,6 +70,7 @@ local Dressing = require("avante.ui.dressing")
---@field has fun(): boolean
---@field api_key_name string
---@field tokenizer_id string | "gpt-4o"
---@field use_xml_format boolean
---@field model? string
---@field parse_api_key fun(): string | nil
---@field parse_stream_data? AvanteStreamParser
@@ -275,6 +276,10 @@ M = setmetatable(M, {
t[k].tokenizer_id = "gpt-4o"
end
if t[k].use_xml_format == nil then
t[k].use_xml_format = false
end
if t[k].has == nil then
t[k].has = function()
return E.parse_envvar(t[k]) ~= nil

View File

@@ -30,12 +30,10 @@ M.tokenizer_id = "gpt-4o"
---@param opts AvantePromptOptions
M.get_user_message = function(opts)
return table.concat(opts.user_prompts, "\n\n")
return opts.user_prompt
end
M.parse_message = function(opts)
local user_prompt = table.concat(opts.user_prompts, "\n\n")
---@type string | OpenAIMessage[]
local user_content
if Config.behaviour.support_paste_from_clipboard and opts.image_paths and #opts.image_paths > 0 then
@@ -48,9 +46,9 @@ M.parse_message = function(opts)
},
})
end
table.insert(user_content, { type = "text", text = user_prompt })
table.insert(user_content, { type = "text", text = opts.user_prompt })
else
user_content = user_prompt
user_content = opts.user_prompt
end
return {