Skip to content

Commit 992d345

Browse files
committed
更换提示词
1 parent 13dd36f commit 992d345

2 files changed

Lines changed: 40 additions & 31 deletions

File tree

lua/kide/tools/ai.lua

Lines changed: 36 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ M.config = {
1414
local request_json = {
1515
messages = {
1616
{
17-
content = "You will be provided with a sentence in {{from}}, and your task is to translate it into {{to}}.",
17+
content = "",
1818
role = "system",
1919
},
2020
{
@@ -44,43 +44,46 @@ local request_json = {
4444
local function trans_system_prompt(request)
4545
local from = request.from
4646
if request.from == "auto" then
47-
from = "(detect language)"
47+
return "你会得到一个需要你检测语言的文本, 将他翻译为" .. request.to .. ""
4848
end
49-
return "You will be provided with a sentence in "
50-
.. from
51-
.. ", and your task is to translate it into "
52-
.. request.to
53-
.. "."
49+
return "你会得到一个" .. from .. "文本, 将他翻译为" .. request.to .. ""
5450
end
5551

5652
local function token()
5753
return "Bearer " .. M.config.API_KEY
5854
end
5955

6056
---@param cmd string[]
61-
---@param callback fun(data: string)
57+
---@param callback fun(data: string, done: boolean)
6258
local function handle_sse_events(cmd, callback)
6359
local _ = vim.fn.jobstart(cmd, {
6460
on_stdout = function(_, data, _)
6561
for _, value in ipairs(data) do
6662
if vim.startswith(value, "data:") then
6763
local _, _, text = string.find(value, "data: (.+)")
68-
if text ~= "[DONE]" then
69-
local resp_json = vim.fn.json_decode(text)
70-
if resp_json.usage ~= nil and M.config.show_usage then
71-
callback "\n"
72-
callback(
73-
"API[token usage]: "
74-
.. vim.inspect(resp_json.usage.prompt_cache_hit_tokens)
75-
.. " "
76-
.. vim.inspect(resp_json.usage.prompt_tokens)
77-
.. " + "
78-
.. vim.inspect(resp_json.usage.completion_tokens)
79-
.. " = "
80-
.. vim.inspect(resp_json.usage.total_tokens)
81-
)
64+
if text == "[DONE]" then
65+
callback(text, true)
66+
else
67+
local ok, resp_json = pcall(vim.fn.json_decode, text)
68+
if ok then
69+
if resp_json.usage ~= nil and M.config.show_usage then
70+
callback("\n", false)
71+
callback(
72+
"API[token usage]: "
73+
.. vim.inspect(resp_json.usage.prompt_cache_hit_tokens)
74+
.. " "
75+
.. vim.inspect(resp_json.usage.prompt_tokens)
76+
.. " + "
77+
.. vim.inspect(resp_json.usage.completion_tokens)
78+
.. " = "
79+
.. vim.inspect(resp_json.usage.total_tokens),
80+
false
81+
)
82+
else
83+
callback(resp_json.choices[1].delta.content, false)
84+
end
8285
else
83-
callback(resp_json.choices[1].delta.content)
86+
callback("Error: " .. text, false)
8487
end
8588
end
8689
end
@@ -160,12 +163,17 @@ M.translate_float = function(request)
160163
end, { noremap = true, silent = true, buffer = buf })
161164

162165
local curlines = 0
163-
local callback = function(data)
166+
local callback = function(data, done)
164167
if closed then
165168
return
166169
end
170+
if done then
171+
vim.bo[buf].readonly = true
172+
vim.bo[buf].modifiable = false
173+
return
174+
end
167175
-- 判断是否换行符
168-
if data == "\n" then
176+
if data:match "\n" then
169177
curlines = 0
170178
else
171179
curlines = curlines + vim.fn.strdisplaywidth(data)
@@ -181,8 +189,9 @@ M.translate_float = function(request)
181189
end
182190

183191
if vim.api.nvim_buf_is_valid(buf) then
184-
if data == "\n" then
185-
vim.api.nvim_put({ "", "" }, "c", true, true)
192+
if data:match "\n" then
193+
local ln = vim.split(data, "\n")
194+
vim.api.nvim_put(ln, "c", true, true)
186195
else
187196
vim.api.nvim_put({ data }, "c", true, true)
188197
end

lua/mappings.lua

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,7 @@ local function creat_trans_command(name, from, to)
346346
})
347347
end
348348

349-
creat_trans_command("TransAutoZh", "auto", "zh")
350-
creat_trans_command("TransEnZh", "en", "zh")
351-
creat_trans_command("TransZhEn", "zh", "en")
352-
creat_trans_command("TransIdZh", "id", "zh")
349+
creat_trans_command("TransAutoZh", "auto", "中文")
350+
creat_trans_command("TransEnZh", "英语", "中文")
351+
creat_trans_command("TransZhEn", "中文", "英语")
352+
creat_trans_command("TransIdZh", "印尼语", "中文")

0 commit comments

Comments
 (0)