Skip to content
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

In-context document support for Anthropic and Google models #5130

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Prev Previous commit
Next Next commit
PDF support for claude sonnet 3.5
Added support for attaching documents to claude 3.5
  • Loading branch information
alex-torregrosa committed Jan 11, 2025
commit f85bbf3606256016f2e03e5825f025fccf72a66d
21 changes: 17 additions & 4 deletions api/app/clients/AnthropicClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -346,10 +346,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 @@ -399,10 +405,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