mirror of
https://github.com/danny-avila/LibreChat.git
synced 2025-12-18 09:20:15 +01:00
📄 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:
parent
bc77bbd1ba
commit
f59daaeecc
2 changed files with 53 additions and 26 deletions
|
|
@ -2,7 +2,7 @@ import { Providers } from '@librechat/agents';
|
||||||
import { isOpenAILikeProvider, isDocumentSupportedProvider } from 'librechat-data-provider';
|
import { isOpenAILikeProvider, isDocumentSupportedProvider } from 'librechat-data-provider';
|
||||||
import type { IMongoFile } from '@librechat/data-schemas';
|
import type { IMongoFile } from '@librechat/data-schemas';
|
||||||
import type { Request } from 'express';
|
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 { validatePdf } from '~/files/validation';
|
||||||
import { getFileStream } from './utils';
|
import { getFileStream } from './utils';
|
||||||
|
|
||||||
|
|
@ -69,16 +69,21 @@ export async function encodeAndFormatDocuments(
|
||||||
}
|
}
|
||||||
|
|
||||||
if (provider === Providers.ANTHROPIC) {
|
if (provider === Providers.ANTHROPIC) {
|
||||||
result.documents.push({
|
const document: AnthropicDocumentBlock = {
|
||||||
type: 'document',
|
type: 'document',
|
||||||
source: {
|
source: {
|
||||||
type: 'base64',
|
type: 'base64',
|
||||||
media_type: 'application/pdf',
|
media_type: 'application/pdf',
|
||||||
data: content,
|
data: content,
|
||||||
},
|
},
|
||||||
cache_control: { type: 'ephemeral' },
|
|
||||||
citations: { enabled: true },
|
citations: { enabled: true },
|
||||||
});
|
};
|
||||||
|
|
||||||
|
if (file.filename) {
|
||||||
|
document.context = `File: "${file.filename}"`;
|
||||||
|
}
|
||||||
|
|
||||||
|
result.documents.push(document);
|
||||||
} else if (useResponsesApi) {
|
} else if (useResponsesApi) {
|
||||||
result.documents.push({
|
result.documents.push({
|
||||||
type: 'input_file',
|
type: 'input_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 {
|
export interface DocumentResult {
|
||||||
documents: Array<{
|
documents: DocumentBlock[];
|
||||||
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;
|
|
||||||
}>;
|
|
||||||
files: Array<{
|
files: Array<{
|
||||||
file_id?: string;
|
file_id?: string;
|
||||||
temp_file_id?: string;
|
temp_file_id?: string;
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue