Replies: 1 comment 2 replies
-
Hello, @g1henx! I'm here to assist you with any issues or questions you may have. I can help you solve bugs, answer questions, and even guide you on how to become a contributor. Let's tackle this together! To handle Google GenAI files in your
Here's the updated code: import { HumanMessage, AIMessage, SystemMessage } from "@langchain/core/messages";
import fs from "fs";
import path from "path";
export function messageToLangchain(message: MessageType, provider: string, files: FileType[]) {
if (message.sender === 'ai') {
return new AIMessage({
content: 'text',
});
}
let messageText = message.text;
const content: any[] = [];
if (message.files) {
for (const getFile of message.files) {
const file = files.find((f) => f.name === getFile.name);
if (!file) {
continue;
}
// Convert file to base64
const fileData = fs.readFileSync(path.join(__dirname, file.data)).toString("base64");
if (provider === 'google') {
content.push({
type: "fileData",
mimeType: file.mime_type,
fileUri: `data:${file.mime_type};base64,${fileData}`,
});
} else if (provider === 'anthropic' || provider === 'openai') {
if (file.mime_type && file.mime_type.startsWith('image')) {
content.push({
type: "image_url",
image_url: `data:${file.mime_type};base64,${fileData}`,
});
} else {
messageText += `
Transcriptions of the attached files:
-------------- ${file.name} --------------
${fileData}
-----------------------------------------
`;
}
}
}
}
content.push({
type: "text",
text: messageText,
});
if (message.sender === 'human') {
return new HumanMessage({
content: content,
});
}
return new SystemMessage({
content: content,
});
} Key Changes:
This approach ensures that the files are correctly processed and included in the message content for Google GenAI. For more details on handling multimodal inputs, refer to the LangChain.js documentation [1]. |
Beta Was this translation helpful? Give feedback.
-
Checked other resources
Commit to Help
Example Code
Description
How am i supposed to input google genai files to the message?
System Info
langchain@0.2.11 | MIT | deps: 16 | versions: 283
Typescript bindings for langchain
https://github.com/langchain-ai/langchainjs/tree/main/langchain/
keywords: llm, ai, gpt3, chain, prompt, prompt engineering, chatgpt, machine learning, ml, openai, embeddings, vectorstores
dist
.tarball: https://registry.npmjs.org/langchain/-/langchain-0.2.11.tgz
.shasum: d97e5bbd57e8954f21356fd85603aa39e3efe03f
.integrity: sha512-6FQWKNAXuTmwuhHHMOmurLo8pydSRu5C/FwCYvYbR4ulCLqcsj+jre/kfXvA5BdHOZHNo6oQn0/5kxDNnhxMUA==
.unpackedSize: 5.4 MB
dependencies:
@langchain/core: >=0.2.11 <0.3.0 jsonpointer: ^5.0.1 uuid: ^10.0.0
@langchain/openai: >=0.1.0 <0.3.0 langchainhub: ~0.0.8 yaml: ^2.2.1
@langchain/textsplitters: ~0.0.0 langsmith: ~0.1.30 zod-to-json-schema: ^3.22.3
binary-extensions: ^2.2.0 ml-distance: ^4.0.0 zod: ^3.22.4
js-tiktoken: ^1.0.12 openapi-types: ^12.1.3
js-yaml: ^4.1.0 p-retry: 4
maintainers:
dist-tags:
latest: 0.2.11 next: 0.2.3-rc.0
published 2 days ago by basproul braceasproul@gmail.com
Node.js v18.17.1
Beta Was this translation helpful? Give feedback.
All reactions