-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.ts
265 lines (238 loc) · 6.45 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
import { OpenAI } from '../openai'
import { VertexAI } from '../vertexai'
import {
createFineTuningJob,
showFineTuningJob,
uploadJsonlFile,
} from './fineTune'
import { skeetAiPrisma } from './prisma'
import { skeetPrompt } from './skeet'
import * as dotenv from 'dotenv'
import { skeetGenTypedoc } from './typedoc'
import { skeetNaming } from './naming'
import { skeetAiTranslates } from './tranlsate'
import { Example, InstanceType, NamingEnum } from '../types/skeetaiTypes'
import { skeetFirestore } from './firestore'
import { generatePrompt } from '../genPrompt'
import { OpenAIPromptParams, VertexPromptParams } from '../types'
import { skeetMethod } from './method'
import { skeetFunction } from './function'
dotenv.config()
/**
* Configuration options for initializing a SkeetAI instance.
*/
export interface SkeetAIOptions {
/**
* AI platform type (either 'VertexAI' or 'OpenAI'). Default is 'VertexAI'.
*/
ai?: 'VertexAI' | 'OpenAI'
/**
* Model name to be used by the chosen AI platform. Defaults are 'chat-bison@001' for VertexAI and 'gpt-4' for OpenAI.
*/
model?: string
/**
* Maximum number of tokens to be returned in the response. Default is 1000.
*/
maxTokens?: number
/**
* Temperature parameter for the AI platform. Default is 0.2.
* @see https://beta.openai.com/docs/api-reference/completions/create#temperature
*/
temperature?: number
}
export interface AIPromptable {
prompt(input: string): Promise<any>
}
/**
* The main SkeetAI class for handling AI interactions.
*/
export class SkeetAI {
ai: 'VertexAI' | 'OpenAI'
model: string
maxTokens: number
temperature: number
private _initOptions: SkeetAIOptions
static readonly PRISMA_SCHEMA_PATH: string = './graphql/prisma/schema.prisma'
static readonly MODEL_PATH: string = './common/models'
static readonly FUNCTION_ROOT = './functions'
aiInstance: VertexAI | OpenAI
functionConfigPaths = (functionName: string) => {
const paths = {
packageJson: `${SkeetAI.FUNCTION_ROOT}/${functionName}/package.json`,
tsconfigJson: `${SkeetAI.FUNCTION_ROOT}/${functionName}/tsconfig.json`,
}
return paths
}
/**
* Creates an instance of SkeetAI.
* @param options - Configuration options for initializing the SkeetAI.
* @example
* ```typescript
* // Using default settings:
* const defaultSkeet = new SkeetAI();
*
* // Specifying the AI platform and model:
* const customSkeet = new SkeetAI({
* ai: 'OpenAI',
* model: 'gpt-4',
* maxTokens: 500
* });
* ```
*/
constructor(options: SkeetAIOptions = {}) {
this.ai = options.ai || 'VertexAI'
this.model =
options.model || (this.ai === 'VertexAI' ? 'chat-bison@001' : 'gpt-4')
this.maxTokens = options.maxTokens || 1000
this.temperature = options.temperature || 0
if (this.ai === 'VertexAI') {
this.aiInstance = new VertexAI({
model: this.model,
maxOutputTokens: this.maxTokens,
})
} else {
this.aiInstance = new OpenAI({
model: this.model,
maxTokens: this.maxTokens,
apiKey: process.env.CHAT_GPT_KEY || '',
organizationKey: process.env.CHAT_GPT_ORG || '',
})
}
this._initOptions = options
}
get initOptions(): SkeetAIOptions {
return this._initOptions
}
async prisma(content: string) {
try {
return await skeetAiPrisma(content, this)
} catch (error: any) {
this.handleError(error)
}
}
async skeet(content: string) {
try {
return await skeetPrompt(content, this)
} catch (error: any) {
throw new Error(`skeet: ${error}`)
}
}
async uploadFile(filePath: string) {
try {
return await uploadJsonlFile(filePath, this.ai, this.aiInstance)
} catch (error: any) {
this.handleError(error)
}
}
async createFineTuningJob(fileId: string, model = 'gpt-3.5-turbo-0613') {
try {
return await createFineTuningJob(fileId, model, this.ai, this.aiInstance)
} catch (error) {
this.handleError(error)
}
}
async showFineTuningJob(jobId: string) {
try {
return await showFineTuningJob(jobId, this.ai, this.aiInstance)
} catch (error) {
this.handleError(error)
}
}
async typedoc(content: string) {
try {
return await skeetGenTypedoc(content, this)
} catch (error: any) {
this.handleError(error)
}
}
async naming(content: string, namingEnum = NamingEnum.FUNCTION) {
try {
return await skeetNaming(content, this, namingEnum)
} catch (error: any) {
this.handleError(error)
}
}
async translates(paths: string[], langFrom = 'ja', langTo = 'en') {
try {
return await skeetAiTranslates(paths, langFrom, langTo, this)
} catch (error: any) {
this.handleError(error)
}
}
async firestore(content: string) {
try {
return await skeetFirestore(content, this)
} catch (error: any) {
this.handleError(error)
}
}
async function(
content: string,
tsconfig: string,
packageJson: string,
prettierrc: string,
existingFunctions: string,
existingModels: string,
instanceType: InstanceType,
) {
try {
return await skeetFunction(
content,
this,
tsconfig,
packageJson,
prettierrc,
existingFunctions,
existingModels,
instanceType,
)
} catch (error: any) {
this.handleError(error)
}
}
async method(
content: string,
tsconfig: string,
packageJson: string,
prettierrc: string,
existingFunctions: string,
existingModels: string,
) {
try {
return await skeetMethod(
content,
this,
tsconfig,
packageJson,
prettierrc,
existingFunctions,
existingModels,
)
} catch (error: any) {
this.handleError(error)
}
}
async run(example: Example, content: string) {
try {
const prompt = generatePrompt(
example.context,
example.examples,
content,
this.ai,
)
if (this.ai === 'VertexAI') {
const aiInstance = this.aiInstance as VertexAI
return await aiInstance.prompt(prompt as VertexPromptParams)
} else {
const aiInstance = this.aiInstance as OpenAI
return await aiInstance.prompt(prompt as OpenAIPromptParams)
}
} catch (error) {
throw new Error(`run: ${error}`)
}
}
private handleError(error: any) {
console.error('Error:', error)
}
}
export default SkeetAI