Skip to content

anthropic pdf #1074

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 61 additions & 1 deletion src/providers/anthropic/chatComplete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,40 @@ interface AnthropicTextContentItem {
text: string;
}

interface AnthropicUrlPdfContentItem {
type: string;
source: {
type: string;
url: string;
};
}

interface AnthropicBase64PdfContentItem {
type: string;
source: {
type: string;
data: string;
media_type: string;
};
}

interface AnthropicPlainTextContentItem {
type: string;
source: {
type: string;
data: string;
media_type: string;
};
}

type AnthropicMessageContentItem =
| AnthropicToolResultContentItem
| AnthropicBase64ImageContentItem
| AnthropicUrlImageContentItem
| AnthropicTextContentItem;
| AnthropicTextContentItem
| AnthropicUrlPdfContentItem
| AnthropicBase64PdfContentItem
| AnthropicPlainTextContentItem;

interface AnthropicMessage extends Message, PromptCache {
content: AnthropicMessageContentItem[];
Expand Down Expand Up @@ -166,6 +195,35 @@ const transformAndAppendImageContentItem = (
}
};

const transformAndAppendFileContentItem = (
item: ContentType,
transformedMessage: AnthropicMessage
) => {
const mimeType =
(item.file?.mime_type as keyof typeof fileExtensionMimeTypeMap) ||
fileExtensionMimeTypeMap.pdf;
if (item.file?.file_url) {
transformedMessage.content.push({
type: 'document',
source: {
type: 'url',
url: item.file.file_url,
},
});
} else if (item.file?.file_data) {
const contentType =
mimeType === fileExtensionMimeTypeMap.txt ? 'text' : 'base64';
transformedMessage.content.push({
type: 'document',
source: {
type: contentType,
data: item.file.file_data,
media_type: mimeType,
},
});
}
};

export const AnthropicChatCompleteConfig: ProviderConfig = {
model: {
param: 'model',
Expand Down Expand Up @@ -205,6 +263,8 @@ export const AnthropicChatCompleteConfig: ProviderConfig = {
});
} else if (item.type === 'image_url') {
transformAndAppendImageContentItem(item, transformedMessage);
} else if (item.type === 'file') {
transformAndAppendFileContentItem(item, transformedMessage);
}
});
messages.push(transformedMessage as AnthropicMessage);
Expand Down
38 changes: 36 additions & 2 deletions src/providers/bedrock/chatComplete.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { BEDROCK, documentMimeTypes, imagesMimeTypes } from '../../globals';
import {
BEDROCK,
documentMimeTypes,
fileExtensionMimeTypeMap,
imagesMimeTypes,
} from '../../globals';
import {
Message,
Params,
Expand Down Expand Up @@ -183,6 +188,32 @@ const getMessageContent = (message: Message) => {
},
});
}
} else if (item.type === 'file') {
const mimeType = item.file?.mime_type || fileExtensionMimeTypeMap.pdf;
const fileFormat = mimeType.split('/')[1];
if (item.file?.file_url) {
out.push({
document: {
format: fileFormat,
name: item.file.file_name || crypto.randomUUID(),
source: {
s3Location: {
uri: item.file.file_url,
},
},
},
});
} else if (item.file?.file_data) {
out.push({
document: {
format: fileFormat,
name: item.file.file_name || crypto.randomUUID(),
source: {
bytes: item.file.file_data,
},
},
});
}
}

if (item.cache_control) {
Expand Down Expand Up @@ -402,7 +433,10 @@ type BedrockContentItem = {
format: string;
name: string;
source: {
bytes: string;
bytes?: string;
s3Location?: {
uri: string;
};
};
};
cachePoint?: {
Expand Down
12 changes: 12 additions & 0 deletions src/types/requestBody.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@ export interface Config {
}

/**
* TODO: make this a union type
* A message content type.
* @interface
*/
Expand All @@ -230,6 +231,17 @@ export interface ContentType extends PromptCache {
mime_type?: string;
};
data?: string;
file?: {
file_data?: string;
file_id?: string;
file_name?: string;
file_url?: string;
mime_type?: string;
};
input_audio?: {
data: string;
format: string; //defaults to auto
};
}

export interface ToolCall {
Expand Down