forked from Rbb666/llm_chat
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathllm.c
More file actions
381 lines (327 loc) · 10.4 KB
/
llm.c
File metadata and controls
381 lines (327 loc) · 10.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
/*
* Copyright (c) 2006-2025, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2025/02/01 Rbb666 Add license info
* 2025/02/10 CXSforHPU Add llm history support
*/
#include "llm.h"
#include "shell.h"
static struct llm_obj handle = {0};
static rt_uint8_t llm_getc(void)
{
rt_uint8_t ch = 0;
while (rt_device_read(handle.device, (-1), &ch, 1) != 1)
{
rt_sem_take(&(handle.rx_sem), RT_WAITING_FOREVER);
}
return ch;
}
static rt_err_t llm_rxcb(rt_device_t dev, rt_size_t size)
{
return rt_sem_release(&(handle.rx_sem));
}
static rt_bool_t llm_handle_history(const char *prompt)
{
rt_kprintf("\033[2K\r");
rt_kprintf("%s%s", prompt, handle.line);
return RT_FALSE;
}
static void llm_push_history(void)
{
if (handle.line_position > 0)
{
if (handle.history_count >= LLM_HISTORY_LINES)
{
if (rt_memcmp(&handle.llm_history[LLM_HISTORY_LINES - 1], handle.line, PKG_LLM_CMD_BUFFER_SIZE))
{
int index;
for (index = 0; index < FINSH_HISTORY_LINES - 1; index++)
{
rt_memcpy(&handle.llm_history[index][0], &handle.llm_history[index + 1][0], PKG_LLM_CMD_BUFFER_SIZE);
}
rt_memset(&handle.llm_history[index][0], 0, PKG_LLM_CMD_BUFFER_SIZE);
rt_memcpy(&handle.llm_history[index][0], handle.line, handle.line_position);
handle.history_count = LLM_HISTORY_LINES;
}
}
else
{
if (handle.history_count == 0 || rt_memcmp(&handle.llm_history[handle.history_count - 1], handle.line, PKG_LLM_CMD_BUFFER_SIZE))
{
handle.history_current = handle.history_count;
rt_memset(&handle.llm_history[handle.history_count][0], 0, PKG_LLM_CMD_BUFFER_SIZE);
rt_memcpy(&handle.llm_history[handle.history_count][0], handle.line, handle.line_position);
handle.history_count++;
}
}
}
handle.history_current = handle.history_count;
}
int llm_readline(const char *prompt, char *buffer, int buffer_size)
{
rt_uint8_t ch;
start:
rt_kprintf(prompt);
while (1)
{
ch = llm_getc();
if (ch == 0x1b)
{
handle.stat = LLM_WAIT_SPEC_KEY;
continue;
}
else if (handle.stat == LLM_WAIT_SPEC_KEY)
{
if (ch == 0x5b)
{
handle.stat = LLM_WAIT_FUNC_KEY;
continue;
}
handle.stat = LLM_WAIT_NORMAL;
}
else if (handle.stat == LLM_WAIT_FUNC_KEY)
{
handle.stat = LLM_WAIT_NORMAL;
if (ch == 0x41)
{
if (handle.history_current > 0)
{
handle.history_current--;
}
else
{
handle.history_current = 0;
continue;
}
rt_memcpy(handle.line, &handle.llm_history[handle.history_current][0], PKG_LLM_CMD_BUFFER_SIZE);
handle.line_curpos = handle.line_position = rt_strlen(handle.line);
llm_handle_history(prompt);
continue;
}
else if (ch == 0x42)
{
if (handle.history_current < (handle.history_count - 1))
{
handle.history_current++;
}
else
{
if (handle.history_count != 0)
{
handle.history_current = handle.history_count - 1;
}
else
{
continue;
}
}
rt_memcpy(handle.line, &handle.llm_history[handle.history_current][0], PKG_LLM_CMD_BUFFER_SIZE);
handle.line_curpos = handle.line_position = rt_strlen(handle.line);
llm_handle_history(prompt);
continue;
}
else if (ch == 0x44)
{
if (handle.line_curpos)
{
rt_kprintf("\b");
handle.line_curpos--;
}
continue;
}
else if (ch == 0x43)
{
if (handle.line_curpos < handle.line_position)
{
rt_kprintf("%c", handle.line[handle.line_curpos]);
handle.line_curpos++;
}
continue;
}
}
if (ch == '\0' || ch == 0xFF)
{
continue;
}
else if (ch == 0x7f || ch == 0x08)
{
if (handle.line_curpos == 0)
{
continue;
}
handle.line_curpos--;
handle.line_position--;
if (handle.line_position > handle.line_curpos)
{
rt_memmove(&handle.line[handle.line_curpos], &handle.line[handle.line_curpos + 1],
handle.line_position - handle.line_curpos);
handle.line[handle.line_position] = 0;
rt_kprintf("\b%s \b", &handle.line[handle.line_curpos]);
int index;
for (index = (handle.line_curpos); index <= (handle.line_position); index++)
{
rt_kprintf("\b");
}
}
else
{
rt_kprintf("\b \b");
handle.line[handle.line_position] = 0;
}
continue;
}
else if (ch == '\r' || ch == '\n')
{
llm_push_history();
rt_kprintf("\n");
if (handle.line_position == 0)
{
goto start;
}
else
{
rt_uint8_t temp = handle.line_position;
rt_strncpy(buffer, handle.line, handle.line_position);
rt_memset(handle.line, 0x00, sizeof(handle.line));
buffer[handle.line_position] = 0;
handle.line_curpos = handle.line_position = 0;
return temp;
}
}
else if (ch == 0x04)
{
if (handle.line_position == 0)
{
return 0;
}
else
{
continue;
}
}
else if (ch == '\t')
{
continue;
}
if (handle.line_position >= PKG_LLM_CMD_BUFFER_SIZE)
{
continue;
}
if (handle.line_curpos < handle.line_position)
{
rt_memmove(&handle.line[handle.line_curpos + 1], &handle.line[handle.line_curpos],
handle.line_position - handle.line_curpos);
handle.line[handle.line_curpos] = ch;
rt_kprintf("%s", &handle.line[handle.line_curpos]);
int index;
for (index = (handle.line_curpos); index < (handle.line_position); index++)
{
rt_kprintf("\b");
}
}
else
{
handle.line[handle.line_position] = ch;
rt_kprintf("%c", ch);
}
ch = 0;
handle.line_curpos++;
handle.line_position++;
}
}
static void llm_run(void *p)
{
char input_buffer[PKG_LLM_CMD_BUFFER_SIZE] = {0};
const char *device_name = RT_CONSOLE_DEVICE_NAME;
handle.device = rt_device_find(device_name);
if (handle.device == RT_NULL)
{
LLM_DBG("The msh device find failed.\n");
return;
}
handle.rx_indicate = handle.device->rx_indicate;
rt_device_set_rx_indicate(handle.device, llm_rxcb);
if (handle.argc == 1)
{
rt_kprintf("\nPress CTRL+D to exit llm shell.\n");
}
while (1)
{
int length = llm_readline("Enter command: ", input_buffer, PKG_LLM_CMD_BUFFER_SIZE);
if (length == 0)
{
rt_kprintf("Exit terminal.\n");
break;
}
else if (length > 0)
{
#ifdef PKG_LLMCHAT_HISTORY_PAYLOAD
add_message2messages(input_buffer, "user", &handle);
char *result = handle.get_answer(handle.messages);
add_message2messages(result, "assistant", &handle);
#else
add_message2messages(input_buffer, "user", &handle);
char *result = handle.get_answer(handle.messages);
rt_free(result);
clear_messages(&handle);
#endif
}
else
{
rt_kprintf("No valid input.\n");
}
rt_memset(input_buffer, 0, sizeof(input_buffer));
}
rt_sem_detach(&(handle.rx_sem));
rt_device_set_rx_indicate(handle.device, handle.rx_indicate);
rt_kprintf(FINSH_PROMPT);
}
static int llm2rtt(int argc, char **argv)
{
static rt_bool_t history_init = RT_FALSE;
if (history_init == RT_FALSE)
{
rt_memset(&handle, 0x00, sizeof(struct llm_obj));
history_init = RT_TRUE;
}
else
{
handle.stat = LLM_WAIT_NORMAL;
handle.argc = 0;
rt_memset(handle.line, 0x00, PKG_LLM_CMD_BUFFER_SIZE);
handle.line_position = 0;
handle.line_curpos = 0;
handle.device = RT_NULL;
handle.rx_indicate = RT_NULL;
}
rt_sem_init(&(handle.rx_sem), "llm_rxsem", 0, RT_IPC_FLAG_FIFO);
handle.argc = argc;
handle.get_answer = get_llm_answer;
if (!cJSON_IsArray(handle.messages))
{
handle.messages = cJSON_CreateArray();
}
#if defined(RT_VERSION_CHECK) && (RTTHREAD_VERSION >= RT_VERSION_CHECK(5, 1, 0))
rt_uint8_t prio = RT_SCHED_PRIV(rt_thread_self()).current_priority + 1;
#else
rt_uint8_t prio = rt_thread_self()->current_priority + 1;
#endif
rt_err_t result = rt_thread_init(&handle.thread,
"llm_td",
llm_run, RT_NULL,
&handle.thread_stack[0], sizeof(handle.thread_stack),
prio, 10);
if (result != RT_EOK)
{
rt_sem_detach(&(handle.rx_sem));
LLM_DBG("The llm interpreter thread create failed.\n");
return RT_ERROR;
}
rt_thread_startup(&handle.thread);
return RT_EOK;
}
MSH_CMD_EXPORT_ALIAS(llm2rtt, llm, llm Interactive Terminal.);