@@ -6,44 +6,189 @@ import { INestiaChatAgent } from "./structures/INestiaChatAgent";
6
6
import { INestiaChatEvent } from "./structures/INestiaChatEvent" ;
7
7
import { INestiaChatPrompt } from "./structures/INestiaChatPrompt" ;
8
8
9
+ /**
10
+ * Nestia A.I. chatbot agent.
11
+ *
12
+ * `NestiaChatAgent` is a facade class for the A.I. chatbot agent
13
+ * which performs the {@link converstate user's conversation function}
14
+ * with LLM (Large Language Model) function calling and manages the
15
+ * {@link getHistories prompt histories}.
16
+ *
17
+ * @author Jeongho Nam - https://github.com/samchon
18
+ */
9
19
export class NestiaChatAgent implements INestiaChatAgent {
20
+ /**
21
+ * @hidden
22
+ */
10
23
private readonly agent : INestiaChatAgent ;
11
24
25
+ /**
26
+ * Initializer constructor.
27
+ *
28
+ * @param props Properties to construct the agent
29
+ */
12
30
public constructor ( props : NestiaChatAgent . IProps ) {
13
31
this . agent = new ChatGptAgent ( props ) ;
14
32
}
15
33
34
+ /**
35
+ * Conversate with the A.I. chatbot.
36
+ *
37
+ * User talks to the A.I. chatbot with the content.
38
+ *
39
+ * When the user's conversation implies the A.I. chatbot to execute a
40
+ * function calling, the returned chat prompts will contain the
41
+ * function calling information like {@link INestiaChatPrompt.IExecute}.
42
+ *
43
+ * @param content The content to talk
44
+ * @returns List of newly created chat prompts
45
+ */
16
46
public conversate ( content : string ) : Promise < INestiaChatPrompt [ ] > {
17
47
return this . agent . conversate ( content ) ;
18
48
}
19
49
50
+ /**
51
+ * Get the chatbot's history.
52
+ *
53
+ * Get list of chat prompts that the chatbot has been conversated.
54
+ *
55
+ * @returns List of chat prompts
56
+ */
20
57
public getHistories ( ) : INestiaChatPrompt [ ] {
21
58
return this . agent . getHistories ( ) ;
22
59
}
23
60
61
+ /**
62
+ * Add an event listener.
63
+ *
64
+ * Add an event listener to be called whenever the event is emitted.
65
+ *
66
+ * @param type Type of event
67
+ * @param listener Callback function to be called whenever the event is emitted
68
+ */
24
69
public on < Type extends INestiaChatEvent . Type > (
25
70
type : Type ,
26
- listener : ( event : INestiaChatEvent . Mapper [ Type ] ) => void ,
71
+ listener : ( event : INestiaChatEvent . Mapper [ Type ] ) => void | Promise < void > ,
27
72
) : void {
28
73
this . agent . on ( type , listener ) ;
29
74
}
75
+
76
+ /**
77
+ * Erase an event listener.
78
+ *
79
+ * Erase an event listener to stop calling the callback function.
80
+ *
81
+ * @param type Type of event
82
+ * @param listener Callback function to erase
83
+ */
84
+ public off < Type extends INestiaChatEvent . Type > (
85
+ type : Type ,
86
+ listener : ( event : INestiaChatEvent . Mapper [ Type ] ) => void | Promise < void > ,
87
+ ) : void {
88
+ this . agent . off ( type , listener ) ;
89
+ }
30
90
}
31
91
export namespace NestiaChatAgent {
92
+ /**
93
+ * Properties of the A.I. chatbot agent.
94
+ */
32
95
export interface IProps {
96
+ /**
97
+ * Application instance for LLM function calling.
98
+ */
33
99
application : IHttpLlmApplication < "chatgpt" > ;
100
+
101
+ /**
102
+ * Service of the ChatGPT (OpenAI) API.
103
+ */
34
104
service : IChatGptService ;
105
+
106
+ /**
107
+ * HTTP connection to the backend server.
108
+ */
35
109
connection : IHttpConnection ;
110
+
111
+ /**
112
+ * Initial chat prompts.
113
+ *
114
+ * If you configure this property, the chatbot will start the
115
+ * pre-defined conversations.
116
+ */
36
117
histories ?: INestiaChatPrompt [ ] | undefined ;
118
+
119
+ /**
120
+ * Configuration for the A.I. chatbot.
121
+ */
37
122
config ?: IConfig | undefined ;
38
123
}
39
124
125
+ /**
126
+ * Configuration for the A.I. chatbot.
127
+ */
40
128
export interface IConfig {
129
+ /**
130
+ * Retry count.
131
+ *
132
+ * If LLM function calling composed arguments are invalid,
133
+ * the A.I. chatbot will retry to call the function with
134
+ * the modified arguments.
135
+ *
136
+ * By the way, if you configure it to 0 or 1, the A.I. chatbot
137
+ * will not retry the LLM function calling for correcting the
138
+ * arguments.
139
+ *
140
+ * @default 3
141
+ */
41
142
retry ?: number ;
143
+
144
+ /**
145
+ * Capacity of the LLM function selecting.
146
+ *
147
+ * When the A.I. chatbot selects a proper function to call, if the
148
+ * number of functions registered in the {@link IProps.application}
149
+ * is too much greater, the A.I. chatbot often fallen into the
150
+ * hallucination.
151
+ *
152
+ * In that case, if you configure this property value, `NestiaChatAgent`
153
+ * will divide the functions into the several groups with the configured
154
+ * capacity and select proper functions to call by operating the multiple
155
+ * LLM function selecting agents parallelly.
156
+ *
157
+ * @default 0
158
+ */
42
159
capacity ?: number ;
160
+
161
+ /**
162
+ * Eliticism for the LLM function selecting.
163
+ *
164
+ * If you configure {@link capacity}, the A.I. chatbot will complete
165
+ * the candidate functions to call which are selected by the multiple
166
+ * LLM function selecting agents.
167
+ *
168
+ * Otherwise you configure this property as `false`, the A.I. chatbot
169
+ * will not complete the candidate functions to call and just accept
170
+ * every candidate functions to call which are selected by the multiple
171
+ * LLM function selecting agents.
172
+ *
173
+ * @default true
174
+ */
43
175
eliticism ?: boolean ;
176
+
177
+ /**
178
+ * System prompt messages.
179
+ *
180
+ * System prompt messages if you want to customize the system prompt
181
+ * messages for each situation.
182
+ */
44
183
systemPrompt ?: Partial < ISytemPrompt > ;
45
184
}
46
185
186
+ /**
187
+ * System prompt messages.
188
+ *
189
+ * System prompt messages if you want to customize the system prompt
190
+ * messages for each situation.
191
+ */
47
192
export interface ISytemPrompt {
48
193
initial ?: ( histories : INestiaChatPrompt [ ] ) => string ;
49
194
select ?: ( histories : INestiaChatPrompt [ ] ) => string ;
0 commit comments