aimyapi 0.2.0 → 0.2.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/dist/index.cjs CHANGED
@@ -340,10 +340,33 @@ const openai = new openai$1.OpenAI({
340
340
  apiKey: process.env.OPENAI_API_KEY
341
341
  // This is also the default, can be omitted
342
342
  });
343
+ const CODE_START = `\`\`\`
344
+ import * as ApiDefs from 'api.ts'
345
+
346
+ (async() {
347
+ try {`;
348
+ const CODE_END = ` } catch(err) {
349
+ console.error(err);
350
+ }
351
+ })();
352
+ \`\`\``;
353
+ function indentString(str, numTabs) {
354
+ const tabs = " ".repeat(numTabs);
355
+ return str.split("\n").map((line) => tabs + line).join("\n");
356
+ }
357
+ function replaceCodeBlocks(text) {
358
+ const codeBlockRegex = /```[\s\S]*?```/g;
359
+ return text.replace(codeBlockRegex, (match) => {
360
+ const code = match.slice(3, -3).trim();
361
+ return `${CODE_START}
362
+ ${indentString(code, 2)}
363
+ ${CODE_END}`;
364
+ });
365
+ }
343
366
  function createBasePrompt(apiFilePath, documentationPath) {
344
367
  const apiText = fs.readFileSync(apiFilePath, "utf8");
345
368
  const documentationText = documentationPath ? fs.readFileSync(documentationPath, "utf8") : "";
346
- const createTaskPrompt = fs.readFileSync(path.join(__dirname, "../prompts/create-task-prompt.md"), "utf8").replace("{{DOCUMENTATION}}", documentationText).replace("{{API}}", apiText);
369
+ const createTaskPrompt = fs.readFileSync(path.join(__dirname, "../prompts/create-task-prompt.md"), "utf8").replace("{{DOCUMENTATION}}", replaceCodeBlocks(documentationText)).replace("{{API}}", apiText);
347
370
  return createTaskPrompt;
348
371
  }
