📄 feat: Context Field for Anthropic Documents (PDF) (#10148)

* fix: Remove ephemeral cache control from document encoding function

* refactor: Improve document encoding types and add file context for anthropic messages api

- Added AnthropicDocumentBlock interface to define the structure for documents from the Anthropic provider.
- Updated encodeAndFormatDocuments function to utilize the new type and include optional context for filenames.
- Refactored DocumentResult to use a union type for various document formats, improving type safety and clarity.
This commit is contained in:
Danny Avila 2025-10-16 23:24:14 +03:00 committed by GitHub
parent bc77bbd1ba
commit f59daaeecc
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 53 additions and 26 deletions

View file

@ -2,7 +2,7 @@ import { Providers } from '@librechat/agents';
import { isOpenAILikeProvider, isDocumentSupportedProvider } from 'librechat-data-provider';
import type { IMongoFile } from '@librechat/data-schemas';
import type { Request } from 'express';
import type { StrategyFunctions, DocumentResult } from '~/types/files';
import type { StrategyFunctions, DocumentResult, AnthropicDocumentBlock } from '~/types/files';
import { validatePdf } from '~/files/validation';
import { getFileStream } from './utils';
@ -69,16 +69,21 @@ export async function encodeAndFormatDocuments(
}
if (provider === Providers.ANTHROPIC) {
result.documents.push({
const document: AnthropicDocumentBlock = {
type: 'document',
source: {
type: 'base64',
media_type: 'application/pdf',
data: content,
},
cache_control: { type: 'ephemeral' },
citations: { enabled: true },
});
};
if (file.filename) {
document.context = `File: "${file.filename}"`;
}
result.documents.push(document);
} else if (useResponsesApi) {
result.documents.push({
type: 'input_file',

View file

@ -46,29 +46,51 @@ export interface VideoResult {
}>;
}
/** Anthropic document block format */
export interface AnthropicDocumentBlock {
type: 'document';
source: {
type: string;
media_type: string;
data: string;
};
context?: string;
title?: string;
cache_control?: { type: string };
citations?: { enabled: boolean };
}
/** Google document block format */
export interface GoogleDocumentBlock {
type: 'document';
mimeType: string;
data: string;
}
/** OpenAI file block format */
export interface OpenAIFileBlock {
type: 'file';
file: {
filename: string;
file_data: string;
};
}
/** OpenAI Responses API file format */
export interface OpenAIInputFileBlock {
type: 'input_file';
filename: string;
file_data: string;
}
export type DocumentBlock =
| AnthropicDocumentBlock
| GoogleDocumentBlock
| OpenAIFileBlock
| OpenAIInputFileBlock;
export interface DocumentResult {
documents: Array<{
type: 'document' | 'file' | 'input_file';
/** Anthropic File Format, `document` */
source?: {
type: string;
media_type: string;
data: string;
};
cache_control?: { type: string };
citations?: { enabled: boolean };
/** Google File Format, `document` */
mimeType?: string;
data?: string;
/** OpenAI File Format, `file` */
file?: {
filename?: string;
file_data?: string;
};
/** OpenAI Responses API File Format, `input_file` */
filename?: string;
file_data?: string;
}>;
documents: DocumentBlock[];
files: Array<{
file_id?: string;
temp_file_id?: string;