mirror of
https://github.com/danny-avila/LibreChat.git
synced 2026-07-11 16:53:45 +00:00
feat: calibration ratio
This commit is contained in:
parent
ef7f9f1b14
commit
b8bdb0d0fc
6 changed files with 64 additions and 0 deletions
|
|
@ -855,6 +855,10 @@ class BaseClient {
|
|||
}
|
||||
}
|
||||
|
||||
if (this.contextMeta) {
|
||||
responseMessage.contextMeta = this.contextMeta;
|
||||
}
|
||||
|
||||
responseMessage.databasePromise = this.saveMessageToDatabase(
|
||||
responseMessage,
|
||||
saveOptions,
|
||||
|
|
|
|||
|
|
@ -334,6 +334,14 @@ class AgentClient extends BaseClient {
|
|||
/** Preserve canonical pre-format token counts for all history entering graph formatting */
|
||||
this.indexTokenCountMap = canonicalTokenCountMap;
|
||||
|
||||
/** Extract contextMeta from the parent response (second-to-last in ordered chain;
|
||||
* last is the current user message). Seeds the pruner's calibration EMA for this run. */
|
||||
const parentResponse =
|
||||
orderedMessages.length >= 2 ? orderedMessages[orderedMessages.length - 2] : undefined;
|
||||
if (parentResponse?.contextMeta && !parentResponse.isCreatedByUser) {
|
||||
this.contextMeta = parentResponse.contextMeta;
|
||||
}
|
||||
|
||||
const result = {
|
||||
tokenCountMap,
|
||||
prompt: payload,
|
||||
|
|
@ -807,11 +815,26 @@ class AgentClient extends BaseClient {
|
|||
|
||||
memoryPromise = this.runMemory(messages);
|
||||
|
||||
/** Seed calibration ratio from previous run if encoding matches */
|
||||
const currentEncoding = this.getEncoding();
|
||||
const prevMeta = this.contextMeta;
|
||||
const calibrationRatio =
|
||||
prevMeta?.encoding === currentEncoding && prevMeta?.calibrationRatio > 0
|
||||
? prevMeta.calibrationRatio
|
||||
: undefined;
|
||||
|
||||
if (prevMeta) {
|
||||
logger.debug(
|
||||
`[AgentClient] contextMeta from parent: ratio=${prevMeta.calibrationRatio}, encoding=${prevMeta.encoding}, current=${currentEncoding}, seeded=${calibrationRatio ?? 'none'}`,
|
||||
);
|
||||
}
|
||||
|
||||
run = await createRun({
|
||||
agents,
|
||||
messages,
|
||||
indexTokenCountMap,
|
||||
initialSummary,
|
||||
calibrationRatio,
|
||||
runId: this.responseMessageId,
|
||||
signal: abortController.signal,
|
||||
customHandlers: this.options.eventHandlers,
|
||||
|
|
@ -849,6 +872,21 @@ class AgentClient extends BaseClient {
|
|||
|
||||
const hideSequentialOutputs = config.configurable.hide_sequential_outputs;
|
||||
await runAgents(initialMessages);
|
||||
|
||||
/** Capture calibration ratio from the completed run for persistence on the response message */
|
||||
if (this.run) {
|
||||
const ratio = this.run.getCalibrationRatio();
|
||||
if (ratio > 0 && ratio !== 1) {
|
||||
this.contextMeta = {
|
||||
calibrationRatio: Math.round(ratio * 1000) / 1000,
|
||||
encoding: this.getEncoding(),
|
||||
};
|
||||
logger.debug(
|
||||
`[AgentClient] contextMeta to persist: ratio=${this.contextMeta.calibrationRatio}, encoding=${this.contextMeta.encoding}`,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/** @deprecated Agent Chain */
|
||||
if (hideSequentialOutputs) {
|
||||
this.contentParts = this.contentParts.filter((part, index) => {
|
||||
|
|
|
|||
|
|
@ -209,6 +209,7 @@ export async function createRun({
|
|||
indexTokenCountMap,
|
||||
summarizationConfig,
|
||||
initialSummary,
|
||||
calibrationRatio,
|
||||
streaming = true,
|
||||
streamUsage = true,
|
||||
}: {
|
||||
|
|
@ -224,6 +225,8 @@ export async function createRun({
|
|||
summarizationConfig?: SummarizationConfig;
|
||||
/** Cross-run summary from formatAgentMessages, forwarded to AgentContext */
|
||||
initialSummary?: { text: string; tokenCount: number };
|
||||
/** Calibration ratio from previous run's contextMeta, seeds the pruner EMA */
|
||||
calibrationRatio?: number;
|
||||
} & Pick<RunConfig, 'tokenCounter' | 'customHandlers' | 'indexTokenCountMap'>): Promise<
|
||||
Run<IState>
|
||||
> {
|
||||
|
|
@ -421,5 +424,6 @@ export async function createRun({
|
|||
tokenCounter,
|
||||
customHandlers,
|
||||
indexTokenCountMap,
|
||||
calibrationRatio,
|
||||
});
|
||||
}
|
||||
|
|
|
|||
|
|
@ -630,6 +630,12 @@ export const tMessageSchema = z.object({
|
|||
feedback: feedbackSchema.optional(),
|
||||
/** metadata */
|
||||
metadata: z.record(z.unknown()).optional(),
|
||||
contextMeta: z
|
||||
.object({
|
||||
calibrationRatio: z.number().optional(),
|
||||
encoding: z.string().optional(),
|
||||
})
|
||||
.optional(),
|
||||
});
|
||||
|
||||
export type MemoryArtifact = {
|
||||
|
|
|
|||
|
|
@ -114,6 +114,14 @@ const messageSchema: Schema<IMessage> = new Schema(
|
|||
type: String,
|
||||
},
|
||||
metadata: { type: mongoose.Schema.Types.Mixed },
|
||||
contextMeta: {
|
||||
type: {
|
||||
calibrationRatio: { type: Number },
|
||||
encoding: { type: String },
|
||||
},
|
||||
_id: false,
|
||||
default: undefined,
|
||||
},
|
||||
attachments: { type: [{ type: mongoose.Schema.Types.Mixed }], default: undefined },
|
||||
/*
|
||||
attachments: {
|
||||
|
|
|
|||
|
|
@ -39,6 +39,10 @@ export interface IMessage extends Document {
|
|||
iconURL?: string;
|
||||
addedConvo?: boolean;
|
||||
metadata?: Record<string, unknown>;
|
||||
contextMeta?: {
|
||||
calibrationRatio?: number;
|
||||
encoding?: string;
|
||||
};
|
||||
attachments?: unknown[];
|
||||
expiredAt?: Date | null;
|
||||
createdAt?: Date;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue