Skip to content

Commit

Permalink
feat(anthropic): Add Anthropic PDF support (document type) in invoke (#…
Browse files Browse the repository at this point in the history
…7496)

Co-authored-by: jacoblee93 <[email protected]>
  • Loading branch information
adhambadr and jacoblee93 authored Jan 18, 2025
1 parent 1546a2e commit 94467fa
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 0 deletions.
52 changes: 52 additions & 0 deletions examples/src/models/chat/integration_anthropic_pdf.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { ChatAnthropic } from "@langchain/anthropic";

import * as fs from "fs";

export const run = async () => {
const llm = new ChatAnthropic({
model: "claude-3-5-sonnet-20240620", // Only claude-3-5-sonnet-20240620 , claude-3-5-sonnet-20241022 as of Jan 2025 support PDF documents as in base64
});

// PDF needs to be in Base64.
const getLocalFile = async (path: string) => {
const localFile = await fs.readFileSync(path);
const base64File = localFile.toString("base64");
return base64File;
};

// Or remotely
const getRemoteFile = async (url: string) => {
const response = await fetch(url);
const arrayBuffer = await response.arrayBuffer();
const base64File = Buffer.from(arrayBuffer).toString("base64");
return base64File;
};

const base64 = await getRemoteFile(
"https://www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf"
);

const prompt = "Summarise the contents of this PDF";

const response = await llm.invoke([
{
role: "user",
content: [
{
type: "text",
text: prompt,
},
{
type: "document",
source: {
media_type: "application/pdf",
type: "base64",
data: base64,
},
},
],
},
]);
console.log(response.content);
return response.content;
};
7 changes: 7 additions & 0 deletions libs/langchain-anthropic/src/utils/message_inputs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,13 @@ function _formatContent(content: MessageContent) {
source,
...(cacheControl ? { cache_control: cacheControl } : {}),
};
} else if (contentPart.type === "document") {
// PDF
return {
type: "document",
source: contentPart.source,
...(cacheControl ? { cache_control: cacheControl } : {}),
};
} else if (
textTypes.find((t) => t === contentPart.type) &&
"text" in contentPart
Expand Down

0 comments on commit 94467fa

Please sign in to comment.