mirror of
https://github.com/run-llama/LlamaIndexTS.git
synced 2026-07-16 07:14:29 -04:00
chore: Use base64 for encoding files (#1965)
This commit is contained in:
@@ -0,0 +1,7 @@
|
||||
---
|
||||
"@llamaindex/anthropic": patch
|
||||
"@llamaindex/openai": patch
|
||||
"@llamaindex/core": patch
|
||||
---
|
||||
|
||||
Use base64 for encoding files
|
||||
@@ -25,7 +25,7 @@ async function main() {
|
||||
},
|
||||
{
|
||||
type: "file",
|
||||
data: Uint8Array.from(fs.readFileSync("./data/manga.pdf")),
|
||||
data: fs.readFileSync("./data/manga.pdf").toString("base64"),
|
||||
mimeType: "application/pdf",
|
||||
},
|
||||
],
|
||||
|
||||
@@ -32,7 +32,7 @@ import fs from "fs";
|
||||
},
|
||||
{
|
||||
type: "file",
|
||||
data: Uint8Array.from(fs.readFileSync("./data/manga.pdf")),
|
||||
data: fs.readFileSync("./data/manga.pdf").toString("base64"),
|
||||
mimeType: "application/pdf",
|
||||
},
|
||||
],
|
||||
|
||||
@@ -26,7 +26,7 @@ import fs from "fs";
|
||||
},
|
||||
{
|
||||
type: "file",
|
||||
data: new Uint8Array(fs.readFileSync("./data/manga.pdf")),
|
||||
data: fs.readFileSync("./data/manga.pdf").toString("base64"),
|
||||
mimeType: "application/pdf",
|
||||
},
|
||||
],
|
||||
|
||||
@@ -21,7 +21,7 @@ async function main() {
|
||||
},
|
||||
{
|
||||
type: "file",
|
||||
data: Uint8Array.from(fs.readFileSync("./data/manga.pdf")),
|
||||
data: fs.readFileSync("./data/manga.pdf").toString("base64"),
|
||||
mimeType: "application/pdf",
|
||||
},
|
||||
],
|
||||
|
||||
@@ -166,28 +166,29 @@ export type MessageContentImageDetail = {
|
||||
|
||||
export type MessageContentAudioDetail = {
|
||||
type: "audio";
|
||||
//audio could be a base64 string as well
|
||||
data: string | Uint8Array;
|
||||
// this is a base64 encoded string
|
||||
data: string;
|
||||
mimeType: string;
|
||||
};
|
||||
|
||||
export type MessageContentVideoDetail = {
|
||||
type: "video";
|
||||
//video could be a base64 string as well
|
||||
data: string | Uint8Array;
|
||||
// this is a base64 encoded string
|
||||
data: string;
|
||||
mimeType: string;
|
||||
};
|
||||
|
||||
export type MessageContentImageDataDetail = {
|
||||
type: "image";
|
||||
//image could be a base64 string as well
|
||||
data: string | Uint8Array;
|
||||
// this is a base64 encoded string
|
||||
data: string;
|
||||
mimeType: string;
|
||||
};
|
||||
|
||||
export type MessageContentFileDetail = {
|
||||
type: "file";
|
||||
data: Uint8Array;
|
||||
// this is a base64 encoded string
|
||||
data: string;
|
||||
mimeType: string;
|
||||
};
|
||||
|
||||
|
||||
@@ -28,7 +28,7 @@ import type {
|
||||
} from "@llamaindex/core/llms";
|
||||
import { ToolCallLLM } from "@llamaindex/core/llms";
|
||||
import { extractText } from "@llamaindex/core/utils";
|
||||
import { getEnv, uint8ArrayToBase64 } from "@llamaindex/env";
|
||||
import { getEnv } from "@llamaindex/env";
|
||||
import { isDeepEqual } from "remeda";
|
||||
|
||||
export class AnthropicSession {
|
||||
@@ -342,7 +342,7 @@ export class Anthropic extends ToolCallLLM<
|
||||
source: {
|
||||
type: "base64" as const,
|
||||
media_type: content.mimeType,
|
||||
data: uint8ArrayToBase64(content.data),
|
||||
data: content.data,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
@@ -178,7 +178,7 @@ describe("Message Formatting", () => {
|
||||
{
|
||||
type: "file",
|
||||
mimeType: "application/pdf",
|
||||
data: pdfBuffer,
|
||||
data: pdfBuffer.toString("base64"),
|
||||
},
|
||||
],
|
||||
role: "user",
|
||||
|
||||
@@ -13,7 +13,7 @@ import {
|
||||
type ToolCallLLMMessageOptions,
|
||||
} from "@llamaindex/core/llms";
|
||||
import { extractText } from "@llamaindex/core/utils";
|
||||
import { getEnv, uint8ArrayToBase64 } from "@llamaindex/env";
|
||||
import { getEnv } from "@llamaindex/env";
|
||||
import { Tokenizers } from "@llamaindex/env/tokenizers";
|
||||
import type {
|
||||
ClientOptions as OpenAIClientOptions,
|
||||
@@ -206,7 +206,7 @@ export class OpenAI extends ToolCallLLM<OpenAIAdditionalChatOptions> {
|
||||
if (item.mimeType !== "application/pdf") {
|
||||
throw new Error("Only PDF files are supported");
|
||||
}
|
||||
const base64Data = uint8ArrayToBase64(item.data);
|
||||
const base64Data = item.data;
|
||||
return {
|
||||
type: "file",
|
||||
file: {
|
||||
|
||||
@@ -18,7 +18,7 @@ import {
|
||||
} from "@llamaindex/core/llms";
|
||||
import type { StoredValue } from "@llamaindex/core/schema";
|
||||
import { extractText } from "@llamaindex/core/utils";
|
||||
import { getEnv, uint8ArrayToBase64 } from "@llamaindex/env";
|
||||
import { getEnv } from "@llamaindex/env";
|
||||
|
||||
import { wrapEventCaller } from "@llamaindex/core/decorator";
|
||||
import { Tokenizers } from "@llamaindex/env/tokenizers";
|
||||
@@ -694,7 +694,7 @@ export class OpenAIResponses extends ToolCallLLM<OpenAIResponsesChatOptions> {
|
||||
);
|
||||
}
|
||||
|
||||
const base64Data = uint8ArrayToBase64(item.data);
|
||||
const base64Data = item.data;
|
||||
return {
|
||||
type: "input_file",
|
||||
filename: `part-${index}.pdf`,
|
||||
|
||||
@@ -141,7 +141,7 @@ describe("OpenAI Static Methods", () => {
|
||||
});
|
||||
|
||||
it("should convert user messages with file content", () => {
|
||||
const pdfBuffer = Buffer.from("test PDF content");
|
||||
const pdfBuffer = Buffer.from("test PDF content").toString("base64");
|
||||
const messages: ChatMessage<ToolCallLLMMessageOptions>[] = [
|
||||
{
|
||||
role: "user",
|
||||
@@ -163,7 +163,7 @@ describe("OpenAI Static Methods", () => {
|
||||
{
|
||||
type: "file",
|
||||
file: {
|
||||
file_data: `data:application/pdf;base64,${pdfBuffer.toString("base64")}`,
|
||||
file_data: `data:application/pdf;base64,${pdfBuffer}`,
|
||||
filename: "part-0.pdf",
|
||||
},
|
||||
},
|
||||
@@ -173,7 +173,7 @@ describe("OpenAI Static Methods", () => {
|
||||
});
|
||||
|
||||
it("should convert user messages with mixed content", () => {
|
||||
const pdfBuffer = Buffer.from("test PDF content");
|
||||
const pdfBuffer = Buffer.from("test PDF content").toString("base64");
|
||||
const messages: ChatMessage<ToolCallLLMMessageOptions>[] = [
|
||||
{
|
||||
role: "user",
|
||||
@@ -203,7 +203,7 @@ describe("OpenAI Static Methods", () => {
|
||||
{
|
||||
type: "file",
|
||||
file: {
|
||||
file_data: `data:application/pdf;base64,${pdfBuffer.toString("base64")}`,
|
||||
file_data: `data:application/pdf;base64,${pdfBuffer}`,
|
||||
filename: "part-1.pdf",
|
||||
},
|
||||
},
|
||||
|
||||
@@ -183,7 +183,7 @@ describe("OpenAIResponses Unit Tests", () => {
|
||||
});
|
||||
|
||||
it("should process file content with PDF type", () => {
|
||||
const pdfBuffer = Buffer.from("test PDF content");
|
||||
const pdfBuffer = Buffer.from("test PDF content").toString("base64");
|
||||
const content = [
|
||||
{
|
||||
type: "file",
|
||||
@@ -196,7 +196,7 @@ describe("OpenAIResponses Unit Tests", () => {
|
||||
expect(result[0]).toEqual({
|
||||
type: "input_file",
|
||||
filename: "part-0.pdf",
|
||||
file_data: `data:application/pdf;base64,${pdfBuffer.toString("base64")}`,
|
||||
file_data: `data:application/pdf;base64,${pdfBuffer}`,
|
||||
});
|
||||
});
|
||||
|
||||
@@ -205,7 +205,7 @@ describe("OpenAIResponses Unit Tests", () => {
|
||||
{
|
||||
type: "file",
|
||||
mimeType: "image/jpeg",
|
||||
data: Uint8Array.from(Buffer.from("test image content")),
|
||||
data: Buffer.from("test image content").toString("base64"),
|
||||
},
|
||||
];
|
||||
// @ts-expect-error accessing private method
|
||||
|
||||
Reference in New Issue
Block a user