Allow configuring OpenAI base URL

This commit is contained in:
Remi
2025-10-16 11:15:31 +02:00
parent 5037da189f
commit 4480469c4b
5 changed files with 17 additions and 3 deletions

View File

@@ -4,11 +4,14 @@ import fs from "node:fs";
import { normalizeRadioPhrase } from "../../shared/utils/radioSpeech";
import { getServerRuntimeConfig } from "./runtimeConfig";
const { openaiKey, openaiProject, llmModel, ttsModel } = getServerRuntimeConfig();
const { openaiKey, openaiProject, openaiBaseUrl, llmModel, ttsModel } = getServerRuntimeConfig();
const normalizeClientOptions: ConstructorParameters<typeof OpenAI>[0] = { apiKey: openaiKey };
if (openaiProject) {
normalizeClientOptions.project = openaiProject;
}
if (openaiBaseUrl) {
normalizeClientOptions.baseURL = openaiBaseUrl;
}
export const normalize = new OpenAI(normalizeClientOptions);

View File

@@ -28,7 +28,7 @@ const httpsAgent = new https.Agent({
function ensureOpenAI(): OpenAI {
if (!openaiClient) {
const {openaiKey, openaiProject, llmModel} = getServerRuntimeConfig()
const {openaiKey, openaiProject, openaiBaseUrl, llmModel} = getServerRuntimeConfig()
if (!openaiKey) {
throw new Error('OPENAI_API_KEY is missing. Please set the key before using AI features.')
}
@@ -39,6 +39,9 @@ function ensureOpenAI(): OpenAI {
if (openaiProject) {
clientOptions.project = openaiProject
}
if (openaiBaseUrl) {
clientOptions.baseURL = openaiBaseUrl
}
console.log("using connection opened client")
openaiClient = new OpenAI(clientOptions)
cachedModel = llmModel

View File

@@ -3,6 +3,7 @@ import { useRuntimeConfig } from '#imports'
export interface ServerRuntimeConfig {
openaiKey: string
openaiProject?: string
openaiBaseUrl?: string
llmModel: string
ttsModel: string
voiceId: string
@@ -62,9 +63,13 @@ export function getServerRuntimeConfig(): ServerRuntimeConfig {
warnedMissingOpenAIKey = true
}
const openaiProject = String(runtimeConfig.openaiProject || '').trim() || undefined
const openaiBaseUrl = String(runtimeConfig.openaiBaseUrl || '').trim() || undefined
const config: ServerRuntimeConfig = {
openaiKey,
openaiProject: String(runtimeConfig.openaiProject || '').trim() || undefined,
openaiProject,
openaiBaseUrl,
llmModel: String(runtimeConfig.llmModel || '').trim() || 'gpt-5-nano',
ttsModel: String(runtimeConfig.ttsModel || '').trim() || 'tts-1',
voiceId: String(runtimeConfig.defaultVoiceId || '').trim() || 'alloy',