Skip to content

Commit 15e8aa4

Browse files
committed
Documentation of @nestia/agent
1 parent 5903fc8 commit 15e8aa4

7 files changed

+357
-6
lines changed

packages/agent/src/NestiaChatAgent.ts

+146-1
Original file line numberDiff line numberDiff line change
@@ -6,44 +6,189 @@ import { INestiaChatAgent } from "./structures/INestiaChatAgent";
66
import { INestiaChatEvent } from "./structures/INestiaChatEvent";
77
import { INestiaChatPrompt } from "./structures/INestiaChatPrompt";
88

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+
*/
919
export class NestiaChatAgent implements INestiaChatAgent {
20+
/**
21+
* @hidden
22+
*/
1023
private readonly agent: INestiaChatAgent;
1124

25+
/**
26+
* Initializer constructor.
27+
*
28+
* @param props Properties to construct the agent
29+
*/
1230
public constructor(props: NestiaChatAgent.IProps) {
1331
this.agent = new ChatGptAgent(props);
1432
}
1533

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+
*/
1646
public conversate(content: string): Promise<INestiaChatPrompt[]> {
1747
return this.agent.conversate(content);
1848
}
1949

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+
*/
2057
public getHistories(): INestiaChatPrompt[] {
2158
return this.agent.getHistories();
2259
}
2360

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+
*/
2469
public on<Type extends INestiaChatEvent.Type>(
2570
type: Type,
26-
listener: (event: INestiaChatEvent.Mapper[Type]) => void,
71+
listener: (event: INestiaChatEvent.Mapper[Type]) => void | Promise<void>,
2772
): void {
2873
this.agent.on(type, listener);
2974
}
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+
}
3090
}
3191
export namespace NestiaChatAgent {
92+
/**
93+
* Properties of the A.I. chatbot agent.
94+
*/
3295
export interface IProps {
96+
/**
97+
* Application instance for LLM function calling.
98+
*/
3399
application: IHttpLlmApplication<"chatgpt">;
100+
101+
/**
102+
* Service of the ChatGPT (OpenAI) API.
103+
*/
34104
service: IChatGptService;
105+
106+
/**
107+
* HTTP connection to the backend server.
108+
*/
35109
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+
*/
36117
histories?: INestiaChatPrompt[] | undefined;
118+
119+
/**
120+
* Configuration for the A.I. chatbot.
121+
*/
37122
config?: IConfig | undefined;
38123
}
39124

125+
/**
126+
* Configuration for the A.I. chatbot.
127+
*/
40128
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+
*/
41142
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+
*/
42159
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+
*/
43175
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+
*/
44183
systemPrompt?: Partial<ISytemPrompt>;
45184
}
46185

