-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Splits out Azure provider from OpenAI Compatible providers #4275
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
5 commits
Select commit
Hold shift + click to select a range
c0e8903
Splits out Azure provider from OpenAI Compatible providers
axosoft-ramint 8234746
Consolidates models
axosoft-ramint 6b57c51
Updates provider label
axosoft-ramint de6c441
Uses correct provider descriptor
axosoft-ramint 7e40e34
Fixes setting regex
axosoft-ramint 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
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,115 @@ | ||
import type { Disposable } from 'vscode'; | ||
import { window } from 'vscode'; | ||
import { azureProviderDescriptor as provider } from '../../constants.ai'; | ||
import { configuration } from '../../system/-webview/configuration'; | ||
import type { AIActionType, AIModel } from './models/model'; | ||
import { openAIModels } from './models/model'; | ||
import { OpenAICompatibleProviderBase } from './openAICompatibleProviderBase'; | ||
import { isAzureUrl } from './utils/-webview/ai.utils'; | ||
|
||
type AzureModel = AIModel<typeof provider.id>; | ||
const models: AzureModel[] = openAIModels(provider); | ||
|
||
export class AzureProvider extends OpenAICompatibleProviderBase<typeof provider.id> { | ||
readonly id = provider.id; | ||
readonly name = provider.name; | ||
protected readonly descriptor = provider; | ||
protected readonly config = { | ||
keyUrl: undefined, | ||
keyValidator: /(?:sk-)?[a-zA-Z0-9]{32,}/, | ||
}; | ||
|
||
getModels(): Promise<readonly AIModel<typeof provider.id>[]> { | ||
return Promise.resolve(models); | ||
} | ||
|
||
protected getUrl(_model?: AIModel<typeof provider.id>): string | undefined { | ||
return configuration.get('ai.azure.url') ?? undefined; | ||
} | ||
|
||
private async getOrPromptBaseUrl(silent: boolean): Promise<string | undefined> { | ||
let url: string | undefined = this.getUrl(); | ||
|
||
if (silent || url != null) return url; | ||
|
||
const input = window.createInputBox(); | ||
input.ignoreFocusOut = true; | ||
|
||
const disposables: Disposable[] = []; | ||
|
||
try { | ||
url = await new Promise<string | undefined>(resolve => { | ||
disposables.push( | ||
input.onDidHide(() => resolve(undefined)), | ||
input.onDidChangeValue(value => { | ||
if (value) { | ||
try { | ||
new URL(value); | ||
} catch { | ||
input.validationMessage = `Please enter a valid URL`; | ||
return; | ||
} | ||
} | ||
input.validationMessage = undefined; | ||
}), | ||
input.onDidAccept(() => { | ||
const value = input.value.trim(); | ||
if (!value) { | ||
input.validationMessage = `Please enter a valid URL`; | ||
return; | ||
} | ||
|
||
try { | ||
new URL(value); | ||
} catch { | ||
input.validationMessage = `Please enter a valid URL`; | ||
return; | ||
} | ||
|
||
if (!isAzureUrl(value)) { | ||
input.validationMessage = `Please enter a valid Azure OpenAI URL`; | ||
return; | ||
} | ||
|
||
resolve(value); | ||
}), | ||
); | ||
|
||
input.title = `Connect to Azure OpenAI Provider`; | ||
input.placeholder = `Please enter your provider's URL to use this feature`; | ||
input.prompt = `Enter your Azure OpenAI Provider URL`; | ||
|
||
input.show(); | ||
}); | ||
} finally { | ||
input.dispose(); | ||
disposables.forEach(d => void d.dispose()); | ||
} | ||
|
||
if (url) { | ||
void configuration.updateEffective('ai.azure.url', url); | ||
} | ||
|
||
return url; | ||
} | ||
|
||
override async configured(silent: boolean): Promise<boolean> { | ||
const url = await this.getOrPromptBaseUrl(silent); | ||
if (url == null) return false; | ||
|
||
return super.configured(silent); | ||
} | ||
|
||
protected override getHeaders<TAction extends AIActionType>( | ||
_action: TAction, | ||
apiKey: string, | ||
_model: AIModel<typeof provider.id>, | ||
_url: string, | ||
): Record<string, string> | Promise<Record<string, string>> { | ||
return { | ||
Accept: 'application/json', | ||
'Content-Type': 'application/json', | ||
'api-key': apiKey, | ||
}; | ||
} | ||
} |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note, the dash
-
character was not escaped here, so it was being interpreted as "any character between.
and:
.