Skip to content

Commit

Permalink
get it working!
Browse files Browse the repository at this point in the history
  • Loading branch information
zeke committed Jun 13, 2024
1 parent e1df9d3 commit df6670c
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 27 deletions.
32 changes: 32 additions & 0 deletions client.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import OpenAI from 'openai'

export function createClient (provider) {
let client
switch (provider) {
case 'openai':
client = new OpenAI({
apiKey: process.env.OPENAI_API_KEY
})

client.completionOptions = {
stream: true
}

break
case 'replicate':
client = new OpenAI({
apiKey: process.env.REPLICATE_API_TOKEN,
baseURL: 'https://openai-proxy.replicate.com/v1'
})

// Lifeboat and OpenAI have slight differences...
client.completionOptions = {
maxTokens: 64,
stream: true
}

break
}

return client
}
50 changes: 23 additions & 27 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,38 +1,34 @@
import OpenAI from 'openai'
import { execSync } from 'node:child_process'
import minimist from 'minimist'
import { createClient } from './client.js'

const argv = minimist(process.argv.slice(2))
const englishCommand = argv._.join(' ')
let model = argv.model || 'gpt-4o'
const model = argv.model || 'gpt-4o'

// Support shorthands
if (model === 'llama' || model === 'llama3') {
model = 'meta/llama-3-70b-instruct'
const models = {
llama: 'replicate:meta/meta-llama-3-70b-instruct',
llama3: 'replicate:meta/meta-llama-3-70b-instruct',
'gpt-4o': 'openai:gpt-4o',
gpt4: 'openai:gpt-4o'
}

let client
let completionOptions
if (!englishCommand) {
console.log('Usage: yolox <english-command>')
console.log('Example: yolox "list png files in current directory with human-friendly sizes"')
process.exit()
}

// Use Replicate Lifeboat -- https://lifeboat.replicate.dev
if (model === 'meta/llama-3-70b-instruct') {
client = new OpenAI({
apiKey: process.env.REPLICATE_API_TOKEN,
baseURL: 'https://openai-proxy.replicate.com/v1'
})
completionOptions = {
maxTokens: 64,
stream: false
}
} else {
client = new OpenAI({
apiKey: process.env.OPENAI_API_KEY
})
completionOptions = {
stream: true
}
if (!models[model]) {
console.error(`Error: Model '${model}' is not supported.`)
console.log('Available models are:', Object.keys(models).join(', '))
process.exit()
}

const provider = models[model].split(':')[0]
const modelFullName = models[model].split(':')[1]
const client = createClient(provider)

const prompt = [
`Write a one-line shell command to ${englishCommand}.`,
'Do not write code that will delete files or folders.',
Expand All @@ -42,17 +38,17 @@ const prompt = [
'Just show the command.'
].join(' ')

console.log(`Model: ${model}`)
console.log(`Model: ${modelFullName}`)

const options = {
model,
model: modelFullName,
messages: [
{
role: 'user',
content: prompt
}
],
...completionOptions
...client.completionOptions
}

const completions = await client.chat.completions.create(options)
Expand Down

0 comments on commit df6670c

Please sign in to comment.