-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
108 lines (90 loc) · 2.4 KB
/
index.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
#!/usr/bin/env node
import chalk from 'chalk';
import { execa } from 'execa';
import fs from 'fs/promises';
import inquirer from 'inquirer';
import ora from 'ora';
const createAIAgent = async name => {
const spinner = ora('Creating AI agent...').start();
try {
// Create project directory
await fs.mkdir(name);
process.chdir(name);
// Initialize npm project
await execa('pnpm', ['init']);
// Install dependencies
spinner.text = 'Installing dependencies...';
await execa('pnpm', ['add', 'langbase', 'dotenv']);
// Create main file
const mainFileContent = `
import 'dotenv/config';
import { Pipe } from 'langbase';
async function main() {
const pipe = new Pipe({
apiKey: process.env.LANGBASE_MY_PIPE_API_KEY,
});
const result = await pipe.generateText({
messages: [{ role: 'user', content: 'Hello, AI agent!' }],
});
console.log(result.completion);
}
main().catch(console.error);
`;
await fs.writeFile('index.js', mainFileContent.trim());
// Create .env file
await fs.writeFile(
'.env',
'LANGBASE_MY_PIPE_API_KEY=your_api_key_here',
);
// Update package.json
const packageJson = JSON.parse(
await fs.readFile('package.json', 'utf-8'),
);
packageJson.type = 'module'; // Add type: module
packageJson.scripts = {
...packageJson.scripts,
dev: 'node index.js', // Change start to dev
};
await fs.writeFile(
'package.json',
JSON.stringify(packageJson, null, 2),
);
spinner.succeed(
chalk.green(`AI agent "${name}" created successfully!`),
);
console.log(chalk.yellow('\nNext steps:'));
console.log(chalk.cyan(`1. cd ${name}`));
console.log(
chalk.cyan('2. Add your Langbase Pipe API key to the .env file'),
);
console.log(chalk.cyan('3. pnpm dev')); // Update this line to use 'dev' instead of 'start'
} catch (error) {
spinner.fail(chalk.red('Failed to create AI agent'));
console.error(error);
process.exit(1);
}
};
const run = async () => {
const {name} = await inquirer.prompt([
{
type: 'input',
name: 'name',
message: 'What is the name of your AI agent project?',
default: 'my-baseai',
},
]);
const {proceed} = await inquirer.prompt([
{
type: 'confirm',
name: 'proceed',
message: `Create a new AI agent project named "${name}"?`,
default: true,
},
]);
if (proceed) {
await createAIAgent(name);
} else {
console.log(chalk.yellow('Operation cancelled'));
}
};
run().catch(console.error);