aimyapi 0.1.11 → 0.1.14
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 +16 -4
- package/dist/index.cjs +492 -0
- package/dist/index.d.cts +36 -0
- package/dist/index.d.mts +36 -0
- package/dist/index.d.ts +14 -11
- package/dist/index.js +480 -163
- package/dist/index.mjs +468 -0
- package/example/example1.ts +1 -1
- package/example/example2.ts +1 -1
- package/example/example3.ts +3 -3
- package/example/ordering_api_impl.ts +5 -5
- package/example/sales_api.md +2 -0
- package/example/sales_api.ts +1 -1
- package/example/sales_api_impl.ts +1 -1
- package/package.json +19 -7
- package/prompts/create-task-prompt.md +1 -1
- package/{lib → src}/index.ts +21 -21
- package/tsconfig.json +2 -2
- /package/{lib → src}/sandbox-wrappers.ts +0 -0
- /package/{lib → src}/sandbox.ts +0 -0
- /package/{lib → src}/utils.ts +0 -0
package/{lib → src}/index.ts
RENAMED
|
@@ -1,18 +1,18 @@
|
|
|
1
1
|
require('dotenv').config()
|
|
2
2
|
import fs from 'fs';
|
|
3
|
-
import {
|
|
3
|
+
import { OpenAI } from "openai";
|
|
4
4
|
import path from 'path';
|
|
5
5
|
|
|
6
6
|
import createSandbox from "./sandbox";
|
|
7
7
|
import { rejectOpenPromises } from './sandbox-wrappers';
|
|
8
8
|
|
|
9
|
+
import { ChatCompletionMessageParam } from 'openai/resources/index.mjs';
|
|
9
10
|
import { QuickJSWASMModule, getQuickJS } from "quickjs-emscripten";
|
|
10
11
|
|
|
11
|
-
const
|
|
12
|
-
apiKey: process.env.OPENAI_API_KEY,
|
|
12
|
+
const openai = new OpenAI({
|
|
13
|
+
apiKey: process.env.OPENAI_API_KEY // This is also the default, can be omitted
|
|
13
14
|
});
|
|
14
15
|
|
|
15
|
-
const openai = new OpenAIApi(configuration);
|
|
16
16
|
|
|
17
17
|
// function formatObject(obj: any): string {
|
|
18
18
|
// return Object.keys(obj).reduce((acc, key) => {
|
|
@@ -38,13 +38,13 @@ export interface GenerateCodeResult {
|
|
|
38
38
|
}
|
|
39
39
|
|
|
40
40
|
// FIXME: add an options parameter and allow specifying the token limit
|
|
41
|
-
export const generateCode = async function(queryText:string, userChatHistory:
|
|
41
|
+
export const generateCode = async function(queryText:string, userChatHistory:ChatCompletionMessageParam[], createTaskPrompt:string, apiPath:string, debug:boolean = false, model="gpt-4o"): Promise<GenerateCodeResult> {
|
|
42
42
|
if(!queryText)
|
|
43
43
|
return { code: '', loggableCode: '' };
|
|
44
44
|
|
|
45
45
|
let generatedCode = '';
|
|
46
46
|
const prompt = createTaskPrompt.replace("{{QUERY_TEXT}}", queryText);
|
|
47
|
-
const messages:
|
|
47
|
+
const messages:ChatCompletionMessageParam[] = [
|
|
48
48
|
{
|
|
49
49
|
role: "system",
|
|
50
50
|
content: prompt,
|
|
@@ -57,18 +57,18 @@ export const generateCode = async function(queryText:string, userChatHistory:Cha
|
|
|
57
57
|
}
|
|
58
58
|
];
|
|
59
59
|
try {
|
|
60
|
-
const results = await openai.
|
|
61
|
-
model
|
|
60
|
+
const results = await openai.chat.completions.create({
|
|
61
|
+
model,
|
|
62
62
|
messages: messages,
|
|
63
|
-
temperature: 0.
|
|
63
|
+
temperature: 0.0,
|
|
64
64
|
max_tokens: 700,
|
|
65
65
|
}
|
|
66
66
|
);
|
|
67
67
|
|
|
68
68
|
if(debug)
|
|
69
|
-
console.log(results.
|
|
69
|
+
console.log(results.usage);
|
|
70
70
|
|
|
71
|
-
const response = results.
|
|
71
|
+
const response = results.choices[0].message.content;
|
|
72
72
|
|
|
73
73
|
if(debug)
|
|
74
74
|
console.log(response);
|
|
@@ -104,12 +104,13 @@ export interface AIMyAPIOptions {
|
|
|
104
104
|
apiGlobalName?: string;
|
|
105
105
|
apiDocsPath?: string;
|
|
106
106
|
debug?: boolean;
|
|
107
|
+
model?: string;
|
|
107
108
|
};
|
|
108
109
|
|
|
109
110
|
export interface AIMyAPIInstance {
|
|
110
111
|
// see xample 2
|
|
111
112
|
options:AIMyAPIOptions;
|
|
112
|
-
generateCode: (queryText:string, userChatHistory:
|
|
113
|
+
generateCode: (queryText:string, userChatHistory:ChatCompletionMessageParam[]) => Promise<GenerateCodeResult>;
|
|
113
114
|
runCode: (task:string) => Promise<void>;
|
|
114
115
|
// run a single request with no history
|
|
115
116
|
processRequest: (userQuery:string, context?: object) => Promise<GenerateCodeResult>;
|
|
@@ -122,7 +123,8 @@ async function createWithAPI(options:AIMyAPIOptions): Promise<AIMyAPIInstance> {
|
|
|
122
123
|
apiGlobalName: "api",
|
|
123
124
|
apiGlobals: {},
|
|
124
125
|
apiWhitelist: Object.getOwnPropertyNames(Object.getPrototypeOf(options.apiObject)).filter((f) => f !== "constructor" && !f.startsWith("_")),
|
|
125
|
-
debug: false,
|
|
126
|
+
debug: false,
|
|
127
|
+
model: "gpt-4o",
|
|
126
128
|
...options
|
|
127
129
|
} as AIMyAPIOptions;
|
|
128
130
|
|
|
@@ -130,17 +132,17 @@ async function createWithAPI(options:AIMyAPIOptions): Promise<AIMyAPIInstance> {
|
|
|
130
132
|
throw new Error("apiObject and apiFilePath are required");
|
|
131
133
|
}
|
|
132
134
|
|
|
133
|
-
const {apiObject, apiWhitelist, apiGlobalName, apiExports, apiDefFilePath, apiDocsPath, debug} = options;
|
|
135
|
+
const {apiObject, apiWhitelist, apiGlobalName, apiExports, apiDefFilePath, apiDocsPath, debug, model} = options;
|
|
134
136
|
|
|
135
137
|
const createTaskPrompt = createBasePrompt( apiDefFilePath, apiDocsPath);
|
|
136
138
|
|
|
137
139
|
return {
|
|
138
140
|
options,
|
|
139
|
-
generateCode: async function(queryText:string, userChatHistory:
|
|
141
|
+
generateCode: async function(queryText:string, userChatHistory:ChatCompletionMessageParam[], currentContext:any = undefined) {
|
|
140
142
|
if(!queryText)
|
|
141
143
|
return { code: '', loggableCode: '' };
|
|
142
144
|
|
|
143
|
-
return await generateCode(queryText, userChatHistory, createTaskPrompt.replace("{{CONTEXT}}", currentContext ? "```" + JSON.stringify(currentContext, null, 2) + "```" : ""), apiDefFilePath, debug);
|
|
145
|
+
return await generateCode(queryText, userChatHistory, createTaskPrompt.replace("{{CONTEXT}}", currentContext ? "```" + JSON.stringify(currentContext, null, 2) + "```" : ""), apiDefFilePath, debug, model);
|
|
144
146
|
},
|
|
145
147
|
runCode: async function(generatedCode:string) {
|
|
146
148
|
if(!QuickJS) {
|
|
@@ -226,11 +228,11 @@ export interface AIMyAPIModuleExports {
|
|
|
226
228
|
createWithAPI: (options:AIMyAPIOptions) => Promise<AIMyAPIInstance>;
|
|
227
229
|
|
|
228
230
|
createBasePrompt: (apiFilePath:string, documentationPath: string) => string;
|
|
229
|
-
generateCode: (queryText:string, userChatHistory:
|
|
231
|
+
generateCode: (queryText:string, userChatHistory:ChatCompletionMessageParam[], createTaskPrompt:string, apiPath:string, debug:boolean) => Promise<GenerateCodeResult>;
|
|
230
232
|
createSandbox: (QuickJS:QuickJSWASMModule, globals: any) => Promise<any>;
|
|
231
233
|
}
|
|
232
234
|
|
|
233
|
-
const aimyapi:AIMyAPIModuleExports = {
|
|
235
|
+
export const aimyapi:AIMyAPIModuleExports = {
|
|
234
236
|
// Standard way of using the library
|
|
235
237
|
createWithAPI,
|
|
236
238
|
|
|
@@ -238,6 +240,4 @@ const aimyapi:AIMyAPIModuleExports = {
|
|
|
238
240
|
createBasePrompt,
|
|
239
241
|
generateCode,
|
|
240
242
|
createSandbox,
|
|
241
|
-
}
|
|
242
|
-
|
|
243
|
-
export default aimyapi;
|
|
243
|
+
}
|
package/tsconfig.json
CHANGED
|
@@ -4,9 +4,9 @@
|
|
|
4
4
|
"module": "nodenext" /* Some imports are really imports: https://www.typescriptlang.org/docs/handbook/esm-node.html */,
|
|
5
5
|
"moduleResolution": "nodenext" /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */,
|
|
6
6
|
"lib": ["ES2021", "dom"] /* Specify library files to be included in the compilation. */,
|
|
7
|
-
"outDir": "dist" /* Redirect output structure to the directory. */,
|
|
7
|
+
"outDir": "./dist" /* Redirect output structure to the directory. */,
|
|
8
8
|
"declaration": true
|
|
9
9
|
},
|
|
10
|
-
"include": ["**/
|
|
10
|
+
"include": ["**/src/*.ts"],
|
|
11
11
|
"exclude": ["**/node_modules, **/dist"]
|
|
12
12
|
}
|
|
File without changes
|
/package/{lib → src}/sandbox.ts
RENAMED
|
File without changes
|
/package/{lib → src}/utils.ts
RENAMED
|
File without changes
|