mirror of
https://github.com/danny-avila/LibreChat.git
synced 2025-12-17 00:40:14 +01:00
Some checks failed
Docker Dev Branch Images Build / build (Dockerfile, lc-dev, node) (push) Has been cancelled
Docker Dev Branch Images Build / build (Dockerfile.multi, lc-dev-api, api-build) (push) Has been cancelled
Docker Dev Images Build / build (Dockerfile, librechat-dev, node) (push) Has been cancelled
Docker Dev Images Build / build (Dockerfile.multi, librechat-dev-api, api-build) (push) Has been cancelled
Sync Locize Translations & Create Translation PR / Sync Translation Keys with Locize (push) Has been cancelled
Sync Locize Translations & Create Translation PR / Create Translation PR on Version Published (push) Has been cancelled
* updated gpt5-pro it is here and on openrouter https://platform.openai.com/docs/models/gpt-5-pro * feat: Add gpt-5-pro pricing - Implemented handling for the new gpt-5-pro model in the getValueKey function. - Updated tests to ensure correct behavior for gpt-5-pro across various scenarios. - Adjusted token limits and multipliers for gpt-5-pro in the tokens utility files. - Enhanced model matching functionality to include gpt-5-pro variations. * refactor: optimize model pricing and validation logic - Added new model pricing entries for llama2, llama3, and qwen variants in tx.js. - Updated tokenValues to include additional models and their pricing structures. - Implemented validation tests in tx.spec.js to ensure all models resolve correctly to pricing. - Refactored getValueKey function to improve model matching and resolution efficiency. - Removed outdated model entries from tokens.ts to streamline pricing management. * fix: add missing pricing * chore: update model pricing for qwen and gemma variants * chore: update model pricing and add validation for context windows - Removed outdated model entries from tx.js and updated tokenValues with new models. - Added a test in tx.spec.js to ensure all models with pricing have corresponding context windows defined in tokens.ts. - Introduced 'command-text' model pricing in tokens.ts to maintain consistency across model definitions. * chore: update model names and pricing for AI21 and Amazon models - Refactored model names in tx.js for AI21 and Amazon models to remove versioning and improve consistency. - Updated pricing values in tokens.ts to reflect the new model names. - Added comprehensive tests in tx.spec.js to validate pricing for both short and full model names across AI21 and Amazon models. * feat: add pricing and validation for Claude Haiku 4.5 model * chore: increase default max context tokens to 18000 for agents * feat: add Qwen3 model pricing and validation tests * chore: reorganize and update Qwen model pricing in tx.js and tokens.ts --------- Co-authored-by: khfung <68192841+khfung@users.noreply.github.com>
210 lines
6.1 KiB
JavaScript
210 lines
6.1 KiB
JavaScript
const { Providers } = require('@librechat/agents');
|
|
const {
|
|
primeResources,
|
|
getModelMaxTokens,
|
|
extractLibreChatParams,
|
|
optionalChainWithEmptyCheck,
|
|
} = require('@librechat/api');
|
|
const {
|
|
ErrorTypes,
|
|
EModelEndpoint,
|
|
EToolResources,
|
|
isAgentsEndpoint,
|
|
replaceSpecialVars,
|
|
providerEndpointMap,
|
|
} = require('librechat-data-provider');
|
|
const generateArtifactsPrompt = require('~/app/clients/prompts/artifacts');
|
|
const { getProviderConfig } = require('~/server/services/Endpoints');
|
|
const { processFiles } = require('~/server/services/Files/process');
|
|
const { getFiles, getToolFilesByIds } = require('~/models/File');
|
|
const { getConvoFiles } = require('~/models/Conversation');
|
|
|
|
/**
|
|
* @param {object} params
|
|
* @param {ServerRequest} params.req
|
|
* @param {ServerResponse} params.res
|
|
* @param {Agent} params.agent
|
|
* @param {string | null} [params.conversationId]
|
|
* @param {Array<IMongoFile>} [params.requestFiles]
|
|
* @param {typeof import('~/server/services/ToolService').loadAgentTools | undefined} [params.loadTools]
|
|
* @param {TEndpointOption} [params.endpointOption]
|
|
* @param {Set<string>} [params.allowedProviders]
|
|
* @param {boolean} [params.isInitialAgent]
|
|
* @returns {Promise<Agent & {
|
|
* tools: StructuredTool[],
|
|
* attachments: Array<MongoFile>,
|
|
* toolContextMap: Record<string, unknown>,
|
|
* maxContextTokens: number,
|
|
* userMCPAuthMap?: Record<string, Record<string, string>>
|
|
* }>}
|
|
*/
|
|
const initializeAgent = async ({
|
|
req,
|
|
res,
|
|
agent,
|
|
loadTools,
|
|
requestFiles,
|
|
conversationId,
|
|
endpointOption,
|
|
allowedProviders,
|
|
isInitialAgent = false,
|
|
}) => {
|
|
const appConfig = req.config;
|
|
if (
|
|
isAgentsEndpoint(endpointOption?.endpoint) &&
|
|
allowedProviders.size > 0 &&
|
|
!allowedProviders.has(agent.provider)
|
|
) {
|
|
throw new Error(
|
|
`{ "type": "${ErrorTypes.INVALID_AGENT_PROVIDER}", "info": "${agent.provider}" }`,
|
|
);
|
|
}
|
|
let currentFiles;
|
|
|
|
const _modelOptions = structuredClone(
|
|
Object.assign(
|
|
{ model: agent.model },
|
|
agent.model_parameters ?? { model: agent.model },
|
|
isInitialAgent === true ? endpointOption?.model_parameters : {},
|
|
),
|
|
);
|
|
|
|
const { resendFiles, maxContextTokens, modelOptions } = extractLibreChatParams(_modelOptions);
|
|
|
|
if (isInitialAgent && conversationId != null && resendFiles) {
|
|
const fileIds = (await getConvoFiles(conversationId)) ?? [];
|
|
/** @type {Set<EToolResources>} */
|
|
const toolResourceSet = new Set();
|
|
for (const tool of agent.tools) {
|
|
if (EToolResources[tool]) {
|
|
toolResourceSet.add(EToolResources[tool]);
|
|
}
|
|
}
|
|
const toolFiles = await getToolFilesByIds(fileIds, toolResourceSet);
|
|
if (requestFiles.length || toolFiles.length) {
|
|
currentFiles = await processFiles(requestFiles.concat(toolFiles));
|
|
}
|
|
} else if (isInitialAgent && requestFiles.length) {
|
|
currentFiles = await processFiles(requestFiles);
|
|
}
|
|
|
|
const { attachments, tool_resources } = await primeResources({
|
|
req,
|
|
getFiles,
|
|
appConfig,
|
|
agentId: agent.id,
|
|
attachments: currentFiles,
|
|
tool_resources: agent.tool_resources,
|
|
requestFileSet: new Set(requestFiles?.map((file) => file.file_id)),
|
|
});
|
|
|
|
const provider = agent.provider;
|
|
const {
|
|
tools: structuredTools,
|
|
toolContextMap,
|
|
userMCPAuthMap,
|
|
} = (await loadTools?.({
|
|
req,
|
|
res,
|
|
provider,
|
|
agentId: agent.id,
|
|
tools: agent.tools,
|
|
model: agent.model,
|
|
tool_resources,
|
|
})) ?? {};
|
|
|
|
agent.endpoint = provider;
|
|
const { getOptions, overrideProvider } = getProviderConfig({ provider, appConfig });
|
|
if (overrideProvider !== agent.provider) {
|
|
agent.provider = overrideProvider;
|
|
}
|
|
|
|
const _endpointOption =
|
|
isInitialAgent === true
|
|
? Object.assign({}, endpointOption, { model_parameters: modelOptions })
|
|
: { model_parameters: modelOptions };
|
|
|
|
const options = await getOptions({
|
|
req,
|
|
res,
|
|
optionsOnly: true,
|
|
overrideEndpoint: provider,
|
|
overrideModel: agent.model,
|
|
endpointOption: _endpointOption,
|
|
});
|
|
|
|
const tokensModel =
|
|
agent.provider === EModelEndpoint.azureOpenAI ? agent.model : modelOptions.model;
|
|
const maxTokens = optionalChainWithEmptyCheck(
|
|
modelOptions.maxOutputTokens,
|
|
modelOptions.maxTokens,
|
|
0,
|
|
);
|
|
const agentMaxContextTokens = optionalChainWithEmptyCheck(
|
|
maxContextTokens,
|
|
getModelMaxTokens(tokensModel, providerEndpointMap[provider], options.endpointTokenConfig),
|
|
18000,
|
|
);
|
|
|
|
if (
|
|
agent.endpoint === EModelEndpoint.azureOpenAI &&
|
|
options.llmConfig?.azureOpenAIApiInstanceName == null
|
|
) {
|
|
agent.provider = Providers.OPENAI;
|
|
}
|
|
|
|
if (options.provider != null) {
|
|
agent.provider = options.provider;
|
|
}
|
|
|
|
/** @type {import('@librechat/agents').GenericTool[]} */
|
|
let tools = options.tools?.length ? options.tools : structuredTools;
|
|
if (
|
|
(agent.provider === Providers.GOOGLE || agent.provider === Providers.VERTEXAI) &&
|
|
options.tools?.length &&
|
|
structuredTools?.length
|
|
) {
|
|
throw new Error(`{ "type": "${ErrorTypes.GOOGLE_TOOL_CONFLICT}"}`);
|
|
} else if (
|
|
(agent.provider === Providers.OPENAI ||
|
|
agent.provider === Providers.AZURE ||
|
|
agent.provider === Providers.ANTHROPIC) &&
|
|
options.tools?.length &&
|
|
structuredTools?.length
|
|
) {
|
|
tools = structuredTools.concat(options.tools);
|
|
}
|
|
|
|
/** @type {import('@librechat/agents').ClientOptions} */
|
|
agent.model_parameters = { ...options.llmConfig };
|
|
if (options.configOptions) {
|
|
agent.model_parameters.configuration = options.configOptions;
|
|
}
|
|
|
|
if (agent.instructions && agent.instructions !== '') {
|
|
agent.instructions = replaceSpecialVars({
|
|
text: agent.instructions,
|
|
user: req.user,
|
|
});
|
|
}
|
|
|
|
if (typeof agent.artifacts === 'string' && agent.artifacts !== '') {
|
|
agent.additional_instructions = generateArtifactsPrompt({
|
|
endpoint: agent.provider,
|
|
artifacts: agent.artifacts,
|
|
});
|
|
}
|
|
|
|
return {
|
|
...agent,
|
|
tools,
|
|
attachments,
|
|
resendFiles,
|
|
userMCPAuthMap,
|
|
toolContextMap,
|
|
useLegacyContent: !!options.useLegacyContent,
|
|
maxContextTokens: Math.round((agentMaxContextTokens - maxTokens) * 0.9),
|
|
};
|
|
};
|
|
|
|
module.exports = { initializeAgent };
|