5
|
1 local Luan = require "luan:Luan.luan"
|
|
2 local error = Luan.error
|
6
|
3 local ipairs = Luan.ipairs or error()
|
|
4 local type = Luan.type or error()
|
|
5 local String = require "luan:String.luan"
|
|
6 local starts_with = String.starts_with or error()
|
|
7 local Html = require "luan:Html.luan"
|
|
8 local html_encode = Html.encode or error()
|
|
9 local Parsers = require "luan:Parsers.luan"
|
|
10 local json_parse = Parsers.json_parse or error()
|
|
11 local json_string = Parsers.json_string or error()
|
|
12 local Claude = require "site:/lib/ai/claude/Claude.luan"
|
|
13 local claude_chat = Claude.chat or error()
|
|
14 local Logging = require "luan:logging/Logging.luan"
|
19
|
15 local logger = Logging.logger "claude/Ai_chat"
|
5
|
16
|
|
17
|
19
|
18 local Ai_chat = {}
|
5
|
19
|
19
|
20 function Ai_chat.output_system_prompt(thread)
|
9
|
21 if thread == nil then
|
|
22 return
|
|
23 end
|
|
24 thread = json_parse(thread)
|
|
25 local system_prompt = thread.system or error
|
|
26 system_prompt = html_encode(system_prompt)
|
|
27 %><%=system_prompt%><%
|
|
28 end
|
|
29
|
19
|
30 function Ai_chat.output_messages_html(lang,thread,old_thread)
|
9
|
31 if thread == nil then
|
|
32 return
|
|
33 end
|
|
34 thread = json_parse(thread)
|
|
35 local messages = thread.messages or error
|
13
|
36 local n = 0
|
|
37 if old_thread ~= nil then
|
|
38 old_thread = json_parse(old_thread)
|
|
39 local old_messages = old_thread.messages or error
|
|
40 n = #old_messages
|
|
41 end
|
|
42 for i, message in ipairs(messages) do
|
|
43 if i <= n then
|
|
44 continue
|
|
45 end
|
6
|
46 local role = message.role or error()
|
|
47 local who
|
|
48 if role=="assistant" then
|
|
49 who = "Claude"
|
|
50 elseif role=="user" then
|
|
51 who = "You"
|
|
52 else
|
|
53 error(role)
|
|
54 end
|
|
55 local function output(text)
|
9
|
56 text = html_encode(text)
|
6
|
57 %>
|
7
|
58 <h3><%=who%></h3>
|
16
|
59 <div markdown role="<%=role%>" lang="<%=lang%>"><%=text%></div>
|
6
|
60 <%
|
|
61 end
|
|
62 local content = message.content or error()
|
|
63 if type(content) == "string" then
|
|
64 output(content)
|
|
65 else
|
|
66 for _, part in ipairs(content) do
|
|
67 if part.type=="text" then
|
|
68 local text = part.text or error()
|
|
69 output(text)
|
|
70 end
|
|
71 end
|
|
72 end
|
|
73 end_for
|
5
|
74 end
|
|
75
|
9
|
76
|
|
77 local system_prompt = [[
|
20
|
78 # Your Role
|
|
79
|
|
80 You are a Japanese language teacher.
|
|
81
|
|
82 # Romaji
|
|
83
|
9
|
84 CRITICAL REQUIREMENT: When writing Japanese, use ruby markdown syntax {japanese|romaji} for pronunciation guidance.
|
|
85
|
|
86 Apply ruby tags to meaningful pronunciation units:
|
|
87 - Individual kanji or kanji compounds: {私|watashi}, {学生|gakusei}
|
|
88 - Hiragana/katakana words and particles: {は|wa}, {です|desu}, {ありがとう|arigatō}
|
|
89 - Grammatical elements: {ました|mashita}, {ません|masen}
|
|
90
|
|
91 The romaji must reflect ACTUAL PRONUNCIATION, not character-by-character readings.
|
|
92 Use macrons for long vowels: ā, ī, ū, ē, ō
|
|
93
|
|
94 APPLIES TO ALL JAPANESE TEXT: Example sentences, grammar explanations, vocabulary lists, casual mentions - ANY Japanese characters in your response need ruby tags.
|
|
95
|
|
96 VERIFICATION STEP: Before sending, scan your ENTIRE response for any Japanese characters (hiragana, katakana, kanji) and ensure they all have ruby tags.
|
|
97 ]]
|
|
98
|
|
99
|
19
|
100 function Ai_chat.ask(thread,input)
|
9
|
101 thread = thread and json_parse(thread) or {
|
|
102 system = system_prompt
|
|
103 messages = {nil}
|
|
104 }
|
|
105 local messages = thread.messages or error
|
6
|
106 messages[#messages+1] = {
|
|
107 role = "user"
|
|
108 content = input
|
|
109 }
|
|
110 --[=[
|
|
111 messages[#messages+1] = {
|
|
112 role = "assistant"
|
|
113 content = [[
|
13
|
114 hello
|
6
|
115 ]]
|
|
116 }
|
|
117 if true then
|
9
|
118 return json_string(thread)
|
6
|
119 end
|
|
120 --]=]
|
20
|
121 -- logger.info(json_string(thread))
|
9
|
122 local resultJson = claude_chat(thread)
|
6
|
123 local result = json_parse(resultJson)
|
|
124 -- logger.info(json_string(result))
|
|
125 result.type == "message" or error()
|
|
126 result.role == "assistant" or error()
|
|
127 result.stop_reason == "end_turn" or result.stop_reason == "tool_use" or error()
|
|
128 local content = result.content or error()
|
|
129 messages[#messages+1] = {
|
|
130 role = "assistant"
|
|
131 content = content
|
|
132 }
|
9
|
133 return json_string(thread)
|
5
|
134 end
|
|
135
|
19
|
136 return Ai_chat
|