diff src/lib/ai/claude/Chat.luan @ 9:46097e607701

romaji
author Franklin Schmidt <fschmidt@gmail.com>
date Mon, 21 Jul 2025 15:16:47 -0600
parents 255c36830154
children 003a90ce72d7
line wrap: on
line diff
--- a/src/lib/ai/claude/Chat.luan	Fri Jul 18 23:46:48 2025 -0600
+++ b/src/lib/ai/claude/Chat.luan	Mon Jul 21 15:16:47 2025 -0600
@@ -18,8 +18,22 @@
 
 local Chat = {}
 
+function Chat.output_system_prompt(thread)
+	if thread == nil then
+		return
+	end
+	thread = json_parse(thread)
+	local system_prompt = thread.system or error
+	system_prompt = html_encode(system_prompt)
+	%><%=system_prompt%><%
+end
+
 function Chat.output_messages_html(thread)
-	local messages = thread and json_parse(thread) or {nil}
+	if thread == nil then
+		return
+	end
+	thread = json_parse(thread)
+	local messages = thread.messages or error
 	for _, message in ipairs(messages) do
 		local role = message.role or error()
 		local who
@@ -31,9 +45,10 @@
 			error(role)
 		end
 		local function output(text)
+			text = html_encode(text)
 %>
 			<h3><%=who%></h3>
-			<div role="<%=role%>"><%=html_encode(text)%></div>
+			<div markdown role="<%=role%>"><%=text%></div>
 <%
 		end
 		local content = message.content or error()
@@ -50,8 +65,30 @@
 	end_for
 end
 
+
+local system_prompt = [[
+CRITICAL REQUIREMENT: When writing Japanese, use ruby markdown syntax {japanese|romaji} for pronunciation guidance.
+
+Apply ruby tags to meaningful pronunciation units:
+- Individual kanji or kanji compounds: {私|watashi}, {学生|gakusei}
+- Hiragana/katakana words and particles: {は|wa}, {です|desu}, {ありがとう|arigatō}
+- Grammatical elements: {ました|mashita}, {ません|masen}
+
+The romaji must reflect ACTUAL PRONUNCIATION, not character-by-character readings.
+Use macrons for long vowels: ā, ī, ū, ē, ō
+
+APPLIES TO ALL JAPANESE TEXT: Example sentences, grammar explanations, vocabulary lists, casual mentions - ANY Japanese characters in your response need ruby tags.
+
+VERIFICATION STEP: Before sending, scan your ENTIRE response for any Japanese characters (hiragana, katakana, kanji) and ensure they all have ruby tags.
+]]
+
+
 function Chat.ask(thread,input)
-	local messages = thread and json_parse(thread) or {nil}
+	thread = thread and json_parse(thread) or {
+		system = system_prompt
+		messages = {nil}
+	}
+	local messages = thread.messages or error
 	messages[#messages+1] = {
 		role = "user"
 		content = input
@@ -74,12 +111,10 @@
 	Thread.sleep(5000)
 	}
 	if true then
-		return json_string(messages)
+		return json_string(thread)
 	end
 --]=]
-	local resultJson = claude_chat{
-		messages = messages
-	}
+	local resultJson = claude_chat(thread)
 	local result = json_parse(resultJson)
 	-- logger.info(json_string(result))
 	result.type == "message" or error()
@@ -90,7 +125,7 @@
 		role = "assistant"
 		content = content
 	}
-	return json_string(messages)
+	return json_string(thread)
 end
 
 return Chat