Skip to content

Commit 11aa7f2

Browse files
authored
Merge pull request #4 from zeke/use-openai-npm-package
use openai npm package
2 parents 0b0e467 + 2aebc66 commit 11aa7f2

File tree

4 files changed

+1027
-971
lines changed

4 files changed

+1027
-971
lines changed

index.js

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import OpenAI from 'openai'
2+
import { execSync } from 'node:child_process'
3+
4+
const model = 'gpt-4'
5+
const openai = new OpenAI({
6+
apiKey: process.env.OPENAI_API_KEY
7+
})
8+
9+
const englishCommand = process.argv.slice(2).join(' ')
10+
11+
const prompt = [
12+
`Write a one-line shell command to ${englishCommand}.`,
13+
'Do not write code that will delete files or folders.',
14+
'Do not explain the code.',
15+
'Do not fence the code.',
16+
'No code fencing.',
17+
'Just show the command.'
18+
].join(' ')
19+
20+
const completions = await openai.chat.completions.create({
21+
model,
22+
messages: [
23+
{
24+
role: 'user',
25+
content: prompt
26+
}
27+
],
28+
stream: true
29+
})
30+
31+
const output = []
32+
for await (const part of completions) {
33+
output.push(part.choices[0]?.delta?.content || '')
34+
}
35+
36+
const shellCommand = output.join('')
37+
38+
try {
39+
console.log(`# ${shellCommand}\n`)
40+
const result = execSync(shellCommand).toString()
41+
console.log(result)
42+
} catch (error) {
43+
console.error(`Error executing command: ${error.message}`)
44+
}

index.sh

-5
This file was deleted.

0 commit comments

Comments
 (0)