ai-functions 0.2.2 → 0.2.3
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.ts +10 -7
- package/package.json +2 -2
- package/utils/schema.ts +4 -4
package/functions/ai.ts
CHANGED
|
@@ -3,6 +3,7 @@ import { ChatCompletion, ChatCompletionCreateParamsBase } from 'openai/resources
|
|
|
3
3
|
import { AIDB, AIDBConfig } from '../db/mongo'
|
|
4
4
|
import { dump } from 'js-yaml'
|
|
5
5
|
import { generateSchema } from '../utils/schema'
|
|
6
|
+
import { FromSchema } from 'json-schema-to-ts'
|
|
6
7
|
|
|
7
8
|
export type AIConfig = ClientOptions & {
|
|
8
9
|
db?: AIDBConfig
|
|
@@ -17,14 +18,14 @@ export type FunctionCallOptions = Omit<ChatCompletionCreateParamsBase, 'messages
|
|
|
17
18
|
description?: string
|
|
18
19
|
}
|
|
19
20
|
|
|
20
|
-
type AIFunctions<T extends Record<string, any> = Record<string,
|
|
21
|
+
type AIFunctions<T extends Record<string, any> = Record<string,any>> = {
|
|
21
22
|
[K in keyof T]: (
|
|
22
|
-
|
|
23
|
-
|
|
23
|
+
returnSchema: T[K],
|
|
24
|
+
callOptions?: FunctionCallOptions
|
|
24
25
|
) => (
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
) => Promise<T[K]>
|
|
26
|
+
args: string | object,
|
|
27
|
+
callOptions?: FunctionCallOptions
|
|
28
|
+
) => Promise<{ [P in keyof T[K]]: T[K][P] }>
|
|
28
29
|
}
|
|
29
30
|
|
|
30
31
|
export const AI = (config: AIConfig = {}) => {
|
|
@@ -78,7 +79,9 @@ export const AI = (config: AIConfig = {}) => {
|
|
|
78
79
|
}
|
|
79
80
|
if (system) prompt.messages.unshift({ role: 'system', content: system })
|
|
80
81
|
const completion = await openai.chat.completions.create(prompt) as ChatCompletion
|
|
81
|
-
|
|
82
|
+
const schema = generateSchema(returnSchema)
|
|
83
|
+
let data: FromSchema<typeof schema>
|
|
84
|
+
let error
|
|
82
85
|
const { message } = completion.choices?.[0]
|
|
83
86
|
console.log({ message })
|
|
84
87
|
prompt.messages.push(message)
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "ai-functions",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.3",
|
|
4
4
|
"description": "Library for Developing and Managing AI Functions (including OpenAI GPT4 / GPT3.5)",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"type": "module",
|
|
@@ -37,7 +37,7 @@
|
|
|
37
37
|
},
|
|
38
38
|
"dependencies": {
|
|
39
39
|
"js-yaml": "^4.1.0",
|
|
40
|
-
"json-schema-to-ts": "^
|
|
40
|
+
"json-schema-to-ts": "^3.0.0",
|
|
41
41
|
"kafkajs": "^2.2.4",
|
|
42
42
|
"lodash-es": "^4.17.21",
|
|
43
43
|
"mongodb": "^6.2.0",
|
package/utils/schema.ts
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { JSONSchema } from 'json-schema-to-ts'
|
|
2
2
|
|
|
3
3
|
// This is a helper function to generate a JSON schema for string properties.
|
|
4
4
|
// It checks if a string includes '|' which would indicate an enum,
|
|
5
5
|
// or if it starts with 'number: ' or 'boolean: ' which would indicate
|
|
6
6
|
// a number or boolean type respectively, otherwise it defaults to string.
|
|
7
|
-
export const parseStringDescription = (description: string):
|
|
7
|
+
export const parseStringDescription = (description: string): JSONSchema => {
|
|
8
8
|
// Check if the description indicates an enum for string type
|
|
9
9
|
if (description.includes('|')) {
|
|
10
10
|
return { type: 'string', enum: description.split('|').map((v) => v.trim()) }
|
|
@@ -37,13 +37,13 @@ export const parseStringDescription = (description: string): JSONSchema7 => {
|
|
|
37
37
|
* and values as descriptions or nested property description objects.
|
|
38
38
|
* @returns A JSON schema object based on the provided descriptions.
|
|
39
39
|
*/
|
|
40
|
-
export const generateSchema = (propDescriptions: Record<string, string | Record<string, any>>):
|
|
40
|
+
export const generateSchema = (propDescriptions: Record<string, string | Record<string, any>>): JSONSchema => {
|
|
41
41
|
// If the propDescriptions is for an object structure
|
|
42
42
|
if (typeof propDescriptions !== 'object' || propDescriptions === null || Array.isArray(propDescriptions)) {
|
|
43
43
|
throw new Error('The propDescriptions parameter should be an object.')
|
|
44
44
|
}
|
|
45
45
|
|
|
46
|
-
const properties: Record<string,
|
|
46
|
+
const properties: Record<string, JSONSchema> = {}
|
|
47
47
|
const required: string[] = Object.keys(propDescriptions)
|
|
48
48
|
|
|
49
49
|
for (const [key, description] of Object.entries(propDescriptions)) {
|