-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChatLLM.cs
126 lines (100 loc) · 4.69 KB
/
ChatLLM.cs
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
using System;
using System.Runtime.InteropServices;
public class ChatLLM
{
[DllImport("libchatllm", CallingConvention = CallingConvention.StdCall)]
private static extern IntPtr chatllm_create();
[DllImport("libchatllm", CallingConvention = CallingConvention.StdCall)]
private static extern void chatllm_append_param(IntPtr obj, string param);
[DllImport("libchatllm", CallingConvention = CallingConvention.StdCall)]
private static extern int chatllm_start(IntPtr obj, PrintCallback printCallback, EndCallback endCallback, IntPtr userData);
[DllImport("libchatllm", CallingConvention = CallingConvention.StdCall)]
private static extern void chatllm_set_gen_max_tokens(IntPtr obj, int genMaxTokens);
[DllImport("libchatllm", CallingConvention = CallingConvention.StdCall)]
private static extern void chatllm_restart(IntPtr obj, string sysPrompt);
[DllImport("libchatllm", CallingConvention = CallingConvention.StdCall)]
private static extern int chatllm_user_input(IntPtr obj, string input);
[DllImport("libchatllm", CallingConvention = CallingConvention.StdCall)]
private static extern int chatllm_set_ai_prefix(IntPtr obj, string prefix);
[DllImport("libchatllm", CallingConvention = CallingConvention.StdCall)]
private static extern int chatllm_tool_input(IntPtr obj, string input);
[DllImport("libchatllm", CallingConvention = CallingConvention.StdCall)]
private static extern int chatllm_tool_completion(IntPtr obj, string completion);
[DllImport("libchatllm", CallingConvention = CallingConvention.StdCall)]
private static extern int chatllm_text_tokenize(IntPtr obj, string text);
[DllImport("libchatllm", CallingConvention = CallingConvention.StdCall)]
private static extern int chatllm_text_embedding(IntPtr obj, string text);
[DllImport("libchatllm", CallingConvention = CallingConvention.StdCall)]
private static extern int chatllm_qa_rank(IntPtr obj, string question, string answer);
[DllImport("libchatllm", CallingConvention = CallingConvention.StdCall)]
private static extern int chatllm_rag_select_store(IntPtr obj, string storeName);
[DllImport("libchatllm", CallingConvention = CallingConvention.StdCall)]
private static extern void chatllm_abort_generation(IntPtr obj);
[DllImport("libchatllm", CallingConvention = CallingConvention.StdCall)]
private static extern void chatllm_show_statistics(IntPtr obj);
[DllImport("libchatllm", CallingConvention = CallingConvention.StdCall)]
private static extern int chatllm_save_session(IntPtr obj, string fileName);
[DllImport("libchatllm", CallingConvention = CallingConvention.StdCall)]
private static extern int chatllm_load_session(IntPtr obj, string fileName);
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
private delegate void PrintCallback(IntPtr userData, int printType, string utf8Str);
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
private delegate void EndCallback(IntPtr userData);
private enum PrintType
{
PRINT_CHAT_CHUNK = 0,
PRINTLN_META = 1,
PRINTLN_ERROR = 2,
PRINTLN_REF = 3,
PRINTLN_REWRITTEN_QUERY = 4,
PRINTLN_HISTORY_USER = 5,
PRINTLN_HISTORY_AI = 6,
PRINTLN_TOOL_CALLING = 7,
PRINTLN_EMBEDDING = 8,
PRINTLN_RANKING = 9,
PRINTLN_TOKEN_IDS = 10,
}
private static void ChatLLMPrint(IntPtr userData, int printType, string utf8Str)
{
switch (printType)
{
case (int)PrintType.PRINT_CHAT_CHUNK:
Console.Write(utf8Str);
break;
default:
Console.WriteLine(utf8Str);
break;
}
}
private static void ChatLLMEnd(IntPtr userData)
{
Console.WriteLine();
}
public static void Main(string[] args)
{
IntPtr obj = chatllm_create();
foreach (string arg in args)
{
chatllm_append_param(obj, arg);
}
int r = chatllm_start(obj, ChatLLMPrint, ChatLLMEnd, IntPtr.Zero);
if (r != 0)
{
Console.WriteLine(">>> chatllm_start error: " + r);
return;
}
while (true)
{
Console.Write("You > ");
string input = Console.ReadLine();
if (string.IsNullOrEmpty(input)) continue;
Console.Write("A.I. > ");
r = chatllm_user_input(obj, input);
if (r != 0)
{
Console.WriteLine(">>> chatllm_user_input error: " + r);
break;
}
}
}
}