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:
@@ -1,13 +1,18 @@
|
||||
import type { AIProvider, AIProviderConfig, AIProviderResult } from './provider';
|
||||
import { ProviderSettings } from './openai';
|
||||
|
||||
export class LMStudioProvider implements AIProvider {
|
||||
provider = 'lmstudio' as const;
|
||||
private baseUrl: string;
|
||||
private model: string;
|
||||
private customHeaders?: Record<string, string>;
|
||||
private customParameters?: Record<string, unknown>;
|
||||
|
||||
constructor(config: AIProviderConfig) {
|
||||
constructor(config: AIProviderConfig, settings?: ProviderSettings) {
|
||||
this.baseUrl = config.baseUrl || 'http://localhost:1234/v1';
|
||||
this.model = config.model || 'local-model';
|
||||
this.customHeaders = settings?.headers;
|
||||
this.customParameters = settings?.parameters;
|
||||
}
|
||||
|
||||
async generate(prompt: string, systemPrompt?: string, options?: { jsonMode?: boolean }): Promise<AIProviderResult> {
|
||||
@@ -19,18 +24,26 @@ export class LMStudioProvider implements AIProvider {
|
||||
|
||||
messages.push({ role: 'user', content: prompt });
|
||||
|
||||
const requestBody = {
|
||||
const requestBody: Record<string, unknown> = {
|
||||
model: this.model,
|
||||
messages,
|
||||
temperature: 0.7,
|
||||
max_tokens: 2000,
|
||||
...this.customParameters,
|
||||
};
|
||||
|
||||
if (options?.jsonMode) {
|
||||
requestBody.response_format = { type: 'json_object' };
|
||||
}
|
||||
|
||||
const headers: Record<string, string> = {
|
||||
'Content-Type': 'application/json',
|
||||
...this.customHeaders,
|
||||
};
|
||||
|
||||
const response = await fetch(`${this.baseUrl}/chat/completions`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
headers,
|
||||
body: JSON.stringify(requestBody),
|
||||
});
|
||||
|
||||
@@ -63,7 +76,9 @@ export class LMStudioProvider implements AIProvider {
|
||||
|
||||
async validate(): Promise<boolean> {
|
||||
try {
|
||||
const response = await fetch(`${this.baseUrl}/models`);
|
||||
const response = await fetch(`${this.baseUrl}/models`, {
|
||||
headers: this.customHeaders,
|
||||
});
|
||||
return response.ok;
|
||||
} catch {
|
||||
return false;
|
||||
|
||||
Reference in New Issue
Block a user