comparison src/lib/ai/claude/Ai_chat.luan @ 25:3a80ddafe5a4

courses work
author Franklin Schmidt <fschmidt@gmail.com>
date Fri, 01 Aug 2025 00:33:51 -0600
parents 27989d63fc71
children 505185272dd7
comparison
equal deleted inserted replaced
24:87fe70201aa8 25:3a80ddafe5a4
72 end 72 end
73 end_for 73 end_for
74 end 74 end
75 75
76 76
77 local system_prompt = [[ 77 local function ask(thread,input)
78 # Your Role
79
80 You are a Japanese language teacher.
81
82 # Romaji
83
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
100 function Ai_chat.ask(thread,input)
101 thread = thread and json_parse(thread) or {
102 system = system_prompt
103 messages = {nil}
104 }
105 local messages = thread.messages or error 78 local messages = thread.messages or error
106 messages[#messages+1] = { 79 messages[#messages+1] = {
107 role = "user" 80 role = "user"
108 content = input 81 content = input
109 } 82 }
113 content = [[ 86 content = [[
114 hello 87 hello
115 ]] 88 ]]
116 } 89 }
117 if true then 90 if true then
118 return json_string(thread) 91 return
119 end 92 end
120 --]=] 93 --]=]
121 -- logger.info(json_string(thread)) 94 -- logger.info(json_string(thread))
122 local resultJson = claude_chat(thread) 95 local resultJson = claude_chat(thread)
123 local result = json_parse(resultJson) 96 local result = json_parse(resultJson)
128 local content = result.content or error() 101 local content = result.content or error()
129 messages[#messages+1] = { 102 messages[#messages+1] = {
130 role = "assistant" 103 role = "assistant"
131 content = content 104 content = content
132 } 105 }
106 end
107
108 function Ai_chat.ask_first(system_prompt,input)
109 local thread = {
110 system = system_prompt
111 messages = {nil}
112 }
113 if input ~= nil then
114 ask(thread,input)
115 end
116 return json_string(thread)
117 end
118
119 function Ai_chat.ask_more(thread,input)
120 thread = json_parse(thread)
121 ask(thread,input)
133 return json_string(thread) 122 return json_string(thread)
134 end 123 end
135 124
136 return Ai_chat 125 return Ai_chat