mirror of
https://github.com/danny-avila/LibreChat.git
synced 2025-09-22 08:12:00 +02:00

* agents - phase 1 (#30) * chore: copy assistant files * feat: frontend and data-provider * feat: backend get endpoint test * fix(MessageEndpointIcon): switched to AgentName and AgentAvatar * fix: small fixes * fix: agent endpoint config * fix: show Agent Builder * chore: install agentus * chore: initial scaffolding for agents * fix: updated Assistant logic to Agent Logic for some Agent components * WIP first pass, demo of agent package * WIP: initial backend infra for agents * fix: agent list error * wip: agents routing * chore: Refactor useSSE hook to handle different data events * wip: correctly emit events * chore: Update @librechat/agentus npm dependency to version 1.0.9 * remove comment * first pass: streaming agent text * chore: Remove @librechat/agentus root-level workspace npm dependency * feat: Agent Schema and Model * fix: content handling fixes * fix: content message save * WIP: new content data * fix: run step issue with tool calls * chore: Update @librechat/agentus npm dependency to version 1.1.5 * feat: update controller and agent routes * wip: initial backend tool and tool error handling support * wip: tool chunks * chore: Update @librechat/agentus npm dependency to version 1.1.7 * chore: update tool_call typing, add test conditions and logs * fix: create agent * fix: create agent * first pass: render completed content parts * fix: remove logging, fix step handler typing * chore: Update @librechat/agentus npm dependency to version 1.1.9 * refactor: cleanup maps on unmount * chore: Update BaseClient.js to safely count tokens for string, number, and boolean values * fix: support subsequent messages with tool_calls * chore: export order * fix: select agent * fix: tool call types and handling * chore: switch to anthropic for testing * fix: AgentSelect * refactor: experimental: OpenAIClient to use array for intermediateReply * fix(useSSE): revert old condition for streaming legacy client tokens * fix: lint * revert `agent_id` to `id` * chore: update localization keys for agent-related components * feat: zod schema handling for actions * refactor(actions): if no params, no zodSchema * chore: Update @librechat/agentus npm dependency to version 1.2.1 * feat: first pass, actions * refactor: empty schema for actions without params * feat: Update createRun function to accept additional options * fix: message payload formatting; feat: add more client options * fix: ToolCall component rendering when action has no args but has output * refactor(ToolCall): allow non-stringy args * WIP: first pass, correctly formatted tool_calls between providers * refactor: Remove duplicate import of 'roles' module * refactor: Exclude 'vite.config.ts' from TypeScript compilation * refactor: fix agent related types > - no need to use endpoint/model fields for identifying agent metadata > - add `provider` distinction for agent-configured 'endpoint' - no need for agent-endpoint map - reduce complexity of tools as functions into tools as string[] - fix types related to above changes - reduce unnecessary variables for queries/mutations and corresponding react-query keys * refactor: Add tools and tool_kwargs fields to agent schema * refactor: Remove unused code and update dependencies * refactor: Update updateAgentHandler to use req.body directly * refactor: Update AgentSelect component to use localized hooks * refactor: Update agent schema to include tools and provider fields * refactor(AgentPanel): add scrollbar gutter, add provider field to form, fix agent schema required values * refactor: Update AgentSwitcher component to use selectedAgentId instead of selectedAgent * refactor: Update AgentPanel component to include alternateName import and defaultAgentFormValues * refactor(SelectDropDown): allow setting value as option while still supporting legacy usage (string values only) * refactor: SelectDropdown changes - Only necessary when the available values are objects with label/value fields and the selected value is expected to be a string. * refactor: TypeError issues and handle provider as option * feat: Add placeholder for provider selection in AgentPanel component * refactor: Update agent schema to include author and provider fields * fix: show expected 'create agent' placeholder when creating agent * chore: fix localization strings, hide capabilities form for now * chore: typing * refactor: import order and use compact agents schema for now * chore: typing * refactor: Update AgentForm type to use AgentCapabilities * fix agent form agent selection issues * feat: responsive agent selection * fix: Handle cancelled fetch in useSelectAgent hook * fix: reset agent form on accordion close/open * feat: Add agent_id to default conversation for agents endpoint * feat: agents endpoint request handling * refactor: reset conversation model on agent select * refactor: add `additional_instructions` to conversation schema, organize other fields * chore: casing * chore: types * refactor(loadAgentTools): explicitly pass agent_id, do not pass `model` to loadAgentTools for now, load action sets by agent_id * WIP: initial draft of real agent client initialization * WIP: first pass, anthropic agent requests * feat: remember last selected agent * feat: openai and azure connected * fix: prioritize agent model for runs unless an explicit override model is passed from client * feat: Agent Actions * fix: save agent id to convo * feat: model panel (#29) * feat: model panel * bring back comments * fix: method still null * fix: AgentPanel FormContext * feat: add more parameters * fix: style issues; refactor: Agent Controller * fix: cherry-pick * fix: Update AgentAvatar component to use AssistantIcon instead of BrainCircuit * feat: OGDialog for delete agent; feat(assistant): update Agent types, introduced `model_parameters` * feat: icon and general `model_parameters` update * feat: use react-hook-form better * fix: agent builder form reset issue when switching panels * refactor: modularize agent builder form --------- Co-authored-by: Danny Avila <danny@librechat.ai> * fix: AgentPanel and ModelPanel type issues and use `useFormContext` and `watch` instead of `methods` directly and `useWatch`. * fix: tool call issues due to invalid input (anthropic) of empty string * fix: handle empty text in Part component --------- Co-authored-by: Marco Beretta <81851188+berry-13@users.noreply.github.com> * refactor: remove form ModelPanel and fixed nested ternary expressions in AgentConfig * fix: Model Parameters not saved correctly * refactor: remove console log * feat: avatar upload and get for Agents (#36) Co-authored-by: Marco Beretta <81851188+berry-13@users.noreply.github.com> * chore: update to public package * fix: typing, optional chaining * fix: cursor not showing for content parts * chore: conditionally enable agents * ci: fix azure test * ci: fix frontend tests, fix eslint api * refactor: Remove unused errorContentPart variable * continue of the agent message PR (#40) * last fixes * fix: agentMap * pr merge test (#41) * fix: model icon not fetching correctly * remove console logs * feat: agent name * refactor: pass documentsMap as a prop to allow re-render of assistant form * refactor: pass documentsMap as a prop to allow re-render of assistant form * chore: Bump version to 0.7.419 * fix: TypeError: Cannot read properties of undefined (reading 'id') * refactor: update AgentSwitcher component to use ControlCombobox instead of Combobox --------- Co-authored-by: Marco Beretta <81851188+berry-13@users.noreply.github.com>
462 lines
14 KiB
JavaScript
462 lines
14 KiB
JavaScript
// const { HttpsProxyAgent } = require('https-proxy-agent');
|
|
// const {
|
|
// Constants,
|
|
// ImageDetail,
|
|
// EModelEndpoint,
|
|
// resolveHeaders,
|
|
// validateVisionModel,
|
|
// mapModelToAzureConfig,
|
|
// } = require('librechat-data-provider');
|
|
const { Callback } = require('@librechat/agents');
|
|
const {
|
|
EModelEndpoint,
|
|
providerEndpointMap,
|
|
removeNullishValues,
|
|
} = require('librechat-data-provider');
|
|
const {
|
|
extractBaseURL,
|
|
// constructAzureURL,
|
|
// genAzureChatCompletion,
|
|
} = require('~/utils');
|
|
const {
|
|
formatMessage,
|
|
formatAgentMessages,
|
|
createContextHandlers,
|
|
} = require('~/app/clients/prompts');
|
|
const Tokenizer = require('~/server/services/Tokenizer');
|
|
const BaseClient = require('~/app/clients/BaseClient');
|
|
// const { sleep } = require('~/server/utils');
|
|
const { createRun } = require('./run');
|
|
const { logger } = require('~/config');
|
|
|
|
class AgentClient extends BaseClient {
|
|
constructor(options = {}) {
|
|
super(options);
|
|
|
|
/** @type {'discard' | 'summarize'} */
|
|
this.contextStrategy = 'discard';
|
|
|
|
/** @deprecated @type {true} - Is a Chat Completion Request */
|
|
this.isChatCompletion = true;
|
|
|
|
const { maxContextTokens, modelOptions = {}, ...clientOptions } = options;
|
|
|
|
this.modelOptions = modelOptions;
|
|
this.maxContextTokens = maxContextTokens;
|
|
this.options = Object.assign({ endpoint: EModelEndpoint.agents }, clientOptions);
|
|
}
|
|
|
|
setOptions(options) {
|
|
logger.info('[api/server/controllers/agents/client.js] setOptions', options);
|
|
}
|
|
|
|
/**
|
|
*
|
|
* Checks if the model is a vision model based on request attachments and sets the appropriate options:
|
|
* - Sets `this.modelOptions.model` to `gpt-4-vision-preview` if the request is a vision request.
|
|
* - Sets `this.isVisionModel` to `true` if vision request.
|
|
* - Deletes `this.modelOptions.stop` if vision request.
|
|
* @param {MongoFile[]} attachments
|
|
*/
|
|
checkVisionRequest(attachments) {
|
|
logger.info(
|
|
'[api/server/controllers/agents/client.js #checkVisionRequest] not implemented',
|
|
attachments,
|
|
);
|
|
// if (!attachments) {
|
|
// return;
|
|
// }
|
|
|
|
// const availableModels = this.options.modelsConfig?.[this.options.endpoint];
|
|
// if (!availableModels) {
|
|
// return;
|
|
// }
|
|
|
|
// let visionRequestDetected = false;
|
|
// for (const file of attachments) {
|
|
// if (file?.type?.includes('image')) {
|
|
// visionRequestDetected = true;
|
|
// break;
|
|
// }
|
|
// }
|
|
// if (!visionRequestDetected) {
|
|
// return;
|
|
// }
|
|
|
|
// this.isVisionModel = validateVisionModel({ model: this.modelOptions.model, availableModels });
|
|
// if (this.isVisionModel) {
|
|
// delete this.modelOptions.stop;
|
|
// return;
|
|
// }
|
|
|
|
// for (const model of availableModels) {
|
|
// if (!validateVisionModel({ model, availableModels })) {
|
|
// continue;
|
|
// }
|
|
// this.modelOptions.model = model;
|
|
// this.isVisionModel = true;
|
|
// delete this.modelOptions.stop;
|
|
// return;
|
|
// }
|
|
|
|
// if (!availableModels.includes(this.defaultVisionModel)) {
|
|
// return;
|
|
// }
|
|
// if (!validateVisionModel({ model: this.defaultVisionModel, availableModels })) {
|
|
// return;
|
|
// }
|
|
|
|
// this.modelOptions.model = this.defaultVisionModel;
|
|
// this.isVisionModel = true;
|
|
// delete this.modelOptions.stop;
|
|
}
|
|
|
|
getSaveOptions() {
|
|
return removeNullishValues(
|
|
Object.assign(
|
|
{
|
|
agent_id: this.options.agent.id,
|
|
modelLabel: this.options.modelLabel,
|
|
maxContextTokens: this.options.maxContextTokens,
|
|
resendFiles: this.options.resendFiles,
|
|
imageDetail: this.options.imageDetail,
|
|
spec: this.options.spec,
|
|
},
|
|
this.modelOptions,
|
|
{
|
|
model: undefined,
|
|
// TODO:
|
|
// would need to be override settings; otherwise, model needs to be undefined
|
|
// model: this.override.model,
|
|
// instructions: this.override.instructions,
|
|
// additional_instructions: this.override.additional_instructions,
|
|
},
|
|
),
|
|
);
|
|
}
|
|
|
|
getBuildMessagesOptions(opts) {
|
|
return {
|
|
instructions: opts.instructions,
|
|
additional_instructions: opts.additional_instructions,
|
|
};
|
|
}
|
|
|
|
async buildMessages(
|
|
messages,
|
|
parentMessageId,
|
|
{ instructions = null, additional_instructions = null },
|
|
opts,
|
|
) {
|
|
let orderedMessages = this.constructor.getMessagesForConversation({
|
|
messages,
|
|
parentMessageId,
|
|
summary: this.shouldSummarize,
|
|
});
|
|
|
|
let payload;
|
|
/** @type {{ role: string; name: string; content: string } | undefined} */
|
|
let systemMessage;
|
|
/** @type {number | undefined} */
|
|
let promptTokens;
|
|
|
|
/** @type {string} */
|
|
let systemContent = `${instructions ?? ''}${additional_instructions ?? ''}`;
|
|
|
|
if (this.options.attachments) {
|
|
const attachments = await this.options.attachments;
|
|
|
|
if (this.message_file_map) {
|
|
this.message_file_map[orderedMessages[orderedMessages.length - 1].messageId] = attachments;
|
|
} else {
|
|
this.message_file_map = {
|
|
[orderedMessages[orderedMessages.length - 1].messageId]: attachments,
|
|
};
|
|
}
|
|
|
|
const files = await this.addImageURLs(
|
|
orderedMessages[orderedMessages.length - 1],
|
|
attachments,
|
|
);
|
|
|
|
this.options.attachments = files;
|
|
}
|
|
|
|
if (this.message_file_map) {
|
|
this.contextHandlers = createContextHandlers(
|
|
this.options.req,
|
|
orderedMessages[orderedMessages.length - 1].text,
|
|
);
|
|
}
|
|
|
|
const formattedMessages = orderedMessages.map((message, i) => {
|
|
const formattedMessage = formatMessage({
|
|
message,
|
|
userName: this.options?.name,
|
|
assistantName: this.options?.modelLabel,
|
|
});
|
|
|
|
const needsTokenCount = this.contextStrategy && !orderedMessages[i].tokenCount;
|
|
|
|
/* If tokens were never counted, or, is a Vision request and the message has files, count again */
|
|
if (needsTokenCount || (this.isVisionModel && (message.image_urls || message.files))) {
|
|
orderedMessages[i].tokenCount = this.getTokenCountForMessage(formattedMessage);
|
|
}
|
|
|
|
/* If message has files, calculate image token cost */
|
|
// if (this.message_file_map && this.message_file_map[message.messageId]) {
|
|
// const attachments = this.message_file_map[message.messageId];
|
|
// for (const file of attachments) {
|
|
// if (file.embedded) {
|
|
// this.contextHandlers?.processFile(file);
|
|
// continue;
|
|
// }
|
|
|
|
// orderedMessages[i].tokenCount += this.calculateImageTokenCost({
|
|
// width: file.width,
|
|
// height: file.height,
|
|
// detail: this.options.imageDetail ?? ImageDetail.auto,
|
|
// });
|
|
// }
|
|
// }
|
|
|
|
return formattedMessage;
|
|
});
|
|
|
|
if (this.contextHandlers) {
|
|
this.augmentedPrompt = await this.contextHandlers.createContext();
|
|
systemContent = this.augmentedPrompt + systemContent;
|
|
}
|
|
|
|
if (systemContent) {
|
|
systemContent = `${systemContent.trim()}`;
|
|
systemMessage = {
|
|
role: 'system',
|
|
name: 'instructions',
|
|
content: systemContent,
|
|
};
|
|
|
|
if (this.contextStrategy) {
|
|
const instructionTokens = this.getTokenCountForMessage(systemMessage);
|
|
if (instructionTokens >= 0) {
|
|
const firstMessageTokens = orderedMessages[0].tokenCount ?? 0;
|
|
orderedMessages[0].tokenCount = firstMessageTokens + instructionTokens;
|
|
}
|
|
}
|
|
}
|
|
|
|
if (this.contextStrategy) {
|
|
({ payload, promptTokens, messages } = await this.handleContextStrategy({
|
|
orderedMessages,
|
|
formattedMessages,
|
|
/* prefer usage_metadata from final message */
|
|
buildTokenMap: false,
|
|
}));
|
|
}
|
|
|
|
const result = {
|
|
prompt: payload,
|
|
promptTokens,
|
|
messages,
|
|
};
|
|
|
|
if (promptTokens >= 0 && typeof opts?.getReqData === 'function') {
|
|
opts.getReqData({ promptTokens });
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
/** @type {sendCompletion} */
|
|
async sendCompletion(payload, opts = {}) {
|
|
this.modelOptions.user = this.user;
|
|
return await this.chatCompletion({
|
|
payload,
|
|
onProgress: opts.onProgress,
|
|
abortController: opts.abortController,
|
|
});
|
|
}
|
|
|
|
// async recordTokenUsage({ promptTokens, completionTokens, context = 'message' }) {
|
|
// await spendTokens(
|
|
// {
|
|
// context,
|
|
// model: this.modelOptions.model,
|
|
// conversationId: this.conversationId,
|
|
// user: this.user ?? this.options.req.user?.id,
|
|
// endpointTokenConfig: this.options.endpointTokenConfig,
|
|
// },
|
|
// { promptTokens, completionTokens },
|
|
// );
|
|
// }
|
|
|
|
async chatCompletion({ payload, abortController = null }) {
|
|
try {
|
|
if (!abortController) {
|
|
abortController = new AbortController();
|
|
}
|
|
|
|
const baseURL = extractBaseURL(this.completionsUrl);
|
|
logger.debug('[api/server/controllers/agents/client.js] chatCompletion', {
|
|
baseURL,
|
|
payload,
|
|
});
|
|
|
|
// if (this.useOpenRouter) {
|
|
// opts.defaultHeaders = {
|
|
// 'HTTP-Referer': 'https://librechat.ai',
|
|
// 'X-Title': 'LibreChat',
|
|
// };
|
|
// }
|
|
|
|
// if (this.options.headers) {
|
|
// opts.defaultHeaders = { ...opts.defaultHeaders, ...this.options.headers };
|
|
// }
|
|
|
|
// if (this.options.proxy) {
|
|
// opts.httpAgent = new HttpsProxyAgent(this.options.proxy);
|
|
// }
|
|
|
|
// if (this.isVisionModel) {
|
|
// modelOptions.max_tokens = 4000;
|
|
// }
|
|
|
|
// /** @type {TAzureConfig | undefined} */
|
|
// const azureConfig = this.options?.req?.app?.locals?.[EModelEndpoint.azureOpenAI];
|
|
|
|
// if (
|
|
// (this.azure && this.isVisionModel && azureConfig) ||
|
|
// (azureConfig && this.isVisionModel && this.options.endpoint === EModelEndpoint.azureOpenAI)
|
|
// ) {
|
|
// const { modelGroupMap, groupMap } = azureConfig;
|
|
// const {
|
|
// azureOptions,
|
|
// baseURL,
|
|
// headers = {},
|
|
// serverless,
|
|
// } = mapModelToAzureConfig({
|
|
// modelName: modelOptions.model,
|
|
// modelGroupMap,
|
|
// groupMap,
|
|
// });
|
|
// opts.defaultHeaders = resolveHeaders(headers);
|
|
// this.langchainProxy = extractBaseURL(baseURL);
|
|
// this.apiKey = azureOptions.azureOpenAIApiKey;
|
|
|
|
// const groupName = modelGroupMap[modelOptions.model].group;
|
|
// this.options.addParams = azureConfig.groupMap[groupName].addParams;
|
|
// this.options.dropParams = azureConfig.groupMap[groupName].dropParams;
|
|
// // Note: `forcePrompt` not re-assigned as only chat models are vision models
|
|
|
|
// this.azure = !serverless && azureOptions;
|
|
// this.azureEndpoint =
|
|
// !serverless && genAzureChatCompletion(this.azure, modelOptions.model, this);
|
|
// }
|
|
|
|
// if (this.azure || this.options.azure) {
|
|
// /* Azure Bug, extremely short default `max_tokens` response */
|
|
// if (!modelOptions.max_tokens && modelOptions.model === 'gpt-4-vision-preview') {
|
|
// modelOptions.max_tokens = 4000;
|
|
// }
|
|
|
|
// /* Azure does not accept `model` in the body, so we need to remove it. */
|
|
// delete modelOptions.model;
|
|
|
|
// opts.baseURL = this.langchainProxy
|
|
// ? constructAzureURL({
|
|
// baseURL: this.langchainProxy,
|
|
// azureOptions: this.azure,
|
|
// })
|
|
// : this.azureEndpoint.split(/(?<!\/)\/(chat|completion)\//)[0];
|
|
|
|
// opts.defaultQuery = { 'api-version': this.azure.azureOpenAIApiVersion };
|
|
// opts.defaultHeaders = { ...opts.defaultHeaders, 'api-key': this.apiKey };
|
|
// }
|
|
|
|
// if (process.env.OPENAI_ORGANIZATION) {
|
|
// opts.organization = process.env.OPENAI_ORGANIZATION;
|
|
// }
|
|
|
|
// if (this.options.addParams && typeof this.options.addParams === 'object') {
|
|
// modelOptions = {
|
|
// ...modelOptions,
|
|
// ...this.options.addParams,
|
|
// };
|
|
// logger.debug('[api/server/controllers/agents/client.js #chatCompletion] added params', {
|
|
// addParams: this.options.addParams,
|
|
// modelOptions,
|
|
// });
|
|
// }
|
|
|
|
// if (this.options.dropParams && Array.isArray(this.options.dropParams)) {
|
|
// this.options.dropParams.forEach((param) => {
|
|
// delete modelOptions[param];
|
|
// });
|
|
// logger.debug('[api/server/controllers/agents/client.js #chatCompletion] dropped params', {
|
|
// dropParams: this.options.dropParams,
|
|
// modelOptions,
|
|
// });
|
|
// }
|
|
|
|
// const streamRate = this.options.streamRate ?? Constants.DEFAULT_STREAM_RATE;
|
|
|
|
const run = await createRun({
|
|
agent: this.options.agent,
|
|
tools: this.options.tools,
|
|
toolMap: this.options.toolMap,
|
|
runId: this.responseMessageId,
|
|
modelOptions: this.modelOptions,
|
|
customHandlers: this.options.eventHandlers,
|
|
});
|
|
|
|
const config = {
|
|
configurable: {
|
|
provider: providerEndpointMap[this.options.agent.provider],
|
|
thread_id: this.conversationId,
|
|
},
|
|
run_id: this.responseMessageId,
|
|
streamMode: 'values',
|
|
version: 'v2',
|
|
};
|
|
|
|
if (!run) {
|
|
throw new Error('Failed to create run');
|
|
}
|
|
|
|
const messages = formatAgentMessages(payload);
|
|
const runMessages = await run.processStream({ messages }, config, {
|
|
[Callback.TOOL_ERROR]: (graph, error, toolId) => {
|
|
logger.error(
|
|
'[api/server/controllers/agents/client.js #chatCompletion] Tool Error',
|
|
error,
|
|
toolId,
|
|
);
|
|
},
|
|
});
|
|
// console.dir(runMessages, { depth: null });
|
|
return runMessages;
|
|
} catch (err) {
|
|
logger.error(
|
|
'[api/server/controllers/agents/client.js #chatCompletion] Unhandled error type',
|
|
err,
|
|
);
|
|
throw err;
|
|
}
|
|
}
|
|
|
|
getEncoding() {
|
|
return this.modelOptions.model?.includes('gpt-4o') ? 'o200k_base' : 'cl100k_base';
|
|
}
|
|
|
|
/**
|
|
* Returns the token count of a given text. It also checks and resets the tokenizers if necessary.
|
|
* @param {string} text - The text to get the token count for.
|
|
* @returns {number} The token count of the given text.
|
|
*/
|
|
getTokenCount(text) {
|
|
const encoding = this.getEncoding();
|
|
return Tokenizer.getTokenCount(text, encoding);
|
|
}
|
|
}
|
|
|
|
module.exports = AgentClient;
|