2024-12-21 06:15:17 +00:00
|
|
|
import { BaseProvider } from '~/lib/modules/llm/base-provider';
|
|
|
|
import type { ModelInfo } from '~/lib/modules/llm/types';
|
|
|
|
import type { IProviderSetting } from '~/types/model';
|
|
|
|
import { createOpenAI } from '@ai-sdk/openai';
|
|
|
|
import type { LanguageModelV1 } from 'ai';
|
2025-01-06 13:48:42 +00:00
|
|
|
import { logger } from '~/utils/logger';
|
2024-12-21 06:15:17 +00:00
|
|
|
|
|
|
|
export default class LMStudioProvider extends BaseProvider {
|
|
|
|
name = 'LMStudio';
|
|
|
|
getApiKeyLink = 'https://lmstudio.ai/';
|
|
|
|
labelForGetApiKey = 'Get LMStudio';
|
|
|
|
icon = 'i-ph:cloud-arrow-down';
|
|
|
|
|
|
|
|
config = {
|
|
|
|
baseUrlKey: 'LMSTUDIO_API_BASE_URL',
|
2024-12-30 12:20:13 +00:00
|
|
|
baseUrl: 'http://localhost:1234/',
|
2024-12-21 06:15:17 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
staticModels: ModelInfo[] = [];
|
|
|
|
|
|
|
|
async getDynamicModels(
|
|
|
|
apiKeys?: Record<string, string>,
|
|
|
|
settings?: IProviderSetting,
|
|
|
|
serverEnv: Record<string, string> = {},
|
|
|
|
): Promise<ModelInfo[]> {
|
2025-01-06 13:48:42 +00:00
|
|
|
let { baseUrl } = this.getProviderBaseUrlAndKey({
|
2024-12-31 17:17:32 +00:00
|
|
|
apiKeys,
|
|
|
|
providerSettings: settings,
|
|
|
|
serverEnv,
|
|
|
|
defaultBaseUrlKey: 'LMSTUDIO_API_BASE_URL',
|
|
|
|
defaultApiTokenKey: '',
|
|
|
|
});
|
2024-12-21 06:15:17 +00:00
|
|
|
|
2024-12-31 17:17:32 +00:00
|
|
|
if (!baseUrl) {
|
2025-01-06 13:48:42 +00:00
|
|
|
throw new Error('No baseUrl found for LMStudio provider');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof window === 'undefined') {
|
|
|
|
/*
|
|
|
|
* Running in Server
|
|
|
|
* Backend: Check if we're running in Docker
|
|
|
|
*/
|
|
|
|
const isDocker = process.env.RUNNING_IN_DOCKER === 'true';
|
|
|
|
|
|
|
|
baseUrl = isDocker ? baseUrl.replace('localhost', 'host.docker.internal') : baseUrl;
|
|
|
|
baseUrl = isDocker ? baseUrl.replace('127.0.0.1', 'host.docker.internal') : baseUrl;
|
2024-12-21 06:15:17 +00:00
|
|
|
}
|
2024-12-31 17:17:32 +00:00
|
|
|
|
|
|
|
const response = await fetch(`${baseUrl}/v1/models`);
|
|
|
|
const data = (await response.json()) as { data: Array<{ id: string }> };
|
|
|
|
|
|
|
|
return data.data.map((model) => ({
|
|
|
|
name: model.id,
|
|
|
|
label: model.id,
|
|
|
|
provider: this.name,
|
|
|
|
maxTokenAllowed: 8000,
|
|
|
|
}));
|
2024-12-21 06:15:17 +00:00
|
|
|
}
|
|
|
|
getModelInstance: (options: {
|
|
|
|
model: string;
|
|
|
|
serverEnv: Env;
|
|
|
|
apiKeys?: Record<string, string>;
|
|
|
|
providerSettings?: Record<string, IProviderSetting>;
|
|
|
|
}) => LanguageModelV1 = (options) => {
|
|
|
|
const { apiKeys, providerSettings, serverEnv, model } = options;
|
2025-01-06 13:48:42 +00:00
|
|
|
let { baseUrl } = this.getProviderBaseUrlAndKey({
|
2024-12-21 06:15:17 +00:00
|
|
|
apiKeys,
|
2025-01-06 13:48:42 +00:00
|
|
|
providerSettings: providerSettings?.[this.name],
|
2024-12-21 06:15:17 +00:00
|
|
|
serverEnv: serverEnv as any,
|
2025-01-06 13:48:42 +00:00
|
|
|
defaultBaseUrlKey: 'LMSTUDIO_API_BASE_URL',
|
2024-12-21 06:15:17 +00:00
|
|
|
defaultApiTokenKey: '',
|
|
|
|
});
|
2025-01-06 13:48:42 +00:00
|
|
|
|
|
|
|
if (!baseUrl) {
|
|
|
|
throw new Error('No baseUrl found for LMStudio provider');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof window === 'undefined') {
|
|
|
|
const isDocker = process.env.RUNNING_IN_DOCKER === 'true';
|
|
|
|
baseUrl = isDocker ? baseUrl.replace('localhost', 'host.docker.internal') : baseUrl;
|
|
|
|
baseUrl = isDocker ? baseUrl.replace('127.0.0.1', 'host.docker.internal') : baseUrl;
|
|
|
|
}
|
|
|
|
|
|
|
|
logger.debug('LMStudio Base Url used: ', baseUrl);
|
|
|
|
|
2024-12-21 06:15:17 +00:00
|
|
|
const lmstudio = createOpenAI({
|
|
|
|
baseUrl: `${baseUrl}/v1`,
|
|
|
|
apiKey: '',
|
|
|
|
});
|
|
|
|
|
|
|
|
return lmstudio(model);
|
|
|
|
};
|
|
|
|
}
|