74
|
1 local Luan = require "luan:Luan.luan"
|
|
2 local error = Luan.error
|
|
3 local Parsers = require "luan:Parsers.luan"
|
|
4 local json_parse = Parsers.json_parse or error()
|
|
5 local json_string = Parsers.json_string or error()
|
|
6 local Claude = require "site:/lib/ai/claude/Claude.luan"
|
|
7 local claude_chat = Claude.chat or error()
|
|
8 local Logging = require "luan:logging/Logging.luan"
|
|
9 local logger = Logging.logger "claude/Translator"
|
|
10
|
|
11
|
|
12 local Translator = {}
|
|
13
|
|
14 function Translator.translate(text,lang)
|
|
15 local thread = {
|
|
16 system = `%>
|
|
17 Translate <%=lang%> in the text you get to English.
|
|
18 Preserve formatting.
|
|
19 The text may also contain English. Just leave that unchanged.
|
|
20 <% `
|
|
21 messages = {{
|
|
22 role = "user"
|
|
23 content = text
|
|
24 }}
|
|
25 temperature = 0
|
|
26 }
|
|
27 local resultJson = claude_chat(thread)
|
|
28 local result = json_parse(resultJson)
|
|
29 -- logger.info(json_string(result))
|
|
30 result.type == "message" or error()
|
|
31 result.role == "assistant" or error()
|
|
32 result.stop_reason == "end_turn" or result.stop_reason == "tool_use" or error()
|
|
33 local content = result.content or error()
|
|
34 #content==1 or error()
|
|
35 content = content[1]
|
|
36 content.type == "text" or error()
|
|
37 return content.text or error()
|
|
38 end
|
|
39
|
|
40 return Translator
|