@agentica/core 0.7.0-dev.20250224
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +464 -0
- package/lib/Agentica.d.ts +124 -0
- package/lib/Agentica.js +271 -0
- package/lib/Agentica.js.map +1 -0
- package/lib/chatgpt/ChatGptAgent.d.ts +6 -0
- package/lib/chatgpt/ChatGptAgent.js +59 -0
- package/lib/chatgpt/ChatGptAgent.js.map +1 -0
- package/lib/chatgpt/ChatGptCallFunctionAgent.d.ts +5 -0
- package/lib/chatgpt/ChatGptCallFunctionAgent.js +362 -0
- package/lib/chatgpt/ChatGptCallFunctionAgent.js.map +1 -0
- package/lib/chatgpt/ChatGptCancelFunctionAgent.d.ts +8 -0
- package/lib/chatgpt/ChatGptCancelFunctionAgent.js +359 -0
- package/lib/chatgpt/ChatGptCancelFunctionAgent.js.map +1 -0
- package/lib/chatgpt/ChatGptDescribeFunctionAgent.d.ts +5 -0
- package/lib/chatgpt/ChatGptDescribeFunctionAgent.js +56 -0
- package/lib/chatgpt/ChatGptDescribeFunctionAgent.js.map +1 -0
- package/lib/chatgpt/ChatGptHistoryDecoder.d.ts +5 -0
- package/lib/chatgpt/ChatGptHistoryDecoder.js +77 -0
- package/lib/chatgpt/ChatGptHistoryDecoder.js.map +1 -0
- package/lib/chatgpt/ChatGptInitializeFunctionAgent.d.ts +5 -0
- package/lib/chatgpt/ChatGptInitializeFunctionAgent.js +1883 -0
- package/lib/chatgpt/ChatGptInitializeFunctionAgent.js.map +1 -0
- package/lib/chatgpt/ChatGptSelectFunctionAgent.d.ts +5 -0
- package/lib/chatgpt/ChatGptSelectFunctionAgent.js +381 -0
- package/lib/chatgpt/ChatGptSelectFunctionAgent.js.map +1 -0
- package/lib/functional/createHttpLlmApplication.d.ts +33 -0
- package/lib/functional/createHttpLlmApplication.js +7766 -0
- package/lib/functional/createHttpLlmApplication.js.map +1 -0
- package/lib/index.d.ts +16 -0
- package/lib/index.js +35 -0
- package/lib/index.js.map +1 -0
- package/lib/index.mjs +10366 -0
- package/lib/index.mjs.map +1 -0
- package/lib/internal/AgenticaConstant.d.ts +4 -0
- package/lib/internal/AgenticaConstant.js +9 -0
- package/lib/internal/AgenticaConstant.js.map +1 -0
- package/lib/internal/AgenticaCostAggregator.d.ts +5 -0
- package/lib/internal/AgenticaCostAggregator.js +30 -0
- package/lib/internal/AgenticaCostAggregator.js.map +1 -0
- package/lib/internal/AgenticaDefaultPrompt.d.ts +4 -0
- package/lib/internal/AgenticaDefaultPrompt.js +32 -0
- package/lib/internal/AgenticaDefaultPrompt.js.map +1 -0
- package/lib/internal/AgenticaOperationComposer.d.ts +9 -0
- package/lib/internal/AgenticaOperationComposer.js +58 -0
- package/lib/internal/AgenticaOperationComposer.js.map +1 -0
- package/lib/internal/AgenticaPromptFactory.d.ts +6 -0
- package/lib/internal/AgenticaPromptFactory.js +9 -0
- package/lib/internal/AgenticaPromptFactory.js.map +1 -0
- package/lib/internal/AgenticaPromptTransformer.d.ts +9 -0
- package/lib/internal/AgenticaPromptTransformer.js +58 -0
- package/lib/internal/AgenticaPromptTransformer.js.map +1 -0
- package/lib/internal/AgenticaSystemPrompt.d.ts +8 -0
- package/lib/internal/AgenticaSystemPrompt.js +13 -0
- package/lib/internal/AgenticaSystemPrompt.js.map +1 -0
- package/lib/internal/MathUtil.d.ts +3 -0
- package/lib/internal/MathUtil.js +8 -0
- package/lib/internal/MathUtil.js.map +1 -0
- package/lib/internal/Singleton.d.ts +1 -0
- package/lib/internal/Singleton.js +23 -0
- package/lib/internal/Singleton.js.map +1 -0
- package/lib/internal/__map_take.d.ts +1 -0
- package/lib/internal/__map_take.js +16 -0
- package/lib/internal/__map_take.js.map +1 -0
- package/lib/structures/IAgenticaConfig.d.ts +112 -0
- package/lib/structures/IAgenticaConfig.js +3 -0
- package/lib/structures/IAgenticaConfig.js.map +1 -0
- package/lib/structures/IAgenticaContext.d.ts +106 -0
- package/lib/structures/IAgenticaContext.js +3 -0
- package/lib/structures/IAgenticaContext.js.map +1 -0
- package/lib/structures/IAgenticaController.d.ts +110 -0
- package/lib/structures/IAgenticaController.js +3 -0
- package/lib/structures/IAgenticaController.js.map +1 -0
- package/lib/structures/IAgenticaEvent.d.ts +191 -0
- package/lib/structures/IAgenticaEvent.js +3 -0
- package/lib/structures/IAgenticaEvent.js.map +1 -0
- package/lib/structures/IAgenticaExecutor.d.ts +144 -0
- package/lib/structures/IAgenticaExecutor.js +3 -0
- package/lib/structures/IAgenticaExecutor.js.map +1 -0
- package/lib/structures/IAgenticaOperation.d.ts +48 -0
- package/lib/structures/IAgenticaOperation.js +3 -0
- package/lib/structures/IAgenticaOperation.js.map +1 -0
- package/lib/structures/IAgenticaOperationCollection.d.ts +46 -0
- package/lib/structures/IAgenticaOperationCollection.js +3 -0
- package/lib/structures/IAgenticaOperationCollection.js.map +1 -0
- package/lib/structures/IAgenticaOperationSelection.d.ts +51 -0
- package/lib/structures/IAgenticaOperationSelection.js +3 -0
- package/lib/structures/IAgenticaOperationSelection.js.map +1 -0
- package/lib/structures/IAgenticaPrompt.d.ts +139 -0
- package/lib/structures/IAgenticaPrompt.js +3 -0
- package/lib/structures/IAgenticaPrompt.js.map +1 -0
- package/lib/structures/IAgenticaProps.d.ts +59 -0
- package/lib/structures/IAgenticaProps.js +3 -0
- package/lib/structures/IAgenticaProps.js.map +1 -0
- package/lib/structures/IAgenticaProvider.d.ts +41 -0
- package/lib/structures/IAgenticaProvider.js +3 -0
- package/lib/structures/IAgenticaProvider.js.map +1 -0
- package/lib/structures/IAgenticaSystemPrompt.d.ts +116 -0
- package/lib/structures/IAgenticaSystemPrompt.js +3 -0
- package/lib/structures/IAgenticaSystemPrompt.js.map +1 -0
- package/lib/structures/IAgenticaTokenUsage.d.ts +50 -0
- package/lib/structures/IAgenticaTokenUsage.js +3 -0
- package/lib/structures/IAgenticaTokenUsage.js.map +1 -0
- package/lib/structures/internal/__IChatCancelFunctionsApplication.d.ts +22 -0
- package/lib/structures/internal/__IChatCancelFunctionsApplication.js +3 -0
- package/lib/structures/internal/__IChatCancelFunctionsApplication.js.map +1 -0
- package/lib/structures/internal/__IChatFunctionReference.d.ts +20 -0
- package/lib/structures/internal/__IChatFunctionReference.js +3 -0
- package/lib/structures/internal/__IChatFunctionReference.js.map +1 -0
- package/lib/structures/internal/__IChatInitialApplication.d.ts +14 -0
- package/lib/structures/internal/__IChatInitialApplication.js +3 -0
- package/lib/structures/internal/__IChatInitialApplication.js.map +1 -0
- package/lib/structures/internal/__IChatSelectFunctionsApplication.d.ts +23 -0
- package/lib/structures/internal/__IChatSelectFunctionsApplication.js +3 -0
- package/lib/structures/internal/__IChatSelectFunctionsApplication.js.map +1 -0
- package/lib/typings/AgenticaSource.d.ts +1 -0
- package/lib/typings/AgenticaSource.js +3 -0
- package/lib/typings/AgenticaSource.js.map +1 -0
- package/package.json +74 -0
- package/prompts/cancel.md +5 -0
- package/prompts/common.md +3 -0
- package/prompts/describe.md +7 -0
- package/prompts/execute.md +7 -0
- package/prompts/initialize.md +3 -0
- package/prompts/select.md +7 -0
- package/src/Agentica.ts +322 -0
- package/src/chatgpt/ChatGptAgent.ts +71 -0
- package/src/chatgpt/ChatGptCallFunctionAgent.ts +445 -0
- package/src/chatgpt/ChatGptCancelFunctionAgent.ts +283 -0
- package/src/chatgpt/ChatGptDescribeFunctionAgent.ts +51 -0
- package/src/chatgpt/ChatGptHistoryDecoder.ts +86 -0
- package/src/chatgpt/ChatGptInitializeFunctionAgent.ts +88 -0
- package/src/chatgpt/ChatGptSelectFunctionAgent.ts +316 -0
- package/src/functional/createHttpLlmApplication.ts +63 -0
- package/src/index.ts +19 -0
- package/src/internal/AgenticaConstant.ts +4 -0
- package/src/internal/AgenticaCostAggregator.ts +35 -0
- package/src/internal/AgenticaDefaultPrompt.ts +39 -0
- package/src/internal/AgenticaOperationComposer.ts +82 -0
- package/src/internal/AgenticaPromptFactory.ts +30 -0
- package/src/internal/AgenticaPromptTransformer.ts +83 -0
- package/src/internal/AgenticaSystemPrompt.ts +14 -0
- package/src/internal/MathUtil.ts +3 -0
- package/src/internal/Singleton.ts +22 -0
- package/src/internal/__map_take.ts +15 -0
- package/src/structures/IAgenticaConfig.ts +121 -0
- package/src/structures/IAgenticaContext.ts +128 -0
- package/src/structures/IAgenticaController.ts +130 -0
- package/src/structures/IAgenticaEvent.ts +224 -0
- package/src/structures/IAgenticaExecutor.ts +152 -0
- package/src/structures/IAgenticaOperation.ts +64 -0
- package/src/structures/IAgenticaOperationCollection.ts +50 -0
- package/src/structures/IAgenticaOperationSelection.ts +69 -0
- package/src/structures/IAgenticaPrompt.ts +173 -0
- package/src/structures/IAgenticaProps.ts +64 -0
- package/src/structures/IAgenticaProvider.ts +45 -0
- package/src/structures/IAgenticaSystemPrompt.ts +122 -0
- package/src/structures/IAgenticaTokenUsage.ts +52 -0
- package/src/structures/internal/__IChatCancelFunctionsApplication.ts +23 -0
- package/src/structures/internal/__IChatFunctionReference.ts +21 -0
- package/src/structures/internal/__IChatInitialApplication.ts +15 -0
- package/src/structures/internal/__IChatSelectFunctionsApplication.ts +24 -0
- package/src/typings/AgenticaSource.ts +6 -0
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @internal
|
|
3
|
+
*/
|
|
4
|
+
export class Singleton<T, Args extends any[] = []> {
|
|
5
|
+
private readonly closure_: (...args: Args) => T;
|
|
6
|
+
private value_: T | object;
|
|
7
|
+
|
|
8
|
+
public constructor(closure: (...args: Args) => T) {
|
|
9
|
+
this.closure_ = closure;
|
|
10
|
+
this.value_ = NOT_MOUNTED_YET;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
public get(...args: Args): T {
|
|
14
|
+
if (this.value_ === NOT_MOUNTED_YET) this.value_ = this.closure_(...args);
|
|
15
|
+
return this.value_ as T;
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* @internal
|
|
21
|
+
*/
|
|
22
|
+
const NOT_MOUNTED_YET = {};
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @internal
|
|
3
|
+
*/
|
|
4
|
+
export const __map_take = <Key, T>(
|
|
5
|
+
dict: Map<Key, T>,
|
|
6
|
+
key: Key,
|
|
7
|
+
generator: () => T,
|
|
8
|
+
): T => {
|
|
9
|
+
const oldbie: T | undefined = dict.get(key);
|
|
10
|
+
if (oldbie) return oldbie;
|
|
11
|
+
|
|
12
|
+
const value: T = generator();
|
|
13
|
+
dict.set(key, value);
|
|
14
|
+
return value;
|
|
15
|
+
};
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
import { IAgenticaContext } from "./IAgenticaContext";
|
|
2
|
+
import { IAgenticaExecutor } from "./IAgenticaExecutor";
|
|
3
|
+
import { IAgenticaPrompt } from "./IAgenticaPrompt";
|
|
4
|
+
import { IAgenticaSystemPrompt } from "./IAgenticaSystemPrompt";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Configuration for Nestia Agent.
|
|
8
|
+
*
|
|
9
|
+
* `IAgenticaConfig` is an interface that defines the configuration
|
|
10
|
+
* properties of the {@link Agentica}. With this configuration, you
|
|
11
|
+
* can set the user's locale, timezone, and some of system prompts.
|
|
12
|
+
*
|
|
13
|
+
* Also, you can affect to the LLM function selecing/calling logic by
|
|
14
|
+
* configuring additional properties. For an example, if you configure the
|
|
15
|
+
* {@link capacity} property, the A.I. chatbot will divide the functions
|
|
16
|
+
* into the several groups with the configured capacity and select proper
|
|
17
|
+
* functions to call by operating the multiple LLM function selecting
|
|
18
|
+
* agents parallelly.
|
|
19
|
+
*
|
|
20
|
+
* @author Samchon
|
|
21
|
+
*/
|
|
22
|
+
export interface IAgenticaConfig {
|
|
23
|
+
/**
|
|
24
|
+
* Locale of the A.I. chatbot.
|
|
25
|
+
*
|
|
26
|
+
* If you configure this property, the A.I. chatbot will conversate with
|
|
27
|
+
* the given locale. You can get the locale value by
|
|
28
|
+
*
|
|
29
|
+
* - Browser: `navigator.language`
|
|
30
|
+
* - NodeJS: `process.env.LANG.split(".")[0]`
|
|
31
|
+
*
|
|
32
|
+
* @default your_locale
|
|
33
|
+
*/
|
|
34
|
+
locale?: string;
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Timezone of the A.I. chatbot.
|
|
38
|
+
*
|
|
39
|
+
* If you configure this property, the A.I. chatbot will consider the
|
|
40
|
+
* given timezone. You can get the timezone value by
|
|
41
|
+
* `Intl.DateTimeFormat().resolvedOptions().timeZone`.
|
|
42
|
+
*
|
|
43
|
+
* @default your_timezone
|
|
44
|
+
*/
|
|
45
|
+
timezone?: string;
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Retry count.
|
|
49
|
+
*
|
|
50
|
+
* If LLM function calling composed arguments are invalid,
|
|
51
|
+
* the A.I. chatbot will retry to call the function with
|
|
52
|
+
* the modified arguments.
|
|
53
|
+
*
|
|
54
|
+
* By the way, if you configure it to 0 or 1, the A.I. chatbot
|
|
55
|
+
* will not retry the LLM function calling for correcting the
|
|
56
|
+
* arguments.
|
|
57
|
+
*
|
|
58
|
+
* @default 3
|
|
59
|
+
*/
|
|
60
|
+
retry?: number;
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Capacity of the LLM function selecting.
|
|
64
|
+
*
|
|
65
|
+
* When the A.I. chatbot selects a proper function to call, if the
|
|
66
|
+
* number of functions registered in the
|
|
67
|
+
* {@link IAgenticaProps.applications} is too much greater,
|
|
68
|
+
* the A.I. chatbot often fallen into the hallucination.
|
|
69
|
+
*
|
|
70
|
+
* In that case, if you configure this property value, `Agentica`
|
|
71
|
+
* will divide the functions into the several groups with the configured
|
|
72
|
+
* capacity and select proper functions to call by operating the multiple
|
|
73
|
+
* LLM function selecting agents parallelly.
|
|
74
|
+
*
|
|
75
|
+
* @default 100
|
|
76
|
+
*/
|
|
77
|
+
capacity?: number;
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Eliticism for the LLM function selecting.
|
|
81
|
+
*
|
|
82
|
+
* If you configure {@link capacity}, the A.I. chatbot will complete
|
|
83
|
+
* the candidate functions to call which are selected by the multiple
|
|
84
|
+
* LLM function selecting agents.
|
|
85
|
+
*
|
|
86
|
+
* Otherwise you configure this property as `false`, the A.I. chatbot
|
|
87
|
+
* will not complete the candidate functions to call and just accept
|
|
88
|
+
* every candidate functions to call which are selected by the multiple
|
|
89
|
+
* LLM function selecting agents.
|
|
90
|
+
*
|
|
91
|
+
* @default true
|
|
92
|
+
*/
|
|
93
|
+
eliticism?: boolean;
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* System prompt messages.
|
|
97
|
+
*
|
|
98
|
+
* System prompt messages if you want to customize the system prompt
|
|
99
|
+
* messages for each situation.
|
|
100
|
+
*/
|
|
101
|
+
systemPrompt?: IAgenticaSystemPrompt;
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* Agent executor.
|
|
105
|
+
*
|
|
106
|
+
* Executor function of Agentic AI's iteration plan to internal agents
|
|
107
|
+
* running by the {@link Agentica.conversate} function.
|
|
108
|
+
*
|
|
109
|
+
* If you want to customize the agent execution plan, you can do it
|
|
110
|
+
* by assigning you logic function of entire or partial to this property.
|
|
111
|
+
* When customizing it, it would better to reference the
|
|
112
|
+
* {@link ChatGptAgent.execute} function.
|
|
113
|
+
*
|
|
114
|
+
* @param ctx Context of the agent
|
|
115
|
+
* @returns Lit of prompts generated by the executor
|
|
116
|
+
* @default ChatGptAgent.execute
|
|
117
|
+
*/
|
|
118
|
+
executor?:
|
|
119
|
+
| Partial<IAgenticaExecutor>
|
|
120
|
+
| ((ctx: IAgenticaContext) => Promise<IAgenticaPrompt[]>);
|
|
121
|
+
}
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
import OpenAI from "openai";
|
|
2
|
+
|
|
3
|
+
import { AgenticaSource } from "../typings/AgenticaSource";
|
|
4
|
+
import { IAgenticaConfig } from "./IAgenticaConfig";
|
|
5
|
+
import { IAgenticaEvent } from "./IAgenticaEvent";
|
|
6
|
+
import { IAgenticaOperationCollection } from "./IAgenticaOperationCollection";
|
|
7
|
+
import { IAgenticaOperationSelection } from "./IAgenticaOperationSelection";
|
|
8
|
+
import { IAgenticaPrompt } from "./IAgenticaPrompt";
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Context of the Nestia A.I. agent.
|
|
12
|
+
*
|
|
13
|
+
* `IAgenticaContext` is a structure defining the context of the
|
|
14
|
+
* internal agents composing the {@link Agentica}, like function
|
|
15
|
+
* selector, executor, and describer, and so on. For example, if an
|
|
16
|
+
* agent has been configured to utilize the OpenAI, the context will
|
|
17
|
+
* be delivered to the below components.
|
|
18
|
+
*
|
|
19
|
+
* - {@link ChatGptAgent}
|
|
20
|
+
* - {@link ChatGptInitializeFunctionAgent}
|
|
21
|
+
* - {@link ChatGptSelectFunctionAgent}
|
|
22
|
+
* - {@link ChatGptExecuteFunctionAgent}
|
|
23
|
+
* - {@link ChatGptDescribeFunctionAgent}
|
|
24
|
+
* - {@link ChatGptCancelFunctionAgent}
|
|
25
|
+
*
|
|
26
|
+
* Also, as its name is context, it contains every information that
|
|
27
|
+
* is required to interact with the AI provider like OpenAI. It
|
|
28
|
+
* contains every operations for LLM function calling, and
|
|
29
|
+
* configuration used for the agent construction. And it contains
|
|
30
|
+
* the prompt histories, and facade controller functions for
|
|
31
|
+
* interacting with the {@link Agentica} like {@link dispatch}.
|
|
32
|
+
*
|
|
33
|
+
* In such reasons, if you're planning to customize some internal
|
|
34
|
+
* agents, or add new agents with new process routine, you have to
|
|
35
|
+
* understand this context structure. Otherwise you don't have any
|
|
36
|
+
* plan to customize the internal agents, this context information is
|
|
37
|
+
* not important for you.
|
|
38
|
+
*
|
|
39
|
+
* @author Samchon
|
|
40
|
+
*/
|
|
41
|
+
export interface IAgenticaContext {
|
|
42
|
+
//----
|
|
43
|
+
// APPLICATION
|
|
44
|
+
//----
|
|
45
|
+
/**
|
|
46
|
+
* Collection of operations.
|
|
47
|
+
*
|
|
48
|
+
* Collection of operations from every controllers, and their
|
|
49
|
+
* groups composed by the divide and conquer rule for the
|
|
50
|
+
* efficient operation selection if configured.
|
|
51
|
+
*/
|
|
52
|
+
operations: IAgenticaOperationCollection;
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Configuration of the agent.
|
|
56
|
+
*
|
|
57
|
+
* Configuration of the agent, that is used when constructing the
|
|
58
|
+
* {@link Agentica} instance.
|
|
59
|
+
*
|
|
60
|
+
* @todo Write detaily after supporting the agent customization feature
|
|
61
|
+
*/
|
|
62
|
+
config: IAgenticaConfig | undefined;
|
|
63
|
+
|
|
64
|
+
//----
|
|
65
|
+
// STATES
|
|
66
|
+
//----
|
|
67
|
+
/**
|
|
68
|
+
* Prompt histories.
|
|
69
|
+
*/
|
|
70
|
+
histories: IAgenticaPrompt[];
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Stacked operations.
|
|
74
|
+
*
|
|
75
|
+
* In other words, list of candidate operations for the LLM function calling.
|
|
76
|
+
*/
|
|
77
|
+
stack: IAgenticaOperationSelection[];
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Text prompt of the user.
|
|
81
|
+
*
|
|
82
|
+
* Text conversation written the by user through the
|
|
83
|
+
* {@link Agentica.conversate} function.
|
|
84
|
+
*/
|
|
85
|
+
prompt: IAgenticaPrompt.IText<"user">;
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* Whether the agent is ready.
|
|
89
|
+
*
|
|
90
|
+
* Returns a boolean value indicates whether the agent is ready to
|
|
91
|
+
* perform the function calling.
|
|
92
|
+
*
|
|
93
|
+
* If the agent has called the {@link IAgenticaContext.initialize},
|
|
94
|
+
* it returns `true`. Otherwise the {@link initialize} has never been
|
|
95
|
+
* called, returns `false`.
|
|
96
|
+
*/
|
|
97
|
+
ready: () => boolean;
|
|
98
|
+
|
|
99
|
+
//----
|
|
100
|
+
// HANDLERS
|
|
101
|
+
//----
|
|
102
|
+
/**
|
|
103
|
+
* Dispatch event.
|
|
104
|
+
*
|
|
105
|
+
* Dispatch event so that the agent can be handle the event
|
|
106
|
+
* through the {@link Agentica.on} function.
|
|
107
|
+
*
|
|
108
|
+
* @param event Event to deliver
|
|
109
|
+
*/
|
|
110
|
+
dispatch: (event: IAgenticaEvent) => Promise<void>;
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* Request to the OpenAI server.
|
|
114
|
+
*
|
|
115
|
+
* @param source The source agent of the agent
|
|
116
|
+
* @param body The request body to the OpenAI server
|
|
117
|
+
* @returns Response from the OpenAI server
|
|
118
|
+
*/
|
|
119
|
+
request: (
|
|
120
|
+
source: AgenticaSource,
|
|
121
|
+
body: Omit<OpenAI.ChatCompletionCreateParamsNonStreaming, "model">,
|
|
122
|
+
) => Promise<OpenAI.ChatCompletion>;
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* Initialize the agent.
|
|
126
|
+
*/
|
|
127
|
+
initialize: () => Promise<void>;
|
|
128
|
+
}
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
import {
|
|
2
|
+
IHttpConnection,
|
|
3
|
+
IHttpLlmApplication,
|
|
4
|
+
IHttpLlmFunction,
|
|
5
|
+
IHttpResponse,
|
|
6
|
+
} from "@samchon/openapi";
|
|
7
|
+
import { ILlmApplicationOfValidate, ILlmFunctionOfValidate } from "typia";
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Controller of the Nestia Agent.
|
|
11
|
+
*
|
|
12
|
+
* `IAgenticaController` is a type represents a controller of the
|
|
13
|
+
* {@link Agentica}, which serves a set of functions to be called
|
|
14
|
+
* by A.I. chatbot from LLM function calling.
|
|
15
|
+
*
|
|
16
|
+
* Also, `IAgenticaController` is an union type which can specify
|
|
17
|
+
* a subtype by checking the {@link protocol} property.
|
|
18
|
+
*
|
|
19
|
+
* - HTTP server: {@link IAgenticaController..IHttp}
|
|
20
|
+
* - TypeScript class: {@link IAgenticaController.IClass}
|
|
21
|
+
*
|
|
22
|
+
* @author Samchon
|
|
23
|
+
*/
|
|
24
|
+
export type IAgenticaController =
|
|
25
|
+
| IAgenticaController.IHttp
|
|
26
|
+
| IAgenticaController.IClass;
|
|
27
|
+
export namespace IAgenticaController {
|
|
28
|
+
/**
|
|
29
|
+
* HTTP controller.
|
|
30
|
+
*
|
|
31
|
+
* You can make it by {@link createHttpLlmApplication} function with
|
|
32
|
+
* the Swagger or OpenAPI document.
|
|
33
|
+
*/
|
|
34
|
+
export interface IHttp extends IBase<"http", IHttpLlmApplication<"chatgpt">> {
|
|
35
|
+
/**
|
|
36
|
+
* Connection to the server.
|
|
37
|
+
*
|
|
38
|
+
* Connection to the API server including the URL and headers.
|
|
39
|
+
*/
|
|
40
|
+
connection: IHttpConnection;
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Executor of the API function.
|
|
44
|
+
*
|
|
45
|
+
* @param props Properties of the API function call
|
|
46
|
+
* @returns HTTP response of the API function call
|
|
47
|
+
*/
|
|
48
|
+
execute?: (props: {
|
|
49
|
+
/**
|
|
50
|
+
* Connection to the server.
|
|
51
|
+
*/
|
|
52
|
+
connection: IHttpConnection;
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Application schema.
|
|
56
|
+
*/
|
|
57
|
+
application: IHttpLlmApplication<"chatgpt">;
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Function schema.
|
|
61
|
+
*/
|
|
62
|
+
function: IHttpLlmFunction<"chatgpt">;
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Arguments of the function calling.
|
|
66
|
+
*
|
|
67
|
+
* It is an object of key-value pairs of the API function's parameters.
|
|
68
|
+
* The property keys are composed by below rules:
|
|
69
|
+
*
|
|
70
|
+
* - parameter names
|
|
71
|
+
* - query parameter as an object type if exists
|
|
72
|
+
* - body parameter if exists
|
|
73
|
+
*/
|
|
74
|
+
arguments: object;
|
|
75
|
+
}) => Promise<IHttpResponse>;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* TypeScript class controller.
|
|
80
|
+
*
|
|
81
|
+
* You can make it by `typia.llm.application<App, Model>()` function.
|
|
82
|
+
*
|
|
83
|
+
* - https://typia.io/docs/llm/application
|
|
84
|
+
*/
|
|
85
|
+
export interface IClass
|
|
86
|
+
extends IBase<"class", ILlmApplicationOfValidate<"chatgpt">> {
|
|
87
|
+
/**
|
|
88
|
+
* Executor of the class function.
|
|
89
|
+
*
|
|
90
|
+
* Executor of the class function, by target class instance
|
|
91
|
+
* or callback function with given schema and arguments
|
|
92
|
+
* information.
|
|
93
|
+
*/
|
|
94
|
+
execute:
|
|
95
|
+
| object
|
|
96
|
+
| ((props: {
|
|
97
|
+
/**
|
|
98
|
+
* Target application schema.
|
|
99
|
+
*/
|
|
100
|
+
application: ILlmApplicationOfValidate<"chatgpt">;
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* Target function schema.
|
|
104
|
+
*/
|
|
105
|
+
function: ILlmFunctionOfValidate<"chatgpt">;
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* Arguments of the function calling.
|
|
109
|
+
*/
|
|
110
|
+
arguments: object;
|
|
111
|
+
}) => Promise<unknown>);
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
interface IBase<Protocol, Application> {
|
|
115
|
+
/**
|
|
116
|
+
* Protocol discrminator.
|
|
117
|
+
*/
|
|
118
|
+
protocol: Protocol;
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* Name of the controller.
|
|
122
|
+
*/
|
|
123
|
+
name: string;
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* Application schema of function calling.
|
|
127
|
+
*/
|
|
128
|
+
application: Application;
|
|
129
|
+
}
|
|
130
|
+
}
|
|
@@ -0,0 +1,224 @@
|
|
|
1
|
+
import OpenAI from "openai";
|
|
2
|
+
|
|
3
|
+
import { AgenticaSource } from "../typings/AgenticaSource";
|
|
4
|
+
import { IAgenticaOperation } from "./IAgenticaOperation";
|
|
5
|
+
import { IAgenticaPrompt } from "./IAgenticaPrompt";
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Nestia A.I. chatbot event.
|
|
9
|
+
*
|
|
10
|
+
* `IAgenticaEvent` is an union type of all possible events that can
|
|
11
|
+
* be emitted by the A.I. chatbot of the {@link Agentica} class. You
|
|
12
|
+
* can discriminate the subtype by checking the {@link type} property.
|
|
13
|
+
*
|
|
14
|
+
* @author Samchon
|
|
15
|
+
*/
|
|
16
|
+
export type IAgenticaEvent =
|
|
17
|
+
| IAgenticaEvent.IInitialize
|
|
18
|
+
| IAgenticaEvent.ISelect
|
|
19
|
+
| IAgenticaEvent.ICancel
|
|
20
|
+
| IAgenticaEvent.ICall
|
|
21
|
+
| IAgenticaEvent.IExecute
|
|
22
|
+
| IAgenticaEvent.IDescribe
|
|
23
|
+
| IAgenticaEvent.IText
|
|
24
|
+
| IAgenticaEvent.IRequest
|
|
25
|
+
| IAgenticaEvent.IResponse;
|
|
26
|
+
export namespace IAgenticaEvent {
|
|
27
|
+
export type Type = IAgenticaEvent["type"];
|
|
28
|
+
export type Mapper = {
|
|
29
|
+
initialize: IInitialize;
|
|
30
|
+
select: ISelect;
|
|
31
|
+
cancel: ICancel;
|
|
32
|
+
call: ICall;
|
|
33
|
+
execute: IExecute;
|
|
34
|
+
describe: IDescribe;
|
|
35
|
+
text: IText;
|
|
36
|
+
request: IRequest;
|
|
37
|
+
response: IResponse;
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Event of initializing the chatbot.
|
|
42
|
+
*/
|
|
43
|
+
export interface IInitialize extends IBase<"initialize"> {}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Event of selecting a function to call.
|
|
47
|
+
*/
|
|
48
|
+
export interface ISelect extends IBase<"select"> {
|
|
49
|
+
/**
|
|
50
|
+
* Selected operation.
|
|
51
|
+
*
|
|
52
|
+
* Operation that has been selected to prepare LLM function calling.
|
|
53
|
+
*/
|
|
54
|
+
operation: IAgenticaOperation;
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Reason of selecting the function.
|
|
58
|
+
*
|
|
59
|
+
* The A.I. chatbot will fill this property describing why the function
|
|
60
|
+
* has been selected.
|
|
61
|
+
*/
|
|
62
|
+
reason: string;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Event of canceling a function calling.
|
|
67
|
+
*/
|
|
68
|
+
export interface ICancel extends IBase<"cancel"> {
|
|
69
|
+
/**
|
|
70
|
+
* Selected operation to cancel.
|
|
71
|
+
*
|
|
72
|
+
* Operation that has been selected to prepare LLM function calling,
|
|
73
|
+
* but canceled due to no more required.
|
|
74
|
+
*/
|
|
75
|
+
operation: IAgenticaOperation;
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Reason of selecting the function.
|
|
79
|
+
*
|
|
80
|
+
* The A.I. chatbot will fill this property describing why the function
|
|
81
|
+
* has been cancelled.
|
|
82
|
+
*
|
|
83
|
+
* For reference, if the A.I. chatbot successfully completes the LLM
|
|
84
|
+
* function calling, the reason of the function cancellation will be
|
|
85
|
+
* "complete".
|
|
86
|
+
*/
|
|
87
|
+
reason: string;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* Event of calling a function.
|
|
92
|
+
*/
|
|
93
|
+
export interface ICall extends IBase<"call"> {
|
|
94
|
+
/**
|
|
95
|
+
* ID of the tool calling.
|
|
96
|
+
*/
|
|
97
|
+
id: string;
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Target operation to call.
|
|
101
|
+
*/
|
|
102
|
+
operation: IAgenticaOperation;
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* Arguments of the function calling.
|
|
106
|
+
*
|
|
107
|
+
* If you modify this {@link arguments} property, it actually modifies
|
|
108
|
+
* the backend server's request. Therefore, be careful when you're
|
|
109
|
+
* trying to modify this property.
|
|
110
|
+
*/
|
|
111
|
+
arguments: object;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* Event of function calling execution.
|
|
116
|
+
*/
|
|
117
|
+
export interface IExecute extends IBase<"execute"> {
|
|
118
|
+
/**
|
|
119
|
+
* ID of the tool calling.
|
|
120
|
+
*/
|
|
121
|
+
id: string;
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* Target operation had called.
|
|
125
|
+
*/
|
|
126
|
+
operation: IAgenticaOperation;
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* Arguments of the function calling.
|
|
130
|
+
*/
|
|
131
|
+
arguments: object;
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* Return value.
|
|
135
|
+
*/
|
|
136
|
+
value: any;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* Event of description.
|
|
141
|
+
*
|
|
142
|
+
* Event describing return values of LLM function callings.
|
|
143
|
+
*/
|
|
144
|
+
export interface IDescribe extends IBase<"describe"> {
|
|
145
|
+
/**
|
|
146
|
+
* Executions of the LLM function calling.
|
|
147
|
+
*
|
|
148
|
+
* This prompt describes the return value of them.
|
|
149
|
+
*/
|
|
150
|
+
executions: IAgenticaPrompt.IExecute[];
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* Description text.
|
|
154
|
+
*/
|
|
155
|
+
text: string;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* Event of text message.
|
|
160
|
+
*/
|
|
161
|
+
export interface IText extends IBase<"text"> {
|
|
162
|
+
/**
|
|
163
|
+
* Role of the orator.
|
|
164
|
+
*/
|
|
165
|
+
role: "assistant" | "user";
|
|
166
|
+
|
|
167
|
+
/**
|
|
168
|
+
* The text content.
|
|
169
|
+
*/
|
|
170
|
+
text: string;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/**
|
|
174
|
+
* Request event of LLM provider API.
|
|
175
|
+
*/
|
|
176
|
+
export interface IRequest extends IBase<"request"> {
|
|
177
|
+
/**
|
|
178
|
+
* The source agent of the request.
|
|
179
|
+
*/
|
|
180
|
+
source: AgenticaSource;
|
|
181
|
+
|
|
182
|
+
/**
|
|
183
|
+
* Request body.
|
|
184
|
+
*/
|
|
185
|
+
body: OpenAI.ChatCompletionCreateParamsNonStreaming;
|
|
186
|
+
|
|
187
|
+
/**
|
|
188
|
+
* Options for the request.
|
|
189
|
+
*/
|
|
190
|
+
options?: OpenAI.RequestOptions | undefined;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
/**
|
|
194
|
+
* Response event of LLM provider API.
|
|
195
|
+
*/
|
|
196
|
+
export interface IResponse extends IBase<"response"> {
|
|
197
|
+
/**
|
|
198
|
+
* The source agent of the response.
|
|
199
|
+
*/
|
|
200
|
+
source: AgenticaSource;
|
|
201
|
+
|
|
202
|
+
/**
|
|
203
|
+
* Request body.
|
|
204
|
+
*/
|
|
205
|
+
body: OpenAI.ChatCompletionCreateParamsNonStreaming;
|
|
206
|
+
|
|
207
|
+
/**
|
|
208
|
+
* Options for the request.
|
|
209
|
+
*/
|
|
210
|
+
options?: OpenAI.RequestOptions | undefined;
|
|
211
|
+
|
|
212
|
+
/**
|
|
213
|
+
* Return value from the LLM provider API.
|
|
214
|
+
*/
|
|
215
|
+
value: OpenAI.ChatCompletion;
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
interface IBase<Type extends string> {
|
|
219
|
+
/**
|
|
220
|
+
* Discriminator type.
|
|
221
|
+
*/
|
|
222
|
+
type: Type;
|
|
223
|
+
}
|
|
224
|
+
}
|