diff --git a/packages/api/src/endpoints/openai/llm.ts b/packages/api/src/endpoints/openai/llm.ts index 0c3135c554..e5251bb83e 100644 --- a/packages/api/src/endpoints/openai/llm.ts +++ b/packages/api/src/endpoints/openai/llm.ts @@ -1,5 +1,6 @@ import { ProxyAgent } from 'undici'; import { KnownEndpoints, removeNullishValues } from 'librechat-data-provider'; +import type { AzureOpenAIInput } from '@langchain/openai'; import type { OpenAI } from 'openai'; import type * as t from '~/types'; import { sanitizeModelName, constructAzureURL } from '~/utils/azure'; @@ -42,7 +43,9 @@ export function getOpenAIConfig( dropParams, } = options; const { reasoning_effort, reasoning_summary, ...modelOptions } = _modelOptions; - const llmConfig: Partial & Partial = Object.assign( + const llmConfig: Partial & + Partial & + Partial = Object.assign( { streaming, model: modelOptions.model ?? '', @@ -100,7 +103,10 @@ export function getOpenAIConfig( llmConfig.model = process.env.AZURE_OPENAI_DEFAULT_MODEL; } - if (configOptions.baseURL) { + const constructBaseURL = () => { + if (!configOptions.baseURL) { + return; + } const azureURL = constructAzureURL({ baseURL: configOptions.baseURL, azureOptions: updatedAzure, @@ -108,9 +114,39 @@ export function getOpenAIConfig( updatedAzure.azureOpenAIBasePath = azureURL.split( `/${updatedAzure.azureOpenAIApiDeploymentName}`, )[0]; - } + }; + constructBaseURL(); Object.assign(llmConfig, updatedAzure); + + const constructAzureResponsesApi = () => { + if (!llmConfig.useResponsesApi) { + return; + } + + configOptions.baseURL = constructAzureURL({ + baseURL: configOptions.baseURL || 'https://${INSTANCE_NAME}.openai.azure.com/openai/v1', + azureOptions: llmConfig, + }); + + delete llmConfig.azureOpenAIApiDeploymentName; + delete llmConfig.azureOpenAIApiInstanceName; + delete llmConfig.azureOpenAIApiVersion; + delete llmConfig.azureOpenAIBasePath; + delete llmConfig.azureOpenAIApiKey; + + configOptions.defaultHeaders = { + ...configOptions.defaultHeaders, + 'api-key': apiKey, + }; + configOptions.defaultQuery = { + ...configOptions.defaultQuery, + 'api-version': 'preview', + }; + }; + + constructAzureResponsesApi(); + llmConfig.model = updatedAzure.azureOpenAIApiDeploymentName; } else { llmConfig.apiKey = apiKey;