Skip to content

Commit

Permalink
PDF support for claude sonnet 3.5
Browse files Browse the repository at this point in the history
Added support for attaching documents to claude 3.5
  • Loading branch information
alex-torregrosa committed Dec 28, 2024
1 parent 9779431 commit a7cc8b0
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
21 changes: 17 additions & 4 deletions api/app/clients/AnthropicClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -349,10 +349,16 @@ class AnthropicClient extends BaseClient {
if (this.options.attachments) {
const attachments = await this.options.attachments;
const images = attachments.filter((file) => file.type.includes('image'));
const documents = attachments.filter((file) => file.type == 'application/pdf');

if (images.length && !this.isVisionModel) {
throw new Error('Images are only supported with the Claude 3 family of models');
}
if (documents.length && !this.modelOptions.model.includes('3-5-sonnet')) {
throw new Error(
'PDF documents are only supported with the Claude 3.5 Sonnet family of models',
);
}

const latestMessage = orderedMessages[orderedMessages.length - 1];

Expand Down Expand Up @@ -402,10 +408,17 @@ class AnthropicClient extends BaseClient {
continue;
}

orderedMessages[i].tokenCount += this.calculateImageTokenCost({
width: file.width,
height: file.height,
});
if (file.type.includes('image')) {
orderedMessages[i].tokenCount += this.calculateImageTokenCost({
width: file.width,
height: file.height,
});
} else {
// File is a pdf.
// A reasonable estimate is 1500-3000 tokens per page
// without parsing the pdf to get the page count, assume it has one.
orderedMessages[i].tokenCount += 2000;
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion api/server/services/Files/encode.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ async function encodeAndFormat(req, files, endpoint, mode) {
} else if (validEndpoint && endpoint === EModelEndpoint.google) {
filePart.image_url = dataURL;
} else if (validEndpoint && endpoint === EModelEndpoint.anthropic) {
filePart.type = 'image';
filePart.type = file.type.includes('image') ? 'image' : 'document';
filePart.source = {
type: 'base64',
media_type: file.type,
Expand Down

0 comments on commit a7cc8b0

Please sign in to comment.