mirror of
https://github.com/danny-avila/LibreChat.git
synced 2025-12-17 00:40:14 +01:00
🎯 refactor: Custom Endpoint Request-based Header Resolution (#9344)
* refactor: resolve request-based headers for custom endpoints right before LLM request * ci: clarify request-based header resolution in initializeClient test
This commit is contained in:
parent
1764de53a5
commit
43add11b05
3 changed files with 31 additions and 1 deletions
|
|
@ -7,6 +7,7 @@ const {
|
||||||
createRun,
|
createRun,
|
||||||
Tokenizer,
|
Tokenizer,
|
||||||
checkAccess,
|
checkAccess,
|
||||||
|
resolveHeaders,
|
||||||
getBalanceConfig,
|
getBalanceConfig,
|
||||||
memoryInstructions,
|
memoryInstructions,
|
||||||
formatContentStrings,
|
formatContentStrings,
|
||||||
|
|
@ -879,6 +880,16 @@ class AgentClient extends BaseClient {
|
||||||
memoryPromise = this.runMemory(messages);
|
memoryPromise = this.runMemory(messages);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Resolve request-based headers for Custom Endpoints. Note: if this is added to
|
||||||
|
* non-custom endpoints, needs consideration of varying provider header configs.
|
||||||
|
*/
|
||||||
|
if (agent.model_parameters?.configuration?.defaultHeaders != null) {
|
||||||
|
agent.model_parameters.configuration.defaultHeaders = resolveHeaders({
|
||||||
|
headers: agent.model_parameters.configuration.defaultHeaders,
|
||||||
|
body: config.configurable.requestBody,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
run = await createRun({
|
run = await createRun({
|
||||||
agent,
|
agent,
|
||||||
req: this.options.req,
|
req: this.options.req,
|
||||||
|
|
@ -1181,6 +1192,20 @@ class AgentClient extends BaseClient {
|
||||||
clientOptions.json = true;
|
clientOptions.json = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Resolve request-based headers for Custom Endpoints. Note: if this is added to
|
||||||
|
* non-custom endpoints, needs consideration of varying provider header configs.
|
||||||
|
*/
|
||||||
|
if (clientOptions?.configuration?.defaultHeaders != null) {
|
||||||
|
clientOptions.configuration.defaultHeaders = resolveHeaders({
|
||||||
|
headers: clientOptions.configuration.defaultHeaders,
|
||||||
|
body: {
|
||||||
|
messageId: this.responseMessageId,
|
||||||
|
conversationId: this.conversationId,
|
||||||
|
parentMessageId: this.parentMessageId,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const titleResult = await this.run.generateTitle({
|
const titleResult = await this.run.generateTitle({
|
||||||
provider,
|
provider,
|
||||||
|
|
|
||||||
|
|
@ -36,10 +36,12 @@ const initializeClient = async ({ req, res, endpointOption, optionsOnly, overrid
|
||||||
const CUSTOM_API_KEY = extractEnvVariable(endpointConfig.apiKey);
|
const CUSTOM_API_KEY = extractEnvVariable(endpointConfig.apiKey);
|
||||||
const CUSTOM_BASE_URL = extractEnvVariable(endpointConfig.baseURL);
|
const CUSTOM_BASE_URL = extractEnvVariable(endpointConfig.baseURL);
|
||||||
|
|
||||||
|
/** Intentionally excludes passing `body`, i.e. `req.body`, as
|
||||||
|
* values may not be accurate until `AgentClient` is initialized
|
||||||
|
*/
|
||||||
let resolvedHeaders = resolveHeaders({
|
let resolvedHeaders = resolveHeaders({
|
||||||
headers: endpointConfig.headers,
|
headers: endpointConfig.headers,
|
||||||
user: req.user,
|
user: req.user,
|
||||||
body: req.body,
|
|
||||||
});
|
});
|
||||||
|
|
||||||
if (CUSTOM_API_KEY.match(envVarRegex)) {
|
if (CUSTOM_API_KEY.match(envVarRegex)) {
|
||||||
|
|
|
||||||
|
|
@ -76,7 +76,10 @@ describe('custom/initializeClient', () => {
|
||||||
expect(resolveHeaders).toHaveBeenCalledWith({
|
expect(resolveHeaders).toHaveBeenCalledWith({
|
||||||
headers: { 'x-user': '{{LIBRECHAT_USER_ID}}', 'x-email': '{{LIBRECHAT_USER_EMAIL}}' },
|
headers: { 'x-user': '{{LIBRECHAT_USER_ID}}', 'x-email': '{{LIBRECHAT_USER_EMAIL}}' },
|
||||||
user: { id: 'user-123', email: 'test@example.com', role: 'user' },
|
user: { id: 'user-123', email: 'test@example.com', role: 'user' },
|
||||||
|
/**
|
||||||
|
* Note: Request-based Header Resolution is deferred until right before LLM request is made
|
||||||
body: { endpoint: 'test-endpoint' }, // body - supports {{LIBRECHAT_BODY_*}} placeholders
|
body: { endpoint: 'test-endpoint' }, // body - supports {{LIBRECHAT_BODY_*}} placeholders
|
||||||
|
*/
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue