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(api.ts): add support for Azure OpenAI API #167

Open
wants to merge 5 commits into
base: dev
Choose a base branch
from
Open
Changes from all 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
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -54,6 +54,20 @@ oc

## Features

### Switch to Azure OpenAI

By default OpenCommit uses [OpenAI](https://openai.com).

You could switch to [Azure OpenAI Service](https://learn.microsoft.com/azure/cognitive-services/openai/)🚀

```sh
opencommit config set OPENAI_API_TYPE=azure
```

Of course need to set 'OPENAI_API_KEY'. And also need to set the
'OPENAI_BASE_PATH' for the endpoint and set the deployment name to
'model'.

### Switch to GPT-4

By default OpenCommit uses GPT-3.5-turbo (ChatGPT).
23 changes: 21 additions & 2 deletions src/api.ts
Original file line number Diff line number Diff line change
@@ -14,6 +14,7 @@ const config = getConfig();
let apiKey = config?.OPENAI_API_KEY;
let basePath = config?.OPENAI_BASE_PATH;
let maxTokens = config?.OPENAI_MAX_TOKENS;
let apiType = config?.OPENAI_API_TYPE || 'openai';

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

@@ -39,8 +40,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-03-15-preview',

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs to be configurable, depending on the specifics of the deployment. Another valid value is '2023-07-01-preview' and new ones will be added as time goes on.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

agree, @takuya-o please take a look

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we make this a configurable variable param?

}
};
if (basePath) {
this.openAiApiConfiguration.basePath = basePath + 'openai/deployments/' + MODEL;
}
break;
case 'openai':
default:
if (basePath) {
this.openAiApiConfiguration.basePath = basePath;
}
break;
}
this.openAI = new OpenAIApi(this.openAiApiConfiguration);
}
30 changes: 23 additions & 7 deletions src/commands/config.ts
Original file line number Diff line number Diff line change
@@ -12,6 +12,7 @@ export enum CONFIG_KEYS {
OPENAI_API_KEY = 'OPENAI_API_KEY',
OPENAI_MAX_TOKENS = 'OPENAI_MAX_TOKENS',
OPENAI_BASE_PATH = 'OPENAI_BASE_PATH',
OPENAI_API_TYPE = 'OPENAI_API_TYPE',
description = 'description',
emoji = 'emoji',
model = 'model',
@@ -42,13 +43,13 @@ export const configValidators = {
validateConfig(CONFIG_KEYS.OPENAI_API_KEY, value, 'Cannot be empty');
validateConfig(
CONFIG_KEYS.OPENAI_API_KEY,
value.startsWith('sk-'),
'Must start with "sk-"'
value.startsWith('sk-') || value.match(/^[a-z0-9]{32}$/),
'Must start with "sk-". Or a valid Azure OpenAI API key'
);
validateConfig(
CONFIG_KEYS.OPENAI_API_KEY,
value.length === 51,
'Must be 51 characters long'
value.length === 51 || value.length === 32,
'Must be 51 or 32 characters long'
);

return value;
@@ -102,11 +103,26 @@ export const configValidators = {
return value;
},

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

[CONFIG_KEYS.model](value: any) {
validateConfig(
CONFIG_KEYS.OPENAI_BASE_PATH,
value === 'gpt-3.5-turbo' || value === 'gpt-4',
`${value} is not supported yet, use 'gpt-4' or 'gpt-3.5-turbo' (default)`
CONFIG_KEYS.model,
value === 'gpt-3.5-turbo' || value === 'gpt-4'
|| ( typeof value === 'string' && value.match(/^[a-zA-Z0-9~\-]{1,63}[a-zA-Z0-9]$/) ),
`${value} is not supported yet, use 'gpt-4' or 'gpt-3.5-turbo' (default) or model deployed name.`
);
return value;
}