-
Notifications
You must be signed in to change notification settings - Fork 2
feat: add init command #12
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import type { CommandModule } from 'yargs'; | ||
import { init } from './init'; | ||
|
||
export const command: CommandModule<{}> = { | ||
command: ['$0', 'init'], | ||
describe: 'User-friendly config setup', | ||
handler: () => init(), | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
import prompts from 'prompts'; | ||
import { checkIfConfigExists, createConfigFile } from '../../config-file'; | ||
import * as output from '../../output'; | ||
import { resolveProvider } from '../../providers'; | ||
|
||
export async function init() { | ||
try { | ||
await initInternal(); | ||
} catch (error) { | ||
output.clearLine(); | ||
output.outputError(error); | ||
process.exit(1); | ||
} | ||
} | ||
|
||
async function initInternal() { | ||
const configExists = checkIfConfigExists(); | ||
|
||
if (configExists) { | ||
const response = await prompts({ | ||
type: 'confirm', | ||
message: 'Config found, do you want to re-initialize it?', | ||
name: 'reinitialize', | ||
}); | ||
|
||
if (!response.reinitialize) { | ||
output.outputBold('Cancelling initialization'); | ||
return; | ||
} | ||
} | ||
|
||
output.outputBold("Welcome to AI CLI. Let's set you up quickly."); | ||
|
||
const response = await prompts([ | ||
{ | ||
type: 'select', | ||
name: 'provider', | ||
message: 'Which inference provider would you like to use:', | ||
choices: [ | ||
{ title: 'OpenAI', value: 'openai' }, | ||
{ title: 'Perplexity', value: 'perplexity' }, | ||
], | ||
initial: 0, | ||
hint: '', | ||
}, | ||
{ | ||
type: 'confirm', | ||
message: (_, { provider }) => | ||
`Do you already have ${resolveProvider(provider).label} API key?`, | ||
name: 'hasApiKey', | ||
}, | ||
{ | ||
type: (prev) => (prev ? 'password' : null), | ||
name: 'apiKey', | ||
message: (_, { provider }) => `Paste ${resolveProvider(provider).label} API key here:`, | ||
mask: '', | ||
validate: (value) => (value === '' ? 'API key cannot be an empty string' : true), | ||
}, | ||
]); | ||
|
||
if (!response.hasApiKey) { | ||
const provider = resolveProvider(response.provider); | ||
output.outputDefault(`You can get your ${provider.label} API key here:`); | ||
output.outputDefault(provider.apiKeyUrl); | ||
return; | ||
} | ||
|
||
await createConfigFile({ | ||
providers: { | ||
[response.provider]: { | ||
apiKey: response.apiKey, | ||
}, | ||
}, | ||
}); | ||
|
||
output.outputBold( | ||
"\nI have written your settings into '~/.airc.json` file. You can now start using AI CLI.\n" | ||
); | ||
output.outputBold('For a single question and answer just pass the prompt as param'); | ||
output.outputDefault('$ ai "Tell me a joke" \n'); | ||
|
||
output.outputBold('For interactive session use "-i" (or "--interactive") option. '); | ||
output.outputDefault('$ ai -i "Tell me an interesting fact about JavaScript"\n'); | ||
|
||
output.outputBold('or just start "ai" without any params.'); | ||
output.outputDefault('$ ai \n'); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,29 @@ | ||
import OpenAI from 'openai'; | ||
import { type Message } from '../inference'; | ||
import type { ProviderConfig } from './config'; | ||
import type { Provider } from '.'; | ||
|
||
export async function getChatCompletion(config: ProviderConfig, messages: Message[]) { | ||
const openai = new OpenAI({ | ||
apiKey: config.apiKey, | ||
}); | ||
const OpenAi: Provider = { | ||
label: 'OpenAI', | ||
name: 'openAi', | ||
apiKeyUrl: 'https://www.platform.openai.com/api-keys', | ||
getChatCompletion: async (config: ProviderConfig, messages: Message[]) => { | ||
const openai = new OpenAI({ | ||
apiKey: config.apiKey, | ||
}); | ||
|
||
const systemMessage: Message = { | ||
role: 'system', | ||
content: config.systemPrompt, | ||
}; | ||
const systemMessage: Message = { | ||
role: 'system', | ||
content: config.systemPrompt, | ||
}; | ||
|
||
const response = await openai.chat.completions.create({ | ||
messages: [systemMessage, ...messages], | ||
model: config.model, | ||
}); | ||
const response = await openai.chat.completions.create({ | ||
messages: [systemMessage, ...messages], | ||
model: config.model, | ||
}); | ||
|
||
return [response.choices[0]?.message.content ?? null, response] as const; | ||
} | ||
return [response.choices[0]?.message.content ?? null, response] as const; | ||
}, | ||
}; | ||
|
||
export default OpenAi; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,30 @@ | ||
import OpenAI from 'openai'; | ||
import { type Message } from '../inference'; | ||
import type { ProviderConfig } from './config'; | ||
import type { Provider } from '.'; | ||
|
||
export async function getChatCompletion(config: ProviderConfig, messages: Message[]) { | ||
const perplexity = new OpenAI({ | ||
apiKey: config.apiKey, | ||
baseURL: 'https://api.perplexity.ai', | ||
}); | ||
const Perplexity: Provider = { | ||
label: 'Perplexity', | ||
name: 'perplexity', | ||
apiKeyUrl: 'https://www.perplexity.ai/settings/api', | ||
getChatCompletion: async (config: ProviderConfig, messages: Message[]) => { | ||
const perplexity = new OpenAI({ | ||
apiKey: config.apiKey, | ||
baseURL: 'https://api.perplexity.ai', | ||
}); | ||
|
||
const systemMessage: Message = { | ||
role: 'system', | ||
content: config.systemPrompt, | ||
}; | ||
const systemMessage: Message = { | ||
role: 'system', | ||
content: config.systemPrompt, | ||
}; | ||
|
||
const response = await perplexity.chat.completions.create({ | ||
messages: [systemMessage, ...messages], | ||
model: config.model, | ||
}); | ||
const response = await perplexity.chat.completions.create({ | ||
messages: [systemMessage, ...messages], | ||
model: config.model, | ||
}); | ||
|
||
return [response.choices[0]?.message.content ?? null, response] as const; | ||
} | ||
return [response.choices[0]?.message.content ?? null, response] as const; | ||
}, | ||
}; | ||
|
||
export default Perplexity; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.