feat: add custom OpenAI-compatible provider with custom headers and parameters
- Add Custom provider for any OpenAI-compatible API - All providers now support custom headers and parameters - providerSettings stored per-provider with headers and parameters - Example: disable thinking in Ollama, add tools, etc.
This commit is contained in:
@@ -6,16 +6,33 @@ export interface AIProviderResult {
|
||||
}
|
||||
|
||||
export interface AIProvider {
|
||||
provider: 'openai' | 'anthropic' | 'ollama' | 'lmstudio' | 'groq';
|
||||
provider: string;
|
||||
generate(prompt: string, systemPrompt?: string, options?: { jsonMode?: boolean }): Promise<AIProviderResult>;
|
||||
validate?(): Promise<boolean>;
|
||||
}
|
||||
|
||||
export interface AIProviderConfig {
|
||||
provider: 'openai' | 'anthropic' | 'ollama' | 'lmstudio' | 'groq';
|
||||
apiKey: string;
|
||||
provider: string;
|
||||
apiKey?: string;
|
||||
model?: string;
|
||||
baseUrl?: string;
|
||||
providerSettings?: string;
|
||||
}
|
||||
|
||||
function parseProviderSettings(settingsJson?: string): {
|
||||
headers?: Record<string, string>;
|
||||
parameters?: Record<string, unknown>;
|
||||
} {
|
||||
if (!settingsJson) return {};
|
||||
try {
|
||||
const parsed = JSON.parse(settingsJson);
|
||||
return {
|
||||
headers: parsed.headers,
|
||||
parameters: parsed.parameters,
|
||||
};
|
||||
} catch {
|
||||
return {};
|
||||
}
|
||||
}
|
||||
|
||||
import { OpenAIProvider } from './openai';
|
||||
@@ -23,19 +40,24 @@ import { AnthropicProvider } from './anthropic';
|
||||
import { OllamaProvider } from './ollama';
|
||||
import { LMStudioProvider } from './lmstudio';
|
||||
import { GroqProvider } from './groq';
|
||||
import { CustomProvider } from './custom';
|
||||
|
||||
export function createAIProvider(config: AIProviderConfig): AIProvider {
|
||||
const settings = parseProviderSettings(config.providerSettings);
|
||||
|
||||
switch (config.provider) {
|
||||
case 'openai':
|
||||
return new OpenAIProvider(config);
|
||||
return new OpenAIProvider(config, settings);
|
||||
case 'anthropic':
|
||||
return new AnthropicProvider(config);
|
||||
return new AnthropicProvider(config, settings);
|
||||
case 'ollama':
|
||||
return new OllamaProvider(config);
|
||||
return new OllamaProvider(config, settings);
|
||||
case 'lmstudio':
|
||||
return new LMStudioProvider(config);
|
||||
return new LMStudioProvider(config, settings);
|
||||
case 'groq':
|
||||
return new GroqProvider(config);
|
||||
return new GroqProvider(config, settings);
|
||||
case 'custom':
|
||||
return new CustomProvider(config, settings);
|
||||
default:
|
||||
throw new Error(`Unknown AI provider: ${config.provider}`);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user