ai-functions 0.2.1 → 0.2.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/functions/ai.test.ts +15 -5
- package/functions/ai.ts +12 -3
- package/package.json +1 -1
package/functions/ai.test.ts
CHANGED
|
@@ -13,6 +13,8 @@ describe('AI Functions', async () => {
|
|
|
13
13
|
|
|
14
14
|
const categorizeWord = ai.categorizeWord({
|
|
15
15
|
type: 'Noun | Verb | Adjective | Adverb | Pronoun | Preposition | Conjunction | Interjection | Other',
|
|
16
|
+
example: 'use the word in a sentence',
|
|
17
|
+
// partOfSpeech: 'Part of speech'
|
|
16
18
|
}, { seed: 1, model: 'gpt-3.5-turbo' })
|
|
17
19
|
|
|
18
20
|
it('should be a function', () => {
|
|
@@ -20,12 +22,20 @@ describe('AI Functions', async () => {
|
|
|
20
22
|
expect(typeof categorizeWord).toBe('function')
|
|
21
23
|
})
|
|
22
24
|
|
|
23
|
-
it('should
|
|
24
|
-
expect(await categorizeWord('destroy')).toMatchObject({ type: 'Verb' })
|
|
25
|
+
it('Destroy should be a verb', async () => {
|
|
26
|
+
expect(await categorizeWord('destroy')).toMatchObject({ type: 'Verb', example: 'I will destroy the old building.' })
|
|
25
27
|
})
|
|
26
28
|
|
|
27
|
-
it('should
|
|
28
|
-
|
|
29
|
+
it('Dog should be a Noun', async () => {
|
|
30
|
+
const dog = await categorizeWord({ word: 'dog' })
|
|
31
|
+
expect(dog).toMatchObject({ type: 'Noun', example: 'I have a dog.' })
|
|
29
32
|
})
|
|
30
|
-
|
|
33
|
+
|
|
34
|
+
it('Large should be an Adjective', async () => {
|
|
35
|
+
expect(await categorizeWord({ word: 'large' })).toMatchObject({ type: 'Adjective', example: 'She has a large collection of books.' })
|
|
36
|
+
})
|
|
37
|
+
it('To should be an Preposition', async () => {
|
|
38
|
+
expect(await categorizeWord('to')).toMatchObject({ type: 'Preposition', example: "I'm going to the park." })
|
|
39
|
+
})
|
|
40
|
+
|
|
31
41
|
})
|
package/functions/ai.ts
CHANGED
|
@@ -17,6 +17,16 @@ export type FunctionCallOptions = Omit<ChatCompletionCreateParamsBase, 'messages
|
|
|
17
17
|
description?: string
|
|
18
18
|
}
|
|
19
19
|
|
|
20
|
+
type AIFunctions<T extends Record<string, any> = Record<string, any>> = {
|
|
21
|
+
[K in keyof T]: (
|
|
22
|
+
returnSchema: T[K],
|
|
23
|
+
callOptions?: FunctionCallOptions
|
|
24
|
+
) => (
|
|
25
|
+
args: string | object,
|
|
26
|
+
callOptions?: FunctionCallOptions
|
|
27
|
+
) => Promise<T[K]>
|
|
28
|
+
}
|
|
29
|
+
|
|
20
30
|
export const AI = (config: AIConfig = {}) => {
|
|
21
31
|
const { model = 'gpt-4-1106-preview', system, ...rest } = config
|
|
22
32
|
const openai = config.openai ?? new OpenAI(rest)
|
|
@@ -32,8 +42,7 @@ export const AI = (config: AIConfig = {}) => {
|
|
|
32
42
|
// messages: [{ role: 'user', content: 'hello' }],
|
|
33
43
|
// })
|
|
34
44
|
|
|
35
|
-
const ai:
|
|
36
|
-
(args: string | object, callOptions?: FunctionCallOptions) => Promise<any>> = new Proxy(
|
|
45
|
+
const ai: AIFunctions = new Proxy(
|
|
37
46
|
{},
|
|
38
47
|
{
|
|
39
48
|
get: (target, functionName: string, receiver) => {
|
|
@@ -89,7 +98,7 @@ export const AI = (config: AIConfig = {}) => {
|
|
|
89
98
|
: completion.usage.prompt_tokens * 0.00015 + completion.usage.completion_tokens * 0.0002) * 100000
|
|
90
99
|
) / 100000 : undefined
|
|
91
100
|
// completion.usage = camelcaseKeys(completion.usage)
|
|
92
|
-
console.log({ data, content, error, cost })
|
|
101
|
+
console.log({ data, content, error, cost, usage: completion.usage })
|
|
93
102
|
return meta ? { prompt, content, data, error, cost, ...completion } : data ?? content
|
|
94
103
|
}
|
|
95
104
|
},
|