186+
/**
187+
* System prompt messages.
188+
*
189+
* System prompt messages if you want to customize the system prompt
190+
* messages for each situation.
191+
*/
47192
export interface ISytemPrompt {
48193
initial?: (histories: INestiaChatPrompt[]) => string;
49194
select?: (histories: INestiaChatPrompt[]) => string;

packages/agent/src/chatgpt/ChatGptAgent.ts

+11
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,17 @@ export class ChatGptAgent implements INestiaChatAgent {
146146
take(this.listeners_, type, () => new Set()).add(listener);
147147
}
148148

149+
public off<Type extends INestiaChatEvent.Type>(
150+
type: Type,
151+
listener: (event: INestiaChatEvent.Mapper[Type]) => void,
152+
): void {
153+
const set: Set<Function> | undefined = this.listeners_.get(type);
154+
if (set) {
155+
set.delete(listener);
156+
if (set.size === 0) this.listeners_.delete(type);
157+
}
158+
}
159+
149160
private async dispatch<Event extends INestiaChatEvent>(
150161
event: Event,
151162
): Promise<void> {
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,21 @@
11
import OpenAI from "openai";
22

3+
/**
4+
* Service of the ChatGPT (OpenAI) API.
5+
*/
36
export interface IChatGptService {
7+
/**
8+
* OpenAI API instance.
9+
*/
410
api: OpenAI;
11+
12+
/**
13+
* Chat model to be used.
14+
*/
515
model: OpenAI.ChatModel;
16+
17+
/**
18+
* Options for the request.
19+
*/
620
options?: OpenAI.RequestOptions | undefined;
721
}

packages/agent/src/structures/INestiaChatAgent.ts

+8-1
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,16 @@ import { INestiaChatPrompt } from "./INestiaChatPrompt";
33

44
export interface INestiaChatAgent {
55
conversate(content: string): Promise<INestiaChatPrompt[]>;
6+
67
getHistories(): INestiaChatPrompt[];
8+
79
on<Type extends INestiaChatEvent.Type>(
810
type: Type,
9-
listener: (event: INestiaChatEvent.Mapper[Type]) => void,
11+
listener: (event: INestiaChatEvent.Mapper[Type]) => void | Promise<void>,
12+
): void;
13+
14+
off<Type extends INestiaChatEvent.Type>(
15+
type: Type,
16+
listener: (event: INestiaChatEvent.Mapper[Type]) => void | Promise<void>,
1017
): void;
1118
}

packages/agent/src/structures/INestiaChatEvent.ts

+78
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,116 @@
11
import { IHttpLlmFunction, IHttpResponse } from "@samchon/openapi";
22

3+
/**
4+
* Nestia A.I. chatbot event.
5+
*
6+
* `INestiaChatEvent` is an union type of all possible events that can
7+
* be emitted by the A.I. chatbot of the {@link NestiaChatAgent} class.
8+
*
9+
* @author Jeongho Nam - https://github.com/samchon
10+
*/
311
export type INestiaChatEvent =
412
| INestiaChatEvent.IIintializeEvent
513
| INestiaChatEvent.ISelectFunctionEvent
614
| INestiaChatEvent.ICancelFunctionEvent
715
| INestiaChatEvent.ICallFunctionEvent
816
| INestiaChatEvent.IFunctionCompleteEvent;
917
export namespace INestiaChatEvent {
18+
/**
19+
* Event of initializing the chatbot.
20+
*/
1021
export interface IIintializeEvent {
1122
type: "initialize";
1223
}
1324

25+
/**
26+
* Event of selecting a function to call.
27+
*/
1428
export interface ISelectFunctionEvent {
1529
type: "select";
30+
31+
/**
32+
* Selected function.
33+
*
34+
* Function that has been selected to prepare LLM function calling.
35+
*/
1636
function: IHttpLlmFunction<"chatgpt">;
37+
38+
/**
39+
* Reason of selecting the function.
40+
*
41+
* The A.I. chatbot will fill this property describing why the function
42+
* has been selected.
43+
*/
1744
reason: string;
1845
}
1946

47+
/**
48+
* Event of canceling a function calling.
49+
*/
2050
export interface ICancelFunctionEvent {
2151
type: "cancel";
52+
53+
/**
54+
* Selected function to cancel.
55+
*
56+
* Function that has been selected to prepare LLM function calling,
57+
* but canceled due to no more required.
58+
*/
2259
function: IHttpLlmFunction<"chatgpt">;
60+
61+
/**
62+
* Reason of selecting the function.
63+
*
64+
* The A.I. chatbot will fill this property describing why the function
65+
* has been cancelled.
66+
*
67+
* For reference, if the A.I. chatbot successfully completes the LLM
68+
* function calling, the reason of the fnction cancellation will be
69+
* "complete".
70+
*/
2371
reason: string;
2472
}
2573

74+
/**
75+
* Event of calling a function.
76+
*/
2677
export interface ICallFunctionEvent {
2778
type: "call";
79+
80+
/**
81+
* Target function to call.
82+
*/
2883
function: IHttpLlmFunction<"chatgpt">;
84+
85+
/**
86+
* Arguments of the function calling.
87+
*
88+
* If you modify this {@link arguments} property, it actually modifies
89+
* the backend server's request. Therefore, be careful when you're
90+
* trying to modify this property.
91+
*/
2992
arguments: object;
3093
}
3194

95+
/**
96+
* Event of completing a function calling.
97+
*/
3298
export interface IFunctionCompleteEvent {
3399
type: "complete";
100+
101+
/**
102+
* Target funtion that has been called.
103+
*/
34104
function: IHttpLlmFunction<"chatgpt">;
105+
106+
/**
107+
* Arguments of the function calling.
108+
*/
35109
arguments: object;
110+
111+
/**
112+
* Response of the function calling.
113+
*/
36114
response: IHttpResponse;
37115
}
38116

Original file line numberDiff line numberDiff line change
@@ -1,6 +1,21 @@
11
import { IHttpLlmFunction } from "@samchon/openapi";
22

3+
/**
4+
* Nestia A.I. chatbot function selection.
5+
*
6+
* @author Jeongho Nam - https://github.com/samchon
7+
*/
38
export interface INestiaChatFunctionSelection {
9+
/**
10+
* Target function.
11+
*
12+
* Function that has been selected to prepare LLM function calling,
13+
* or canceled due to no more required.
14+
*/
415
function: IHttpLlmFunction<"chatgpt">;
16+
17+
/**
18+
* The reason of the function selection or cancellation.
19+
*/
520
reason: string;
621
}

0 commit comments

Comments
 (0)