ctod 0.7.1 → 0.7.2
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/examples/basic.ts +97 -0
- package/examples/llama.cpp.ts +56 -0
- package/examples/plugin.ts +118 -0
- package/lib/broker/chat.ts +435 -0
- package/lib/core/parser.ts +62 -0
- package/lib/core/plugin.ts +46 -0
- package/lib/core/translator.ts +115 -0
- package/lib/ctod.ts +71 -0
- package/lib/index.ts +41 -0
- package/lib/plugins/index.ts +38 -0
- package/lib/plugins/limiter.ts +103 -0
- package/lib/plugins/print-log.ts +35 -0
- package/lib/plugins/retry.ts +25 -0
- package/lib/plugins/role.ts +28 -0
- package/lib/service/llama3.cpp/completion.ts +313 -0
- package/lib/service/llama3.cpp/index.ts +53 -0
- package/lib/service/openai/chat.ts +244 -0
- package/lib/service/openai/images-generation.ts +64 -0
- package/lib/service/openai/index.ts +97 -0
- package/lib/service/openai/vision.ts +111 -0
- package/lib/shims.d.ts +4 -0
- package/lib/templates.ts +71 -0
- package/lib/types.ts +4 -0
- package/lib/utils/error.ts +14 -0
- package/lib/utils/validate.ts +64 -0
- package/package.json +1 -1
- package/types/examples/basic.d.ts +1 -0
- package/types/examples/chat-demo.d.ts +2 -0
- package/types/examples/chat-for-llama.cpp-demo.d.ts +2 -0
- package/types/examples/chat-with-json-schema-demo.d.ts +2 -0
- package/types/examples/llama.cpp.d.ts +2 -0
- package/types/examples/plugin-demo.d.ts +2 -0
- package/types/examples/plugin.d.ts +2 -0
- package/types/examples/stream-for-llama.cpp-demo.d.ts +2 -0
- package/types/examples/vision-demo.d.ts +2 -0
- package/types/lib/broker/chat.d.ts +150 -0
- package/types/lib/core/parser.d.ts +32 -0
- package/types/lib/core/plugin.d.ts +34 -0
- package/types/lib/core/translator.d.ts +67 -0
- package/types/lib/ctod.d.ts +32 -0
- package/types/lib/index.d.ts +34 -0
- package/types/lib/plugins/index.d.ts +47 -0
- package/types/lib/plugins/limiter.d.ts +36 -0
- package/types/lib/plugins/print-log.d.ts +5 -0
- package/types/lib/plugins/retry.d.ts +6 -0
- package/types/lib/plugins/role.d.ts +5 -0
- package/types/lib/service/llama3.cpp/completion.d.ts +61 -0
- package/types/lib/service/llama3.cpp/index.d.ts +19 -0
- package/types/lib/service/openai/chat.d.ts +110 -0
- package/types/lib/service/openai/completion.d.ts +59 -0
- package/types/lib/service/openai/images-generation.d.ts +35 -0
- package/types/lib/service/openai/index.d.ts +29 -0
- package/types/lib/service/openai/vision.d.ts +74 -0
- package/types/lib/templates.d.ts +20 -0
- package/types/lib/types.d.ts +1 -0
- package/types/lib/utils/error.d.ts +11 -0
- package/types/lib/utils/validate.d.ts +16 -0
- package/.api-key +0 -1
- package/.nyc_output/42919e68-b472-4a5d-b2d3-5d5153f28467.json +0 -1
- package/.nyc_output/processinfo/42919e68-b472-4a5d-b2d3-5d5153f28467.json +0 -1
- package/.nyc_output/processinfo/index.json +0 -1
- package/.output/.output/stores//344/270/200/345/240/264/346/234/237/345/276/205/345/267/262/344/271/205/347/232/204/345/222/214/350/247/243/cover-0.png +0 -1
- package/.output/.output/stores//344/270/200/345/240/264/346/234/237/345/276/205/345/267/262/344/271/205/347/232/204/345/222/214/350/247/243/cover-1.png +0 -1
- package/.output/.output/stores//344/270/200/345/240/264/346/234/237/345/276/205/345/267/262/344/271/205/347/232/204/345/222/214/350/247/243/story-config.json +0 -4
- package/.output/.output/stores//344/270/200/345/240/264/346/234/237/345/276/205/345/267/262/344/271/205/347/232/204/345/222/214/350/247/243/story.json +0 -22
- package/.output/.output/stores//345/276/236/351/273/221/346/232/227/350/265/260/345/220/221/345/205/211/346/230/216/cover-0.png +0 -1
- package/.output/.output/stores//345/276/236/351/273/221/346/232/227/350/265/260/345/220/221/345/205/211/346/230/216/cover-1.png +0 -1
- package/.output/.output/stores//345/276/236/351/273/221/346/232/227/350/265/260/345/220/221/345/205/211/346/230/216/story-config.json +0 -4
- package/.output/.output/stores//345/276/236/351/273/221/346/232/227/350/265/260/345/220/221/345/205/211/346/230/216/story.json +0 -24
- package/.output/.output/stores//347/240/264/347/242/216/347/232/204/345/271/273/350/261/241/cover-0.png +0 -1
- package/.output/.output/stores//347/240/264/347/242/216/347/232/204/345/271/273/350/261/241/cover-1.png +0 -1
- package/.output/.output/stores//347/240/264/347/242/216/347/232/204/345/271/273/350/261/241/story-config.json +0 -4
- package/.output/.output/stores//347/240/264/347/242/216/347/232/204/345/271/273/350/261/241/story.json +0 -24
- package/.output/.output/stores//350/227/235/350/241/223/345/256/266/347/232/204/351/235/210/346/204/237/cover-0.png +0 -1
- package/.output/.output/stores//350/227/235/350/241/223/345/256/266/347/232/204/351/235/210/346/204/237/cover-1.png +0 -1
- package/.output/.output/stores//350/227/235/350/241/223/345/256/266/347/232/204/351/235/210/346/204/237/story-config.json +0 -4
- package/.output/.output/stores//350/227/235/350/241/223/345/256/266/347/232/204/351/235/210/346/204/237/story.json +0 -28
- package/.output/.output/stores//350/250/230/346/206/266/345/225/206/344/272/272/cover-0.png +0 -1
- package/.output/.output/stores//350/250/230/346/206/266/345/225/206/344/272/272/cover-1.png +0 -1
- package/.output/.output/stores//350/250/230/346/206/266/345/225/206/344/272/272/story-config.json +0 -4
- package/.output/.output/stores//350/250/230/346/206/266/345/225/206/344/272/272/story.json +0 -18
- package/.output/.output/stores//350/250/255/350/250/210/350/210/207/351/226/213/347/231/274/347/232/204/346/214/221/346/210/260/content.json +0 -22
- package/.output/.output/talks//344/273/245/347/254/221/350/251/261/347/202/272/345/237/272/347/244/216/347/232/204/351/235/242/350/251/246/content.json +0 -30
- package/.output/.output/talks//344/273/245/347/254/221/350/251/261/347/202/272/345/237/272/347/244/216/347/232/204/351/235/242/350/251/246/cover-1684055229695.png +0 -0
- package/.output/.output/talks//345/234/250/346/224/277/346/262/273/345/200/231/351/201/270/344/272/272/350/276/257/350/253/226/344/270/255/347/232/204/350/252/252/346/234/215/346/200/247/346/272/235/351/200/232/content.json +0 -30
- package/.output/.output/talks//345/234/250/346/224/277/346/262/273/345/200/231/351/201/270/344/272/272/350/276/257/350/253/226/344/270/255/347/232/204/350/252/252/346/234/215/346/200/247/346/272/235/351/200/232/cover-1684056611678.png +0 -0
- package/.output/.output/talks//346/224/276/351/254/206/347/232/204/345/200/231/351/201/270/344/272/272/351/200/262/350/241/214/346/224/277/346/262/273/350/276/257/350/253/226/content.json +0 -36
- package/.output/.output/talks//346/224/276/351/254/206/347/232/204/345/200/231/351/201/270/344/272/272/351/200/262/350/241/214/346/224/277/346/262/273/350/276/257/350/253/226/cover-1684055140609.png +0 -0
- package/.output/.output/talks//346/224/277/346/262/273/347/254/221/350/251/261/content.json +0 -30
- package/.output/.output/talks//346/224/277/346/262/273/347/254/221/350/251/261/cover-1684056246465.png +0 -0
- package/.output/.output/talks//350/251/274/350/253/247/345/256/266/351/225/267/346/225/231/345/270/253/346/234/203/350/255/260/content.json +0 -26
- package/.output/.output/talks//350/251/274/350/253/247/345/256/266/351/225/267/346/225/231/345/270/253/346/234/203/350/255/260/cover-1685785935121.png +0 -0
- package/.output/.output/talks//350/262/241/345/213/231/351/241/247/345/225/217/350/253/256/350/251/242/content.json +0 -26
- package/.output/.output/talks//350/262/241/345/213/231/351/241/247/345/225/217/350/253/256/350/251/242/cover-1685785115833.png +0 -0
- package/.output/.output/talks//351/206/253/347/224/237/345/222/214/347/227/205/344/272/272/350/250/216/350/253/226/346/202/262/345/202/267/content.json +0 -32
- package/.output/.output/talks//351/206/253/347/224/237/345/222/214/347/227/205/344/272/272/350/250/216/350/253/226/346/202/262/345/202/267/cover-1684055075942.png +0 -0
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { TextParser } from './parser';
|
|
2
|
+
import { ValidateCallback, ValidateCallbackOutputs } from '../utils/validate';
|
|
3
|
+
export type TranslatorParams<S extends ValidateCallback<any>, O extends ValidateCallback<any>> = {
|
|
4
|
+
/**
|
|
5
|
+
* @zh 輸入的資料格式。
|
|
6
|
+
* @en The input data format.
|
|
7
|
+
*/
|
|
8
|
+
input?: S;
|
|
9
|
+
/**
|
|
10
|
+
* @zh 輸出的資料格式。
|
|
11
|
+
* @en The output data format.
|
|
12
|
+
*/
|
|
13
|
+
output: O;
|
|
14
|
+
/**
|
|
15
|
+
* @zh 註冊解讀文字的解析器。
|
|
16
|
+
* @en Register the parser to interpret the text.
|
|
17
|
+
*/
|
|
18
|
+
parsers: TextParser[];
|
|
19
|
+
/**
|
|
20
|
+
* @zh 組合輸入資料成為提示文字。
|
|
21
|
+
* @en Combine the input data into a prompt.
|
|
22
|
+
*/
|
|
23
|
+
question?: (data: ValidateCallbackOutputs<S>, context: {
|
|
24
|
+
schema: {
|
|
25
|
+
input?: S;
|
|
26
|
+
output: O;
|
|
27
|
+
};
|
|
28
|
+
}) => Promise<string | string[]>;
|
|
29
|
+
};
|
|
30
|
+
export declare class Translator<S extends ValidateCallback<any>, O extends ValidateCallback<any>> {
|
|
31
|
+
private params;
|
|
32
|
+
constructor(params: TranslatorParams<S, O>);
|
|
33
|
+
get __schemeType(): ValidateCallbackOutputs<S>;
|
|
34
|
+
get __outputType(): ValidateCallbackOutputs<O>;
|
|
35
|
+
/**
|
|
36
|
+
* @zh 組合輸入資料成為提示文字。
|
|
37
|
+
* @en Combine the input data into a prompt.
|
|
38
|
+
*/
|
|
39
|
+
compile(data: ValidateCallbackOutputs<S>, context: {
|
|
40
|
+
schema: {
|
|
41
|
+
input?: S;
|
|
42
|
+
output: O;
|
|
43
|
+
};
|
|
44
|
+
}): Promise<{
|
|
45
|
+
scheme: ValidateCallbackOutputs<S, ReturnType<S>>;
|
|
46
|
+
prompt: string;
|
|
47
|
+
}>;
|
|
48
|
+
getValidate(): {
|
|
49
|
+
input: S | undefined;
|
|
50
|
+
output: O;
|
|
51
|
+
};
|
|
52
|
+
changeOutputSchema(schema: O): void;
|
|
53
|
+
/**
|
|
54
|
+
* @zh 將文字轉換成序列化資料。
|
|
55
|
+
* @en Convert text to serialized data.
|
|
56
|
+
*/
|
|
57
|
+
parse(text: string): Promise<{
|
|
58
|
+
output: ReturnType<O> extends infer T ? { [K in keyof T]: ReturnType<O>[K] extends {
|
|
59
|
+
__outputType: any;
|
|
60
|
+
} ? ReturnType<O>[K]["__outputType"] : ReturnType<O>[K]; } : never;
|
|
61
|
+
parserName: string;
|
|
62
|
+
parserFails: {
|
|
63
|
+
name: string;
|
|
64
|
+
error: any;
|
|
65
|
+
}[];
|
|
66
|
+
}>;
|
|
67
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { ChatBroker, Message, Params as ChatBrokerParams, ChatBrokerHooks, RequestContext } from './broker/chat';
|
|
2
|
+
import { ChatBrokerPlugin } from './core/plugin';
|
|
3
|
+
import { Schema } from 'yup';
|
|
4
|
+
import * as Yup from 'yup';
|
|
5
|
+
type IO = any;
|
|
6
|
+
export declare class CtoD<P extends ChatBrokerPlugin<IO, IO>, PS extends Record<string, ReturnType<P['use']>>> {
|
|
7
|
+
params: {
|
|
8
|
+
request: (messages: Message[], context: RequestContext) => Promise<string>;
|
|
9
|
+
plugins?: (() => PS) | undefined;
|
|
10
|
+
};
|
|
11
|
+
constructor(params: {
|
|
12
|
+
request: (messages: Message[], context: RequestContext) => Promise<string>;
|
|
13
|
+
plugins?: () => PS;
|
|
14
|
+
});
|
|
15
|
+
createBrokerBuilder<I extends Record<string, any>>(params?: {
|
|
16
|
+
install?: ChatBrokerParams<() => I, IO, ChatBrokerHooks<() => I, IO, P, PS>, P, PS>['install'];
|
|
17
|
+
}): {
|
|
18
|
+
create: <O extends Record<string, Schema<any, any, any, "">>>(install: (context: {
|
|
19
|
+
id: string;
|
|
20
|
+
yup: typeof Yup;
|
|
21
|
+
data: I;
|
|
22
|
+
plugins: { [K in keyof PS]: {
|
|
23
|
+
send: (data: PS[K]["__receiveData"]) => void;
|
|
24
|
+
}; };
|
|
25
|
+
setMessages: (messages: (Omit<Message, 'content'> & {
|
|
26
|
+
content: string | string[];
|
|
27
|
+
})[]) => void;
|
|
28
|
+
metadata: Map<string, any>;
|
|
29
|
+
}) => Promise<O>) => ChatBroker<() => I, () => O, P, PS, ChatBrokerHooks<() => I, () => O, P, PS>>;
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
export {};
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
export * as plugins from './plugins';
|
|
2
|
+
export * as templates from './templates';
|
|
3
|
+
export { CtoD } from './ctod';
|
|
4
|
+
export { validateToJsonSchema, defineYupSchema, ValidateCallback } from './utils/validate';
|
|
5
|
+
export { OpenAI } from './service/openai';
|
|
6
|
+
export { Llama3Cpp } from './service/llama3.cpp';
|
|
7
|
+
export { TextParser } from './core/parser';
|
|
8
|
+
export { ChatGPTMessage } from './service/openai/chat';
|
|
9
|
+
export { ChatBroker } from './broker/chat';
|
|
10
|
+
export { ChatBrokerPlugin } from './core/plugin';
|
|
11
|
+
export { Translator, TranslatorParams } from './core/translator';
|
|
12
|
+
import * as plugins from './plugins';
|
|
13
|
+
import * as templates from './templates';
|
|
14
|
+
import { OpenAI } from './service/openai';
|
|
15
|
+
import { CtoD } from './ctod';
|
|
16
|
+
import { Llama3Cpp } from './service/llama3.cpp';
|
|
17
|
+
import { Translator } from './core/translator';
|
|
18
|
+
import { TextParser } from './core/parser';
|
|
19
|
+
import { ChatBroker } from './broker/chat';
|
|
20
|
+
import { ChatBrokerPlugin } from './core/plugin';
|
|
21
|
+
export declare const ctod: {
|
|
22
|
+
CtoD: typeof CtoD;
|
|
23
|
+
OpenAI: typeof OpenAI;
|
|
24
|
+
Llama3Cpp: typeof Llama3Cpp;
|
|
25
|
+
plugins: typeof plugins;
|
|
26
|
+
templates: typeof templates;
|
|
27
|
+
ChatBroker: typeof ChatBroker;
|
|
28
|
+
Translator: typeof Translator;
|
|
29
|
+
TextParser: typeof TextParser;
|
|
30
|
+
ChatBrokerPlugin: typeof ChatBrokerPlugin;
|
|
31
|
+
defineYupSchema: <T extends Record<string, import("yup").Schema<any, any, any, "">>>(cb: import("./utils/validate").ValidateCallback<T>) => T;
|
|
32
|
+
validateToJsonSchema: <T_1 extends import("./utils/validate").ValidateCallback<any>>(cb: T_1) => import("json-schema").JSONSchema7;
|
|
33
|
+
};
|
|
34
|
+
export default ctod;
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @zh 一個基於印出 log 的 plugin。
|
|
3
|
+
* @en A plugin based on printing log.
|
|
4
|
+
*/
|
|
5
|
+
export declare const PrintLogPlugin: import("..").ChatBrokerPlugin<(yup: typeof import("yup")) => {
|
|
6
|
+
detail: import("yup").BooleanSchema<boolean | undefined, import("yup").AnyObject, false, "d">;
|
|
7
|
+
}, () => {}>;
|
|
8
|
+
/**
|
|
9
|
+
* @zh 當解析失敗時,會自動重試的對話。
|
|
10
|
+
* @en A conversation that will automatically retry when parsing fails.
|
|
11
|
+
*/
|
|
12
|
+
export declare const RetryPlugin: import("..").ChatBrokerPlugin<(yup: typeof import("yup")) => {
|
|
13
|
+
retry: import("yup").NumberSchema<number, import("yup").AnyObject, 1, "d">;
|
|
14
|
+
printWarn: import("yup").BooleanSchema<NonNullable<boolean | undefined>, import("yup").AnyObject, true, "d">;
|
|
15
|
+
}, () => {}>;
|
|
16
|
+
/**
|
|
17
|
+
* @zh 限制使用流量,這個 plugin 可以有效讓所有對話不會再限制內同時發送,可用於在開發過程中遭遇伺服器因頻率過高而阻擋請求。
|
|
18
|
+
* @en Limit the use of traffic. This plugin can effectively prevent all conversations from being sent at the same time within the limit, and can be used when the server blocks requests due to high frequency during development.
|
|
19
|
+
*/
|
|
20
|
+
export declare const LimiterPlugin: import("..").ChatBrokerPlugin<() => {}, () => {}>;
|
|
21
|
+
/**
|
|
22
|
+
* @zh 排程系統將全域託管,有什麼必要設定可以來更動它的狀態,例如:關閉排程。
|
|
23
|
+
* @en The scheduling system will be globally hosted. What is necessary to set can come to change its status, for example: close the schedule.
|
|
24
|
+
*/
|
|
25
|
+
export declare const LimiterPluginGlobState: {
|
|
26
|
+
event: import("power-helper").Event<{
|
|
27
|
+
run: {
|
|
28
|
+
id: string;
|
|
29
|
+
};
|
|
30
|
+
waitTimeChange: {
|
|
31
|
+
waitTime: number;
|
|
32
|
+
};
|
|
33
|
+
}>;
|
|
34
|
+
config: {
|
|
35
|
+
limit: number;
|
|
36
|
+
interval: number;
|
|
37
|
+
};
|
|
38
|
+
closeSchedule: () => void;
|
|
39
|
+
plugin: import("..").ChatBrokerPlugin<() => {}, () => {}>;
|
|
40
|
+
};
|
|
41
|
+
/**
|
|
42
|
+
* @zh 設定角色扮演。
|
|
43
|
+
* @en Set role play.
|
|
44
|
+
*/
|
|
45
|
+
export declare const RolePlugin: import("..").ChatBrokerPlugin<(yup: typeof import("yup")) => {
|
|
46
|
+
role: import("yup").StringSchema<string, import("yup").AnyObject, undefined, "">;
|
|
47
|
+
}, () => {}>;
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { ChatBrokerPlugin } from '../core/plugin';
|
|
2
|
+
import { Event } from 'power-helper';
|
|
3
|
+
type Events = {
|
|
4
|
+
run: {
|
|
5
|
+
id: string;
|
|
6
|
+
};
|
|
7
|
+
waitTimeChange: {
|
|
8
|
+
waitTime: number;
|
|
9
|
+
};
|
|
10
|
+
};
|
|
11
|
+
declare const _default: {
|
|
12
|
+
/**
|
|
13
|
+
* @zh 你可以監聽一些事件行為,例如還需要等待多少時間。
|
|
14
|
+
* @en You can listen for some event behaviors, such as how long you still need to wait.
|
|
15
|
+
*/
|
|
16
|
+
event: Event<Events>;
|
|
17
|
+
/**
|
|
18
|
+
* @zh 預設是每分鐘限制3次,你可以在設定中改變限制流量。
|
|
19
|
+
* @en By default, the limit is 3 times per minute, and you can change the limit flow in the settings.
|
|
20
|
+
*/
|
|
21
|
+
config: {
|
|
22
|
+
limit: number;
|
|
23
|
+
interval: number;
|
|
24
|
+
};
|
|
25
|
+
/**
|
|
26
|
+
* @zh 由於排程會在背景持續倒數,如果有關閉程式的需求,需要手動進行移除。
|
|
27
|
+
* @en Since the schedule will continue to count down in the background, if there is a need to close the program, you need to manually remove it.
|
|
28
|
+
*/
|
|
29
|
+
closeSchedule: () => void;
|
|
30
|
+
/**
|
|
31
|
+
* @zh Plugin 的接口
|
|
32
|
+
* @en Plugin interface
|
|
33
|
+
*/
|
|
34
|
+
plugin: ChatBrokerPlugin<() => {}, () => {}>;
|
|
35
|
+
};
|
|
36
|
+
export default _default;
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { ChatBrokerPlugin } from '../core/plugin';
|
|
2
|
+
declare const _default: ChatBrokerPlugin<(yup: typeof import("yup")) => {
|
|
3
|
+
retry: import("yup").NumberSchema<number, import("yup").AnyObject, 1, "d">;
|
|
4
|
+
printWarn: import("yup").BooleanSchema<NonNullable<boolean | undefined>, import("yup").AnyObject, true, "d">;
|
|
5
|
+
}, () => {}>;
|
|
6
|
+
export default _default;
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { Llama3Cpp } from './index';
|
|
2
|
+
type Message = {
|
|
3
|
+
role: string;
|
|
4
|
+
content: string;
|
|
5
|
+
};
|
|
6
|
+
type Options = any;
|
|
7
|
+
export type Config = {
|
|
8
|
+
baseUrl: string;
|
|
9
|
+
headers: Record<string, string>;
|
|
10
|
+
autoConvertTraditionalChinese: boolean;
|
|
11
|
+
};
|
|
12
|
+
type Stream = {
|
|
13
|
+
onMessage: (data: {
|
|
14
|
+
message: string;
|
|
15
|
+
}) => void;
|
|
16
|
+
onEnd?: () => void;
|
|
17
|
+
onWarn?: (error: any) => void;
|
|
18
|
+
onError?: (error: any) => void;
|
|
19
|
+
};
|
|
20
|
+
export declare class Llama3CppCompletion {
|
|
21
|
+
core: Llama3Cpp;
|
|
22
|
+
config: Config;
|
|
23
|
+
constructor(core: Llama3Cpp);
|
|
24
|
+
setConfig(config: Partial<Config>): void;
|
|
25
|
+
completion(params: {
|
|
26
|
+
options?: Options;
|
|
27
|
+
messages: Message[];
|
|
28
|
+
}): {
|
|
29
|
+
run: () => Promise<{
|
|
30
|
+
message: string;
|
|
31
|
+
fullMessage: string;
|
|
32
|
+
}>;
|
|
33
|
+
cancel: () => void;
|
|
34
|
+
};
|
|
35
|
+
completionStream(params: Stream & {
|
|
36
|
+
messages: Message[];
|
|
37
|
+
options?: Options;
|
|
38
|
+
}): {
|
|
39
|
+
cancel: () => void;
|
|
40
|
+
};
|
|
41
|
+
talk(params: {
|
|
42
|
+
options?: Options;
|
|
43
|
+
messages: Message[];
|
|
44
|
+
response_format?: {
|
|
45
|
+
type: 'json_object';
|
|
46
|
+
schema: any;
|
|
47
|
+
};
|
|
48
|
+
}): {
|
|
49
|
+
run: () => Promise<{
|
|
50
|
+
message: string;
|
|
51
|
+
}>;
|
|
52
|
+
cancel: () => void;
|
|
53
|
+
};
|
|
54
|
+
talkStream(params: Stream & {
|
|
55
|
+
options?: Options;
|
|
56
|
+
messages: Message[];
|
|
57
|
+
}): {
|
|
58
|
+
cancel: () => void;
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
export {};
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { AxiosInstance } from 'axios';
|
|
2
|
+
import { Llama3CppCompletion, Config } from './completion';
|
|
3
|
+
export declare class Llama3Cpp {
|
|
4
|
+
_axios: AxiosInstance;
|
|
5
|
+
static createChatRequest(params: {
|
|
6
|
+
config: Partial<Config> | (() => Promise<Partial<Config>>);
|
|
7
|
+
talkOptions?: any;
|
|
8
|
+
}): (messages: any[], { schema, onCancel }: any) => Promise<string>;
|
|
9
|
+
/**
|
|
10
|
+
* @zh 如果你有需要特別設定 axios,請使用這方法。
|
|
11
|
+
* @en If you need to set axios, please use this method.
|
|
12
|
+
*/
|
|
13
|
+
setAxios(axios: AxiosInstance): void;
|
|
14
|
+
/**
|
|
15
|
+
* @zh 建立 補強/ 對話物件。
|
|
16
|
+
* @en Create completion object.
|
|
17
|
+
*/
|
|
18
|
+
createCompletion(): Llama3CppCompletion;
|
|
19
|
+
}
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
import { OpenAI } from './index';
|
|
2
|
+
import { PromiseResponseType } from '../../types';
|
|
3
|
+
export type ChatGPTMessage = {
|
|
4
|
+
role: 'system' | 'user' | 'assistant';
|
|
5
|
+
name?: string;
|
|
6
|
+
content: string;
|
|
7
|
+
};
|
|
8
|
+
type ApiResponse = {
|
|
9
|
+
id: string;
|
|
10
|
+
object: string;
|
|
11
|
+
created: number;
|
|
12
|
+
choices: Array<{
|
|
13
|
+
index: number;
|
|
14
|
+
finish_reason: string;
|
|
15
|
+
message: {
|
|
16
|
+
role: 'system' | 'user' | 'assistant';
|
|
17
|
+
name?: string;
|
|
18
|
+
content: string;
|
|
19
|
+
};
|
|
20
|
+
}>;
|
|
21
|
+
usage: {
|
|
22
|
+
prompt_tokens: number;
|
|
23
|
+
completion_tokens: number;
|
|
24
|
+
total_tokens: number;
|
|
25
|
+
};
|
|
26
|
+
};
|
|
27
|
+
export type Config = {
|
|
28
|
+
/**
|
|
29
|
+
* @zh 一次回應數量
|
|
30
|
+
* @en How many chat completion choices to generate for each input message.
|
|
31
|
+
*/
|
|
32
|
+
n: number;
|
|
33
|
+
/**
|
|
34
|
+
* @zh 選擇運行的模型,16k意味著能處理長度為 16,384 的文本,32k意味著能處理長度為 32768 的文本。
|
|
35
|
+
* @en How many chat completion choices to generate for each input message.
|
|
36
|
+
*/
|
|
37
|
+
model: 'gpt-4' | 'gpt-3.5-turbo' | 'gpt-4-turbo' | 'gpt-4o' | 'gpt-4o-mini' | 'o1-preview' | 'o1' | 'o1-mini';
|
|
38
|
+
/**
|
|
39
|
+
* @zh 冒險指數,數值由 0 ~ 2 之間,越低回應越穩定。
|
|
40
|
+
* @en What sampling temperature to use, between 0 and 2. Higher values like 0.8 will make the output more random, while lower values like 0.2 will make it more focused and deterministic.
|
|
41
|
+
*/
|
|
42
|
+
temperature: number;
|
|
43
|
+
/**
|
|
44
|
+
* @zh 是否強制要回傳 JSON 格式的資料
|
|
45
|
+
* @en Whether to force the return of JSON format data
|
|
46
|
+
*/
|
|
47
|
+
forceJsonFormat: boolean;
|
|
48
|
+
/**
|
|
49
|
+
* @zh 每次對話最多產生幾個 tokens。
|
|
50
|
+
* @en How many tokens to complete to.
|
|
51
|
+
*/
|
|
52
|
+
maxTokens?: number;
|
|
53
|
+
};
|
|
54
|
+
export declare class OpenAIChat {
|
|
55
|
+
openai: OpenAI;
|
|
56
|
+
config: Config;
|
|
57
|
+
constructor(openai: OpenAI);
|
|
58
|
+
/**
|
|
59
|
+
* @zh 改變對話的一些設定
|
|
60
|
+
* @en Change some settings of the conversation
|
|
61
|
+
*/
|
|
62
|
+
setConfig(options: Partial<Config>): void;
|
|
63
|
+
/**
|
|
64
|
+
* @zh 檢視內容是否符合 OpenAI 的審查
|
|
65
|
+
* @en View content for OpenAI moderation
|
|
66
|
+
*/
|
|
67
|
+
moderations(input: string): Promise<{
|
|
68
|
+
isSafe: boolean;
|
|
69
|
+
result: any;
|
|
70
|
+
}>;
|
|
71
|
+
/**
|
|
72
|
+
* @zh 進行對話
|
|
73
|
+
* @en Talk to the AI
|
|
74
|
+
*/
|
|
75
|
+
talk(messages?: ChatGPTMessage[], options?: {
|
|
76
|
+
/** 要 forceJsonFormat 為 true 才會生效 */
|
|
77
|
+
jsonSchema?: any;
|
|
78
|
+
abortController?: AbortController;
|
|
79
|
+
}): Promise<{
|
|
80
|
+
id: string;
|
|
81
|
+
text: string;
|
|
82
|
+
newMessages: ChatGPTMessage[];
|
|
83
|
+
isDone: boolean;
|
|
84
|
+
apiReseponse: ApiResponse;
|
|
85
|
+
}>;
|
|
86
|
+
talkStream(params: {
|
|
87
|
+
messages: any[];
|
|
88
|
+
onMessage: (message: string) => void;
|
|
89
|
+
onEnd: () => void;
|
|
90
|
+
onWarn: (warn: any) => void;
|
|
91
|
+
onError: (error: any) => void;
|
|
92
|
+
}): {
|
|
93
|
+
cancel: () => void;
|
|
94
|
+
};
|
|
95
|
+
/**
|
|
96
|
+
* @zh 開啟持續性對話
|
|
97
|
+
*/
|
|
98
|
+
keepTalk(prompt: string | string[], oldMessages?: ChatGPTMessage[]): Promise<{
|
|
99
|
+
result: {
|
|
100
|
+
id: string;
|
|
101
|
+
text: string;
|
|
102
|
+
newMessages: ChatGPTMessage[];
|
|
103
|
+
isDone: boolean;
|
|
104
|
+
apiReseponse: ApiResponse;
|
|
105
|
+
};
|
|
106
|
+
nextTalk: (prompt: string | string[]) => Promise<any>;
|
|
107
|
+
}>;
|
|
108
|
+
}
|
|
109
|
+
export type OpenAIChatTalkResponse = PromiseResponseType<OpenAIChat['talk']>;
|
|
110
|
+
export {};
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { OpenAI } from './index';
|
|
2
|
+
import { PromiseResponseType } from '../../types';
|
|
3
|
+
declare type Config = {
|
|
4
|
+
/**
|
|
5
|
+
* @zh 一次回應數量
|
|
6
|
+
* @en How many chat completion choices to generate for each input message.
|
|
7
|
+
*/
|
|
8
|
+
n: number;
|
|
9
|
+
/**
|
|
10
|
+
* @zh 最長回應長度,最大值為 4096。
|
|
11
|
+
* @en The token count of your prompt plus max_tokens cannot exceed the model's context length. Most models have a context length of 2048 tokens (except for the newest models, which support 4096).
|
|
12
|
+
* @see https://platform.openai.com/tokenizer
|
|
13
|
+
*/
|
|
14
|
+
maxTokens: number;
|
|
15
|
+
/**
|
|
16
|
+
* @zh 冒險指數,數值由 0 ~ 2 之間。
|
|
17
|
+
* @en What sampling temperature to use, between 0 and 2. Higher values like 0.8 will make the output more random, while lower values like 0.2 will make it more focused and deterministic.
|
|
18
|
+
*/
|
|
19
|
+
temperature: number;
|
|
20
|
+
};
|
|
21
|
+
declare type ApiResponse = {
|
|
22
|
+
id: string;
|
|
23
|
+
object: string;
|
|
24
|
+
created: number;
|
|
25
|
+
model: string;
|
|
26
|
+
choices: Array<{
|
|
27
|
+
text: string;
|
|
28
|
+
index: number;
|
|
29
|
+
logprobs: any;
|
|
30
|
+
finish_reason: string;
|
|
31
|
+
}>;
|
|
32
|
+
usage: {
|
|
33
|
+
prompt_tokens: number;
|
|
34
|
+
completion_tokens: number;
|
|
35
|
+
total_tokens: number;
|
|
36
|
+
};
|
|
37
|
+
};
|
|
38
|
+
export declare class OpenAICompletion {
|
|
39
|
+
private openai;
|
|
40
|
+
private config;
|
|
41
|
+
constructor(openai: OpenAI);
|
|
42
|
+
/**
|
|
43
|
+
* @zh 改變對話的一些設定。
|
|
44
|
+
* @en Change some settings of the chat.
|
|
45
|
+
*/
|
|
46
|
+
setConfig(options: Partial<Config>): void;
|
|
47
|
+
/**
|
|
48
|
+
* @zh 進行補文。
|
|
49
|
+
* @en Do completion.
|
|
50
|
+
*/
|
|
51
|
+
run(prompt: string | string[]): Promise<{
|
|
52
|
+
id: string;
|
|
53
|
+
text: string;
|
|
54
|
+
isDone: boolean;
|
|
55
|
+
apiReseponse: ApiResponse;
|
|
56
|
+
}>;
|
|
57
|
+
}
|
|
58
|
+
export declare type OpenAiOpenAICompletionResponse = PromiseResponseType<OpenAICompletion['run']>;
|
|
59
|
+
export {};
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { OpenAI } from './index';
|
|
2
|
+
type ApiResponse = {
|
|
3
|
+
created: string;
|
|
4
|
+
data: {
|
|
5
|
+
b64_json: string;
|
|
6
|
+
}[];
|
|
7
|
+
};
|
|
8
|
+
type Config = {
|
|
9
|
+
/**
|
|
10
|
+
* @zh 模型,支援 dall-e-2 和 dall-e-3
|
|
11
|
+
* @en Model, support dall-e-2 and dall-e-3
|
|
12
|
+
*/
|
|
13
|
+
model: 'dall-e-2' | 'dall-e-3';
|
|
14
|
+
/**
|
|
15
|
+
* @zh 解析度,例如 1024x1024
|
|
16
|
+
* @en Resolution, for example 1024x1024
|
|
17
|
+
*/
|
|
18
|
+
size: `${number}x${number}`;
|
|
19
|
+
};
|
|
20
|
+
export declare class OpenAIImagesGeneration {
|
|
21
|
+
private openai;
|
|
22
|
+
private config;
|
|
23
|
+
constructor(openai: OpenAI);
|
|
24
|
+
/**
|
|
25
|
+
* @zh 改變對話的一些設定
|
|
26
|
+
* @en Change some settings of the conversation
|
|
27
|
+
*/
|
|
28
|
+
setConfig(options: Partial<Config>): void;
|
|
29
|
+
/**
|
|
30
|
+
* @zh 產生圖片
|
|
31
|
+
* @en Generate image
|
|
32
|
+
*/
|
|
33
|
+
create(prompt: string): Promise<ApiResponse>;
|
|
34
|
+
}
|
|
35
|
+
export {};
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { OpenAIVision } from './vision';
|
|
2
|
+
import { OpenAIChat, Config } from './chat';
|
|
3
|
+
import { OpenAIImagesGeneration } from './images-generation';
|
|
4
|
+
export declare class OpenAI {
|
|
5
|
+
_axios: import("axios").AxiosInstance;
|
|
6
|
+
_apiKey: string;
|
|
7
|
+
static createChatRequest(apiKey: string | (() => Promise<string>), config?: Partial<Config> | (() => Promise<Partial<Config>>), options?: {
|
|
8
|
+
axios?: any;
|
|
9
|
+
}): (messages: any[], { onCancel }: any) => Promise<string>;
|
|
10
|
+
static createChatRequestWithJsonSchema(params: {
|
|
11
|
+
axios?: any;
|
|
12
|
+
apiKey: string | (() => Promise<string>);
|
|
13
|
+
config?: Partial<Pick<Config, 'model' | 'temperature'>> | (() => Promise<Partial<Pick<Config, 'model' | 'temperature'>>>);
|
|
14
|
+
}): (messages: any[], { schema, onCancel }: any) => Promise<string>;
|
|
15
|
+
constructor(apiKey?: string);
|
|
16
|
+
/**
|
|
17
|
+
* @zh 如果你有需要特別設定 axios,請使用這方法。
|
|
18
|
+
* @en If you need to set axios, please use this method.
|
|
19
|
+
*/
|
|
20
|
+
setAxios(axios: any): void;
|
|
21
|
+
/**
|
|
22
|
+
* @zh 設定 api key。
|
|
23
|
+
* @en Set api key.
|
|
24
|
+
*/
|
|
25
|
+
setConfiguration(apiKey: string): void;
|
|
26
|
+
createChat(): OpenAIChat;
|
|
27
|
+
createVision(): OpenAIVision;
|
|
28
|
+
createImagesGeneration(): OpenAIImagesGeneration;
|
|
29
|
+
}
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import { OpenAI } from './index';
|
|
2
|
+
import { PromiseResponseType } from '../../types';
|
|
3
|
+
type ImageContent = {
|
|
4
|
+
type: 'image_url' | 'text';
|
|
5
|
+
text?: string;
|
|
6
|
+
image_url?: {
|
|
7
|
+
url: string;
|
|
8
|
+
detail?: string;
|
|
9
|
+
};
|
|
10
|
+
};
|
|
11
|
+
type VisionMessage = {
|
|
12
|
+
role: 'system' | 'user' | 'assistant';
|
|
13
|
+
name?: string;
|
|
14
|
+
content: string | ImageContent[];
|
|
15
|
+
};
|
|
16
|
+
type ApiResponse = {
|
|
17
|
+
id: string;
|
|
18
|
+
object: string;
|
|
19
|
+
created: number;
|
|
20
|
+
model: string;
|
|
21
|
+
usage: {
|
|
22
|
+
prompt_tokens: number;
|
|
23
|
+
completion_tokens: number;
|
|
24
|
+
total_tokens: number;
|
|
25
|
+
};
|
|
26
|
+
choices: Array<{
|
|
27
|
+
message: {
|
|
28
|
+
role: string;
|
|
29
|
+
content: string;
|
|
30
|
+
};
|
|
31
|
+
finish_details: {
|
|
32
|
+
type: string;
|
|
33
|
+
};
|
|
34
|
+
index: number;
|
|
35
|
+
}>;
|
|
36
|
+
};
|
|
37
|
+
export type Config = {
|
|
38
|
+
/**
|
|
39
|
+
* @zh 選擇運行的模型。
|
|
40
|
+
* @en How many chat completion choices to generate for each input message.
|
|
41
|
+
*/
|
|
42
|
+
model: 'gpt-4-vision-preview';
|
|
43
|
+
/**
|
|
44
|
+
* @zh 冒險指數,數值由 0 ~ 2 之間。
|
|
45
|
+
* @en What sampling temperature to use, between 0 and 2. Higher values like 0.8 will make the output more random, while lower values like 0.2 will make it more focused and deterministic.
|
|
46
|
+
*/
|
|
47
|
+
temperature: number;
|
|
48
|
+
/**
|
|
49
|
+
* @zh 每次對話最多產生幾個 tokens。
|
|
50
|
+
* @en How many tokens to complete to.
|
|
51
|
+
*/
|
|
52
|
+
maxTokens?: number;
|
|
53
|
+
};
|
|
54
|
+
export declare class OpenAIVision {
|
|
55
|
+
openai: OpenAI;
|
|
56
|
+
config: Config;
|
|
57
|
+
constructor(openai: OpenAI);
|
|
58
|
+
/**
|
|
59
|
+
* @zh 改變對話的一些設定
|
|
60
|
+
* @en Change some settings of the conversation
|
|
61
|
+
*/
|
|
62
|
+
setConfig(options: Partial<Config>): void;
|
|
63
|
+
/**
|
|
64
|
+
* @zh 辨識圖片
|
|
65
|
+
* @en Recognize images
|
|
66
|
+
*/
|
|
67
|
+
view(messages: VisionMessage[]): Promise<{
|
|
68
|
+
id: string;
|
|
69
|
+
text: string;
|
|
70
|
+
apiReseponse: ApiResponse;
|
|
71
|
+
}>;
|
|
72
|
+
}
|
|
73
|
+
export type OpenAIChatVisionResponse = PromiseResponseType<OpenAIVision['view']>;
|
|
74
|
+
export {};
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
type JsonResponseFormat = {
|
|
2
|
+
desc: string;
|
|
3
|
+
example: any;
|
|
4
|
+
};
|
|
5
|
+
/**
|
|
6
|
+
* @zh 協助使用者將格式需求轉成論敘語句。
|
|
7
|
+
* @en Assist the user in converting the formatting requirements into declarative sentences.
|
|
8
|
+
*/
|
|
9
|
+
export declare const requireJsonResponse: (question: string | string[], format: Record<string, JsonResponseFormat>) => string;
|
|
10
|
+
/**
|
|
11
|
+
* @zh 協助使用者將格式需求轉成論敘語句,支援 Handlebars。
|
|
12
|
+
* @en Assist the user in converting the formatting requirements into declarative sentences, support Handlebars.
|
|
13
|
+
*/
|
|
14
|
+
export declare const requireJsonResponseWithHandlebars: (value: Record<string, any>, question: string | string[], format: Record<string, JsonResponseFormat>) => string;
|
|
15
|
+
/**
|
|
16
|
+
* @zh 協助使用者將格式需求轉成論敘語句,並支援 Json Schema。
|
|
17
|
+
* @en Assist the user in converting the formatting requirements into declarative sentences, and support Json Schema.
|
|
18
|
+
*/
|
|
19
|
+
export declare const requireJsonResponseWithJsonSchema: (question: string | string[], format: Record<string, any>) => string;
|
|
20
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export type PromiseResponseType<T extends (...args: any) => Promise<any>, R = Parameters<ReturnType<T>['then']>[0]> = R extends (value: infer P) => any ? P : never;
|