ai-functions 0.1.4 → 0.2.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/LICENSE +21 -0
- package/README.md +7 -6
- package/db/cache.ts +6 -0
- package/db/mongo.ts +75 -0
- package/examples/data.ts +1105 -0
- package/functions/ai.test.ts +31 -0
- package/functions/ai.ts +99 -0
- package/functions/gpt.ts +12 -0
- package/functions/list.ts +84 -0
- package/index.ts +3 -0
- package/package.json +23 -6
- package/queue/memory.ts +0 -0
- package/queue/mongo.ts +88 -0
- package/streams/kafka.ts +0 -0
- package/streams/memory.ts +0 -0
- package/streams/mongo.ts +0 -0
- package/streams/types.ts +0 -0
- package/tsconfig.json +105 -0
- package/types.ts +12 -0
- package/utils/completion.ts +28 -0
- package/utils/schema.test.ts +69 -0
- package/utils/schema.ts +62 -0
- package/utils/state.ts +23 -0
- package/example.js +0 -38
- package/fetcher.js +0 -48
- package/functions/generateBlogPost.js +0 -12
- package/functions/generateLandingPage.js +0 -11
- package/functions/generatePossibleBlogPostTitles.js +0 -11
- package/index.js +0 -3
- package/index.test.js +0 -64
- package/proxy.js +0 -121
- package/schema.js +0 -50
- package/withAI.js +0 -8
- /package/{test.d.ts → queue/kafka.ts} +0 -0
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { describe, test, it, expect } from 'vitest'
|
|
2
|
+
|
|
3
|
+
import { AI } from './ai'
|
|
4
|
+
|
|
5
|
+
describe('AI Functions', async () => {
|
|
6
|
+
|
|
7
|
+
const { ai } = AI()
|
|
8
|
+
|
|
9
|
+
it('should be initialized', () => {
|
|
10
|
+
expect(ai).toBeDefined()
|
|
11
|
+
})
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
const categorizeWord = ai.categorizeWord({
|
|
15
|
+
type: 'Noun | Verb | Adjective | Adverb | Pronoun | Preposition | Conjunction | Interjection | Other',
|
|
16
|
+
}, { seed: 1, model: 'gpt-3.5-turbo' })
|
|
17
|
+
|
|
18
|
+
it('should be a function', () => {
|
|
19
|
+
|
|
20
|
+
expect(typeof categorizeWord).toBe('function')
|
|
21
|
+
})
|
|
22
|
+
|
|
23
|
+
it('should return a promise', async () => {
|
|
24
|
+
expect(await categorizeWord('destroy')).toMatchObject({ type: 'Verb' })
|
|
25
|
+
})
|
|
26
|
+
|
|
27
|
+
it('should return a promise', async () => {
|
|
28
|
+
expect(await categorizeWord('dog')).toMatchObject({ type: 'Noun' })
|
|
29
|
+
})
|
|
30
|
+
|
|
31
|
+
})
|
package/functions/ai.ts
ADDED
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import { ClientOptions, OpenAI, } from 'openai'
|
|
2
|
+
import { ChatCompletion, ChatCompletionCreateParamsBase } from 'openai/resources/chat/completions'
|
|
3
|
+
import { AIDB, AIDBConfig } from '../db/mongo'
|
|
4
|
+
import { dump } from 'js-yaml'
|
|
5
|
+
import { generateSchema } from '../utils/schema'
|
|
6
|
+
|
|
7
|
+
export type AIConfig = ClientOptions & {
|
|
8
|
+
db?: AIDBConfig
|
|
9
|
+
openai?: OpenAI
|
|
10
|
+
system?: string
|
|
11
|
+
model?: ChatCompletionCreateParamsBase['model']
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export type FunctionCallOptions = Omit<ChatCompletionCreateParamsBase, 'messages' | 'stream' > & {
|
|
15
|
+
system?: string
|
|
16
|
+
meta?: boolean
|
|
17
|
+
description?: string
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export const AI = (config: AIConfig = {}) => {
|
|
21
|
+
const { model = 'gpt-4-1106-preview', system, ...rest } = config
|
|
22
|
+
const openai = config.openai ?? new OpenAI(rest)
|
|
23
|
+
// const { client, db, cache, events, queue } = config.db ? AIDB(config.db) : {}
|
|
24
|
+
// const prompt = {
|
|
25
|
+
// model,
|
|
26
|
+
// messages: [{ role: 'user', content: user }],
|
|
27
|
+
// }
|
|
28
|
+
// if (system) prompt.messages.unshift({ role: 'system', content: system })
|
|
29
|
+
// const messages = system ? [{ role: 'system', content: system }] : []
|
|
30
|
+
// const completion = openai.chat.completions.create({
|
|
31
|
+
// model,
|
|
32
|
+
// messages: [{ role: 'user', content: 'hello' }],
|
|
33
|
+
// })
|
|
34
|
+
|
|
35
|
+
const ai: Record<string, (args: string | object, callOptions?: FunctionCallOptions) =>
|
|
36
|
+
(args: string | object, callOptions?: FunctionCallOptions) => Promise<object>> = new Proxy(
|
|
37
|
+
{},
|
|
38
|
+
{
|
|
39
|
+
get: (target, functionName: string, receiver) => {
|
|
40
|
+
return (returnSchema: Record<string,any>, options: FunctionCallOptions) => async (args: string | object, callOptions?: FunctionCallOptions) => {
|
|
41
|
+
console.log(generateSchema(returnSchema))
|
|
42
|
+
const { system, description, model = 'gpt-3.5-turbo', meta = false, ...rest } = { ...options, ...callOptions }
|
|
43
|
+
const prompt: ChatCompletionCreateParamsBase = {
|
|
44
|
+
model,
|
|
45
|
+
messages: [
|
|
46
|
+
{
|
|
47
|
+
role: 'user',
|
|
48
|
+
content: `Call ${functionName} given the context:\n${dump(args)}`,
|
|
49
|
+
}, // \nThere is no additional information, so make assumptions/guesses as necessary` },
|
|
50
|
+
],
|
|
51
|
+
|
|
52
|
+
tools: [
|
|
53
|
+
{
|
|
54
|
+
type: 'function',
|
|
55
|
+
function: {
|
|
56
|
+
name: functionName,
|
|
57
|
+
description,
|
|
58
|
+
parameters: generateSchema(returnSchema) as Record<string,unknown>,
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
],
|
|
62
|
+
|
|
63
|
+
tool_choice: {
|
|
64
|
+
type: 'function',
|
|
65
|
+
function: { name: functionName }
|
|
66
|
+
},
|
|
67
|
+
|
|
68
|
+
...rest,
|
|
69
|
+
}
|
|
70
|
+
if (system) prompt.messages.unshift({ role: 'system', content: system })
|
|
71
|
+
const completion = await openai.chat.completions.create(prompt) as ChatCompletion
|
|
72
|
+
let data, error
|
|
73
|
+
const { message } = completion.choices?.[0]
|
|
74
|
+
console.log({ message })
|
|
75
|
+
prompt.messages.push(message)
|
|
76
|
+
const { content, tool_calls } = message
|
|
77
|
+
if (tool_calls) {
|
|
78
|
+
try {
|
|
79
|
+
data = JSON.parse(tool_calls[0].function.arguments)
|
|
80
|
+
} catch (err: any) {
|
|
81
|
+
error = err.message
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
const gpt4 = model.includes('gpt-4')
|
|
85
|
+
const cost = completion.usage ?
|
|
86
|
+
Math.round(
|
|
87
|
+
(gpt4
|
|
88
|
+
? completion.usage.prompt_tokens * 0.003 + completion.usage.completion_tokens * 0.006
|
|
89
|
+
: completion.usage.prompt_tokens * 0.00015 + completion.usage.completion_tokens * 0.0002) * 100000
|
|
90
|
+
) / 100000 : undefined
|
|
91
|
+
// completion.usage = camelcaseKeys(completion.usage)
|
|
92
|
+
console.log({ data, content, error, cost })
|
|
93
|
+
return meta ? { prompt, content, data, error, cost, ...completion } : data ?? content
|
|
94
|
+
}
|
|
95
|
+
},
|
|
96
|
+
}
|
|
97
|
+
)
|
|
98
|
+
return { ai, openai } //, client, db, cache, events, queue }
|
|
99
|
+
}
|
package/functions/gpt.ts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { GPTConfig } from '../types'
|
|
2
|
+
import { chatCompletion } from '../utils/completion'
|
|
3
|
+
|
|
4
|
+
export const GPT = (config: GPTConfig) => {
|
|
5
|
+
const gpt = async (strings: string[], ...values: string[]) => {
|
|
6
|
+
const user = values.map((value, i) => strings[i] + value).join('') + strings[strings.length - 1]
|
|
7
|
+
const completion = await chatCompletion({ user, ...config })
|
|
8
|
+
const content = completion.choices?.[0].message.content
|
|
9
|
+
return content
|
|
10
|
+
}
|
|
11
|
+
return { gpt }
|
|
12
|
+
}
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import { OpenAI } from 'openai'
|
|
2
|
+
import { ChatCompletionCreateParamsStreaming } from 'openai/resources/index.mjs'
|
|
3
|
+
import { GPTConfig } from '../types'
|
|
4
|
+
import { chatCompletion } from '../utils/completion'
|
|
5
|
+
|
|
6
|
+
export const List = (config: GPTConfig) => {
|
|
7
|
+
const list = async (strings: string[], ...values: string[]) => {
|
|
8
|
+
const user = values.map((value, i) => strings[i] + value).join('') + strings[strings.length - 1]
|
|
9
|
+
const completion = await chatCompletion({ user, ...config })
|
|
10
|
+
const content = completion.choices?.[0].message.content
|
|
11
|
+
return content ? parseList(content) : []
|
|
12
|
+
}
|
|
13
|
+
return { list }
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
function parseList(listStr: string): string[] {
|
|
17
|
+
// Define a regex pattern to match lines with '1. value', '1) value', '- value', or ' - value'
|
|
18
|
+
const listItemRegex = /^\s*\d+\.\s*(.+)|^\s*\d+\)\s*(.+)|^\s*-\s*(.+)$/gm
|
|
19
|
+
let match: RegExpExecArray | null
|
|
20
|
+
const result: string[] = []
|
|
21
|
+
|
|
22
|
+
// Loop over the list string, finding matches with the regex pattern
|
|
23
|
+
while ((match = listItemRegex.exec(listStr)) !== null) {
|
|
24
|
+
// The actual value is in one of the capturing groups (1, 2 or 3)
|
|
25
|
+
const value = match[1] || match[2] || match[3]
|
|
26
|
+
if (value) {
|
|
27
|
+
result.push(value.trim())
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
return result
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export const StreamingList = (config: GPTConfig) => {
|
|
35
|
+
async function* list(strings: string[], ...values: string[]) {
|
|
36
|
+
const user = values.map((value, i) => strings[i] + value).join('') + strings[strings.length - 1]
|
|
37
|
+
const { system, model, db, queue, ...rest } = config
|
|
38
|
+
const openai = config.openai ?? new OpenAI(rest)
|
|
39
|
+
const prompt = {
|
|
40
|
+
model,
|
|
41
|
+
messages: [{ role: 'user', content: user }],
|
|
42
|
+
stream: true,
|
|
43
|
+
} as ChatCompletionCreateParamsStreaming
|
|
44
|
+
if (system) prompt.messages.unshift({ role: 'system', content: system })
|
|
45
|
+
const completion = await openai.chat.completions.create(prompt)
|
|
46
|
+
|
|
47
|
+
const stream = await openai.chat.completions.create(prompt)
|
|
48
|
+
let content = ''
|
|
49
|
+
let seperator: string | undefined
|
|
50
|
+
let numberedList: RegExpMatchArray | undefined | null
|
|
51
|
+
|
|
52
|
+
for await (const part of stream) {
|
|
53
|
+
const { delta, finish_reason } = part.choices[0]
|
|
54
|
+
content += delta?.content || ''
|
|
55
|
+
if (seperator === undefined && content.length > 4) {
|
|
56
|
+
numberedList = content.match(/(\d+\.\s)/g)
|
|
57
|
+
seperator = numberedList ? '\n' : ', '
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
const numberedRegex = /\d+\.\s(?:")?([^"]+)(?:")?/
|
|
61
|
+
|
|
62
|
+
if (seperator && content.includes(seperator)) {
|
|
63
|
+
// get the string before the newline, and modify `content` to be the string after the newline
|
|
64
|
+
// then yield the string before the newline
|
|
65
|
+
const items = content.split(seperator)
|
|
66
|
+
while (items.length > 1) {
|
|
67
|
+
const item = items.shift()
|
|
68
|
+
yield numberedList ? item?.match(numberedRegex)?.[1] : item
|
|
69
|
+
}
|
|
70
|
+
content = items[0]
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
if (finish_reason) {
|
|
74
|
+
// TODO: Figure out DB saving strategy for streaming
|
|
75
|
+
// if (db) {
|
|
76
|
+
// db.log(prompt, completion as ChatCompletion)
|
|
77
|
+
// }
|
|
78
|
+
|
|
79
|
+
yield numberedList ? content.match(numberedRegex)?.[1] : content
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
return { list }
|
|
84
|
+
}
|
package/index.ts
ADDED
package/package.json
CHANGED
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "ai-functions",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"description": "Library for Developing and Managing AI Functions (including OpenAI GPT4 / GPT3.5)",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"type": "module",
|
|
7
7
|
"scripts": {
|
|
8
|
-
"
|
|
9
|
-
"
|
|
8
|
+
"build": "tsc",
|
|
9
|
+
"format": "prettier --write .",
|
|
10
|
+
"test": "dotenv -- vitest --globals"
|
|
10
11
|
},
|
|
11
12
|
"repository": {
|
|
12
13
|
"type": "git",
|
|
@@ -19,14 +20,30 @@
|
|
|
19
20
|
"url": "https://github.com/nathanclevenger/ai-functions/issues"
|
|
20
21
|
},
|
|
21
22
|
"homepage": "https://github.com/nathanclevenger/ai-functions#readme",
|
|
23
|
+
"prettier": {
|
|
24
|
+
"semi": false,
|
|
25
|
+
"singleQuote": true,
|
|
26
|
+
"trailingComma": "es5",
|
|
27
|
+
"tabWidth": 2,
|
|
28
|
+
"printWidth": 120
|
|
29
|
+
},
|
|
22
30
|
"devDependencies": {
|
|
31
|
+
"@types/js-yaml": "^4.0.9",
|
|
32
|
+
"@types/json-schema": "^7.0.14",
|
|
33
|
+
"dotenv-cli": "^7.3.0",
|
|
34
|
+
"prettier": "^3.0.3",
|
|
35
|
+
"typescript": "^5.3.3",
|
|
23
36
|
"vitest": "^0.33.0"
|
|
24
37
|
},
|
|
25
38
|
"dependencies": {
|
|
26
|
-
"camelcase-keys": "^9.1.0",
|
|
27
39
|
"js-yaml": "^4.1.0",
|
|
28
|
-
"
|
|
40
|
+
"json-schema-to-ts": "^2.9.2",
|
|
41
|
+
"kafkajs": "^2.2.4",
|
|
42
|
+
"lodash-es": "^4.17.21",
|
|
43
|
+
"mongodb": "^6.2.0",
|
|
44
|
+
"openai": "^4.16.1",
|
|
45
|
+
"p-queue": "^7.4.1",
|
|
29
46
|
"partial-json-parser": "^1.2.2",
|
|
30
|
-
"
|
|
47
|
+
"xstate": "^5.0.0-beta.40"
|
|
31
48
|
}
|
|
32
49
|
}
|
package/queue/memory.ts
ADDED
|
File without changes
|
package/queue/mongo.ts
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import { BSON, ObjectId } from 'bson'
|
|
2
|
+
import { AIDB } from '../db/mongo'
|
|
3
|
+
import PQueue from 'p-queue'
|
|
4
|
+
import { chatCompletion, CompletionInput } from '../utils/completion'
|
|
5
|
+
import { createMachine, createActor } from 'xstate'
|
|
6
|
+
import { ChangeStreamInsertDocument, InsertOneResult, UpdateOneModel, UpdateResult } from 'mongodb'
|
|
7
|
+
|
|
8
|
+
let localQueue
|
|
9
|
+
|
|
10
|
+
export type QueueConfig = AIDB & {
|
|
11
|
+
batchSize?: number
|
|
12
|
+
concurrency?: number
|
|
13
|
+
lockExpiration?: number
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export type QueueInputMerge = {
|
|
17
|
+
into: string
|
|
18
|
+
on?: BSON.Document
|
|
19
|
+
let?: BSON.Document
|
|
20
|
+
contentAs?: string
|
|
21
|
+
itemsAs?: string
|
|
22
|
+
functionDataAs?: string
|
|
23
|
+
forEachItem?: boolean
|
|
24
|
+
}
|
|
25
|
+
export type QueueInput = (CompletionInput | ({ list: string } & Partial<CompletionInput>)) & {
|
|
26
|
+
_id?: ObjectId
|
|
27
|
+
metadata?: object
|
|
28
|
+
merge?: string | QueueInputMerge
|
|
29
|
+
target?: string
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export type QueueDocument = QueueInput & {
|
|
33
|
+
lockedAt: Date
|
|
34
|
+
lockedBy: string
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export const startQueue = async (config: QueueConfig) => {
|
|
38
|
+
|
|
39
|
+
const { concurrency = 10, batchSize = 100, lockExpiration = 3600, ...db } = config
|
|
40
|
+
const instance = Math.random().toString(36).substring(2, 6)
|
|
41
|
+
const queue = new PQueue({ concurrency })
|
|
42
|
+
|
|
43
|
+
const clearExpiredLocks = () => db.queue.updateMany(
|
|
44
|
+
{ lockedAt: { $lt: new Date(Date.now() - lockExpiration * 1000) } },
|
|
45
|
+
{ $unset: { lockedAt: '', lockedBy: '' } }
|
|
46
|
+
)
|
|
47
|
+
|
|
48
|
+
const lockBatch = () => db.queue.aggregate([
|
|
49
|
+
{ $match: { lockedAt: { $exists: false } } },
|
|
50
|
+
{ $limit: batchSize },
|
|
51
|
+
{ $set: { lockedAt: new Date(), lockedBy: instance } },
|
|
52
|
+
{ $merge: { into: 'queue', on: '_id', whenMatched: 'replace' } }, // TODO: Change the `into` to the name of the `queue` collection
|
|
53
|
+
]).toArray()
|
|
54
|
+
|
|
55
|
+
db.queue.watch<QueueInput, ChangeStreamInsertDocument< QueueInput >>([
|
|
56
|
+
{ $match: { lockedBy: instance } },
|
|
57
|
+
]).on('change', async (change) => {
|
|
58
|
+
const { _id, metadata, merge, target, ...input } = change.fullDocument // TODO: Move input to child object not catchall spread
|
|
59
|
+
const completion = await queue.add(() => chatCompletion(input))
|
|
60
|
+
if (merge) {
|
|
61
|
+
const coll = typeof merge === 'string' ? merge : merge.into
|
|
62
|
+
const match = typeof merge === 'string' ? undefined : merge.on
|
|
63
|
+
// TODO: Add support to make completion optional, and specify field names for content, items, and functionData
|
|
64
|
+
const mergeResult = match
|
|
65
|
+
? await db.db.collection(coll).updateOne(match, { $set: { completion, ...metadata ?? {} } })
|
|
66
|
+
: await db.db.collection(coll).insertOne({ completion, ...metadata ?? {} })
|
|
67
|
+
if (mergeResult.acknowledged && (
|
|
68
|
+
(mergeResult as UpdateResult).modifiedCount ||
|
|
69
|
+
(mergeResult as InsertOneResult).insertedId)) {
|
|
70
|
+
// TODO: We may want to store the merge result in the Events collection to link the queue input to the merge result
|
|
71
|
+
await db.queue.deleteOne({ _id })
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
})
|
|
75
|
+
|
|
76
|
+
queue.on('idle', async () => {
|
|
77
|
+
await clearExpiredLocks()
|
|
78
|
+
await lockBatch()
|
|
79
|
+
})
|
|
80
|
+
|
|
81
|
+
queue.start()
|
|
82
|
+
|
|
83
|
+
// TODO: Add Find() and Watch() for Actors collection
|
|
84
|
+
// TODO: For each actor, create a state machine and start it
|
|
85
|
+
// TODO: Figure out how to create new queued jobs from the results of the state machine
|
|
86
|
+
|
|
87
|
+
}
|
|
88
|
+
|
package/streams/kafka.ts
ADDED
|
File without changes
|
|
File without changes
|
package/streams/mongo.ts
ADDED
|
File without changes
|
package/streams/types.ts
ADDED
|
File without changes
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
/* Visit https://aka.ms/tsconfig to read more about this file */
|
|
4
|
+
|
|
5
|
+
/* Projects */
|
|
6
|
+
// "incremental": true, /* Save .tsbuildinfo files to allow for incremental compilation of projects. */
|
|
7
|
+
// "composite": true, /* Enable constraints that allow a TypeScript project to be used with project references. */
|
|
8
|
+
// "tsBuildInfoFile": "./.tsbuildinfo", /* Specify the path to .tsbuildinfo incremental compilation file. */
|
|
9
|
+
// "disableSourceOfProjectReferenceRedirect": true, /* Disable preferring source files instead of declaration files when referencing composite projects. */
|
|
10
|
+
// "disableSolutionSearching": true, /* Opt a project out of multi-project reference checking when editing. */
|
|
11
|
+
// "disableReferencedProjectLoad": true, /* Reduce the number of projects loaded automatically by TypeScript. */
|
|
12
|
+
|
|
13
|
+
/* Language and Environment */
|
|
14
|
+
"target": "es2016" /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */,
|
|
15
|
+
// "lib": [], /* Specify a set of bundled library declaration files that describe the target runtime environment. */
|
|
16
|
+
// "jsx": "preserve", /* Specify what JSX code is generated. */
|
|
17
|
+
// "experimentalDecorators": true, /* Enable experimental support for TC39 stage 2 draft decorators. */
|
|
18
|
+
// "emitDecoratorMetadata": true, /* Emit design-type metadata for decorated declarations in source files. */
|
|
19
|
+
// "jsxFactory": "", /* Specify the JSX factory function used when targeting React JSX emit, e.g. 'React.createElement' or 'h'. */
|
|
20
|
+
// "jsxFragmentFactory": "", /* Specify the JSX Fragment reference used for fragments when targeting React JSX emit e.g. 'React.Fragment' or 'Fragment'. */
|
|
21
|
+
// "jsxImportSource": "", /* Specify module specifier used to import the JSX factory functions when using 'jsx: react-jsx*'. */
|
|
22
|
+
// "reactNamespace": "", /* Specify the object invoked for 'createElement'. This only applies when targeting 'react' JSX emit. */
|
|
23
|
+
// "noLib": true, /* Disable including any library files, including the default lib.d.ts. */
|
|
24
|
+
// "useDefineForClassFields": true, /* Emit ECMAScript-standard-compliant class fields. */
|
|
25
|
+
// "moduleDetection": "auto", /* Control what method is used to detect module-format JS files. */
|
|
26
|
+
|
|
27
|
+
/* Modules */
|
|
28
|
+
"module": "ES6" /* Specify what module code is generated. */,
|
|
29
|
+
// "rootDir": "./", /* Specify the root folder within your source files. */
|
|
30
|
+
"moduleResolution": "Bundler" /* Specify how TypeScript looks up a file from a given module specifier. */,
|
|
31
|
+
// "baseUrl": "./", /* Specify the base directory to resolve non-relative module names. */
|
|
32
|
+
// "paths": {}, /* Specify a set of entries that re-map imports to additional lookup locations. */
|
|
33
|
+
// "rootDirs": [], /* Allow multiple folders to be treated as one when resolving modules. */
|
|
34
|
+
// "typeRoots": [], /* Specify multiple folders that act like './node_modules/@types'. */
|
|
35
|
+
"types": [
|
|
36
|
+
"vitest/globals"
|
|
37
|
+
] /* Specify type package names to be included without being referenced in a source file. */,
|
|
38
|
+
// "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */
|
|
39
|
+
// "moduleSuffixes": [], /* List of file name suffixes to search when resolving a module. */
|
|
40
|
+
// "resolveJsonModule": true, /* Enable importing .json files. */
|
|
41
|
+
// "noResolve": true, /* Disallow 'import's, 'require's or '<reference>'s from expanding the number of files TypeScript should add to a project. */
|
|
42
|
+
|
|
43
|
+
/* JavaScript Support */
|
|
44
|
+
// "allowJs": true, /* Allow JavaScript files to be a part of your program. Use the 'checkJS' option to get errors from these files. */
|
|
45
|
+
// "checkJs": true, /* Enable error reporting in type-checked JavaScript files. */
|
|
46
|
+
// "maxNodeModuleJsDepth": 1, /* Specify the maximum folder depth used for checking JavaScript files from 'node_modules'. Only applicable with 'allowJs'. */
|
|
47
|
+
|
|
48
|
+
/* Emit */
|
|
49
|
+
// "declaration": true, /* Generate .d.ts files from TypeScript and JavaScript files in your project. */
|
|
50
|
+
// "declarationMap": true, /* Create sourcemaps for d.ts files. */
|
|
51
|
+
// "emitDeclarationOnly": true, /* Only output d.ts files and not JavaScript files. */
|
|
52
|
+
// "sourceMap": true, /* Create source map files for emitted JavaScript files. */
|
|
53
|
+
// "outFile": "./dist", /* Specify a file that bundles all outputs into one JavaScript file. If 'declaration' is true, also designates a file that bundles all .d.ts output. */
|
|
54
|
+
"outDir": "./dist" /* Specify an output folder for all emitted files. */,
|
|
55
|
+
// "removeComments": true, /* Disable emitting comments. */
|
|
56
|
+
// "noEmit": true, /* Disable emitting files from a compilation. */
|
|
57
|
+
// "importHelpers": true, /* Allow importing helper functions from tslib once per project, instead of including them per-file. */
|
|
58
|
+
// "importsNotUsedAsValues": "remove", /* Specify emit/checking behavior for imports that are only used for types. */
|
|
59
|
+
// "downlevelIteration": true, /* Emit more compliant, but verbose and less performant JavaScript for iteration. */
|
|
60
|
+
// "sourceRoot": "", /* Specify the root path for debuggers to find the reference source code. */
|
|
61
|
+
// "mapRoot": "", /* Specify the location where debugger should locate map files instead of generated locations. */
|
|
62
|
+
// "inlineSourceMap": true, /* Include sourcemap files inside the emitted JavaScript. */
|
|
63
|
+
// "inlineSources": true, /* Include source code in the sourcemaps inside the emitted JavaScript. */
|
|
64
|
+
// "emitBOM": true, /* Emit a UTF-8 Byte Order Mark (BOM) in the beginning of output files. */
|
|
65
|
+
// "newLine": "crlf", /* Set the newline character for emitting files. */
|
|
66
|
+
// "stripInternal": true, /* Disable emitting declarations that have '@internal' in their JSDoc comments. */
|
|
67
|
+
// "noEmitHelpers": true, /* Disable generating custom helper functions like '__extends' in compiled output. */
|
|
68
|
+
// "noEmitOnError": true, /* Disable emitting files if any type checking errors are reported. */
|
|
69
|
+
// "preserveConstEnums": true, /* Disable erasing 'const enum' declarations in generated code. */
|
|
70
|
+
// "declarationDir": "./", /* Specify the output directory for generated declaration files. */
|
|
71
|
+
// "preserveValueImports": true, /* Preserve unused imported values in the JavaScript output that would otherwise be removed. */
|
|
72
|
+
|
|
73
|
+
/* Interop Constraints */
|
|
74
|
+
// "isolatedModules": true, /* Ensure that each file can be safely transpiled without relying on other imports. */
|
|
75
|
+
// "allowSyntheticDefaultImports": true, /* Allow 'import x from y' when a module doesn't have a default export. */
|
|
76
|
+
"esModuleInterop": true /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables 'allowSyntheticDefaultImports' for type compatibility. */,
|
|
77
|
+
// "preserveSymlinks": true, /* Disable resolving symlinks to their realpath. This correlates to the same flag in node. */
|
|
78
|
+
"forceConsistentCasingInFileNames": true /* Ensure that casing is correct in imports. */,
|
|
79
|
+
|
|
80
|
+
/* Type Checking */
|
|
81
|
+
"strict": true /* Enable all strict type-checking options. */,
|
|
82
|
+
// "noImplicitAny": true, /* Enable error reporting for expressions and declarations with an implied 'any' type. */
|
|
83
|
+
// "strictNullChecks": true, /* When type checking, take into account 'null' and 'undefined'. */
|
|
84
|
+
// "strictFunctionTypes": true, /* When assigning functions, check to ensure parameters and the return values are subtype-compatible. */
|
|
85
|
+
// "strictBindCallApply": true, /* Check that the arguments for 'bind', 'call', and 'apply' methods match the original function. */
|
|
86
|
+
// "strictPropertyInitialization": true, /* Check for class properties that are declared but not set in the constructor. */
|
|
87
|
+
// "noImplicitThis": true, /* Enable error reporting when 'this' is given the type 'any'. */
|
|
88
|
+
// "useUnknownInCatchVariables": true, /* Default catch clause variables as 'unknown' instead of 'any'. */
|
|
89
|
+
// "alwaysStrict": true, /* Ensure 'use strict' is always emitted. */
|
|
90
|
+
// "noUnusedLocals": true, /* Enable error reporting when local variables aren't read. */
|
|
91
|
+
// "noUnusedParameters": true, /* Raise an error when a function parameter isn't read. */
|
|
92
|
+
// "exactOptionalPropertyTypes": true, /* Interpret optional property types as written, rather than adding 'undefined'. */
|
|
93
|
+
// "noImplicitReturns": true, /* Enable error reporting for codepaths that do not explicitly return in a function. */
|
|
94
|
+
// "noFallthroughCasesInSwitch": true, /* Enable error reporting for fallthrough cases in switch statements. */
|
|
95
|
+
// "noUncheckedIndexedAccess": true, /* Add 'undefined' to a type when accessed using an index. */
|
|
96
|
+
// "noImplicitOverride": true, /* Ensure overriding members in derived classes are marked with an override modifier. */
|
|
97
|
+
// "noPropertyAccessFromIndexSignature": true, /* Enforces using indexed accessors for keys declared using an indexed type. */
|
|
98
|
+
// "allowUnusedLabels": true, /* Disable error reporting for unused labels. */
|
|
99
|
+
// "allowUnreachableCode": true, /* Disable error reporting for unreachable code. */
|
|
100
|
+
|
|
101
|
+
/* Completeness */
|
|
102
|
+
// "skipDefaultLibCheck": true, /* Skip type checking .d.ts files that are included with TypeScript. */
|
|
103
|
+
"skipLibCheck": true /* Skip type checking all .d.ts files. */
|
|
104
|
+
}
|
|
105
|
+
}
|
package/types.ts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { OpenAI, ClientOptions } from 'openai'
|
|
2
|
+
import type { ChatCompletion, ChatCompletionCreateParamsNonStreaming } from 'openai/resources/index.mjs'
|
|
3
|
+
import type { AIDB } from './db/mongo'
|
|
4
|
+
import type PQueue from 'p-queue'
|
|
5
|
+
|
|
6
|
+
export type GPTConfig = {
|
|
7
|
+
openai?: OpenAI
|
|
8
|
+
system?: string
|
|
9
|
+
model?: ChatCompletionCreateParamsNonStreaming['model'] | 'gpt-4-1106-preview' | 'gpt-3.5-turbo-1106'
|
|
10
|
+
db?: AIDB
|
|
11
|
+
queue?: PQueue
|
|
12
|
+
} & ClientOptions
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { OpenAI, ClientOptions } from 'openai'
|
|
2
|
+
import type { ChatCompletion, ChatCompletionCreateParamsNonStreaming } from 'openai/resources/index.mjs'
|
|
3
|
+
import { GPTConfig } from '../types'
|
|
4
|
+
|
|
5
|
+
export type CompletionInput = GPTConfig &
|
|
6
|
+
(
|
|
7
|
+
| (({ user: string } | { list: string }) & Partial<ChatCompletionCreateParamsNonStreaming>)
|
|
8
|
+
| ChatCompletionCreateParamsNonStreaming
|
|
9
|
+
)
|
|
10
|
+
// TODO: add support for list input and parsing
|
|
11
|
+
// TODO: add support for caching w/ seed generation for unit tests
|
|
12
|
+
|
|
13
|
+
export const chatCompletion = async (config: CompletionInput) => {
|
|
14
|
+
const { user, system, model, db, queue, ...rest } = config
|
|
15
|
+
const openai = config.openai ?? new OpenAI(rest)
|
|
16
|
+
const prompt = {
|
|
17
|
+
model,
|
|
18
|
+
messages: [{ role: 'user', content: user }],
|
|
19
|
+
} as ChatCompletionCreateParamsNonStreaming
|
|
20
|
+
if (system) prompt.messages.unshift({ role: 'system', content: system })
|
|
21
|
+
const completion = queue
|
|
22
|
+
? await queue.add(() => openai.chat.completions.create(prompt))
|
|
23
|
+
: await openai.chat.completions.create(prompt)
|
|
24
|
+
if (db) {
|
|
25
|
+
db.log(prompt, completion as ChatCompletion)
|
|
26
|
+
}
|
|
27
|
+
return completion as ChatCompletion
|
|
28
|
+
}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { describe, it, expect } from 'vitest'
|
|
2
|
+
import { generateSchema, parseStringDescription } from './schema'
|
|
3
|
+
|
|
4
|
+
// Tests for parseStringDescription function
|
|
5
|
+
describe('parseStringDescription', () => {
|
|
6
|
+
it('should create a string schema for plain strings', () => {
|
|
7
|
+
const result = parseStringDescription('A plain string description')
|
|
8
|
+
expect(result).toEqual({
|
|
9
|
+
type: 'string',
|
|
10
|
+
description: 'A plain string description',
|
|
11
|
+
})
|
|
12
|
+
})
|
|
13
|
+
|
|
14
|
+
it('should create a string schema with enum for piped strings', () => {
|
|
15
|
+
const result = parseStringDescription('option1 | option2 | option3')
|
|
16
|
+
expect(result).toEqual({
|
|
17
|
+
type: 'string',
|
|
18
|
+
enum: ['option1', 'option2', 'option3'],
|
|
19
|
+
})
|
|
20
|
+
})
|
|
21
|
+
|
|
22
|
+
it('should create a number schema when prefixed with "number: "', () => {
|
|
23
|
+
const result = parseStringDescription('number: A number description')
|
|
24
|
+
expect(result).toEqual({
|
|
25
|
+
type: 'number',
|
|
26
|
+
description: 'A number description',
|
|
27
|
+
})
|
|
28
|
+
})
|
|
29
|
+
|
|
30
|
+
it('should create a boolean schema when prefixed with "boolean: "', () => {
|
|
31
|
+
const result = parseStringDescription('boolean: A boolean description')
|
|
32
|
+
expect(result).toEqual({
|
|
33
|
+
type: 'boolean',
|
|
34
|
+
description: 'A boolean description',
|
|
35
|
+
})
|
|
36
|
+
})
|
|
37
|
+
})
|
|
38
|
+
|
|
39
|
+
// Tests for generateSchema function
|
|
40
|
+
describe('generateSchema', () => {
|
|
41
|
+
it('should create a complex object schema', () => {
|
|
42
|
+
const result = generateSchema({
|
|
43
|
+
name: 'The name of the person',
|
|
44
|
+
age: 'number: The age of the person',
|
|
45
|
+
isActive: 'boolean: Whether the person is active or not',
|
|
46
|
+
})
|
|
47
|
+
|
|
48
|
+
const expected = {
|
|
49
|
+
type: 'object',
|
|
50
|
+
properties: {
|
|
51
|
+
name: { type: 'string', description: 'The name of the person' },
|
|
52
|
+
age: { type: 'number', description: 'The age of the person' },
|
|
53
|
+
isActive: {
|
|
54
|
+
type: 'boolean',
|
|
55
|
+
description: 'Whether the person is active or not',
|
|
56
|
+
},
|
|
57
|
+
},
|
|
58
|
+
required: ['name', 'age', 'isActive'],
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
expect(result).toEqual(expected)
|
|
62
|
+
})
|
|
63
|
+
|
|
64
|
+
it('should throw an error for invalid propDescriptions', () => {
|
|
65
|
+
const callWithInvalidArg = () => generateSchema('invalid argument' as any)
|
|
66
|
+
|
|
67
|
+
expect(callWithInvalidArg).toThrow('The propDescriptions parameter should be an object.')
|
|
68
|
+
})
|
|
69
|
+
})
|