Skip to content
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

feat: ET-1426: Added Azure support and updated readme #2

Merged
merged 4 commits into from
Oct 26, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 22 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ You can use OpenCommit by simply running it via the CLI like this `oco`. 2 secon

MacOS may ask to run the command with `sudo` when installing a package globally.

2. Get your API key from [OpenAI](https://platform.openai.com/account/api-keys). Make sure that you add your payment details, so the API works.
2. Get your API key from [OpenAI](https://platform.openai.com/account/api-keys) or Azure.
For OpenAI - Make sure that you add your payment details, so the API works. And for Azure - ensure you set up the additional config in the [section](/README.md#configuration) below.

3. Set the key to OpenCommit config:

Expand Down Expand Up @@ -70,15 +71,31 @@ oco
Create a `.env` file and add OpenCommit config variables there like this:

```env
OCO_OPENAI_API_KEY=<your OpenAI API token>
OCO_OPENAI_MAX_TOKENS=<max response tokens from OpenAI API>
OCO_OPENAI_BASE_PATH=<may be used to set proxy path to OpenAI api>
OCO_DESCRIPTION=<postface a message with ~3 sentences description of the changes>
OCO_EMOJI=<boolean, add GitMoji>
OCO_MODEL=<either 'gpt-4', 'gpt-3.5-turbo-16k' (default), 'gpt-3.5-turbo-0613' or 'gpt-3.5-turbo'>
OCO_LANGUAGE=<locale, scroll to the bottom to see options>
OCO_MESSAGE_TEMPLATE_PLACEHOLDER=<message template placeholder, default: '$msg'>
OCO_PROMPT_MODULE=<either conventional-commit or @commitlint, default: conventional-commit>
OCO_OPENAI_MAX_TOKENS=<max response tokens (default: 500)>
```

In addition to these config options, OpenAI and Azure can be set up with the following config variables:

#### OpenAI Config

```env
OCO_OPENAI_API_KEY=<your OpenAI API token>
OCO_OPENAI_BASE_PATH=<may be used to set proxy path to OpenAI api>
OCO_MODEL=<either 'gpt-4', 'gpt-3.5-turbo-16k' (default), 'gpt-3.5-turbo-0613' or 'gpt-3.5-turbo'>
```

#### Azure Config

```env
OCO_OPENAI_API_TYPE='azure'
OCO_OPENAI_API_KEY=<your Azure API token>
OCO_OPENAI_BASE_PATH=<azure path for example: https://EXAMPLE.openai.azure.com/>
OCO_MODEL=<either 'gpt4-613' or 'turbo-613'>
```

### Global config for all repos
Expand Down
23 changes: 21 additions & 2 deletions src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ const config = getConfig();
let maxTokens = config?.OCO_OPENAI_MAX_TOKENS;
let basePath = config?.OCO_OPENAI_BASE_PATH;
let apiKey = config?.OCO_OPENAI_API_KEY;
let apiType = config?.OCO_OPENAI_API_TYPE || 'openai';
mattsalt123 marked this conversation as resolved.
Show resolved Hide resolved

const [command, mode] = process.argv.slice(2);

Expand All @@ -47,8 +48,26 @@ class OpenAi {
private openAI!: OpenAIApi;

constructor() {
if (basePath) {
this.openAiApiConfiguration.basePath = basePath;
switch (apiType) {
case 'azure':
this.openAiApiConfiguration.baseOptions = {
headers: {
"api-key": apiKey,
},
params: {
'api-version': '2023-07-01-preview',
mattsalt123 marked this conversation as resolved.
Show resolved Hide resolved
}
};
if (basePath) {
this.openAiApiConfiguration.basePath = basePath + 'openai/deployments/' + MODEL;
}
break;
case 'openai':
default:
if (basePath) {
this.openAiApiConfiguration.basePath = basePath;
}
break;
mattsalt123 marked this conversation as resolved.
Show resolved Hide resolved
}
this.openAI = new OpenAIApi(this.openAiApiConfiguration);
}
Expand Down
27 changes: 22 additions & 5 deletions src/commands/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export enum CONFIG_KEYS {
OCO_OPENAI_API_KEY = 'OCO_OPENAI_API_KEY',
OCO_OPENAI_MAX_TOKENS = 'OCO_OPENAI_MAX_TOKENS',
OCO_OPENAI_BASE_PATH = 'OCO_OPENAI_BASE_PATH',
OCO_OPENAI_API_TYPE = 'OCO_OPENAI_API_TYPE',
OCO_DESCRIPTION = 'OCO_DESCRIPTION',
OCO_EMOJI = 'OCO_EMOJI',
OCO_MODEL = 'OCO_MODEL',
Expand Down Expand Up @@ -51,13 +52,13 @@ export const configValidators = {
validateConfig(CONFIG_KEYS.OCO_OPENAI_API_KEY, value, 'Cannot be empty');
validateConfig(
CONFIG_KEYS.OCO_OPENAI_API_KEY,
value.startsWith('sk-'),
'Must start with "sk-"'
value.startsWith('sk-') || value.match(/^[a-z0-9]{32}$/),
mattsalt123 marked this conversation as resolved.
Show resolved Hide resolved
'Must start with "sk-" or a valid 32 character Azure OpenAI API key'
);
validateConfig(
CONFIG_KEYS.OCO_OPENAI_API_KEY,
config[CONFIG_KEYS.OCO_OPENAI_BASE_PATH] || value.length === 51,
'Must be 51 characters long'
config[CONFIG_KEYS.OCO_OPENAI_BASE_PATH] || value.length === 51 || value.length === 32,
'Must be 51 or 32 characters long'
mattsalt123 marked this conversation as resolved.
Show resolved Hide resolved
);

return value;
Expand Down Expand Up @@ -120,16 +121,32 @@ export const configValidators = {
return value;
},

[CONFIG_KEYS.OCO_OPENAI_API_TYPE](value: any) {
validateConfig(
CONFIG_KEYS.OCO_OPENAI_API_TYPE,
typeof value === 'string',
'Must be string'
);
validateConfig(
CONFIG_KEYS.OCO_OPENAI_API_TYPE,
value === 'azure' || value === 'openai' || value === '',
`${value} is not supported yet, use 'azure' or 'openai' (default)`
);
return value;
},

[CONFIG_KEYS.OCO_MODEL](value: any) {
validateConfig(
CONFIG_KEYS.OCO_MODEL,
[
'gpt-3.5-turbo',
'gpt-4',
'gpt-3.5-turbo-16k',
'turbo-613',
'gpt4-613',
mattsalt123 marked this conversation as resolved.
Show resolved Hide resolved
'gpt-3.5-turbo-0613'
].includes(value),
`${value} is not supported yet, use 'gpt-4', 'gpt-3.5-turbo-16k' (default), 'gpt-3.5-turbo-0613' or 'gpt-3.5-turbo'`
`${value} is not supported yet, use Azure deployed models: 'turbo-613','gpt4-613', or OpenAi models: 'gpt-4', 'gpt-3.5-turbo-16k' (default), 'gpt-3.5-turbo-0613' or 'gpt-3.5-turbo'`
);
return value;
},
Expand Down