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
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2023 Nathan Clevenger
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -2,7 +2,8 @@
|
|
|
2
2
|
|
|
3
3
|
Library for Developing and Managing AI Functions (including OpenAI GPT4 / GPT3.5)
|
|
4
4
|
|
|
5
|
-
Key Features:
|
|
5
|
+
Key Features:
|
|
6
|
+
|
|
6
7
|
- Enables easy development of AI functions
|
|
7
8
|
|
|
8
9
|
```javascript
|
|
@@ -12,16 +13,16 @@ const { ai, gpt, list } = AI({ apiKey: OPENAI_API_KEY })
|
|
|
12
13
|
```
|
|
13
14
|
|
|
14
15
|
Then you can use magic `ai` functions:
|
|
15
|
-
```javascript
|
|
16
16
|
|
|
17
|
-
|
|
17
|
+
```javascript
|
|
18
|
+
const categorizeProduct = ai.categorizeProduct({
|
|
18
19
|
productType: 'App | API | Marketplace | Platform | Packaged Service | Professional Service | Website',
|
|
19
20
|
customer: 'ideal customer profile in 3-5 words',
|
|
20
21
|
solution: 'describe the offer in 4-10 words',
|
|
21
22
|
description: 'website meta description',
|
|
22
23
|
})
|
|
23
24
|
|
|
24
|
-
const product = await
|
|
25
|
+
const product = await categorizeProduct({ domain: name })
|
|
25
26
|
```
|
|
26
27
|
|
|
27
28
|
you can also use `list` tagged template as a convienence function:
|
|
@@ -43,7 +44,7 @@ Or in a more complex example:
|
|
|
43
44
|
|
|
44
45
|
```javascript
|
|
45
46
|
const listBlogPosts = (count, topic) => list`${count} blog post titles about ${topic}`
|
|
46
|
-
const writeBlogPost = title => gpt`write a blog post in markdown starting with "# ${title}"`
|
|
47
|
+
const writeBlogPost = (title) => gpt`write a blog post in markdown starting with "# ${title}"`
|
|
47
48
|
|
|
48
49
|
async function* writeBlog(count, topic) {
|
|
49
50
|
for await (const title of listBlogPosts(count, topic)) {
|
|
@@ -55,4 +56,4 @@ async function* writeBlog(count, topic) {
|
|
|
55
56
|
for await (const post of writeBlog(25, 'future of car sales')) {
|
|
56
57
|
console.log({ post })
|
|
57
58
|
}
|
|
58
|
-
```
|
|
59
|
+
```
|
package/db/cache.ts
ADDED
package/db/mongo.ts
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import type { MongoClient, Db, Collection, InsertOneResult, BSON } from 'mongodb'
|
|
2
|
+
import type { ChatCompletion, ChatCompletionCreateParamsNonStreaming } from 'openai/resources/index.mjs'
|
|
3
|
+
import { QueueInput } from '../queue/mongo'
|
|
4
|
+
|
|
5
|
+
export let client: MongoClient
|
|
6
|
+
export let db: Db
|
|
7
|
+
export let cache: Collection
|
|
8
|
+
export let cacheTTL: number
|
|
9
|
+
export let events: Collection
|
|
10
|
+
export let queue: Collection
|
|
11
|
+
export let actors: Collection
|
|
12
|
+
|
|
13
|
+
export type AIDBConfig = {
|
|
14
|
+
client: MongoClient
|
|
15
|
+
db?: Db | string
|
|
16
|
+
cache?: Collection | string
|
|
17
|
+
cacheTTL?: number
|
|
18
|
+
actors?: Collection | string
|
|
19
|
+
events?: Collection | string
|
|
20
|
+
queue?: Collection | string
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export type AIDB = {
|
|
24
|
+
client: MongoClient
|
|
25
|
+
db: Db
|
|
26
|
+
cache: Collection
|
|
27
|
+
cacheTTL: number
|
|
28
|
+
actors: Collection
|
|
29
|
+
events: Collection
|
|
30
|
+
queue: Collection
|
|
31
|
+
log: (prompt: ChatCompletionCreateParamsNonStreaming, completion: ChatCompletion) => Promise<InsertOneResult<any>>
|
|
32
|
+
send: (input: QueueInput | QueueInput[]) => Promise<InsertOneResult<any>>
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export const AIDB = (args: AIDBConfig | MongoClient) => {
|
|
36
|
+
const config = (args as AIDBConfig).client ? (args as AIDBConfig) : { client: args as MongoClient }
|
|
37
|
+
client = config.client
|
|
38
|
+
db = typeof config.db === 'string' ? client.db(config.db) : config.db ?? client.db()
|
|
39
|
+
cache = typeof config.cache === 'string' ? db.collection(config.cache) : config.cache ?? db.collection('ai-cache')
|
|
40
|
+
cacheTTL = config.cacheTTL ?? 1000 * 60 * 60 * 24 * 30
|
|
41
|
+
events =
|
|
42
|
+
typeof config.events === 'string' ? db.collection(config.events) : config.events ?? db.collection('ai-events')
|
|
43
|
+
queue = typeof config.queue === 'string' ? db.collection(config.queue) : config.queue ?? db.collection('ai-queue')
|
|
44
|
+
actors = typeof config.queue === 'string' ? db.collection(config.queue) : config.queue ?? db.collection('ai-actors')
|
|
45
|
+
return { client, db, cache, actors, events, queue, log, send } as AIDB
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
const send = (input: QueueInput | QueueInput[]) =>
|
|
49
|
+
Array.isArray(input) ? queue.insertMany(input) : queue.insertOne(input)
|
|
50
|
+
|
|
51
|
+
const log = (prompt: ChatCompletionCreateParamsNonStreaming, completion: ChatCompletion) => {
|
|
52
|
+
const system = prompt.messages.find((message) => message.role === 'system')?.content
|
|
53
|
+
const userMessages = prompt.messages.filter((message) => message.role === 'user').map((message) => message.content)
|
|
54
|
+
const user = userMessages.length === 1 ? userMessages[0] : userMessages
|
|
55
|
+
const content = completion.choices?.[0].message.content
|
|
56
|
+
const tool = completion.choices?.[0].message.tool_calls?.[0].function
|
|
57
|
+
let functionData: object | string | undefined
|
|
58
|
+
try {
|
|
59
|
+
if (tool) functionData = JSON.parse(tool.arguments)
|
|
60
|
+
} catch (err) {
|
|
61
|
+
functionData = tool?.arguments
|
|
62
|
+
}
|
|
63
|
+
const timestamp = new Date()
|
|
64
|
+
const event = {
|
|
65
|
+
timestamp,
|
|
66
|
+
user,
|
|
67
|
+
system,
|
|
68
|
+
content,
|
|
69
|
+
functionName: tool?.name,
|
|
70
|
+
functionData,
|
|
71
|
+
prompt,
|
|
72
|
+
completion,
|
|
73
|
+
}
|
|
74
|
+
return events.insertOne(event, { ignoreUndefined: true })
|
|
75
|
+
}
|