🕒 refactor: Use Legacy Content for Custom Endpoints and Azure Serverless for Improved Compatibility (#8502)

* 🕒 refactor: Use Legacy Content for Custom Endpoints to Improve Compatibility

- Also applies to Azure serverless endpoints from AI Foundry

* chore: move useLegacyContent condition before early return

* fix: Ensure useLegacyContent is set only when options are available
This commit is contained in:
Danny Avila 2025-07-16 17:17:15 -04:00 committed by GitHub
parent 7f8c327509
commit 1dabe96404
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 15 additions and 9 deletions

View file

@ -26,7 +26,6 @@ const {
VisionModes, VisionModes,
ContentTypes, ContentTypes,
EModelEndpoint, EModelEndpoint,
KnownEndpoints,
PermissionTypes, PermissionTypes,
isAgentsEndpoint, isAgentsEndpoint,
AgentCapabilities, AgentCapabilities,
@ -76,8 +75,6 @@ const payloadParser = ({ req, agent, endpoint }) => {
return req.body.endpointOption.model_parameters; return req.body.endpointOption.model_parameters;
}; };
const legacyContentEndpoints = new Set([KnownEndpoints.groq, KnownEndpoints.deepseek]);
const noSystemModelRegex = [/\b(o1-preview|o1-mini|amazon\.titan-text)\b/gi]; const noSystemModelRegex = [/\b(o1-preview|o1-mini|amazon\.titan-text)\b/gi];
function createTokenCounter(encoding) { function createTokenCounter(encoding) {
@ -718,9 +715,6 @@ class AgentClient extends BaseClient {
this.indexTokenCountMap, this.indexTokenCountMap,
toolSet, toolSet,
); );
if (legacyContentEndpoints.has(this.options.agent.endpoint?.toLowerCase())) {
initialMessages = formatContentStrings(initialMessages);
}
/** /**
* *
@ -784,6 +778,9 @@ class AgentClient extends BaseClient {
} }
let messages = _messages; let messages = _messages;
if (agent.useLegacyContent === true) {
messages = formatContentStrings(messages);
}
if ( if (
agent.model_parameters?.clientOptions?.defaultHeaders?.['anthropic-beta']?.includes( agent.model_parameters?.clientOptions?.defaultHeaders?.['anthropic-beta']?.includes(
'prompt-caching', 'prompt-caching',

View file

@ -186,10 +186,11 @@ const initializeAgent = async ({
return { return {
...agent, ...agent,
tools,
attachments, attachments,
resendFiles, resendFiles,
toolContextMap, toolContextMap,
tools, useLegacyContent: !!options.useLegacyContent,
maxContextTokens: (agentMaxContextTokens - maxTokens) * 0.9, maxContextTokens: (agentMaxContextTokens - maxTokens) * 0.9,
}; };
}; };

View file

@ -139,6 +139,9 @@ const initializeClient = async ({ req, res, endpointOption, optionsOnly, overrid
); );
clientOptions.modelOptions.user = req.user.id; clientOptions.modelOptions.user = req.user.id;
const options = getOpenAIConfig(apiKey, clientOptions, endpoint); const options = getOpenAIConfig(apiKey, clientOptions, endpoint);
if (options != null) {
options.useLegacyContent = true;
}
if (!customOptions.streamRate) { if (!customOptions.streamRate) {
return options; return options;
} }
@ -156,6 +159,7 @@ const initializeClient = async ({ req, res, endpointOption, optionsOnly, overrid
} }
return { return {
useLegacyContent: true,
llmConfig: modelOptions, llmConfig: modelOptions,
}; };
} }

View file

@ -65,19 +65,20 @@ const initializeClient = async ({
const isAzureOpenAI = endpoint === EModelEndpoint.azureOpenAI; const isAzureOpenAI = endpoint === EModelEndpoint.azureOpenAI;
/** @type {false | TAzureConfig} */ /** @type {false | TAzureConfig} */
const azureConfig = isAzureOpenAI && req.app.locals[EModelEndpoint.azureOpenAI]; const azureConfig = isAzureOpenAI && req.app.locals[EModelEndpoint.azureOpenAI];
let serverless = false;
if (isAzureOpenAI && azureConfig) { if (isAzureOpenAI && azureConfig) {
const { modelGroupMap, groupMap } = azureConfig; const { modelGroupMap, groupMap } = azureConfig;
const { const {
azureOptions, azureOptions,
baseURL, baseURL,
headers = {}, headers = {},
serverless, serverless: _serverless,
} = mapModelToAzureConfig({ } = mapModelToAzureConfig({
modelName, modelName,
modelGroupMap, modelGroupMap,
groupMap, groupMap,
}); });
serverless = _serverless;
clientOptions.reverseProxyUrl = baseURL ?? clientOptions.reverseProxyUrl; clientOptions.reverseProxyUrl = baseURL ?? clientOptions.reverseProxyUrl;
clientOptions.headers = resolveHeaders( clientOptions.headers = resolveHeaders(
@ -143,6 +144,9 @@ const initializeClient = async ({
clientOptions = Object.assign({ modelOptions }, clientOptions); clientOptions = Object.assign({ modelOptions }, clientOptions);
clientOptions.modelOptions.user = req.user.id; clientOptions.modelOptions.user = req.user.id;
const options = getOpenAIConfig(apiKey, clientOptions); const options = getOpenAIConfig(apiKey, clientOptions);
if (options != null && serverless === true) {
options.useLegacyContent = true;
}
const streamRate = clientOptions.streamRate; const streamRate = clientOptions.streamRate;
if (!streamRate) { if (!streamRate) {
return options; return options;