forked from Rbb666/llm_chat
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathllm.h
More file actions
74 lines (60 loc) · 1.7 KB
/
llm.h
File metadata and controls
74 lines (60 loc) · 1.7 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
/*
* 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/01 Rbb666 Add history support
* 2025/02/10 CXSforHPU Add llm history support
*/
#ifndef __LLM_H_
#define __LLM_H_
#include <rtthread.h>
#include <stdio.h>
#include <string.h>
#include <cJSON.h>
#ifndef PKG_LLMCHAT_DBG
#define LLM_DBG(fmt, ...)
#else
#define LLM_DBG(fmt, ...) \
do \
{ \
rt_kprintf("[\033[32mLLM\033[0m] "); \
rt_kprintf(fmt, ##__VA_ARGS__); \
} while (0)
#endif
#ifndef LLM_HISTORY_LINES
#define LLM_HISTORY_LINES 5
#endif
enum llm_input_stat
{
LLM_WAIT_NORMAL,
LLM_WAIT_SPEC_KEY,
LLM_WAIT_FUNC_KEY,
};
struct llm_obj
{
struct rt_thread thread;
rt_uint8_t thread_stack[PKG_LLM_THREAD_STACK_SIZE];
struct rt_semaphore rx_sem;
enum llm_input_stat stat;
int argc;
char llm_history[LLM_HISTORY_LINES][PKG_LLM_CMD_BUFFER_SIZE];
rt_uint16_t history_count;
rt_uint16_t history_current;
char line[PKG_LLM_CMD_BUFFER_SIZE];
rt_uint16_t line_position;
rt_uint8_t line_curpos;
rt_device_t device;
rt_err_t (*rx_indicate)(rt_device_t dev, rt_size_t size);
cJSON *messages;
char *(*get_answer)(cJSON *messages);
};
typedef struct llm_obj *llm_t;
char *get_llm_answer(cJSON *messages);
void add_message2messages(const char *input_buffer, char *role, struct llm_obj *handle);
cJSON *create_message(const char *input_buffer, char *role);
void clear_messages(struct llm_obj *handle);
#endif