Mercurial Hosting > lang
annotate src/lib/ai/claude/Ai_chat.luan @ 69:f5e72f2d1025
add stt_prompt
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Sat, 23 Aug 2025 07:07:59 -0600 |
parents | 16e5c14eb800 |
children |
rev | line source |
---|---|
5 | 1 local Luan = require "luan:Luan.luan" |
2 local error = Luan.error | |
6 | 3 local ipairs = Luan.ipairs or error() |
38 | 4 local pairs = Luan.pairs or error() |
6 | 5 local type = Luan.type or error() |
6 local String = require "luan:String.luan" | |
7 local starts_with = String.starts_with or error() | |
8 local Html = require "luan:Html.luan" | |
9 local html_encode = Html.encode or error() | |
10 local Parsers = require "luan:Parsers.luan" | |
11 local json_parse = Parsers.json_parse or error() | |
12 local json_string = Parsers.json_string or error() | |
13 local Claude = require "site:/lib/ai/claude/Claude.luan" | |
14 local claude_chat = Claude.chat or error() | |
15 local Logging = require "luan:logging/Logging.luan" | |
19 | 16 local logger = Logging.logger "claude/Ai_chat" |
5 | 17 |
18 | |
19 | 19 local Ai_chat = {} |
5 | 20 |
19 | 21 function Ai_chat.output_system_prompt(thread) |
9 | 22 thread = json_parse(thread) |
23 local system_prompt = thread.system or error | |
24 system_prompt = html_encode(system_prompt) | |
25 %><%=system_prompt%><% | |
26 end | |
27 | |
52 | 28 function Ai_chat.output_messages_html(assistant_controls,thread,old_thread) |
9 | 29 thread = json_parse(thread) |
30 local messages = thread.messages or error | |
13 | 31 local n = 0 |
32 if old_thread ~= nil then | |
33 old_thread = json_parse(old_thread) | |
34 local old_messages = old_thread.messages or error | |
35 n = #old_messages | |
36 end | |
37 for i, message in ipairs(messages) do | |
38 if i <= n then | |
39 continue | |
40 end | |
6 | 41 local role = message.role or error() |
42 local who | |
43 if role=="assistant" then | |
44 who = "Claude" | |
45 elseif role=="user" then | |
46 who = "You" | |
47 else | |
48 error(role) | |
49 end | |
50 local function output(text) | |
9 | 51 text = html_encode(text) |
6 | 52 %> |
7 | 53 <h3><%=who%></h3> |
34 | 54 <div role="<%=role%>"> |
55 <div message markdown><%=text%></div> | |
56 <% if role=="assistant" then %> | |
52 | 57 <%= assistant_controls %> |
34 | 58 <% end %> |
59 </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 | |
54 | 76 local function get_chat(chat_id) |
77 local Chat = require "site:/lib/Chat.luan" | |
78 local User = require "site:/lib/User.luan" | |
79 local chat = Chat.get_by_id(chat_id) or error() | |
80 local user = User.current() | |
81 local is_owner = user ~= nil and user.id == chat.user_id | |
82 is_owner or not chat.is_private or error "private" | |
83 return chat | |
84 end | |
85 | |
38 | 86 local functions = { |
54 | 87 get_chat = { |
38 | 88 tool = { |
54 | 89 description = "Get the contents of a chat/thread with Claude on this website. The contents will be JSON in the format of the Claude API." |
38 | 90 input_schema = { |
91 type = "object" | |
92 properties = { | |
54 | 93 chat_id = { |
94 description = "The ID of the chat" | |
95 type = "integer" | |
38 | 96 } |
97 } | |
98 } | |
99 } | |
100 fn = function(input) | |
54 | 101 local chat_id = input.chat_id or error() |
102 local chat = get_chat(chat_id) | |
38 | 103 return chat.ai_thread or error() |
104 end | |
105 } | |
54 | 106 get_tts_instructions = { |
107 tool = { | |
69 | 108 description = "Get the text-to-speech instructions of a chat/thread on this website. These instructions are passed to OpenAI. If there are no instructions, the empty string is returned." |
54 | 109 input_schema = { |
110 type = "object" | |
111 properties = { | |
112 chat_id = { | |
113 description = "The ID of the chat" | |
114 type = "integer" | |
115 } | |
116 } | |
117 } | |
118 } | |
119 fn = function(input) | |
120 local chat_id = input.chat_id or error() | |
121 local chat = get_chat(chat_id) | |
122 return chat.tts_instructions or error() | |
123 end | |
124 } | |
69 | 125 get_stt_prompt = { |
126 tool = { | |
127 description = "Get the speech-to-text prompt of a chat/thread on this website. This prompt is passed to OpenAI. If there is no prompt, the empty string is returned." | |
128 input_schema = { | |
129 type = "object" | |
130 properties = { | |
131 chat_id = { | |
132 description = "The ID of the chat" | |
133 type = "integer" | |
134 } | |
135 } | |
136 } | |
137 } | |
138 fn = function(input) | |
139 local chat_id = input.chat_id or error() | |
140 local chat = get_chat(chat_id) | |
141 return chat.stt_prompt or error() | |
142 end | |
143 } | |
38 | 144 } |
145 local tools = {nil} | |
146 for name, f in pairs(functions) do | |
147 f.name = name | |
148 f.tool.name = name | |
149 tools[#tools+1] = f.tool | |
150 end | |
9 | 151 |
35
3117876debca
ai_first_message in textarea
Franklin Schmidt <fschmidt@gmail.com>
parents:
34
diff
changeset
|
152 function Ai_chat.init(system_prompt) |
3117876debca
ai_first_message in textarea
Franklin Schmidt <fschmidt@gmail.com>
parents:
34
diff
changeset
|
153 local thread = { |
3117876debca
ai_first_message in textarea
Franklin Schmidt <fschmidt@gmail.com>
parents:
34
diff
changeset
|
154 system = system_prompt |
38 | 155 tools = tools |
35
3117876debca
ai_first_message in textarea
Franklin Schmidt <fschmidt@gmail.com>
parents:
34
diff
changeset
|
156 messages = {nil} |
3117876debca
ai_first_message in textarea
Franklin Schmidt <fschmidt@gmail.com>
parents:
34
diff
changeset
|
157 } |
3117876debca
ai_first_message in textarea
Franklin Schmidt <fschmidt@gmail.com>
parents:
34
diff
changeset
|
158 return json_string(thread) |
3117876debca
ai_first_message in textarea
Franklin Schmidt <fschmidt@gmail.com>
parents:
34
diff
changeset
|
159 end |
3117876debca
ai_first_message in textarea
Franklin Schmidt <fschmidt@gmail.com>
parents:
34
diff
changeset
|
160 |
3117876debca
ai_first_message in textarea
Franklin Schmidt <fschmidt@gmail.com>
parents:
34
diff
changeset
|
161 function Ai_chat.has_messages(thread) |
3117876debca
ai_first_message in textarea
Franklin Schmidt <fschmidt@gmail.com>
parents:
34
diff
changeset
|
162 thread = json_parse(thread) |
3117876debca
ai_first_message in textarea
Franklin Schmidt <fschmidt@gmail.com>
parents:
34
diff
changeset
|
163 return #thread.messages > 0 |
3117876debca
ai_first_message in textarea
Franklin Schmidt <fschmidt@gmail.com>
parents:
34
diff
changeset
|
164 end |
3117876debca
ai_first_message in textarea
Franklin Schmidt <fschmidt@gmail.com>
parents:
34
diff
changeset
|
165 |
38 | 166 local function ask(thread,content) |
9 | 167 local messages = thread.messages or error |
6 | 168 messages[#messages+1] = { |
169 role = "user" | |
38 | 170 content = content |
6 | 171 } |
172 --[=[ | |
173 messages[#messages+1] = { | |
174 role = "assistant" | |
175 content = [[ | |
13 | 176 hello |
6 | 177 ]] |
178 } | |
179 if true then | |
25 | 180 return |
6 | 181 end |
182 --]=] | |
20 | 183 -- logger.info(json_string(thread)) |
9 | 184 local resultJson = claude_chat(thread) |
6 | 185 local result = json_parse(resultJson) |
186 -- logger.info(json_string(result)) | |
187 result.type == "message" or error() | |
188 result.role == "assistant" or error() | |
189 result.stop_reason == "end_turn" or result.stop_reason == "tool_use" or error() | |
190 local content = result.content or error() | |
191 messages[#messages+1] = { | |
192 role = "assistant" | |
193 content = content | |
194 } | |
38 | 195 local stop_reason = result.stop_reason or error() |
196 if stop_reason == "end_turn" then | |
197 -- ok | |
198 elseif stop_reason == "tool_use" then | |
199 local response = {nil} | |
200 for _, part in ipairs(content) do | |
201 if part.type == "tool_use" then | |
202 local f = functions[part.name] or error() | |
203 local input = part.input or error() | |
204 response[#response+1] = { | |
205 type = "tool_result" | |
206 tool_use_id = part.id or error() | |
207 content = f.fn(input) | |
208 } | |
209 end | |
210 end | |
211 ask(thread,response) | |
212 else | |
213 error(stop_reason) | |
214 end | |
215 end | |
216 | |
217 function Ai_chat.ask(thread,input) | |
218 thread = json_parse(thread) | |
219 ask(thread,input) | |
9 | 220 return json_string(thread) |
5 | 221 end |
222 | |
19 | 223 return Ai_chat |