comparison src/lib/ai/claude/Ai_chat.luan @ 35:3117876debca

ai_first_message in textarea
author Franklin Schmidt <fschmidt@gmail.com>
date Tue, 05 Aug 2025 16:41:29 -0600
parents 0fb3488a017d
children 238a91f224b1
comparison
equal deleted inserted replaced
34:0fb3488a017d 35:3117876debca
16 16
17 17
18 local Ai_chat = {} 18 local Ai_chat = {}
19 19
20 function Ai_chat.output_system_prompt(thread) 20 function Ai_chat.output_system_prompt(thread)
21 if thread == nil then
22 return
23 end
24 thread = json_parse(thread) 21 thread = json_parse(thread)
25 local system_prompt = thread.system or error 22 local system_prompt = thread.system or error
26 system_prompt = html_encode(system_prompt) 23 system_prompt = html_encode(system_prompt)
27 %><%=system_prompt%><% 24 %><%=system_prompt%><%
28 end 25 end
29 26
30 function Ai_chat.output_messages_html(show_text,thread,old_thread) 27 function Ai_chat.output_messages_html(show_text,thread,old_thread)
31 if thread == nil then
32 return
33 end
34 thread = json_parse(thread) 28 thread = json_parse(thread)
35 local messages = thread.messages or error 29 local messages = thread.messages or error
36 local n = 0 30 local n = 0
37 if old_thread ~= nil then 31 if old_thread ~= nil then
38 old_thread = json_parse(old_thread) 32 old_thread = json_parse(old_thread)
81 end 75 end
82 end_for 76 end_for
83 end 77 end
84 78
85 79
86 local function ask(thread,input) 80 function Ai_chat.init(system_prompt)
81 local thread = {
82 system = system_prompt
83 messages = {nil}
84 }
85 return json_string(thread)
86 end
87
88 function Ai_chat.has_messages(thread)
89 thread = json_parse(thread)
90 return #thread.messages > 0
91 end
92
93 function Ai_chat.ask(thread,input)
94 thread = json_parse(thread)
87 local messages = thread.messages or error 95 local messages = thread.messages or error
88 messages[#messages+1] = { 96 messages[#messages+1] = {
89 role = "user" 97 role = "user"
90 content = input 98 content = input
91 } 99 }
110 local content = result.content or error() 118 local content = result.content or error()
111 messages[#messages+1] = { 119 messages[#messages+1] = {
112 role = "assistant" 120 role = "assistant"
113 content = content 121 content = content
114 } 122 }
115 end
116
117 function Ai_chat.ask_first(system_prompt,input)
118 local thread = {
119 system = system_prompt
120 messages = {nil}
121 }
122 if input ~= nil then
123 ask(thread,input)
124 end
125 return json_string(thread)
126 end
127
128 function Ai_chat.ask_more(thread,input)
129 thread = json_parse(thread)
130 ask(thread,input)
131 return json_string(thread) 123 return json_string(thread)
132 end 124 end
133 125
134 return Ai_chat 126 return Ai_chat