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

Add support for Custom Model to OpenAI settings #3225

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
18 changes: 15 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3316,15 +3316,17 @@
"gpt-4",
"gpt-4-32k",
"gpt-3.5-turbo",
"gpt-3.5-turbo-16k"
"gpt-3.5-turbo-16k",
"custom"
],
"enumDescriptions": [
"GPT-4 Turbo with Vision",
"GPT-4 Turbo Preview",
"GPT-4",
"GPT-4 32k",
"GPT-3.5 Turbo",
"GPT-3.5 Turbo 16k"
"GPT-3.5 Turbo 16k",
"Custom"
],
"markdownDescription": "Specifies the OpenAI model to use for GitLens' experimental AI features",
"scope": "window",
Expand All @@ -3336,7 +3338,17 @@
"null"
],
"default": null,
"markdownDescription": "Specifies a custom URL to use for access to an OpenAI model via Azure. Azure URLs should be in the following format: https://{your-resource-name}.openai.azure.com/openai/deployments/{deployment-id}/chat/completions?api-version={api-version}",
"markdownDescription": "Specifies a custom URL to use for access to an OpenAI-compatible API. URLs could be in any format as long as the API responds in the same way as OpenAI. eg: https://{serverHost}/v1/chat/completions \n\nAzure URLs should be in the following format: https://{your-resource-name}.openai.azure.com/openai/deployments/{deployment-id}/chat/completions?api-version={api-version}",
"scope": "window",
"order": 102
},
"gitlens.ai.experimental.openai.customModel": {
"type": [
Copy link
Member

Choose a reason for hiding this comment

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

Do we need another setting for the custom model? Can't we just use the model setting above?

"string",
"null"
],
"default": null,
"markdownDescription": "Specifies a custom model to use with an OpenAI-compatible API. Only used when the OpenAI model is set to 'Custom'.",
"scope": "window",
"order": 102
},
Expand Down
2 changes: 1 addition & 1 deletion src/ai/aiProviderService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ async function confirmAIProviderToS(provider: AIProvider, storage: Storage): Pro
return false;
}

export function getMaxCharacters(model: AIModels, outputLength: number): number {
export function getMaxCharacters(model: AIModels | string, outputLength: number): number {
const tokensPerCharacter = 3.1;

let tokens;
Expand Down
26 changes: 21 additions & 5 deletions src/ai/openaiProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export class OpenAIProvider implements AIProvider<'openai'> {
return configuration.get('ai.experimental.openai.url') || 'https://api.openai.com/v1/chat/completions';
}

private async getOrChooseModel(): Promise<OpenAIModels | undefined> {
private async getOrChooseModel(): Promise<OpenAIModels | string | undefined> {
const model = this.model;
if (model != null) return model;

Expand All @@ -38,7 +38,14 @@ export class OpenAIProvider implements AIProvider<'openai'> {
const apiKey = await getApiKey(this.container.storage);
if (apiKey == null) return undefined;

const model = await this.getOrChooseModel();
let model = await this.getOrChooseModel();
if (model == null) return undefined;

if (model === 'custom') {
const customModel = configuration.get('ai.experimental.openai.customModel') || '';
model = customModel ? `${customModel}` : undefined;
}
// Might need to notify the user that they need to set a custom model name
if (model == null) return undefined;

let retries = 0;
Expand Down Expand Up @@ -86,6 +93,7 @@ Follow the user's instructions carefully, don't repeat yourself, don't include t
};

const rsp = await this.fetch(apiKey, request);

if (!rsp.ok) {
if (rsp.status === 404) {
throw new Error(
Expand Down Expand Up @@ -133,7 +141,14 @@ Follow the user's instructions carefully, don't repeat yourself, don't include t
const apiKey = await getApiKey(this.container.storage);
if (apiKey == null) return undefined;

const model = await this.getOrChooseModel();
let model = await this.getOrChooseModel();
if (model == null) return undefined;

if (model === 'custom') {
const customModel = configuration.get('ai.experimental.openai.customModel') || '';
model = customModel ? `${customModel}` : undefined;
}
// Might need to notify the user that they need to set a custom model name
if (model == null) return undefined;

let retries = 0;
Expand Down Expand Up @@ -248,10 +263,11 @@ export type OpenAIModels =
| 'gpt-3.5-turbo'
| 'gpt-3.5-turbo-0125'
| 'gpt-3.5-turbo-1106'
| 'gpt-3.5-turbo-16k';
| 'gpt-3.5-turbo-16k'
| 'custom';

interface OpenAIChatCompletionRequest {
model: OpenAIModels;
model: OpenAIModels | string;
messages: { role: 'system' | 'user' | 'assistant'; content: string }[];
temperature?: number;
top_p?: number;
Expand Down
1 change: 1 addition & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export interface Config {
readonly openai: {
readonly model: OpenAIModels | null;
readonly url: string | null;
readonly customModel: string | null;
};
readonly provider: AIProviders | null;
};
Expand Down
2 changes: 1 addition & 1 deletion src/quickpicks/aiModelPicker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export async function showAIModelPicker(provider?: AIProviders): Promise<ModelQu
{ label: 'OpenAI', description: 'GPT-4', provider: 'openai', model: 'gpt-4' },
{ label: 'OpenAI', description: 'GPT-4 32k', provider: 'openai', model: 'gpt-4-32k' },
{ label: 'OpenAI', description: 'GPT-3.5 Turbo', provider: 'openai', model: 'gpt-3.5-turbo' },

{ label: 'OpenAI', description: 'Custom', provider: 'openai', model: 'custom' },
{ label: 'Anthropic', kind: QuickPickItemKind.Separator },
{ label: 'Anthropic', description: 'Claude 3 Opus', provider: 'anthropic', model: 'claude-3-opus-20240229' },
{
Expand Down
Loading