Fireworks AI: Set session affinity header for improved caching (#5826)
This change adds a chat-specific "x-session-affinity" HTTP header to requests to Fireworks that use the full prompt. Quiet requests like summarization requests that don't use the same prompt prefix are excluded. This improves prompt caching by 1. Adding a stable "x-session-affinity" header, which Fireworks AI recommends to route requests to the same replica 2. Separating different chats into different sessions 3. Separating main LLM calls from quiet calls which have a completely different prompt prefix See https://docs.fireworks.ai/guides/prompt-caching. This uses an HMAC with the cookie secret instead of simply hashing the chat title to avoid leaking chat titles. Session affinity headers may not fall under zero-data retention in Fireworks or could be cached by proxies on the way. Chat titles are tiny, so the HMAC computation is instant.
This commit is contained in:
@@ -14,6 +14,7 @@ import {
|
||||
extension_prompt_roles,
|
||||
extension_prompt_types,
|
||||
Generate,
|
||||
getCurrentChatId,
|
||||
getExtensionPrompt,
|
||||
getExtensionPromptMaxDepth,
|
||||
getMediaDisplay,
|
||||
@@ -2867,6 +2868,10 @@ export async function createGenerationParameters(settings, model, type, messages
|
||||
generate_data.nanogpt_payg_override = settings.nanogpt_payg_override;
|
||||
}
|
||||
|
||||
if (settings.chat_completion_source === chat_completion_sources.FIREWORKS && type !== 'quiet') {
|
||||
generate_data.chat_id = getCurrentChatId();
|
||||
}
|
||||
|
||||
if ([chat_completion_sources.MAKERSUITE, chat_completion_sources.VERTEXAI].includes(settings.chat_completion_source)) {
|
||||
const stopStringsLimit = 5;
|
||||
generate_data.top_k = Number(settings.top_k_openai);
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
/* eslint-disable dot-notation */
|
||||
import { createHmac } from 'node:crypto';
|
||||
import process from 'node:process';
|
||||
import util from 'node:util';
|
||||
import express from 'express';
|
||||
@@ -66,6 +67,7 @@ import {
|
||||
getWebTokenizer,
|
||||
} from '../tokenizers.js';
|
||||
import { getVertexAIAuth, getProjectIdFromServiceAccount } from '../google.js';
|
||||
import { getCookieSecret } from '../../users.js';
|
||||
|
||||
const API_OPENAI = 'https://api.openai.com/v1';
|
||||
const API_CLAUDE = 'https://api.anthropic.com/v1';
|
||||
@@ -108,6 +110,18 @@ const cachingAtDepth = (() => {
|
||||
})();
|
||||
const enableAdaptiveThinking = getConfigValue('claude.enableAdaptiveThinking', true, 'boolean');
|
||||
|
||||
/**
|
||||
* Lazily-cached HMAC key (instance cookie secret) for session-affinity hashing.
|
||||
* @type {string|undefined}
|
||||
*/
|
||||
let affinityKey;
|
||||
function getAffinityKey() {
|
||||
if (affinityKey === undefined) {
|
||||
affinityKey = getCookieSecret(globalThis.DATA_ROOT);
|
||||
}
|
||||
return affinityKey;
|
||||
}
|
||||
|
||||
/**
|
||||
* Cache for cacheable (writing) OpenRouter model IDs.
|
||||
* @type {string[]}
|
||||
@@ -2453,6 +2467,9 @@ router.post('/generate', async function (request, response) {
|
||||
},
|
||||
};
|
||||
}
|
||||
if (request.body.chat_id) {
|
||||
headers['x-session-affinity'] = createHmac('sha256', getAffinityKey()).update(request.body.chat_id).digest('hex').slice(0, 16);
|
||||
}
|
||||
} else if (request.body.chat_completion_source === CHAT_COMPLETION_SOURCES.NANOGPT) {
|
||||
apiUrl = API_NANOGPT;
|
||||
apiKey = readSecret(request.user.directories, SECRET_KEYS.NANOGPT, request.body.secret_id);
|
||||
|
||||
Reference in New Issue
Block a user