mirror of
https://github.com/TriliumNext/Notes.git
synced 2026-05-05 03:39:13 -05:00
feat(llm): have OpenAI provider not require API keys (for endpoints like LM Studio)
This commit is contained in:
@@ -14,7 +14,9 @@ export class OpenAIService extends BaseAIService {
|
||||
}
|
||||
|
||||
override isAvailable(): boolean {
|
||||
return super.isAvailable() && !!options.getOption('openaiApiKey');
|
||||
// Make API key optional to support OpenAI-compatible endpoints that don't require authentication
|
||||
// The provider is considered available as long as the parent checks pass
|
||||
return super.isAvailable();
|
||||
}
|
||||
|
||||
private getClient(apiKey: string, baseUrl?: string): OpenAI {
|
||||
@@ -29,7 +31,7 @@ export class OpenAIService extends BaseAIService {
|
||||
|
||||
async generateChatCompletion(messages: Message[], opts: ChatCompletionOptions = {}): Promise<ChatResponse> {
|
||||
if (!this.isAvailable()) {
|
||||
throw new Error('OpenAI service is not available. Check API key and AI settings.');
|
||||
throw new Error('OpenAI service is not available. Check AI settings.');
|
||||
}
|
||||
|
||||
// Get provider-specific options from the central provider manager
|
||||
|
||||
@@ -134,7 +134,7 @@ export async function createProvidersFromCurrentOptions(): Promise<EmbeddingProv
|
||||
const ollamaEmbeddingBaseUrl = await options.getOption('ollamaEmbeddingBaseUrl');
|
||||
if (ollamaEmbeddingBaseUrl) {
|
||||
const embeddingModel = await options.getOption('ollamaEmbeddingModel');
|
||||
|
||||
|
||||
try {
|
||||
const ollamaProvider = new OllamaEmbeddingProvider({
|
||||
model: embeddingModel,
|
||||
@@ -152,23 +152,30 @@ export async function createProvidersFromCurrentOptions(): Promise<EmbeddingProv
|
||||
}
|
||||
}
|
||||
|
||||
// Create OpenAI provider if API key is configured
|
||||
// Create OpenAI provider even without API key (for OpenAI-compatible endpoints)
|
||||
const openaiApiKey = await options.getOption('openaiApiKey');
|
||||
if (openaiApiKey) {
|
||||
const openaiModel = await options.getOption('openaiEmbeddingModel') || 'text-embedding-3-small';
|
||||
const openaiBaseUrl = await options.getOption('openaiBaseUrl') || 'https://api.openai.com/v1';
|
||||
const openaiBaseUrl = await options.getOption('openaiBaseUrl');
|
||||
|
||||
// Only create OpenAI provider if base URL is set or API key is provided
|
||||
if (openaiApiKey || openaiBaseUrl) {
|
||||
const openaiModel = await options.getOption('openaiEmbeddingModel')
|
||||
const finalBaseUrl = openaiBaseUrl || 'https://api.openai.com/v1';
|
||||
|
||||
if (!openaiApiKey) {
|
||||
log.info('Creating OpenAI embedding provider without API key. This may cause issues with official OpenAI endpoints.');
|
||||
}
|
||||
|
||||
const openaiProvider = new OpenAIEmbeddingProvider({
|
||||
model: openaiModel,
|
||||
dimension: 1536,
|
||||
type: 'float32',
|
||||
apiKey: openaiApiKey,
|
||||
baseUrl: openaiBaseUrl
|
||||
apiKey: openaiApiKey || '', // Default to empty string
|
||||
baseUrl: finalBaseUrl
|
||||
});
|
||||
|
||||
registerEmbeddingProvider(openaiProvider);
|
||||
result.push(openaiProvider);
|
||||
log.info(`Created OpenAI provider on-demand: ${openaiModel}`);
|
||||
log.info(`Created OpenAI provider on-demand: ${openaiModel} at ${finalBaseUrl}`);
|
||||
}
|
||||
|
||||
// Create Voyage provider if API key is configured
|
||||
@@ -221,7 +228,7 @@ export async function getEnabledEmbeddingProviders(): Promise<EmbeddingProvider[
|
||||
|
||||
// First try to get existing registered providers
|
||||
const existingProviders = Array.from(providers.values());
|
||||
|
||||
|
||||
// If no providers are registered, create them on-demand from current options
|
||||
if (existingProviders.length === 0) {
|
||||
log.info('No providers registered, creating from current options');
|
||||
@@ -352,7 +359,8 @@ export function getOpenAIOptions(
|
||||
try {
|
||||
const apiKey = options.getOption('openaiApiKey');
|
||||
if (!apiKey) {
|
||||
throw new Error('OpenAI API key is not configured');
|
||||
// Log warning but don't throw - some OpenAI-compatible endpoints don't require API keys
|
||||
log.info('OpenAI API key is not configured. This may cause issues with official OpenAI endpoints.');
|
||||
}
|
||||
|
||||
const baseUrl = options.getOption('openaiBaseUrl') || PROVIDER_CONSTANTS.OPENAI.BASE_URL;
|
||||
@@ -377,7 +385,7 @@ export function getOpenAIOptions(
|
||||
|
||||
return {
|
||||
// Connection settings
|
||||
apiKey,
|
||||
apiKey: apiKey || '', // Default to empty string if no API key
|
||||
baseUrl,
|
||||
|
||||
// Provider metadata
|
||||
|
||||
Reference in New Issue
Block a user