Skip to content

Commit

Permalink
Add image data to last human message.
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewnguonly committed Mar 13, 2024
1 parent 4d03921 commit 8258c51
Showing 1 changed file with 23 additions and 10 deletions.
33 changes: 23 additions & 10 deletions src/scripts/background.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Document } from "@langchain/core/documents";
import { AIMessage, BaseMessage, HumanMessage } from "@langchain/core/messages";
import { AIMessage, BaseMessage, HumanMessage, MessageContent } from "@langchain/core/messages";
import { StringOutputParser } from "@langchain/core/output_parsers";
import {
ChatPromptTemplate,
Expand Down Expand Up @@ -121,19 +121,32 @@ const getMessages = async (
});
});

// add images to the chat
base64EncodedImages.forEach((image) => {
// add images to the content array
if (base64EncodedImages.length > 0) {
// get the last element (current user prompt) from chatMsgs
const lastMsg = chatMsgs[chatMsgs.length - 1];

// remove the last element from chatMsgs
chatMsgs = chatMsgs.slice(0, chatMsgs.length - 1);

const content: MessageContent = [{
type: "text",
text: lastMsg.content.toString(),
}];
base64EncodedImages.forEach((image) => {
content.push({
type: "image_url",
image_url: `data:image/*;base64,${image}`,
});
});

// replace the last element with a new HumanMessage that contains the image content
chatMsgs.push(
new HumanMessage({
content: [
{
type: "image_url",
image_url: `data:image/*;base64,${image}`,
},
],
content: content,
}),
);
});
}
}
return chatMsgs;
};
Expand Down

0 comments on commit 8258c51

Please sign in to comment.