mirror of
https://github.com/stackblitz-labs/bolt.diy
synced 2025-01-24 03:37:29 +00:00
78 lines
2.1 KiB
TypeScript
78 lines
2.1 KiB
TypeScript
|
import { BaseProvider, getOpenAILikeModel } from '~/lib/modules/llm/base-provider';
|
||
|
import type { ModelInfo } from '~/lib/modules/llm/types';
|
||
|
import type { IProviderSetting } from '~/types/model';
|
||
|
import type { LanguageModelV1 } from 'ai';
|
||
|
|
||
|
export default class OpenAILikeProvider extends BaseProvider {
|
||
|
name = 'OpenAILike';
|
||
|
getApiKeyLink = undefined;
|
||
|
|
||
|
config = {
|
||
|
baseUrlKey: 'OPENAI_LIKE_API_BASE_URL',
|
||
|
apiTokenKey: 'OPENAI_LIKE_API_KEY',
|
||
|
};
|
||
|
|
||
|
staticModels: ModelInfo[] = [];
|
||
|
|
||
|
async getDynamicModels(
|
||
|
apiKeys?: Record<string, string>,
|
||
|
settings?: IProviderSetting,
|
||
|
serverEnv: Record<string, string> = {},
|
||
|
): Promise<ModelInfo[]> {
|
||
|
try {
|
||
|
const { baseUrl, apiKey } = this.getProviderBaseUrlAndKey({
|
||
|
apiKeys,
|
||
|
providerSettings: settings,
|
||
|
serverEnv,
|
||
|
defaultBaseUrlKey: 'OPENAI_LIKE_API_BASE_URL',
|
||
|
defaultApiTokenKey: 'OPENAI_LIKE_API_KEY',
|
||
|
});
|
||
|
|
||
|
if (!baseUrl || !apiKey) {
|
||
|
return [];
|
||
|
}
|
||
|
|
||
|
const response = await fetch(`${baseUrl}/models`, {
|
||
|
headers: {
|
||
|
Authorization: `Bearer ${apiKey}`,
|
||
|
},
|
||
|
});
|
||
|
|
||
|
const res = (await response.json()) as any;
|
||
|
|
||
|
return res.data.map((model: any) => ({
|
||
|
name: model.id,
|
||
|
label: model.id,
|
||
|
provider: this.name,
|
||
|
maxTokenAllowed: 8000,
|
||
|
}));
|
||
|
} catch (error) {
|
||
|
console.error('Error getting OpenAILike models:', error);
|
||
|
return [];
|
||
|
}
|
||
|
}
|
||
|
|
||
|
getModelInstance(options: {
|
||
|
model: string;
|
||
|
serverEnv: Env;
|
||
|
apiKeys?: Record<string, string>;
|
||
|
providerSettings?: Record<string, IProviderSetting>;
|
||
|
}): LanguageModelV1 {
|
||
|
const { model, serverEnv, apiKeys, providerSettings } = options;
|
||
|
|
||
|
const { baseUrl, apiKey } = this.getProviderBaseUrlAndKey({
|
||
|
apiKeys,
|
||
|
providerSettings: providerSettings?.[this.name],
|
||
|
serverEnv: serverEnv as any,
|
||
|
defaultBaseUrlKey: 'OPENAI_LIKE_API_BASE_URL',
|
||
|
defaultApiTokenKey: 'OPENAI_LIKE_API_KEY',
|
||
|
});
|
||
|
|
||
|
if (!baseUrl || !apiKey) {
|
||
|
throw new Error(`Missing configuration for ${this.name} provider`);
|
||
|
}
|
||
|
|
||
|
return getOpenAILikeModel(baseUrl, apiKey, model);
|
||
|
}
|
||
|
}
|