ai-functions 0.2.16 → 0.2.18
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/dist/mjs/functions/ai.d.ts +2 -0
- package/dist/mjs/functions/ai.js +7 -3
- package/dist/mjs/functions/ai.test.js +4 -4
- package/functions/ai.test.ts +4 -4
- package/functions/ai.ts +7 -3
- package/package.json +1 -1
- package/dist/cjs/db/cache.d.ts +0 -1
- package/dist/cjs/db/cache.js +0 -12
- package/dist/cjs/db/mongo.d.ts +0 -31
- package/dist/cjs/db/mongo.js +0 -47
- package/dist/cjs/examples/data.d.ts +0 -1105
- package/dist/cjs/examples/data.js +0 -1108
- package/dist/cjs/functions/ai.d.ts +0 -18
- package/dist/cjs/functions/ai.js +0 -99
- package/dist/cjs/functions/ai.test.d.ts +0 -1
- package/dist/cjs/functions/ai.test.js +0 -40
- package/dist/cjs/functions/gpt.d.ts +0 -4
- package/dist/cjs/functions/gpt.js +0 -24
- package/dist/cjs/functions/list.d.ts +0 -7
- package/dist/cjs/functions/list.js +0 -134
- package/dist/cjs/index.d.ts +0 -3
- package/dist/cjs/index.js +0 -19
- package/dist/cjs/package.json +0 -3
- package/dist/cjs/queue/kafka.d.ts +0 -0
- package/dist/cjs/queue/kafka.js +0 -1
- package/dist/cjs/queue/memory.d.ts +0 -0
- package/dist/cjs/queue/memory.js +0 -1
- package/dist/cjs/queue/mongo.d.ts +0 -30
- package/dist/cjs/queue/mongo.js +0 -69
- package/dist/cjs/streams/kafka.d.ts +0 -0
- package/dist/cjs/streams/kafka.js +0 -1
- package/dist/cjs/streams/memory.d.ts +0 -0
- package/dist/cjs/streams/memory.js +0 -1
- package/dist/cjs/streams/mongo.d.ts +0 -0
- package/dist/cjs/streams/mongo.js +0 -1
- package/dist/cjs/streams/types.d.ts +0 -0
- package/dist/cjs/streams/types.js +0 -1
- package/dist/cjs/types.d.ts +0 -11
- package/dist/cjs/types.js +0 -2
- package/dist/cjs/utils/completion.d.ts +0 -9
- package/dist/cjs/utils/completion.js +0 -45
- package/dist/cjs/utils/schema.d.ts +0 -10
- package/dist/cjs/utils/schema.js +0 -77
- package/dist/cjs/utils/schema.test.d.ts +0 -1
- package/dist/cjs/utils/schema.test.js +0 -62
- package/dist/cjs/utils/state.d.ts +0 -1
- package/dist/cjs/utils/state.js +0 -21
- package/dist/mjs/package.json +0 -3
|
@@ -14,5 +14,7 @@ type AIFunctions<T = Record<string, string>> = Record<string, (returnSchema: T,
|
|
|
14
14
|
export declare const AI: (config?: AIConfig) => {
|
|
15
15
|
ai: AIFunctions<Record<string, string>>;
|
|
16
16
|
openai: OpenAI;
|
|
17
|
+
gpt: (strings: string[], ...values: string[]) => Promise<string | null>;
|
|
18
|
+
list: (strings: string[], ...values: string[]) => AsyncGenerator<string | undefined, void, unknown>;
|
|
17
19
|
};
|
|
18
20
|
export {};
|
package/dist/mjs/functions/ai.js
CHANGED
|
@@ -2,9 +2,13 @@ import { OpenAI, } from 'openai';
|
|
|
2
2
|
// import { AIDB, AIDBConfig } from '../db/mongo'
|
|
3
3
|
import { dump } from 'js-yaml';
|
|
4
4
|
import { generateSchema } from '../utils/schema';
|
|
5
|
+
import { GPT } from './gpt';
|
|
6
|
+
import { StreamingList } from './list';
|
|
5
7
|
export const AI = (config = {}) => {
|
|
6
|
-
const { model = 'gpt-4-
|
|
8
|
+
const { model = 'gpt-4-turbo-preview', system, ...rest } = config;
|
|
7
9
|
const openai = config.openai ?? new OpenAI(rest);
|
|
10
|
+
const { gpt } = GPT(config);
|
|
11
|
+
const { list } = StreamingList(config);
|
|
8
12
|
// const { client, db, cache, events, queue } = config.db ? AIDB(config.db) : {}
|
|
9
13
|
// const prompt = {
|
|
10
14
|
// model,
|
|
@@ -20,7 +24,7 @@ export const AI = (config = {}) => {
|
|
|
20
24
|
get: (target, functionName, receiver) => {
|
|
21
25
|
target[functionName] = (returnSchema, options) => async (args, callOptions) => {
|
|
22
26
|
console.log(generateSchema(returnSchema));
|
|
23
|
-
const { system, description, model = 'gpt-
|
|
27
|
+
const { system, description, model = config.model ?? 'gpt-4-turbo-preview', meta = false, ...rest } = { ...options, ...callOptions };
|
|
24
28
|
const prompt = {
|
|
25
29
|
model,
|
|
26
30
|
messages: [
|
|
@@ -75,5 +79,5 @@ export const AI = (config = {}) => {
|
|
|
75
79
|
return target[functionName];
|
|
76
80
|
},
|
|
77
81
|
});
|
|
78
|
-
return { ai, openai }; //, client, db, cache, events, queue }
|
|
82
|
+
return { ai, openai, gpt, list }; //, client, db, cache, events, queue }
|
|
79
83
|
};
|
|
@@ -9,21 +9,21 @@ describe('AI Functions', async () => {
|
|
|
9
9
|
type: 'Noun | Verb | Adjective | Adverb | Pronoun | Preposition | Conjunction | Interjection | Other',
|
|
10
10
|
example: 'use the word in a sentence',
|
|
11
11
|
// partOfSpeech: 'Part of speech'
|
|
12
|
-
}, { seed: 1, model: 'gpt-
|
|
12
|
+
}, { seed: 1, model: 'gpt-4o' });
|
|
13
13
|
it('should be a function', () => {
|
|
14
14
|
expect(typeof categorizeWord).toBe('function');
|
|
15
15
|
});
|
|
16
16
|
it('Destroy should be a verb', async () => {
|
|
17
|
-
expect(await categorizeWord('destroy')).toMatchObject({ type: 'Verb', example: '
|
|
17
|
+
expect(await categorizeWord('destroy')).toMatchObject({ type: 'Verb', example: 'The earthquake destroyed several buildings in the city.' });
|
|
18
18
|
});
|
|
19
19
|
it('Dog should be a Noun', async () => {
|
|
20
20
|
const dog = await categorizeWord({ word: 'dog' });
|
|
21
|
-
expect(dog).toMatchObject({ type: 'Noun', example: '
|
|
21
|
+
expect(dog).toMatchObject({ type: 'Noun', example: 'She loves playing with her dog.' });
|
|
22
22
|
});
|
|
23
23
|
it('Large should be an Adjective', async () => {
|
|
24
24
|
expect(await categorizeWord({ word: 'large' })).toMatchObject({ type: 'Adjective', example: 'She has a large collection of books.' });
|
|
25
25
|
});
|
|
26
26
|
it('To should be an Preposition', async () => {
|
|
27
|
-
expect(await categorizeWord('to')).toMatchObject({ type: 'Preposition', example: "I
|
|
27
|
+
expect(await categorizeWord('to')).toMatchObject({ type: 'Preposition', example: "I need to go to the store." });
|
|
28
28
|
});
|
|
29
29
|
});
|
package/functions/ai.test.ts
CHANGED
|
@@ -15,7 +15,7 @@ describe('AI Functions', async () => {
|
|
|
15
15
|
type: 'Noun | Verb | Adjective | Adverb | Pronoun | Preposition | Conjunction | Interjection | Other',
|
|
16
16
|
example: 'use the word in a sentence',
|
|
17
17
|
// partOfSpeech: 'Part of speech'
|
|
18
|
-
}, { seed: 1, model: 'gpt-
|
|
18
|
+
}, { seed: 1, model: 'gpt-4o' })
|
|
19
19
|
|
|
20
20
|
it('should be a function', () => {
|
|
21
21
|
|
|
@@ -23,19 +23,19 @@ describe('AI Functions', async () => {
|
|
|
23
23
|
})
|
|
24
24
|
|
|
25
25
|
it('Destroy should be a verb', async () => {
|
|
26
|
-
expect(await categorizeWord('destroy')).toMatchObject({ type: 'Verb', example: '
|
|
26
|
+
expect(await categorizeWord('destroy')).toMatchObject({ type: 'Verb', example: 'The earthquake destroyed several buildings in the city.' })
|
|
27
27
|
})
|
|
28
28
|
|
|
29
29
|
it('Dog should be a Noun', async () => {
|
|
30
30
|
const dog = await categorizeWord({ word: 'dog' })
|
|
31
|
-
expect(dog).toMatchObject({ type: 'Noun', example: '
|
|
31
|
+
expect(dog).toMatchObject({ type: 'Noun', example: 'She loves playing with her dog.' })
|
|
32
32
|
})
|
|
33
33
|
|
|
34
34
|
it('Large should be an Adjective', async () => {
|
|
35
35
|
expect(await categorizeWord({ word: 'large' })).toMatchObject({ type: 'Adjective', example: 'She has a large collection of books.' })
|
|
36
36
|
})
|
|
37
37
|
it('To should be an Preposition', async () => {
|
|
38
|
-
expect(await categorizeWord('to')).toMatchObject({ type: 'Preposition', example: "I
|
|
38
|
+
expect(await categorizeWord('to')).toMatchObject({ type: 'Preposition', example: "I need to go to the store." })
|
|
39
39
|
})
|
|
40
40
|
|
|
41
41
|
})
|
package/functions/ai.ts
CHANGED
|
@@ -4,6 +4,8 @@ import { ChatCompletion, ChatCompletionCreateParamsBase } from 'openai/resources
|
|
|
4
4
|
import { dump } from 'js-yaml'
|
|
5
5
|
import { generateSchema } from '../utils/schema'
|
|
6
6
|
import { FromSchema } from 'json-schema-to-ts'
|
|
7
|
+
import { GPT } from './gpt'
|
|
8
|
+
import { StreamingList } from './list'
|
|
7
9
|
|
|
8
10
|
export type AIConfig = ClientOptions & {
|
|
9
11
|
// db?: AIDBConfig
|
|
@@ -28,8 +30,10 @@ type AIFunctions<T = Record<string,string>> = Record<string, (
|
|
|
28
30
|
>
|
|
29
31
|
|
|
30
32
|
export const AI = (config: AIConfig = {}) => {
|
|
31
|
-
const { model = 'gpt-4-
|
|
33
|
+
const { model = 'gpt-4-turbo-preview', system, ...rest } = config
|
|
32
34
|
const openai = config.openai ?? new OpenAI(rest)
|
|
35
|
+
const { gpt } = GPT(config)
|
|
36
|
+
const { list } = StreamingList(config)
|
|
33
37
|
// const { client, db, cache, events, queue } = config.db ? AIDB(config.db) : {}
|
|
34
38
|
// const prompt = {
|
|
35
39
|
// model,
|
|
@@ -48,7 +52,7 @@ export const AI = (config: AIConfig = {}) => {
|
|
|
48
52
|
get: (target, functionName: string, receiver) => {
|
|
49
53
|
target[functionName] = (returnSchema: Record<string,any>, options: FunctionCallOptions) => async (args: string | object, callOptions?: FunctionCallOptions) => {
|
|
50
54
|
console.log(generateSchema(returnSchema))
|
|
51
|
-
const { system, description, model = 'gpt-
|
|
55
|
+
const { system, description, model = config.model ?? 'gpt-4-turbo-preview', meta = false, ...rest } = { ...options, ...callOptions }
|
|
52
56
|
const prompt: ChatCompletionCreateParamsBase = {
|
|
53
57
|
model,
|
|
54
58
|
messages: [
|
|
@@ -107,5 +111,5 @@ export const AI = (config: AIConfig = {}) => {
|
|
|
107
111
|
},
|
|
108
112
|
}
|
|
109
113
|
)
|
|
110
|
-
return { ai, openai } //, client, db, cache, events, queue }
|
|
114
|
+
return { ai, openai, gpt, list } //, client, db, cache, events, queue }
|
|
111
115
|
}
|
package/package.json
CHANGED
package/dist/cjs/db/cache.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export declare const md5: (data: string | object) => string;
|
package/dist/cjs/db/cache.js
DELETED
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.md5 = void 0;
|
|
7
|
-
const node_crypto_1 = __importDefault(require("node:crypto"));
|
|
8
|
-
const md5 = (data) => {
|
|
9
|
-
const input = typeof data === 'string' ? data : JSON.stringify(data);
|
|
10
|
-
return node_crypto_1.default.createHash('md5').update(input).digest('hex');
|
|
11
|
-
};
|
|
12
|
-
exports.md5 = md5;
|
package/dist/cjs/db/mongo.d.ts
DELETED
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
import type { MongoClient, Db, Collection, InsertOneResult } from 'mongodb';
|
|
2
|
-
import type { ChatCompletion, ChatCompletionCreateParamsNonStreaming } from 'openai/resources';
|
|
3
|
-
import { QueueInput } from '../queue/mongo';
|
|
4
|
-
export declare let client: MongoClient;
|
|
5
|
-
export declare let db: Db;
|
|
6
|
-
export declare let cache: Collection;
|
|
7
|
-
export declare let cacheTTL: number;
|
|
8
|
-
export declare let events: Collection;
|
|
9
|
-
export declare let queue: Collection;
|
|
10
|
-
export declare let actors: Collection;
|
|
11
|
-
export type AIDBConfig = {
|
|
12
|
-
client: MongoClient;
|
|
13
|
-
db?: Db | string;
|
|
14
|
-
cache?: Collection | string;
|
|
15
|
-
cacheTTL?: number;
|
|
16
|
-
actors?: Collection | string;
|
|
17
|
-
events?: Collection | string;
|
|
18
|
-
queue?: Collection | string;
|
|
19
|
-
};
|
|
20
|
-
export type AIDB = {
|
|
21
|
-
client: MongoClient;
|
|
22
|
-
db: Db;
|
|
23
|
-
cache: Collection;
|
|
24
|
-
cacheTTL: number;
|
|
25
|
-
actors: Collection;
|
|
26
|
-
events: Collection;
|
|
27
|
-
queue: Collection;
|
|
28
|
-
log: (prompt: ChatCompletionCreateParamsNonStreaming, completion: ChatCompletion) => Promise<InsertOneResult<any>>;
|
|
29
|
-
send: (input: QueueInput | QueueInput[]) => Promise<InsertOneResult<any>>;
|
|
30
|
-
};
|
|
31
|
-
export declare const AIDB: (args: AIDBConfig | MongoClient) => AIDB;
|
package/dist/cjs/db/mongo.js
DELETED
|
@@ -1,47 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.AIDB = exports.actors = exports.queue = exports.events = exports.cacheTTL = exports.cache = exports.db = exports.client = void 0;
|
|
4
|
-
const AIDB = (args) => {
|
|
5
|
-
var _a, _b, _c, _d;
|
|
6
|
-
const config = args.client ? args : { client: args };
|
|
7
|
-
exports.client = config.client;
|
|
8
|
-
exports.db = typeof config.db === 'string' ? exports.client.db(config.db) : (_a = config.db) !== null && _a !== void 0 ? _a : exports.client.db();
|
|
9
|
-
exports.cache = typeof config.cache === 'string' ? exports.db.collection(config.cache) : (_b = config.cache) !== null && _b !== void 0 ? _b : exports.db.collection('ai-cache');
|
|
10
|
-
exports.cacheTTL = (_c = config.cacheTTL) !== null && _c !== void 0 ? _c : 1000 * 60 * 60 * 24 * 30;
|
|
11
|
-
exports.events =
|
|
12
|
-
typeof config.events === 'string' ? exports.db.collection(config.events) : (_d = config.events) !== null && _d !== void 0 ? _d : exports.db.collection('ai-events');
|
|
13
|
-
// queue = typeof config.queue === 'string' ? db.collection(config.queue) : config.queue ?? db.collection('ai-queue')
|
|
14
|
-
// actors = typeof config.queue === 'string' ? db.collection(config.queue) : config.queue ?? db.collection('ai-actors')
|
|
15
|
-
return { client: exports.client, db: exports.db, cache: exports.cache, actors: exports.actors, events: exports.events, queue: exports.queue, log }; // , send } as AIDB
|
|
16
|
-
};
|
|
17
|
-
exports.AIDB = AIDB;
|
|
18
|
-
// const send = (input: QueueInput | QueueInput[]) =>
|
|
19
|
-
// Array.isArray(input) ? queue.insertMany(input) : queue.insertOne(input)
|
|
20
|
-
const log = (prompt, completion) => {
|
|
21
|
-
var _a, _b, _c, _d;
|
|
22
|
-
const system = (_a = prompt.messages.find((message) => message.role === 'system')) === null || _a === void 0 ? void 0 : _a.content;
|
|
23
|
-
const userMessages = prompt.messages.filter((message) => message.role === 'user').map((message) => message.content);
|
|
24
|
-
const user = userMessages.length === 1 ? userMessages[0] : userMessages;
|
|
25
|
-
const content = (_b = completion.choices) === null || _b === void 0 ? void 0 : _b[0].message.content;
|
|
26
|
-
const tool = (_d = (_c = completion.choices) === null || _c === void 0 ? void 0 : _c[0].message.tool_calls) === null || _d === void 0 ? void 0 : _d[0].function;
|
|
27
|
-
let functionData;
|
|
28
|
-
try {
|
|
29
|
-
if (tool)
|
|
30
|
-
functionData = JSON.parse(tool.arguments);
|
|
31
|
-
}
|
|
32
|
-
catch (err) {
|
|
33
|
-
functionData = tool === null || tool === void 0 ? void 0 : tool.arguments;
|
|
34
|
-
}
|
|
35
|
-
const timestamp = new Date();
|
|
36
|
-
const event = {
|
|
37
|
-
timestamp,
|
|
38
|
-
user,
|
|
39
|
-
system,
|
|
40
|
-
content,
|
|
41
|
-
functionName: tool === null || tool === void 0 ? void 0 : tool.name,
|
|
42
|
-
functionData,
|
|
43
|
-
prompt,
|
|
44
|
-
completion,
|
|
45
|
-
};
|
|
46
|
-
return exports.events.insertOne(event, { ignoreUndefined: true });
|
|
47
|
-
};
|