the code has been updated to ensure that the latest configurations are always retrieved when making calls to the LLM.
This commit is contained in:
@@ -10,36 +10,38 @@ import { URL_CONSTANTS } from '../../constants/coreConstants';
|
|||||||
export class OpenAIProvider extends LLMProvider {
|
export class OpenAIProvider extends LLMProvider {
|
||||||
readonly name = 'OpenAI';
|
readonly name = 'OpenAI';
|
||||||
|
|
||||||
private apiKey: string;
|
|
||||||
private model: string;
|
|
||||||
private flexMode: boolean = false;
|
|
||||||
private baseURL: string;
|
|
||||||
private isCompatibleProvider: boolean;
|
|
||||||
private apiEndpoint: string;
|
|
||||||
private isOllamaFormat: boolean | null = null; // Detected at runtime
|
private isOllamaFormat: boolean | null = null; // Detected at runtime
|
||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
super();
|
super();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get current configuration values from ConfigManager
|
||||||
|
*/
|
||||||
|
private getCurrentConfig() {
|
||||||
const configManager = ConfigManager.instance();
|
const configManager = ConfigManager.instance();
|
||||||
const llmProvider = configManager.get('general.llm_provider') as string;
|
const llmProvider = configManager.get('general.llm_provider') as string;
|
||||||
this.isCompatibleProvider = llmProvider === 'openai_compatible';
|
const isCompatibleProvider = llmProvider === 'openai_compatible';
|
||||||
|
|
||||||
// Set API key, model, base URL, and endpoint based on provider type
|
if (isCompatibleProvider) {
|
||||||
if (this.isCompatibleProvider) {
|
const apiKey = configManager.get('general.openai_compatible.api_key') as string;
|
||||||
this.apiKey = configManager.get('general.openai_compatible.api_key') as string;
|
const model = configManager.get('general.openai_compatible.model') as string;
|
||||||
this.model = configManager.get('general.openai_compatible.model') as string;
|
const baseURL = configManager.get('general.openai_compatible.base_url') as string;
|
||||||
this.baseURL = configManager.get('general.openai_compatible.base_url') as string;
|
|
||||||
// For compatible providers, use the full URL as provided (assume it includes the endpoint)
|
// For compatible providers, use the full URL as provided (assume it includes the endpoint)
|
||||||
// Common patterns: http://localhost:11434/api/chat (Ollama), https://api.openrouter.ai/v1 (OpenRouter)
|
// Common patterns: http://localhost:11434/api/chat (Ollama), https://api.openrouter.ai/v1 (OpenRouter)
|
||||||
this.apiEndpoint = this.baseURL;
|
const apiEndpoint = baseURL;
|
||||||
this.flexMode = false; // Not applicable to compatible providers
|
const flexMode = false; // Not applicable to compatible providers
|
||||||
|
|
||||||
|
return { apiKey, model, baseURL, apiEndpoint, flexMode, isCompatibleProvider };
|
||||||
} else {
|
} else {
|
||||||
this.apiKey = configManager.get('general.openai.api_key') as string;
|
const apiKey = configManager.get('general.openai.api_key') as string;
|
||||||
this.model = configManager.get('general.openai.model') as string;
|
const model = configManager.get('general.openai.model') as string;
|
||||||
this.flexMode = (configManager.get('general.openai.flex') as boolean) === true;
|
const flexMode = (configManager.get('general.openai.flex') as boolean) === true;
|
||||||
this.baseURL = URL_CONSTANTS.DEFAULT_OPENAI_BASE_URL;
|
const baseURL = URL_CONSTANTS.DEFAULT_OPENAI_BASE_URL;
|
||||||
this.apiEndpoint = `${this.baseURL}/chat/completions`;
|
const apiEndpoint = `${baseURL}/chat/completions`;
|
||||||
|
|
||||||
|
return { apiKey, model, baseURL, apiEndpoint, flexMode, isCompatibleProvider };
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -127,6 +129,9 @@ export class OpenAIProvider extends LLMProvider {
|
|||||||
systemPrompt?: string,
|
systemPrompt?: string,
|
||||||
tools?: Record<string, unknown>[]
|
tools?: Record<string, unknown>[]
|
||||||
): AsyncIterableIterator<StreamChunk> {
|
): AsyncIterableIterator<StreamChunk> {
|
||||||
|
// Get fresh config values
|
||||||
|
const config = this.getCurrentConfig();
|
||||||
|
|
||||||
// Build OpenAI messages array with role preservation
|
// Build OpenAI messages array with role preservation
|
||||||
const openAIMessages: Array<{ role: string; content: string }> = [];
|
const openAIMessages: Array<{ role: string; content: string }> = [];
|
||||||
|
|
||||||
@@ -141,15 +146,15 @@ export class OpenAIProvider extends LLMProvider {
|
|||||||
content: msg.content
|
content: msg.content
|
||||||
})));
|
})));
|
||||||
|
|
||||||
const response = await fetch(this.apiEndpoint, {
|
const response = await fetch(config.apiEndpoint, {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
headers: {
|
headers: {
|
||||||
'Authorization': `Bearer ${this.apiKey}`,
|
'Authorization': `Bearer ${config.apiKey}`,
|
||||||
'Content-Type': 'application/json',
|
'Content-Type': 'application/json',
|
||||||
},
|
},
|
||||||
body: JSON.stringify({
|
body: JSON.stringify({
|
||||||
model: this.model,
|
model: config.model,
|
||||||
...(this.flexMode && !this.isCompatibleProvider ? { service_tier: 'flex' } : {}),
|
...(config.flexMode && !config.isCompatibleProvider ? { service_tier: 'flex' } : {}),
|
||||||
messages: openAIMessages,
|
messages: openAIMessages,
|
||||||
stream: true,
|
stream: true,
|
||||||
tools: tools || undefined
|
tools: tools || undefined
|
||||||
@@ -332,6 +337,9 @@ export class OpenAIProvider extends LLMProvider {
|
|||||||
systemPrompt?: string,
|
systemPrompt?: string,
|
||||||
tools?: Record<string, unknown>[]
|
tools?: Record<string, unknown>[]
|
||||||
): Promise<LLMResponse> {
|
): Promise<LLMResponse> {
|
||||||
|
// Get fresh config values
|
||||||
|
const config = this.getCurrentConfig();
|
||||||
|
|
||||||
// Build OpenAI messages array with role preservation
|
// Build OpenAI messages array with role preservation
|
||||||
const openAIMessages: Array<{ role: string; content: string }> = [];
|
const openAIMessages: Array<{ role: string; content: string }> = [];
|
||||||
|
|
||||||
@@ -346,15 +354,15 @@ export class OpenAIProvider extends LLMProvider {
|
|||||||
content: msg.content
|
content: msg.content
|
||||||
})));
|
})));
|
||||||
|
|
||||||
const response = await fetch(this.apiEndpoint, {
|
const response = await fetch(config.apiEndpoint, {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
headers: {
|
headers: {
|
||||||
'Authorization': `Bearer ${this.apiKey}`,
|
'Authorization': `Bearer ${config.apiKey}`,
|
||||||
'Content-Type': 'application/json',
|
'Content-Type': 'application/json',
|
||||||
},
|
},
|
||||||
body: JSON.stringify({
|
body: JSON.stringify({
|
||||||
model: this.model,
|
model: config.model,
|
||||||
...(this.flexMode && !this.isCompatibleProvider ? { service_tier: 'flex' } : {}),
|
...(config.flexMode && !config.isCompatibleProvider ? { service_tier: 'flex' } : {}),
|
||||||
messages: openAIMessages,
|
messages: openAIMessages,
|
||||||
stream: false,
|
stream: false,
|
||||||
tools: tools || undefined
|
tools: tools || undefined
|
||||||
|
|||||||
Reference in New Issue
Block a user