Mercurial Hosting > lang
comparison 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 |
comparison
equal
deleted
inserted
replaced
8:2b7dcf355a78 | 9:46097e607701 |
---|---|
16 local Thread = require "luan:Thread.luan" | 16 local Thread = require "luan:Thread.luan" |
17 | 17 |
18 | 18 |
19 local Chat = {} | 19 local Chat = {} |
20 | 20 |
21 function Chat.output_system_prompt(thread) | |
22 if thread == nil then | |
23 return | |
24 end | |
25 thread = json_parse(thread) | |
26 local system_prompt = thread.system or error | |
27 system_prompt = html_encode(system_prompt) | |
28 %><%=system_prompt%><% | |
29 end | |
30 | |
21 function Chat.output_messages_html(thread) | 31 function Chat.output_messages_html(thread) |
22 local messages = thread and json_parse(thread) or {nil} | 32 if thread == nil then |
33 return | |
34 end | |
35 thread = json_parse(thread) | |
36 local messages = thread.messages or error | |
23 for _, message in ipairs(messages) do | 37 for _, message in ipairs(messages) do |
24 local role = message.role or error() | 38 local role = message.role or error() |
25 local who | 39 local who |
26 if role=="assistant" then | 40 if role=="assistant" then |
27 who = "Claude" | 41 who = "Claude" |
29 who = "You" | 43 who = "You" |
30 else | 44 else |
31 error(role) | 45 error(role) |
32 end | 46 end |
33 local function output(text) | 47 local function output(text) |
48 text = html_encode(text) | |
34 %> | 49 %> |
35 <h3><%=who%></h3> | 50 <h3><%=who%></h3> |
36 <div role="<%=role%>"><%=html_encode(text)%></div> | 51 <div markdown role="<%=role%>"><%=text%></div> |
37 <% | 52 <% |
38 end | 53 end |
39 local content = message.content or error() | 54 local content = message.content or error() |
40 if type(content) == "string" then | 55 if type(content) == "string" then |
41 output(content) | 56 output(content) |
48 end | 63 end |
49 end | 64 end |
50 end_for | 65 end_for |
51 end | 66 end |
52 | 67 |
68 | |
69 local system_prompt = [[ | |
70 CRITICAL REQUIREMENT: When writing Japanese, use ruby markdown syntax {japanese|romaji} for pronunciation guidance. | |
71 | |
72 Apply ruby tags to meaningful pronunciation units: | |
73 - Individual kanji or kanji compounds: {私|watashi}, {学生|gakusei} | |
74 - Hiragana/katakana words and particles: {は|wa}, {です|desu}, {ありがとう|arigatō} | |
75 - Grammatical elements: {ました|mashita}, {ません|masen} | |
76 | |
77 The romaji must reflect ACTUAL PRONUNCIATION, not character-by-character readings. | |
78 Use macrons for long vowels: ā, ī, ū, ē, ō | |
79 | |
80 APPLIES TO ALL JAPANESE TEXT: Example sentences, grammar explanations, vocabulary lists, casual mentions - ANY Japanese characters in your response need ruby tags. | |
81 | |
82 VERIFICATION STEP: Before sending, scan your ENTIRE response for any Japanese characters (hiragana, katakana, kanji) and ensure they all have ruby tags. | |
83 ]] | |
84 | |
85 | |
53 function Chat.ask(thread,input) | 86 function Chat.ask(thread,input) |
54 local messages = thread and json_parse(thread) or {nil} | 87 thread = thread and json_parse(thread) or { |
88 system = system_prompt | |
89 messages = {nil} | |
90 } | |
91 local messages = thread.messages or error | |
55 messages[#messages+1] = { | 92 messages[#messages+1] = { |
56 role = "user" | 93 role = "user" |
57 content = input | 94 content = input |
58 } | 95 } |
59 --[=[ | 96 --[=[ |
72 - 10 | 109 - 10 |
73 ]] | 110 ]] |
74 Thread.sleep(5000) | 111 Thread.sleep(5000) |
75 } | 112 } |
76 if true then | 113 if true then |
77 return json_string(messages) | 114 return json_string(thread) |
78 end | 115 end |
79 --]=] | 116 --]=] |
80 local resultJson = claude_chat{ | 117 local resultJson = claude_chat(thread) |
81 messages = messages | |
82 } | |
83 local result = json_parse(resultJson) | 118 local result = json_parse(resultJson) |
84 -- logger.info(json_string(result)) | 119 -- logger.info(json_string(result)) |
85 result.type == "message" or error() | 120 result.type == "message" or error() |
86 result.role == "assistant" or error() | 121 result.role == "assistant" or error() |
87 result.stop_reason == "end_turn" or result.stop_reason == "tool_use" or error() | 122 result.stop_reason == "end_turn" or result.stop_reason == "tool_use" or error() |
88 local content = result.content or error() | 123 local content = result.content or error() |
89 messages[#messages+1] = { | 124 messages[#messages+1] = { |
90 role = "assistant" | 125 role = "assistant" |
91 content = content | 126 content = content |
92 } | 127 } |
93 return json_string(messages) | 128 return json_string(thread) |
94 end | 129 end |
95 | 130 |
96 return Chat | 131 return Chat |