ai-functions 0.0.1
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/README.md +14 -0
- package/example.js +0 -0
- package/functions/generateBlogPost.js +12 -0
- package/functions/generateLandingPage.js +11 -0
- package/functions/generatePossibleBlogPostTitles.js +11 -0
- package/index.d.ts +0 -0
- package/index.js +84 -0
- package/index.test.js +54 -0
- package/package.json +28 -0
package/README.md
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
# AI Functions
|
|
2
|
+
|
|
3
|
+
Library for Developing and Managing AI Functions (including OpenAI GPT4 / GPT3.5)
|
|
4
|
+
|
|
5
|
+
Key Features:
|
|
6
|
+
- Enables easy development of AI functions
|
|
7
|
+
|
|
8
|
+
```javascript
|
|
9
|
+
import { AI } from 'ai-functions'
|
|
10
|
+
|
|
11
|
+
const ai = AI({ apiKey: OPENAI_API_KEY })
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
```
|
package/example.js
ADDED
|
File without changes
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export const generateBlogPost = {
|
|
2
|
+
name: 'generateBlogPost',
|
|
3
|
+
description: 'Generate high-converting marketing content for a landing page',
|
|
4
|
+
parameters: {
|
|
5
|
+
type: 'object',
|
|
6
|
+
properties: {
|
|
7
|
+
title: { type: 'string', description: 'The title of the Blog Post' },
|
|
8
|
+
tags: { type: 'array', items: { type: 'string' } },
|
|
9
|
+
markdown: { type: 'string', description: 'The content of the Blog Post in Markdown format' },
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export const generateLandingPage = {
|
|
2
|
+
name: 'generateLandingPage',
|
|
3
|
+
description: 'Generate high-converting marketing content for a landing page',
|
|
4
|
+
parameters: {
|
|
5
|
+
type: 'object',
|
|
6
|
+
properties: {
|
|
7
|
+
title: { type: 'string', description: 'The title of the landing page' },
|
|
8
|
+
description: { type: 'string', description: 'The description of the landing page' },
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export const generateLandingPage = {
|
|
2
|
+
name: 'generateLandingPage',
|
|
3
|
+
description: 'Generate high-converting marketing content for a landing page',
|
|
4
|
+
parameters: {
|
|
5
|
+
type: 'object',
|
|
6
|
+
properties: {
|
|
7
|
+
titles: { type: 'array', items: { type: 'string' }, description: 'The title of the landing page' },
|
|
8
|
+
description: { type: 'string', description: 'The description of the landing page' },
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
}
|
package/index.d.ts
ADDED
|
File without changes
|
package/index.js
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import { Configuration, OpenAIApi } from 'openai-edge'
|
|
2
|
+
|
|
3
|
+
const configuration = new Configuration({
|
|
4
|
+
apiKey: process.env.OPENAI_API_KEY,
|
|
5
|
+
basePath: 'https://aihooks.dev/v1',
|
|
6
|
+
baseOptions: {
|
|
7
|
+
headers: {
|
|
8
|
+
'webhook-app-id': process.env.AIHOOKS_APP_ID
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
})
|
|
12
|
+
const openai = new OpenAIApi(configuration)
|
|
13
|
+
|
|
14
|
+
// const startTime = Date.now()
|
|
15
|
+
// const response = await openai.createChatCompletion({
|
|
16
|
+
// // model: 'gpt-3.5-turbo-0613',
|
|
17
|
+
// model: 'gpt-4-0613',
|
|
18
|
+
// messages: [
|
|
19
|
+
// { role: 'system', content: 'You are an expert marketer.' },
|
|
20
|
+
// // { role: 'user', content: 'Write an ES6 function to convert base16 to base62' },
|
|
21
|
+
// // { role: 'user', content: 'Write an ES6 function to do Fizz Buzz' },
|
|
22
|
+
// // { role: 'user', content: 'List 2 possible blog post titles about APIs' },
|
|
23
|
+
// // { role: 'assistant', content: 'The Los Angeles Dodgers won the World Series in 2020.' },
|
|
24
|
+
// // { role: 'user', content: 'Where was it played?' },
|
|
25
|
+
// { role: 'user', content: 'Write a landing page for Driv.ly' },
|
|
26
|
+
// ],
|
|
27
|
+
// functions: [{
|
|
28
|
+
// name: 'writeLandingPage',
|
|
29
|
+
// // description: 'Write a landing page',
|
|
30
|
+
// parameters: {
|
|
31
|
+
// type: 'object',
|
|
32
|
+
// properties: {
|
|
33
|
+
// title: { type: 'string', description: 'The title of the landing page' },
|
|
34
|
+
// description: { type: 'string', description: 'The description of the landing page' },
|
|
35
|
+
// heroTitle: { type: 'string', description: 'The hero title of the landing page' },
|
|
36
|
+
// heroDescription: { type: 'string', description: 'The hero description of the landing page' },
|
|
37
|
+
// // features: { type: 'array', description: 'The features of the landing page' },
|
|
38
|
+
// featuresTitle: { type: 'string', description: 'The features title of the landing page' },
|
|
39
|
+
// featuresDescription: { type: 'string', description: 'The features description of the landing page' },
|
|
40
|
+
// // features: { type: 'array', description: 'The features of the landing page' },
|
|
41
|
+
// },
|
|
42
|
+
// required: ['title', 'description', 'heroTitle', 'heroDescription', 'featuresTitle', 'featuresDescription',]
|
|
43
|
+
// }
|
|
44
|
+
// }]
|
|
45
|
+
|
|
46
|
+
// // max_tokens: 7,
|
|
47
|
+
// // temperature: 0,
|
|
48
|
+
// // stream: true,
|
|
49
|
+
// // go: true,
|
|
50
|
+
// })
|
|
51
|
+
// const completion = await response.json()
|
|
52
|
+
// const requestTime = Date.now() - startTime
|
|
53
|
+
// // console.timeEnd('openai')
|
|
54
|
+
// const processingTime = parseInt(response.headers.get('openai-processing-ms'))
|
|
55
|
+
// const latency = requestTime - processingTime
|
|
56
|
+
// const status = response.status
|
|
57
|
+
|
|
58
|
+
// const functionName = completion?.choices?.[0]?.message?.function_call?.name
|
|
59
|
+
// const args = JSON.parse(completion?.choices?.[0]?.message?.function_call?.arguments)
|
|
60
|
+
|
|
61
|
+
// console.log({ requestTime, processingTime, latency })
|
|
62
|
+
// console.log(completion?.choices?.[0])
|
|
63
|
+
// console.log(completion?.error)
|
|
64
|
+
// console.log({ functionName, args })
|
|
65
|
+
|
|
66
|
+
export const runtime = {
|
|
67
|
+
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export const AI = (functionName, callback) => {
|
|
71
|
+
runtime[functionName] = callback
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
export const getJsonSchema = propDescriptions => {
|
|
75
|
+
// assume an object like this: { name: 'The name of the person', age: 'The age of the person' }
|
|
76
|
+
// return an object like this: { type: 'object', properties: { name: { type: 'string', description: 'The name of the person' }, age: { type: 'number', description: 'The age of the person' } } required: ['name', 'age'] }
|
|
77
|
+
const properties = Object.entries(propDescriptions).reduce((acc, [key, value]) => {
|
|
78
|
+
acc[key] = { type: typeof value, description: value }
|
|
79
|
+
return acc
|
|
80
|
+
}
|
|
81
|
+
, {})
|
|
82
|
+
const required = Object.keys(properties)
|
|
83
|
+
return { type: 'object', properties, required }
|
|
84
|
+
}
|
package/index.test.js
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
// import assert from 'node:assert'
|
|
2
|
+
// import { describe, it } from 'node:test'
|
|
3
|
+
|
|
4
|
+
import { describe, test, it, expect } from 'vitest'
|
|
5
|
+
|
|
6
|
+
import { AI } from './index.js'
|
|
7
|
+
import { getJsonSchema } from './index.js'
|
|
8
|
+
|
|
9
|
+
test('Math.sqrt()', () => {
|
|
10
|
+
expect(Math.sqrt(4)).toBe(2)
|
|
11
|
+
expect(Math.sqrt(144)).toBe(12)
|
|
12
|
+
expect(Math.sqrt(2)).toBe(Math.SQRT2)
|
|
13
|
+
})
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
describe('getJsonSchema', () => {
|
|
17
|
+
|
|
18
|
+
it('JSON Schema', () => {
|
|
19
|
+
|
|
20
|
+
const schema = getJsonSchema({
|
|
21
|
+
name: 'The name of the person',
|
|
22
|
+
age: 'The age of the person'
|
|
23
|
+
})
|
|
24
|
+
|
|
25
|
+
expect(schema).toEqual({
|
|
26
|
+
type: 'object',
|
|
27
|
+
properties: {
|
|
28
|
+
name: { type: 'string', description: 'The name of the person' },
|
|
29
|
+
age: { type: 'string', description: 'The age of the person' } },
|
|
30
|
+
required: ['name', 'age']
|
|
31
|
+
})
|
|
32
|
+
|
|
33
|
+
})
|
|
34
|
+
|
|
35
|
+
it('should be ok', () => {
|
|
36
|
+
assert.strictEqual(2, 2)
|
|
37
|
+
})
|
|
38
|
+
})
|
|
39
|
+
|
|
40
|
+
describe('A thing', () => {
|
|
41
|
+
it('should work', () => {
|
|
42
|
+
assert.strictEqual(1, 1)
|
|
43
|
+
})
|
|
44
|
+
|
|
45
|
+
it('should be ok', () => {
|
|
46
|
+
assert.strictEqual(2, 2)
|
|
47
|
+
})
|
|
48
|
+
|
|
49
|
+
describe('a nested thing', () => {
|
|
50
|
+
it('should work', () => {
|
|
51
|
+
assert.strictEqual(3, 3)
|
|
52
|
+
})
|
|
53
|
+
})
|
|
54
|
+
})
|
package/package.json
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "ai-functions",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"description": "Library for Developing and Managing AI Functions (including OpenAI GPT4 / GPT3.5)",
|
|
5
|
+
"main": "index.js",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"scripts": {
|
|
8
|
+
"start": "doppler run -- node index.js",
|
|
9
|
+
"test": "doppler run -- vitest --globals"
|
|
10
|
+
},
|
|
11
|
+
"repository": {
|
|
12
|
+
"type": "git",
|
|
13
|
+
"url": "git+https://github.com/nathanclevenger/ai-functions.git"
|
|
14
|
+
},
|
|
15
|
+
"keywords": [],
|
|
16
|
+
"author": "",
|
|
17
|
+
"license": "MIT",
|
|
18
|
+
"bugs": {
|
|
19
|
+
"url": "https://github.com/nathanclevenger/ai-functions/issues"
|
|
20
|
+
},
|
|
21
|
+
"homepage": "https://github.com/nathanclevenger/ai-functions#readme",
|
|
22
|
+
"dependencies": {
|
|
23
|
+
"openai-edge": "^1.1.0"
|
|
24
|
+
},
|
|
25
|
+
"devDependencies": {
|
|
26
|
+
"vitest": "^0.33.0"
|
|
27
|
+
}
|
|
28
|
+
}
|