349
372
  const generateCode = async function(queryText, userChatHistory, createTaskPrompt, debug = false, model = "gpt-4o-mini") {
package/dist/index.js CHANGED
@@ -340,10 +340,33 @@ const openai = new openai$1.OpenAI({
340
340
  apiKey: process.env.OPENAI_API_KEY
341
341
  // This is also the default, can be omitted
342
342
  });
343
+ const CODE_START = `\`\`\`
344
+ import * as ApiDefs from 'api.ts'
345
+
346
+ (async() {
347
+ try {`;
348
+ const CODE_END = ` } catch(err) {
349
+ console.error(err);
350
+ }
351
+ })();
352
+ \`\`\``;
353
+ function indentString(str, numTabs) {
354
+ const tabs = " ".repeat(numTabs);
355
+ return str.split("\n").map((line) => tabs + line).join("\n");
356
+ }
357
+ function replaceCodeBlocks(text) {
358
+ const codeBlockRegex = /```[\s\S]*?```/g;
359
+ return text.replace(codeBlockRegex, (match) => {
360
+ const code = match.slice(3, -3).trim();
361
+ return `${CODE_START}
362
+ ${indentString(code, 2)}
363
+ ${CODE_END}`;
364
+ });
365
+ }
343
366
  function createBasePrompt(apiFilePath, documentationPath) {
344
367
  const apiText = fs.readFileSync(apiFilePath, "utf8");
345
368
  const documentationText = documentationPath ? fs.readFileSync(documentationPath, "utf8") : "";
346
- const createTaskPrompt = fs.readFileSync(path.join(__dirname, "../prompts/create-task-prompt.md"), "utf8").replace("{{DOCUMENTATION}}", documentationText).replace("{{API}}", apiText);
369
+ const createTaskPrompt = fs.readFileSync(path.join(__dirname, "../prompts/create-task-prompt.md"), "utf8").replace("{{DOCUMENTATION}}", replaceCodeBlocks(documentationText)).replace("{{API}}", apiText);
347
370
  return createTaskPrompt;
348
371
  }
349
372
  const generateCode = async function(queryText, userChatHistory, createTaskPrompt, debug = false, model = "gpt-4o-mini") {
package/dist/index.mjs CHANGED
@@ -318,10 +318,33 @@ const openai = new OpenAI({
318
318
  apiKey: process.env.OPENAI_API_KEY
319
319
  // This is also the default, can be omitted
320
320
  });
321
+ const CODE_START = `\`\`\`
322
+ import * as ApiDefs from 'api.ts'
323
+
324
+ (async() {
325
+ try {`;
326
+ const CODE_END = ` } catch(err) {
327
+ console.error(err);
328
+ }
329
+ })();
330
+ \`\`\``;
331
+ function indentString(str, numTabs) {
332
+ const tabs = " ".repeat(numTabs);
333
+ return str.split("\n").map((line) => tabs + line).join("\n");
334
+ }
335
+ function replaceCodeBlocks(text) {
336
+ const codeBlockRegex = /```[\s\S]*?```/g;
337
+ return text.replace(codeBlockRegex, (match) => {
338
+ const code = match.slice(3, -3).trim();
339
+ return `${CODE_START}
340
+ ${indentString(code, 2)}
341
+ ${CODE_END}`;
342
+ });
343
+ }
321
344
  function createBasePrompt(apiFilePath, documentationPath) {
322
345
  const apiText = fs.readFileSync(apiFilePath, "utf8");
323
346
  const documentationText = documentationPath ? fs.readFileSync(documentationPath, "utf8") : "";
324
- const createTaskPrompt = fs.readFileSync(path.join(__dirname, "../prompts/create-task-prompt.md"), "utf8").replace("{{DOCUMENTATION}}", documentationText).replace("{{API}}", apiText);
347
+ const createTaskPrompt = fs.readFileSync(path.join(__dirname, "../prompts/create-task-prompt.md"), "utf8").replace("{{DOCUMENTATION}}", replaceCodeBlocks(documentationText)).replace("{{API}}", apiText);
325
348
  return createTaskPrompt;
326
349
  }
327
350
  const generateCode = async function(queryText, userChatHistory, createTaskPrompt, debug = false, model = "gpt-4o-mini") {
@@ -10,37 +10,21 @@ user: How are you today?
10
10
 
11
11
  assistant:
12
12
  ```
13
- import * as ApiDefs from 'api.ts'
14
-
15
- (async() {
16
- try {
17
- orderingApi.respondToUser("I'm great, thanks for asking!");
18
- } catch(err) {
19
- console.error(err);
20
- }
21
- })();
13
+ orderingApi.respondToUser("I'm great, thanks for asking!");
22
14
  ```
23
15
 
24
16
  user: Add a hamburger with no cheese to my order
25
17
 
26
18
  assistant:
27
19
  ```
28
- import * as ApiDefs from 'api.ts'
29
-
30
- (async() {
31
- try {
32
- const itemName = "Hamburger";
33
- const item = ApiDefs.Menu.find((item) => item.name === itemName);
34
- if (!item) {
35
- throw new Error(`Item with name "${itemName}" not found`);
36
- }
37
-
38
- orderingApi.addItemToOrder(itemName, undefined, item.toppings.filter( topping => topping.name != "cheese") );
39
- orderingApi.respondToUser("Ok, I added a Hamburger. What else would you like?");
40
- } catch(err) {
41
- console.error(err);
42
- }
43
- })();
20
+ const itemName = "Hamburger";
21
+ const item = ApiDefs.Menu.find((item) => item.name === itemName);
22
+ if (!item) {
23
+ throw new Error(`Item with name "${itemName}" not found`);
24
+ }
25
+
26
+ orderingApi.addItemToOrder(itemName, undefined, item.toppings.filter( topping => topping.name != "cheese") );
27
+ orderingApi.respondToUser("Ok, I added a Hamburger. What else would you like?");
44
28
  ```
45
29
 
46
30
  log: Added Hamburger with id 1
@@ -49,39 +33,22 @@ user: I'd like a large pizza with mushrooms
49
33
 
50
34
  assistant:
51
35
  ```
52
-
53
- import * as ApiDefs from 'api.ts'
54
-
55
- (async() {
56
- try {
57
- orderingApi.addItemToOrder("Pizza", ["Large"], ["mushrooms"]);
58
- orderingApi.respondToUser("Ok, I added a Large Pizza with mushrooms");
59
- } catch(err) {
60
- console.error(err);
61
- }
62
- })();
36
+ orderingApi.addItemToOrder("Pizza", ["Large"], ["mushrooms"]);
37
+ orderingApi.respondToUser("Ok, I added a Large Pizza with mushrooms");
63
38
  ```
64
39
 
65
40
  user: Add a hamburger with bacon
66
41
 
67
42
  assistant:
68
43
  ```
69
- import * as ApiDefs from 'api.ts'
70
-
71
- (async() {
72
- try {
73
- const itemName = "Hamburger";
74
- const item = ApiDefs.Menu.find((item) => item.name === itemName);
75
- if (!item) {
76
- throw new Error(`Item with name "${itemName}" not found`);
77
- }
78
-
79
- orderingApi.addItemToOrder(itemName, undefined, [...item.toppings, "bacon"]);
80
- orderingApi.respondToUser("Ok, I added a Hamburger with no cheese or onions. What else would you like?");
81
- } catch(err) {
82
- console.error(err);
83
- }
84
- })();
44
+ const itemName = "Hamburger";
45
+ const item = ApiDefs.Menu.find((item) => item.name === itemName);
46
+ if (!item) {
47
+ throw new Error(`Item with name "${itemName}" not found`);
48
+ }
49
+
50
+ orderingApi.addItemToOrder(itemName, undefined, [...item.toppings, "bacon"]);
51
+ orderingApi.respondToUser("Ok, I added a Hamburger with no cheese or onions. What else would you like?");
85
52
  ```
86
53
 
87
54
  log: Added Hamburger with id 2
@@ -90,44 +57,28 @@ user: Add jalapenos to that
90
57
 
91
58
  assistant:
92
59
  ```
93
- import * as ApiDefs from 'api.ts'
94
-
95
- (async() {
96
- try {
97
- const existingItem = orderingApi.getItemInOrder("2");
98
-
99
- if(existingItem) {
100
- orderingApi.modifyItemInOrder("2", existingItem.customizations.map(item => item.name), [...existingItem.toppings, "jalapenos"]);
101
- orderingApi.respondToUser("Ok, I added jalapenos to your Hamburger");
102
- } else {
103
- orderingApi.respondToUser("Hmm, I can't find that item.");
104
- }
105
- } catch(err) {
106
- console.error(err);
107
- }
108
- })();
60
+ const existingItem = orderingApi.getItemInOrder("2");
61
+
62
+ if(existingItem) {
63
+ orderingApi.modifyItemInOrder("2", existingItem.customizations.map(item => item.name), [...existingItem.toppings, "jalapenos"]);
64
+ orderingApi.respondToUser("Ok, I added jalapenos to your Hamburger");
65
+ } else {
66
+ orderingApi.respondToUser("Hmm, I can't find that item.");
67
+ }
109
68
  ```
110
69
 
111
70
  user: Add a hamburger with no lettuce
112
71
 
113
72
  assistant:
114
73
  ```
115
- import * as ApiDefs from 'api.ts'
116
-
117
- (async() {
118
- try {
119
- const itemName = "Hamburger";
120
- const item = ApiDefs.Menu.find((item) => item.name === itemName);
121
- if (!item) {
122
- throw new Error(`Item with name "${itemName}" not found`);
123
- }
124
-
125
- orderingApi.addItemToOrder(itemName, undefined, [...item.toppings.filter(topping => topping != "lettuce")]);
126
- orderingApi.respondToUser("Ok, I added a Hamburger with no cheese or onions. What else would you like?");
127
- } catch(err) {
128
- console.error(err);
129
- }
130
- })();
74
+ const itemName = "Hamburger";
75
+ const item = ApiDefs.Menu.find((item) => item.name === itemName);
76
+ if (!item) {
77
+ throw new Error(`Item with name "${itemName}" not found`);
78
+ }
79
+
80
+ orderingApi.addItemToOrder(itemName, undefined, [...item.toppings.filter(topping => topping != "lettuce")]);
81
+ orderingApi.respondToUser("Ok, I added a Hamburger with no cheese or onions. What else would you like?");
131
82
  ```
132
83
 
133
84
  log: Added Hamburger with id 2
@@ -137,13 +88,5 @@ user: I want an elephant.
137
88
 
138
89
  assistant:
139
90
  ```
140
- import * as ApiDefs from 'api.ts'
141
-
142
- (async() {
143
- try {
144
- orderingApi.respondToUser("I'm sorry, but that isn't on our menu.");
145
- } catch(err) {
146
- console.error(err);
147
- }
148
- })();
91
+ orderingApi.respondToUser("I'm sorry, but that isn't on our menu.");
149
92
  ```
@@ -6,13 +6,5 @@ user: How are you today?
6
6
 
7
7
  assistant:
8
8
  ```
9
- import * as ApiDefs from 'api.ts'
10
-
11
- (async() {
12
- try {
13
- api.print("I'm great, thanks for asking!");
14
- } catch(err) {
15
- console.error(err);
16
- }
17
- })();
9
+ api.print("I'm great, thanks for asking!");
18
10
  ```
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "aimyapi",
3
- "version": "0.2.0",
3
+ "version": "0.2.1",
4
4
  "description": "AI My API! Use LLMs to create small programs that safely* run and make calls against your API. Similar to ChatGPT's Code Interpreter.",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
package/src/index.ts CHANGED
@@ -13,19 +13,37 @@ const openai = new OpenAI({
13
13
  apiKey: process.env.OPENAI_API_KEY // This is also the default, can be omitted
14
14
  });
15
15
 
16
+ const CODE_START = `\`\`\`
17
+ import * as ApiDefs from 'api.ts'
16
18
 
17
- // function formatObject(obj: any): string {
18
- // return Object.keys(obj).reduce((acc, key) => {
19
- // return `${acc}${key}:\n${obj[key]}\n\n`;
20
- // }, "");
21
- // }
19
+ (async() {
20
+ try {`;
21
+
22
+ const CODE_END = ` } catch(err) {
23
+ console.error(err);
24
+ }
25
+ })();
26
+ \`\`\``;
27
+
28
+ function indentString(str, numTabs) {
29
+ const tabs = '\t'.repeat(numTabs);
30
+ return str.split('\n').map(line => tabs + line).join('\n');
31
+ }
32
+
33
+ function replaceCodeBlocks(text) {
34
+ const codeBlockRegex = /```[\s\S]*?```/g;
35
+ return text.replace(codeBlockRegex, (match) => {
36
+ const code = match.slice(3, -3).trim();
37
+ return `${CODE_START}\n${indentString(code, 2)}\n${CODE_END}`;
38
+ });
39
+ }
22
40
 
23
41
  export function createBasePrompt(apiFilePath:string, documentationPath: string): string {
24
42
  const apiText: string = fs.readFileSync(apiFilePath, 'utf8');
25
43
  const documentationText: string = documentationPath ? fs.readFileSync(documentationPath, 'utf8') : '';
26
44
 
27
45
  //console.time("Loading templates");
28
- const createTaskPrompt: string = fs.readFileSync( path.join(__dirname, '../prompts/create-task-prompt.md'), 'utf8').replace("{{DOCUMENTATION}}", documentationText).replace("{{API}}", apiText);
46
+ const createTaskPrompt: string = fs.readFileSync( path.join(__dirname, '../prompts/create-task-prompt.md'), 'utf8').replace("{{DOCUMENTATION}}", replaceCodeBlocks(documentationText)).replace("{{API}}", apiText);
29
47
  //console.timeEnd("Loading templates")
30
48
 
31
49
  return createTaskPrompt;