@nx-ddd/any-func 17.12.0
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/README.md +7 -0
- package/index.d.ts +1 -0
- package/index.js +5 -0
- package/index.js.map +1 -0
- package/lib/agent/agent.d.ts +53 -0
- package/lib/agent/agent.js +103 -0
- package/lib/agent/agent.js.map +1 -0
- package/lib/agent/index.d.ts +1 -0
- package/lib/agent/index.js +5 -0
- package/lib/agent/index.js.map +1 -0
- package/lib/any-func/any-func.service.d.ts +4 -0
- package/lib/any-func/any-func.service.impl.d.ts +48 -0
- package/lib/any-func/any-func.service.impl.js +166 -0
- package/lib/any-func/any-func.service.impl.js.map +1 -0
- package/lib/any-func/any-func.service.js +11 -0
- package/lib/any-func/any-func.service.js.map +1 -0
- package/lib/any-func/index.d.ts +1 -0
- package/lib/any-func/index.js +5 -0
- package/lib/any-func/index.js.map +1 -0
- package/lib/impl/index.d.ts +2 -0
- package/lib/impl/index.js +6 -0
- package/lib/impl/index.js.map +1 -0
- package/lib/impl/json-schema/index.d.ts +1 -0
- package/lib/impl/json-schema/index.js +5 -0
- package/lib/impl/json-schema/index.js.map +1 -0
- package/lib/impl/json-schema/json-schema.d.ts +2 -0
- package/lib/impl/json-schema/json-schema.js +43 -0
- package/lib/impl/json-schema/json-schema.js.map +1 -0
- package/lib/index.d.ts +1 -0
- package/lib/index.js +5 -0
- package/lib/index.js.map +1 -0
- package/lib/utils/debug-price.d.ts +1 -0
- package/lib/utils/debug-price.js +43 -0
- package/lib/utils/debug-price.js.map +1 -0
- package/lib/utils/index.d.ts +1 -0
- package/lib/utils/index.js +5 -0
- package/lib/utils/index.js.map +1 -0
- package/package.json +24 -0
package/README.md
ADDED
package/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './lib';
|
package/index.js
ADDED
package/index.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../packages/@nx-ddd/any-func/src/index.ts"],"names":[],"mappings":";;;AAAA,gDAAsB"}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { ChatCompletionMessageParam, FunctionParameters } from "openai/resources";
|
|
2
|
+
import { FunctionTool } from "openai/resources/beta/assistants";
|
|
3
|
+
import { ChatCompletionCreateParamsBase } from "openai/resources/chat/completions";
|
|
4
|
+
import { OpenAiServiceImpl } from '@nx-ddd/common/infrastructure/externals/openai/open-ai.service.impl';
|
|
5
|
+
declare const AI_TOOLS: "[ai-tools]";
|
|
6
|
+
declare const AI_SYSTEM: "[ai-system]";
|
|
7
|
+
export declare function addTool(obj: {
|
|
8
|
+
[AI_TOOLS]: object[];
|
|
9
|
+
}, tool: object): {
|
|
10
|
+
"[ai-tools]": object[];
|
|
11
|
+
};
|
|
12
|
+
export declare function getTools(obj: {
|
|
13
|
+
[AI_TOOLS]: object[];
|
|
14
|
+
}): any[];
|
|
15
|
+
export declare function buildFunctionTool(params: {
|
|
16
|
+
name: string;
|
|
17
|
+
description: string;
|
|
18
|
+
parameters: FunctionParameters;
|
|
19
|
+
}): FunctionTool;
|
|
20
|
+
export declare function buildToolFromSchema(name: string, description: string, schema: Function): FunctionTool;
|
|
21
|
+
export declare function AiFunction(params?: {
|
|
22
|
+
description?: string;
|
|
23
|
+
}): (target: any, propertyKey: string, descriptor: PropertyDescriptor) => PropertyDescriptor;
|
|
24
|
+
export interface AiSystemConfig {
|
|
25
|
+
model?: ChatCompletionCreateParamsBase['model'];
|
|
26
|
+
instructions?: string;
|
|
27
|
+
shots?: {
|
|
28
|
+
input: string;
|
|
29
|
+
output: string;
|
|
30
|
+
}[];
|
|
31
|
+
}
|
|
32
|
+
export declare function setAiSystemConfig(obj: {
|
|
33
|
+
[AI_SYSTEM]: AiSystemConfig;
|
|
34
|
+
}, config: AiSystemConfig): {
|
|
35
|
+
"[ai-system]": AiSystemConfig;
|
|
36
|
+
};
|
|
37
|
+
export declare function getAiSystemConfig(obj: {
|
|
38
|
+
[AI_SYSTEM]: AiSystemConfig;
|
|
39
|
+
}): AiSystemConfig;
|
|
40
|
+
export declare function AiSystem({ model, instructions, shots, }: AiSystemConfig): (target: any) => any;
|
|
41
|
+
export declare class AiToolsAgent {
|
|
42
|
+
protected openai: OpenAiServiceImpl;
|
|
43
|
+
constructor(openai: OpenAiServiceImpl);
|
|
44
|
+
messages: ChatCompletionMessageParam[];
|
|
45
|
+
results: any[];
|
|
46
|
+
get tools(): any[];
|
|
47
|
+
get config(): AiSystemConfig;
|
|
48
|
+
completion(tools?: any[]): Promise<void>;
|
|
49
|
+
execute<T = any>(message: string, options?: {
|
|
50
|
+
tools?: any[];
|
|
51
|
+
}): Promise<T>;
|
|
52
|
+
}
|
|
53
|
+
export {};
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.AiToolsAgent = exports.AiSystem = exports.getAiSystemConfig = exports.setAiSystemConfig = exports.AiFunction = exports.buildToolFromSchema = exports.buildFunctionTool = exports.getTools = exports.addTool = void 0;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
const json_schema_1 = require("../impl/json-schema");
|
|
6
|
+
const AI_TOOLS = '[ai-tools]';
|
|
7
|
+
const AI_SYSTEM = '[ai-system]';
|
|
8
|
+
function addTool(obj, tool) {
|
|
9
|
+
var _a;
|
|
10
|
+
obj[AI_TOOLS] = (_a = obj[AI_TOOLS]) !== null && _a !== void 0 ? _a : [];
|
|
11
|
+
obj[AI_TOOLS].push(tool);
|
|
12
|
+
return obj;
|
|
13
|
+
}
|
|
14
|
+
exports.addTool = addTool;
|
|
15
|
+
function getTools(obj) {
|
|
16
|
+
var _a;
|
|
17
|
+
return (_a = obj[AI_TOOLS]) !== null && _a !== void 0 ? _a : [];
|
|
18
|
+
}
|
|
19
|
+
exports.getTools = getTools;
|
|
20
|
+
function buildFunctionTool(params) {
|
|
21
|
+
return {
|
|
22
|
+
type: 'function',
|
|
23
|
+
function: {
|
|
24
|
+
name: params.name,
|
|
25
|
+
description: params.description,
|
|
26
|
+
parameters: params.parameters,
|
|
27
|
+
},
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
exports.buildFunctionTool = buildFunctionTool;
|
|
31
|
+
function buildToolFromSchema(name, description, schema) {
|
|
32
|
+
const parameters = (0, json_schema_1.targetConstructorToSchema)(schema);
|
|
33
|
+
return buildFunctionTool({ name, description, parameters });
|
|
34
|
+
}
|
|
35
|
+
exports.buildToolFromSchema = buildToolFromSchema;
|
|
36
|
+
function AiFunction(params) {
|
|
37
|
+
return function (target, propertyKey, descriptor) {
|
|
38
|
+
var _a;
|
|
39
|
+
// 引数の型を取得
|
|
40
|
+
const args = Reflect.getMetadata('design:paramtypes', target, propertyKey);
|
|
41
|
+
const tool = buildToolFromSchema(propertyKey, (_a = params === null || params === void 0 ? void 0 : params.description) !== null && _a !== void 0 ? _a : '', args[0]);
|
|
42
|
+
addTool(target, tool);
|
|
43
|
+
return descriptor;
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
exports.AiFunction = AiFunction;
|
|
47
|
+
function setAiSystemConfig(obj, config) {
|
|
48
|
+
obj[AI_SYSTEM] = config;
|
|
49
|
+
return obj;
|
|
50
|
+
}
|
|
51
|
+
exports.setAiSystemConfig = setAiSystemConfig;
|
|
52
|
+
function getAiSystemConfig(obj) {
|
|
53
|
+
return obj[AI_SYSTEM];
|
|
54
|
+
}
|
|
55
|
+
exports.getAiSystemConfig = getAiSystemConfig;
|
|
56
|
+
function AiSystem({ model = 'gpt-4-1106-preview', instructions = ``, shots = [], }) {
|
|
57
|
+
return function (target) {
|
|
58
|
+
setAiSystemConfig(target, { model, instructions, shots });
|
|
59
|
+
return target;
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
exports.AiSystem = AiSystem;
|
|
63
|
+
class AiToolsAgent {
|
|
64
|
+
constructor(openai) {
|
|
65
|
+
this.openai = openai;
|
|
66
|
+
this.messages = [
|
|
67
|
+
{ role: 'system', content: '' },
|
|
68
|
+
];
|
|
69
|
+
this.results = [];
|
|
70
|
+
}
|
|
71
|
+
get tools() {
|
|
72
|
+
return getTools(this.constructor);
|
|
73
|
+
}
|
|
74
|
+
get config() {
|
|
75
|
+
return getAiSystemConfig(this.constructor);
|
|
76
|
+
}
|
|
77
|
+
completion(tools = this.tools) {
|
|
78
|
+
var _a, _b, _c, _d;
|
|
79
|
+
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
80
|
+
const message = yield this.openai.chat.completions.create({
|
|
81
|
+
model: this.config.model,
|
|
82
|
+
messages: this.messages,
|
|
83
|
+
tools,
|
|
84
|
+
});
|
|
85
|
+
for (const call of (_d = (_c = (_b = (_a = message.choices) === null || _a === void 0 ? void 0 : _a[0]) === null || _b === void 0 ? void 0 : _b.message) === null || _c === void 0 ? void 0 : _c.tool_calls) !== null && _d !== void 0 ? _d : []) {
|
|
86
|
+
if (call.type === 'function') {
|
|
87
|
+
const func = this[call.function.name];
|
|
88
|
+
const result = func(JSON.parse(call.function.arguments));
|
|
89
|
+
this.results.push(result);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
});
|
|
93
|
+
}
|
|
94
|
+
execute(message, options = {}) {
|
|
95
|
+
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
96
|
+
this.messages.push({ role: 'user', content: message });
|
|
97
|
+
yield this.completion(options.tools);
|
|
98
|
+
return this.results.at(-1);
|
|
99
|
+
});
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
exports.AiToolsAgent = AiToolsAgent;
|
|
103
|
+
//# sourceMappingURL=agent.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"agent.js","sourceRoot":"","sources":["../../../../../../packages/@nx-ddd/any-func/src/lib/agent/agent.ts"],"names":[],"mappings":";;;;AAIA,qDAAgE;AAEhE,MAAM,QAAQ,GAAG,YAAqB,CAAC;AAC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Q;gBACvB,KAAK;aACN,CAAC,CAAC;YACH,KAAK,MAAM,IAAI,IAAI,MAAA,MAAA,MAAA,MAAA,OAAO,CAAC,OAAO,0CAAG,CAAC,CAAC,0CAAE,OAAO,0CAAE,UAAU,mCAAI,EAAE,EAAE;gBAClE,IAAI,IAAI,CAAC,IAAI,KAAK,UAAU,EAAE;oBAC5B,MAAM,IAAI,GAAI,IAAY,CAAC,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAC;oBAC/C,MAAM,MAAM,GAAG,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,QAAQ,CAAC,SAAS,CAAC,CAAC,CAAC;oBACzD,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;iBAC3B;aACF;;KACF;IAEK,OAAO,CAAQ,OAAe,EAAE,UAA2B,EAAE;;YACjE,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,EAAC,IAAI,EAAE,MAAM,EAAE,OAAO,EAAE,OAAO,EAAC,CAAC,CAAC;YACrD,MAAM,IAAI,CAAC,UAAU,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC;YACrC,OAAO,IAAI,CAAC,OAAO,CAAC,EAAE,CAAC,CAAC,CAAC,CAAQ,CAAC;QACpC,CAAC;KAAA;CACF;AArCD,oCAqCC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './agent';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../../../packages/@nx-ddd/any-func/src/lib/agent/index.ts"],"names":[],"mappings":";;;AAAA,kDAAwB"}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { InjectionToken } from '@angular/core';
|
|
2
|
+
import type { ChatCompletionMessageParam, FunctionParameters } from 'openai/resources';
|
|
3
|
+
export interface CallOptions<I, O, S extends {
|
|
4
|
+
input: I;
|
|
5
|
+
output: O;
|
|
6
|
+
} = {
|
|
7
|
+
input: I;
|
|
8
|
+
output: O;
|
|
9
|
+
}> {
|
|
10
|
+
instructions?: string;
|
|
11
|
+
model?: string;
|
|
12
|
+
specs?: S[];
|
|
13
|
+
embeddings?: false | ((spec: S) => string);
|
|
14
|
+
sample?: number;
|
|
15
|
+
temperature?: number;
|
|
16
|
+
maxRetry?: number;
|
|
17
|
+
}
|
|
18
|
+
interface AnyFuncConfig {
|
|
19
|
+
debug: boolean;
|
|
20
|
+
}
|
|
21
|
+
export declare function provideAnyFunc(config?: AnyFuncConfig): ({
|
|
22
|
+
provide: InjectionToken<AnyFuncConfig>;
|
|
23
|
+
useValue: AnyFuncConfig;
|
|
24
|
+
}[] | {
|
|
25
|
+
provide: InjectionToken<AnyFuncService>;
|
|
26
|
+
useClass: typeof AnyFuncService;
|
|
27
|
+
})[];
|
|
28
|
+
export declare class AnyFuncService {
|
|
29
|
+
protected readonly openai: import("../../../../../../dist/packages/@nx-ddd/common/infrastructure/externals/openai/open-ai.service.impl").OpenAiServiceImpl;
|
|
30
|
+
protected readonly config: AnyFuncConfig;
|
|
31
|
+
jsonSchema(_input: any, jsonSchema: FunctionParameters, { instructions, model, specs, embeddings, sample, temperature, maxRetry, }?: CallOptions<any, any>): Promise<any>;
|
|
32
|
+
call<I, O>(_input: I, args: {
|
|
33
|
+
new (): O;
|
|
34
|
+
}, { instructions, model, specs, embeddings, sample, temperature, maxRetry, }?: CallOptions<I, O>): Promise<O>;
|
|
35
|
+
protected complete<I, O>(_input: any, jsonSchema: FunctionParameters, messages: ChatCompletionMessageParam[], { model, temperature, }?: CallOptions<I, O> & {
|
|
36
|
+
functionOutput?: object;
|
|
37
|
+
}): Promise<import("openai/resources").ChatCompletionMessage>;
|
|
38
|
+
protected buildChatMessages(specs: {
|
|
39
|
+
input: any;
|
|
40
|
+
output: any;
|
|
41
|
+
}[]): {
|
|
42
|
+
role: 'assistant' | 'user';
|
|
43
|
+
content: string;
|
|
44
|
+
}[];
|
|
45
|
+
sort<T>(items: T[], accessor: (item: T) => string, target: string): Promise<T[]>;
|
|
46
|
+
private parseInput;
|
|
47
|
+
}
|
|
48
|
+
export {};
|
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.AnyFuncService = exports.provideAnyFunc = void 0;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
const core_1 = require("@angular/core");
|
|
6
|
+
const openai_1 = require("@nx-ddd/common/infrastructure/externals/openai");
|
|
7
|
+
const rxjs_1 = require("rxjs");
|
|
8
|
+
const json_schema_library_1 = require("json-schema-library");
|
|
9
|
+
const agent_1 = require("../agent");
|
|
10
|
+
const json_schema_1 = require("../impl/json-schema");
|
|
11
|
+
const any_func_service_1 = require("./any-func.service");
|
|
12
|
+
const ANY_FUNC_CONFIG = new core_1.InjectionToken('[@nx-ddd/any-func] AnyFuncConfig');
|
|
13
|
+
function provideAnyFuncConfig(config) {
|
|
14
|
+
return [
|
|
15
|
+
{ provide: ANY_FUNC_CONFIG, useValue: config },
|
|
16
|
+
];
|
|
17
|
+
}
|
|
18
|
+
function provideAnyFunc(config) {
|
|
19
|
+
return [
|
|
20
|
+
config ? provideAnyFuncConfig(config) : [],
|
|
21
|
+
{ provide: any_func_service_1.ANY_FUNC_SERVICE, useClass: AnyFuncService },
|
|
22
|
+
];
|
|
23
|
+
}
|
|
24
|
+
exports.provideAnyFunc = provideAnyFunc;
|
|
25
|
+
let AnyFuncService = class AnyFuncService {
|
|
26
|
+
constructor() {
|
|
27
|
+
var _a;
|
|
28
|
+
this.openai = (0, openai_1.injectOpenAiService)();
|
|
29
|
+
this.config = (_a = (0, core_1.inject)(ANY_FUNC_CONFIG, { optional: true })) !== null && _a !== void 0 ? _a : { debug: false };
|
|
30
|
+
}
|
|
31
|
+
jsonSchema(_input, jsonSchema, { instructions = '', model = 'gpt-4o', specs = [], embeddings = false, sample = 3, temperature = 0, maxRetry = 3, } = {}) {
|
|
32
|
+
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
33
|
+
const validator = new json_schema_library_1.Draft07(jsonSchema);
|
|
34
|
+
for (const spec of specs) {
|
|
35
|
+
const errors = validator.validate(spec.output);
|
|
36
|
+
if (errors.length > 0) {
|
|
37
|
+
throw new Error(JSON.stringify(errors, null, 2));
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
const sortedSpecs = embeddings ? yield this.sort(specs, embeddings, _input) : specs;
|
|
41
|
+
const messages = [
|
|
42
|
+
{ role: 'system', content: instructions },
|
|
43
|
+
...(this.buildChatMessages(sortedSpecs.slice(0, sample).reverse())),
|
|
44
|
+
{ role: 'user', content: this.parseInput(_input) },
|
|
45
|
+
];
|
|
46
|
+
return (0, rxjs_1.lastValueFrom)((0, rxjs_1.of)(null).pipe((0, rxjs_1.map)(() => messages), (0, rxjs_1.switchMap)(() => this.complete(_input, jsonSchema, messages, {
|
|
47
|
+
model,
|
|
48
|
+
sample,
|
|
49
|
+
temperature,
|
|
50
|
+
})), (0, rxjs_1.tap)(message => messages.push(message)), (0, rxjs_1.map)((message) => {
|
|
51
|
+
const output = JSON.parse(message.tool_calls[0].function.arguments);
|
|
52
|
+
const errors = new json_schema_library_1.Draft07(jsonSchema).validate(output);
|
|
53
|
+
if (errors.length > 0) {
|
|
54
|
+
messages.push({
|
|
55
|
+
role: 'tool',
|
|
56
|
+
content: JSON.stringify(errors),
|
|
57
|
+
tool_call_id: message.tool_calls[0].id,
|
|
58
|
+
});
|
|
59
|
+
if (this.config.debug) {
|
|
60
|
+
console.debug('messages', JSON.stringify(messages, null, 2));
|
|
61
|
+
}
|
|
62
|
+
throw new Error(JSON.stringify(errors, null, 2));
|
|
63
|
+
}
|
|
64
|
+
else {
|
|
65
|
+
messages.push({
|
|
66
|
+
role: 'tool',
|
|
67
|
+
content: JSON.stringify(output),
|
|
68
|
+
tool_call_id: message.tool_calls[0].id,
|
|
69
|
+
});
|
|
70
|
+
if (this.config.debug) {
|
|
71
|
+
console.debug('messages', JSON.stringify(messages, null, 2));
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
return output;
|
|
75
|
+
}), (0, rxjs_1.catchError)((error) => {
|
|
76
|
+
console.error(error);
|
|
77
|
+
throw error;
|
|
78
|
+
}), (0, rxjs_1.retry)(maxRetry)));
|
|
79
|
+
// debugPrice(model, res.usage);
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
call(_input, args, { instructions = '', model = 'gpt-4o', specs = [], embeddings = false, sample = 3, temperature = 0, maxRetry = 3, } = {}) {
|
|
83
|
+
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
84
|
+
return this.jsonSchema(_input, (0, json_schema_1.targetConstructorToSchema)(args), {
|
|
85
|
+
instructions,
|
|
86
|
+
model,
|
|
87
|
+
specs,
|
|
88
|
+
embeddings,
|
|
89
|
+
sample,
|
|
90
|
+
temperature,
|
|
91
|
+
maxRetry,
|
|
92
|
+
});
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
complete(_input, jsonSchema, messages, { model = 'gpt-4o', temperature = 0, } = {}) {
|
|
96
|
+
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
97
|
+
const tool = (0, agent_1.buildFunctionTool)({ name: 'function', description: '', parameters: jsonSchema });
|
|
98
|
+
const res = yield this.openai.chat.completions.create({
|
|
99
|
+
model,
|
|
100
|
+
messages,
|
|
101
|
+
tools: [tool],
|
|
102
|
+
tool_choice: {
|
|
103
|
+
type: 'function',
|
|
104
|
+
function: { name: 'function' }
|
|
105
|
+
},
|
|
106
|
+
temperature,
|
|
107
|
+
});
|
|
108
|
+
return res.choices[0].message;
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
buildChatMessages(specs) {
|
|
112
|
+
return specs.map(({ input, output }) => {
|
|
113
|
+
return [
|
|
114
|
+
{ role: 'user', content: this.parseInput(input) },
|
|
115
|
+
{
|
|
116
|
+
role: 'assistant',
|
|
117
|
+
content: null,
|
|
118
|
+
tool_calls: [
|
|
119
|
+
{
|
|
120
|
+
id: 'call-example',
|
|
121
|
+
type: 'function',
|
|
122
|
+
function: {
|
|
123
|
+
name: 'function',
|
|
124
|
+
arguments: JSON.stringify(output),
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
],
|
|
128
|
+
},
|
|
129
|
+
{ role: 'tool', content: 'success', tool_call_id: 'call-example' },
|
|
130
|
+
];
|
|
131
|
+
}).flat();
|
|
132
|
+
}
|
|
133
|
+
sort(items, accessor, target) {
|
|
134
|
+
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
135
|
+
const getEmbeddings = (item) => {
|
|
136
|
+
return this.openai.embeddings.create({
|
|
137
|
+
model: 'text-embedding-3-small',
|
|
138
|
+
input: accessor(item),
|
|
139
|
+
}).then(res => res.data[0].embedding);
|
|
140
|
+
};
|
|
141
|
+
const dist = (a, b) => Math.sqrt(a.reduce((acc, v, i) => acc + Math.pow((v - b[i]), 2), 0));
|
|
142
|
+
const targetEmbeddings = yield this.openai.embeddings.create({
|
|
143
|
+
model: 'text-embedding-3-small',
|
|
144
|
+
input: target,
|
|
145
|
+
}).then(res => res.data[0].embedding);
|
|
146
|
+
const embeddings = yield Promise.all(items.map(getEmbeddings));
|
|
147
|
+
const items_ = items.map((item, i) => ({
|
|
148
|
+
item,
|
|
149
|
+
embedding: embeddings[i],
|
|
150
|
+
distance: dist(embeddings[i], targetEmbeddings),
|
|
151
|
+
}));
|
|
152
|
+
const sorted = items_.sort((a, b) => a.distance - b.distance);
|
|
153
|
+
// console.debug('target:', target);
|
|
154
|
+
// console.debug('sorted:', sorted.map(({item, distance}) => `${distance}: ${accessor(item)}`));
|
|
155
|
+
return sorted.map(({ item }) => item);
|
|
156
|
+
});
|
|
157
|
+
}
|
|
158
|
+
parseInput(input) {
|
|
159
|
+
return typeof input === 'string' ? input : JSON.stringify(input);
|
|
160
|
+
}
|
|
161
|
+
};
|
|
162
|
+
exports.AnyFuncService = AnyFuncService;
|
|
163
|
+
exports.AnyFuncService = AnyFuncService = tslib_1.__decorate([
|
|
164
|
+
(0, core_1.Injectable)({ providedIn: 'root' })
|
|
165
|
+
], AnyFuncService);
|
|
166
|
+
//# sourceMappingURL=any-func.service.impl.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"any-func.service.impl.js","sourceRoot":"","sources":["../../../../../../packages/@nx-ddd/any-func/src/lib/any-func/any-func.service.impl.ts"],"names":[],"mappings":";;;;AAAA,wCAAmE;AACnE,2EAAqF;AACrF,+BAAiF;AAEjF,6DAA8C;AAC9C,oCAA6C;AAC7C,qDAAgE;AAChE,yDAAsD;AAgBtD,MAAM,eAAe,GAAG,IAAI,qBAAc,CAAgB,kCAAkC,CAAC,CAAC;AAE9F,SAAS,oBAAoB,CAAC,MAAqB;IACjD,OAAO;QACL,EAAE,OAAO,EAAE,eAAe,EAAE,QAAQ,EAAE,MAAM,EAAE;KAC/C,CAAC;AACJ,CAAC;AAED,SAAgB,cAAc,CAAC,MAAsB;IACnD,OAAO;QACL,MAAM,CAAC,CAAC,CAAC,oBAAoB,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,EAAE;QAC1C,EAAE,OAAO,EAAE,mCAAgB,EAAE,QAAQ,EAAE,cAAc,EAAE;KACxD,CAAA;AACH,CAAC;AALD,wCAKC;AAGM,IAAM,cAAc,GAApB,MAAM,cAAc;IAApB;;QACc,WAAM,GAAG,IAAA,4BAAmB,GAAE,CAAC;QAC/B,WAAM,GAAG,MAAA,IAAA,aAAM,EAAC,eAAe,EAAE,EAAC,QAAQ,EAAE,IAAI,EAAC,CAAC,mCAAI,EAAE,KAAK,EAAE,KAAK,EAAE,CAAC;IAuK5F,CAAC;IArKO,UAAU,CAAC,MAAW,EAAE,UAA8B,EAAE,EAC5D,YAAY,GAAG,EAAE,EACjB,KAAK,GAAG,QAAQ,EAChB,KAAK,GAAG,EAAE,EACV,UAAU,GAAG,KAAK,EAClB,MAAM,GAAG,CAAC,EACV,WAAW,GAAG,CAA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C,CAAC;GACpB,cAAc,CAyK1B"}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.injectAnyFunc = exports.ANY_FUNC_SERVICE = void 0;
|
|
4
|
+
const core_1 = require("@angular/core");
|
|
5
|
+
exports.ANY_FUNC_SERVICE = new core_1.InjectionToken('[@nx-ddd/any-func] AnyFuncService');
|
|
6
|
+
function injectAnyFunc() {
|
|
7
|
+
var _a;
|
|
8
|
+
return (_a = (0, core_1.inject)(exports.ANY_FUNC_SERVICE, { optional: true })) !== null && _a !== void 0 ? _a : {};
|
|
9
|
+
}
|
|
10
|
+
exports.injectAnyFunc = injectAnyFunc;
|
|
11
|
+
//# sourceMappingURL=any-func.service.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"any-func.service.js","sourceRoot":"","sources":["../../../../../../packages/@nx-ddd/any-func/src/lib/any-func/any-func.service.ts"],"names":[],"mappings":";;;AAAA,wCAAuD;AAG1C,QAAA,gBAAgB,GAAG,IAAI,qBAAc,CAAiB,mCAAmC,CAAC,CAAC;AAExG,SAAgB,aAAa;;IAC3B,OAAO,MAAA,IAAA,aAAM,EAAC,wBAAgB,EAAE,EAAC,QAAQ,EAAE,IAAI,EAAC,CAAC,mCAAI,EAAoB,CAAC;AAC5E,CAAC;AAFD,sCAEC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './any-func.service';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../../../packages/@nx-ddd/any-func/src/lib/any-func/index.ts"],"names":[],"mappings":";;;AAAA,6DAAmC"}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const tslib_1 = require("tslib");
|
|
4
|
+
tslib_1.__exportStar(require("../any-func/any-func.service.impl"), exports);
|
|
5
|
+
tslib_1.__exportStar(require("./json-schema"), exports);
|
|
6
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../../../packages/@nx-ddd/any-func/src/lib/impl/index.ts"],"names":[],"mappings":";;;AAAA,4EAAkD;AAClD,wDAA8B"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './json-schema';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../../../../packages/@nx-ddd/any-func/src/lib/impl/json-schema/index.ts"],"names":[],"mappings":";;;AAAA,wDAA8B"}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.targetConstructorToSchema = void 0;
|
|
4
|
+
const class_validator_1 = require("class-validator");
|
|
5
|
+
const class_validator_jsonschema_1 = require("class-validator-jsonschema");
|
|
6
|
+
// @ts-ignore
|
|
7
|
+
const { defaultMetadataStorage } = require('class-transformer/cjs/storage');
|
|
8
|
+
function getPropType(target, property) {
|
|
9
|
+
return Reflect.getMetadata('design:type', target, property);
|
|
10
|
+
}
|
|
11
|
+
function targetToSchema(type, options) {
|
|
12
|
+
if (typeof type === 'function') {
|
|
13
|
+
if (type.prototype === String.prototype ||
|
|
14
|
+
type.prototype === Symbol.prototype) {
|
|
15
|
+
return { type: 'string' };
|
|
16
|
+
}
|
|
17
|
+
else if (type.prototype === Number.prototype) {
|
|
18
|
+
return { type: 'number' };
|
|
19
|
+
}
|
|
20
|
+
else if (type.prototype === Boolean.prototype) {
|
|
21
|
+
return { type: 'boolean' };
|
|
22
|
+
}
|
|
23
|
+
return targetConstructorToSchema(type, options);
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
const additionalConverters = {
|
|
27
|
+
[class_validator_1.ValidationTypes.NESTED_VALIDATION]: (meta, options) => {
|
|
28
|
+
if (typeof meta.target === 'function') {
|
|
29
|
+
const typeMeta = options.classTransformerMetadataStorage
|
|
30
|
+
? options.classTransformerMetadataStorage.findTypeMetadata(meta.target, meta.propertyName)
|
|
31
|
+
: null;
|
|
32
|
+
const childType = typeMeta
|
|
33
|
+
? typeMeta.typeFunction()
|
|
34
|
+
: getPropType(meta.target.prototype, meta.propertyName);
|
|
35
|
+
return targetToSchema(childType, options);
|
|
36
|
+
}
|
|
37
|
+
},
|
|
38
|
+
};
|
|
39
|
+
function targetConstructorToSchema(target, options = {}) {
|
|
40
|
+
return (0, class_validator_jsonschema_1.targetConstructorToSchema)(target, Object.assign(Object.assign({}, options), { additionalConverters, classTransformerMetadataStorage: defaultMetadataStorage }));
|
|
41
|
+
}
|
|
42
|
+
exports.targetConstructorToSchema = targetConstructorToSchema;
|
|
43
|
+
//# sourceMappingURL=json-schema.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"json-schema.js","sourceRoot":"","sources":["../../../../../../../packages/@nx-ddd/any-func/src/lib/impl/json-schema/json-schema.ts"],"names":[],"mappings":";;;AAAA,qDAAiD;AACjD,2EAAoG;AAGpG,aAAa;AACb,MAAM,EAAE,sBAAsB,EAAE,GAAG,OAAO,CAAC,+BAA+B,CAAC,CAAC;AAE5E,SAAS,WAAW,CAAC,MAAc,EAAE,QAAgB;IACnD,OAAO,OAAO,CAAC,WAAW,CAAC,aAAa,EAAE,MAAM,EAAE,QAAQ,CAAC,CAAA;AAC7D,CAAC;AAED,SAAS,cAAc,CACrB,IAAS,EACT,OAAY;IAEZ,IAAI,OAAO,IAAI,KAAK,UAAU,EAAE;QAC9B,IACE,IAAI,CAAC,SAAS,KAAK,MAAM,CAAC,SAAS;YACnC,IAAI,CAAC,SAAS,KAAK,MAAM,CAAC,SAAS,EACnC;YACA,OAAO,EAAE,IAAI,EAAE,QAAQ,EAAE,CAAA;SAC1B;aAAM,IAAI,IAAI,CAAC,SAAS,KAAK,MAAM,CAAC,SAAS,EAAE;YAC9C,OAAO,EAAE,IAAI,EAAE,QAAQ,EAAE,CAAA;SAC1B;aAAM,IAAI,IAAI,CAAC,SAAS,KAAK,OAAO,CAAC,SAAS,EAAE;YAC/C,OAAO,EAAE,IAAI,EAAE,SAAS,EAAE,CAAA;SAC3B;QAED,OAAO,yBAAyB,CAAC,IAAI,EAAE,OAAO,CAAC,CAAA;KAChD;AACH,CAAC;AAED,MAAM,oBAAoB,GAAE;IAC1B,CAAC,iCAAe,CAAC,iBAAiB,CAAC,EAAE,CAAC,IAAS,EAAE,OAAY,EAAE,EAAE;QAC/D,IAAI,OAAO,IAAI,CAAC,MAAM,KAAK,UAAU,EAAE;YACrC,MAAM,QAAQ,GAAG,OAAO,CAAC,+BAA+B;gBACtD,CAAC,CAAC,OAAO,CAAC,+BAA+B,CAAC,gBAAgB,CACtD,IAAI,CAAC,MAAM,EACX,IAAI,CAAC,YAAY,CAClB;gBACH,CAAC,CAAC,IAAI,CAAA;YACR,MAAM,SAAS,GAAG,QAAQ;gBACxB,CAAC,CAAC,QAAQ,CAAC,YAAY,EAAE;gBACzB,CAAC,CAAC,WAAW,CAAC,IAAI,CAAC,MAAM,CAAC,SAAS,EAAE,IAAI,CAAC,YAAY,CAAC,CAAA;YACzD,OAAO,cAAc,CAAC,SAAS,EAAE,OAAO,CAAC,CAAA;SAC1C;IACH,CAAC;CACF,CAAC;AAEF,SAAgB,yBAAyB,CAAC,MAAgB,EAAE,UAA6B,EAAE;IACzF,OAAO,IAAA,sDAA0B,EAAC,MAAM,kCACnC,OAAO,KACV,oBAAoB,EACpB,+BAA+B,EAAE,sBAAsB,IACvD,CAAC;AACL,CAAC;AAND,8DAMC"}
|
package/lib/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './any-func';
|
package/lib/index.js
ADDED
package/lib/index.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../../packages/@nx-ddd/any-func/src/lib/index.ts"],"names":[],"mappings":";;;AAAA,qDAA2B"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function debugPrice(model: string, usage: any): void;
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.debugPrice = void 0;
|
|
4
|
+
const PRICE_TABLE = {
|
|
5
|
+
'gpt-3.5-turbo-0125': {
|
|
6
|
+
input: 0.5 / 1000000,
|
|
7
|
+
output: 1.5 / 1000000,
|
|
8
|
+
},
|
|
9
|
+
'gpt-4o': {
|
|
10
|
+
input: 5 / 1000000,
|
|
11
|
+
output: 15 / 1000000,
|
|
12
|
+
}
|
|
13
|
+
};
|
|
14
|
+
function debugPrice(model, usage) {
|
|
15
|
+
// 価格を計算
|
|
16
|
+
const price = PRICE_TABLE[model];
|
|
17
|
+
const inputPrice = usage.prompt_tokens * price.input;
|
|
18
|
+
const outputPrice = usage.completion_tokens * price.output;
|
|
19
|
+
// k桁で丸める
|
|
20
|
+
const round = (num, k) => Math.round(num * Math.pow(10, k)) / Math.pow(10, k);
|
|
21
|
+
console.table([
|
|
22
|
+
{
|
|
23
|
+
name: 'input',
|
|
24
|
+
'price(us)': `$${round(inputPrice, 4)}`,
|
|
25
|
+
'price(jp)': `¥${round(inputPrice * 150, 4)}`,
|
|
26
|
+
token: usage.prompt_tokens
|
|
27
|
+
},
|
|
28
|
+
{
|
|
29
|
+
name: 'output',
|
|
30
|
+
'price(us)': `$${round(outputPrice, 4)}`,
|
|
31
|
+
'price(jp)': `¥${round(outputPrice * 150, 4)}`,
|
|
32
|
+
token: usage.completion_tokens,
|
|
33
|
+
},
|
|
34
|
+
{
|
|
35
|
+
name: 'total',
|
|
36
|
+
'price(us)': `$${round((inputPrice + outputPrice), 4)}`,
|
|
37
|
+
'price(jp)': `¥${round((inputPrice + outputPrice) * 150, 4)}`,
|
|
38
|
+
token: usage.total_tokens,
|
|
39
|
+
},
|
|
40
|
+
]);
|
|
41
|
+
}
|
|
42
|
+
exports.debugPrice = debugPrice;
|
|
43
|
+
//# sourceMappingURL=debug-price.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"debug-price.js","sourceRoot":"","sources":["../../../../../../packages/@nx-ddd/any-func/src/lib/utils/debug-price.ts"],"names":[],"mappings":";;;AAAA,MAAM,WAAW,GAAoD;IACnE,oBAAoB,EAAE;QACpB,KAAK,EAAE,GAAG,GAAG,OAAS;QACtB,MAAM,EAAE,GAAG,GAAG,OAAS;KACxB;IACD,QAAQ,EAAE;QACR,KAAK,EAAE,CAAC,GAAG,OAAS;QACpB,MAAM,EAAE,EAAE,GAAG,OAAS;KACvB;CACF,CAAC;AAEF,SAAgB,UAAU,CAAC,KAAa,EAAE,KAAU;IAClD,QAAQ;IACR,MAAM,KAAK,GAAG,WAAW,CAAC,KAAK,CAAC,CAAC;IACjC,MAAM,UAAU,GAAG,KAAK,CAAC,aAAa,GAAG,KAAK,CAAC,KAAK,CAAC;IACrD,MAAM,WAAW,GAAG,KAAK,CAAC,iBAAiB,GAAG,KAAK,CAAC,MAAM,CAAC;IAC3D,SAAS;IACT,MAAM,KAAK,GAAG,CAAC,GAAW,EAAE,CAAS,EAAE,EAAE,CAAC,IAAI,CAAC,KAAK,CAAC,GAAG,GAAG,SAAA,EAAE,EAAI,CAAC,CAAA,CAAC,GAAG,SAAA,EAAE,EAAI,CAAC,CAAA,CAAC;IAC9E,OAAO,CAAC,KAAK,CAAC;QACZ;YACE,IAAI,EAAE,OAAO;YACb,WAAW,EAAE,IAAI,KAAK,CAAC,UAAU,EAAE,CAAC,CAAC,EAAE;YACvC,WAAW,EAAE,IAAI,KAAK,CAAC,UAAU,GAAG,GAAG,EAAE,CAAC,CAAC,EAAE;YAC7C,KAAK,EAAE,KAAK,CAAC,aAAa;SAC3B;QACD;YACE,IAAI,EAAE,QAAQ;YACd,WAAW,EAAE,IAAI,KAAK,CAAC,WAAW,EAAE,CAAC,CAAC,EAAE;YACxC,WAAW,EAAE,IAAI,KAAK,CAAC,WAAW,GAAG,GAAG,EAAE,CAAC,CAAC,EAAE;YAC9C,KAAK,EAAE,KAAK,CAAC,iBAAiB;SAC/B;QACD;YACE,IAAI,EAAE,OAAO;YACb,WAAW,EAAE,IAAI,KAAK,CAAC,CAAC,UAAU,GAAG,WAAW,CAAC,EAAE,CAAC,CAAC,EAAE;YACvD,WAAW,EAAE,IAAI,KAAK,CAAC,CAAC,UAAU,GAAG,WAAW,CAAC,GAAG,GAAG,EAAE,CAAC,CAAC,EAAE;YAC7D,KAAK,EAAE,KAAK,CAAC,YAAY;SAC1B;KACF,CAAC,CAAC;AACL,CAAC;AA3BD,gCA2BC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './debug-price';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../../../packages/@nx-ddd/any-func/src/lib/utils/index.ts"],"names":[],"mappings":";;;AAAA,wDAA8B"}
|
package/package.json
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@nx-ddd/any-func",
|
|
3
|
+
"version": "17.12.0",
|
|
4
|
+
"main": "./src/index.js",
|
|
5
|
+
"types": "./index.d.ts",
|
|
6
|
+
"homepage": "https://github.com/xx-machina/plaform/tree/main/packages/@nx-ddd/any-func",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "https://github.com/xx-machina/plaform.git"
|
|
10
|
+
},
|
|
11
|
+
"peerDependencies": {
|
|
12
|
+
"@angular/core": "^17.0.0",
|
|
13
|
+
"class-transformer": "^0.5.1",
|
|
14
|
+
"class-validator-jsonschema": "^5.0.0",
|
|
15
|
+
"json-schema-library": "^9.3.5",
|
|
16
|
+
"openai": "^4.37.0",
|
|
17
|
+
"rxjs": "^7.8.0",
|
|
18
|
+
"tslib": "^2.3.0",
|
|
19
|
+
"@nx-ddd/common": "*",
|
|
20
|
+
"class-validator": "^0.14.0"
|
|
21
|
+
},
|
|
22
|
+
"dependencies": {},
|
|
23
|
+
"type": "commonjs"
|
|
24
|
+
}
|