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 3 commits
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
36 changes: 29 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
## DVLA Usage
This is a fork of [OpenCommit](https://github.com/di-sukharev/opencommit).
- It includes functionality to use Azure OpenAPI service as a backend.
- It includes functionality to use Azure OpenAPI service as a backend originating from this [pull request](https://github.com/di-sukharev/opencommit/pull/167).
- The package has been renamed to `@dvla/opencommit` for internal use.
- The MIT License still applies and references the contribution from the original author ` Dima Sukharev`.
- The MIT License still applies and references the contribution from the original author `Dima Sukharev`.

 
 
 
 

<div align="center">
<div>
Expand Down Expand Up @@ -37,7 +41,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 +75,32 @@ 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_AZURE_DEPLOYMENT=<azure deployment name>
OCO_AZURE_API_VERSION=<azure api version>
```

### Global config for all repos
Expand Down
25 changes: 23 additions & 2 deletions src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ 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
let apiVersion = config?.OCO_AZURE_API_VERSION || '2023-07-01-preview';

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

Expand All @@ -39,6 +41,7 @@ if (!apiKey && command !== 'config' && mode !== CONFIG_MODES.set) {
}

const MODEL = config?.OCO_MODEL || 'gpt-3.5-turbo';
const DEPLOYMENT = config?.OCO_AZURE_DEPLOYMENT;

class OpenAi {
private openAiApiConfiguration = new OpenAiApiConfiguration({
Expand All @@ -47,8 +50,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': apiVersion,
}
};
if (basePath) {
this.openAiApiConfiguration.basePath = basePath + 'openai/deployments/' + DEPLOYMENT;
}
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
51 changes: 45 additions & 6 deletions src/commands/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,15 @@ 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_AZURE_DEPLOYMENT = 'OCO_AZURE_DEPLOYMENT',
OCO_MODEL = 'OCO_MODEL',
OCO_LANGUAGE = 'OCO_LANGUAGE',
OCO_MESSAGE_TEMPLATE_PLACEHOLDER = 'OCO_MESSAGE_TEMPLATE_PLACEHOLDER',
OCO_PROMPT_MODULE = 'OCO_PROMPT_MODULE'
OCO_PROMPT_MODULE = 'OCO_PROMPT_MODULE',
OCO_AZURE_API_VERSION = 'OCO_AZURE_API_VERSION'
}

export const DEFAULT_MODEL_TOKEN_LIMIT = 4096;
Expand Down Expand Up @@ -51,13 +54,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 (OpenAI) or 32 (Azure) characters long'
);

return value;
Expand Down Expand Up @@ -120,6 +123,29 @@ export const configValidators = {
return value;
},

[CONFIG_KEYS.OCO_AZURE_API_VERSION](value: any) {
validateConfig(
CONFIG_KEYS.OCO_AZURE_API_VERSION,
typeof value === 'string',
'Must be string'
);
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,
Expand All @@ -129,10 +155,20 @@ export const configValidators = {
'gpt-3.5-turbo-16k',
'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 models: 'gpt-4', 'gpt-3.5-turbo-16k' (default), 'gpt-3.5-turbo-0613' or 'gpt-3.5-turbo'`
);
return value;
},

[CONFIG_KEYS.OCO_AZURE_DEPLOYMENT](value: any) {
validateConfig(
CONFIG_KEYS.OCO_AZURE_DEPLOYMENT,
( typeof value === 'string' && value.match(/^[a-zA-Z0-9]+([-_][a-zA-Z0-9]+)*[a-zA-Z0-9]$/) ),
`${value} is not a valid deployment name, it should only include alphanumeric characters, _ character and - character. It can't end with '_' or '-'.`
);
return value;
},

[CONFIG_KEYS.OCO_MESSAGE_TEMPLATE_PLACEHOLDER](value: any) {
validateConfig(
CONFIG_KEYS.OCO_MESSAGE_TEMPLATE_PLACEHOLDER,
Expand Down Expand Up @@ -166,9 +202,12 @@ export const getConfig = (): ConfigType | null => {
? Number(process.env.OCO_OPENAI_MAX_TOKENS)
: undefined,
OCO_OPENAI_BASE_PATH: process.env.OCO_OPENAI_BASE_PATH,
OCO_OPENAI_API_TYPE: process.env.OCO_OPENAI_API_TYPE || 'openai',
OCO_DESCRIPTION: process.env.OCO_DESCRIPTION === 'true' ? true : false,
OCO_EMOJI: process.env.OCO_EMOJI === 'true' ? true : false,
OCO_MODEL: process.env.OCO_MODEL || 'gpt-3.5-turbo-16k',
OCO_AZURE_API_VERSION: process.env.OCO_AZURE_API_VERSION || '2023-07-01-preview',
OCO_AZURE_DEPLOYMENT: process.env.OCO_AZURE_DEPLOYMENT,
OCO_LANGUAGE: process.env.OCO_LANGUAGE || 'en',
OCO_MESSAGE_TEMPLATE_PLACEHOLDER:
process.env.OCO_MESSAGE_TEMPLATE_PLACEHOLDER || '$msg',
Expand Down