-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
118 lines (100 loc) · 5.27 KB
/
main.js
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
import chalk from 'chalk';
import fs from 'fs';
import ora from 'ora';
import { dirname } from 'path';
import { fileURLToPath } from 'url';
import Assistant from './apiassistant.js';
import utils from './common/utils.js';
import Generator from './generator/generator.js';
import Simulator from './simulator/simulator.js';
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
async function generate() {
let spinner = ora('Reading API client generator prompt...').start();
try {
const apiClientPrompt = fs.readFileSync(__dirname + '/generator/data/client_generator_prompt_neut.txt', 'utf-8');
const apiSpecFile = __dirname + '/generator/data/uber.json'
spinner.succeed(chalk.green('API client generator prompt read successfully'));
spinner = ora(`Generating API client for ${apiSpecFile}...`).start();
const apiClient = await new Generator(
apiClientPrompt,
() => "Use the following OpenAPI spec:\n" + fs.readFileSync(apiSpecFile, 'utf-8')
).generate();
fs.writeFileSync(__dirname + '/generator/results/apiclient.js', utils.extractCodeBlock(apiClient));
spinner.succeed(chalk.green('API client generated'));
spinner = ora('Reading functions generator prompt...').start();
const functionsGeneratorPrompt = fs.readFileSync(__dirname + '/generator/data/functions_generator_prompt.txt', 'utf-8');
spinner.succeed(chalk.green('Functions generator prompt read successfully'));
spinner = ora('Generating functions...').start();
const functions = await new Generator(
functionsGeneratorPrompt,
() => "Use the following NodeJS class to generate 'functions':\n" + fs.readFileSync(__dirname + '/generator/results/apiclient.js', 'utf-8')
).generate();
fs.writeFileSync(__dirname + '/generator/results/functions.json', utils.extractCodeBlock(functions));
spinner.succeed(chalk.green('Functions generated'));
spinner = ora('Reading safety context generator prompt...').start();
const contextGeneratorPrompt = fs.readFileSync(__dirname + '/generator/data/context_generator_prompt.txt', 'utf-8');
spinner.succeed(chalk.green('Context generator prompt read successfully'));
spinner = ora('Generating safety context...').start();
const context = await new Generator(
contextGeneratorPrompt,
() => "Use the following OpenAPI spec:\n" + fs.readFileSync(__dirname + '/generator/data/uber.json', 'utf-8')
).generate();
fs.writeFileSync(__dirname + '/generator/results/safety_context.txt', context);
spinner.succeed(chalk.green('Safety context generated'));
} catch (error) {
spinner.fail(chalk.red('Generation process failed: ' + error.message));
throw error;
}
}
async function simulate() {
let spinner = ora('Initializing simulation...').start();
try {
const ApiClient = (await import('./generator/results/apiclient.js')).default;
const assistant = new Assistant(
JSON.parse(fs.readFileSync(__dirname + '/generator/results/functions.json', 'utf-8')),
new ApiClient(),
);
const question = "What products are available in San Francisco?";
spinner.text = 'Getting test answer from tool...';
console.log(chalk.yellow("\nQ: "), question);
const answer = await assistant.getAnswer(question);
console.log(chalk.blue("\nA: "), answer);
spinner.succeed(chalk.green('Assistant operational'));
spinner = ora('Reading safety context...').start();
const context = fs.readFileSync(__dirname + '/generator/results/safety_context.txt', 'utf8');
spinner.succeed(chalk.green('Safety context read successfully'));
spinner = ora('Reading simulation scenarios...').start();
const scenarios = JSON.parse(fs.readFileSync(__dirname + '/simulator/data/scenarios.json', 'utf8'));
spinner.succeed(chalk.green('Simulation scenarios read successfully'));
const availableScenarios = `[${scenarios.map(scenario => scenario.name).join(', ')}]`
console.log(chalk.blue("Available scenarios: "), availableScenarios);
// We could run them in parallel, but let's run them sequentially for now.
await scenarios.map(scenario => async () => {
let spinner = ora(`Running simulation for ${scenario.name}...`).start();
await new Simulator(assistant, scenario, context).simulate()
spinner.succeed(chalk.green(`Simulation for ${scenario.name} complete`));
}).reduce((prev, next) => prev.then(next), Promise.resolve())
console.log(chalk.green("Scenarios simulated successfully: "), availableScenarios);
} catch (error) {
spinner.fail(chalk.red('Simulation process failed: ' + error.message));
throw error;
}
}
// Get the command line arguments
const args = process.argv.slice(2);
// Check the first argument and call the appropriate function
switch (args[0]) {
case 'generate':
generate();
break;
case 'simulate':
simulate();
break;
case 'full':
generate().then(simulate);
break;
default:
console.log('Invalid argument. Please use "generate", "simulate" or "full".');
break;
}