generative part is in blob

This commit is contained in:
buzz-lightsnack-2007 2024-04-14 18:59:23 +08:00
parent 2809a15738
commit a61bdac6b6

View file

@ -41,8 +41,7 @@ export default class gemini {
/* Ask Google Gemini. /* Ask Google Gemini.
@param {object} prompt the prompts; may accept a string to be converted to an object @param {object} prompt the prompts; may accept a string to be converted to an object; images should already be blob
@param {object} images the images
@param {boolean} continued whether to continue the existing prompt @param {boolean} continued whether to continue the existing prompt
*/ */
async generate(prompt, safetySettings, generationConfig, continued = false) { async generate(prompt, safetySettings, generationConfig, continued = false) {
@ -66,19 +65,17 @@ export default class gemini {
REQUEST[`contents`] = []; REQUEST[`contents`] = [];
// Function below by Google (https://ai.google.dev/tutorials/get_started_web) // Function below by Google (https://ai.google.dev/tutorials/get_started_web)
async function fileToGenerativePart(file) { async function fileToGenerativePart(image) {
if (typeof FileReader != 'undefined') { let image = {"blob": image};
const base64EncodedDataPromise = new Promise((resolve) => { image[`type`] = image[`blob`].type;
const reader = new FileReader();
reader.onloadend = () => resolve(reader.result.split(',')[1]); const reader = new FileReader();
reader.readAsDataURL(file); image[`base64`] = await new Promise((resolve) => {
}); reader.onloadend = () => resolve(reader.result.split(',')[1]);
return { reader.readAsDataURL(image[`blob`]);
inlineData: { data: await base64EncodedDataPromise, mimeType: file.type }, });
};
} else { return {inlineData: { data: image[`base64`], mimeType: image[`type`] }};
return null;
}
}; };
while (REQUEST[`contents`].length < PROMPT.length) { while (REQUEST[`contents`].length < PROMPT.length) {
@ -95,7 +92,7 @@ export default class gemini {
PROMPT[REQUEST[`contents`].length][`images`] = [PROMPT[REQUEST[`contents`].length][`images`]]; PROMPT[REQUEST[`contents`].length][`images`] = [PROMPT[REQUEST[`contents`].length][`images`]];
} }
// Add the photos. // Add the photos, which are already in the blob format.
while ((PROMPT[REQUEST[`contents`].length][`images`]) ? (MESSAGE[`parts`].length < PROMPT[REQUEST[`contents`].length][`images`].length) : false) { while ((PROMPT[REQUEST[`contents`].length][`images`]) ? (MESSAGE[`parts`].length < PROMPT[REQUEST[`contents`].length][`images`].length) : false) {
let MESSAGE_IMAGE = await fileToGenerativePart(PROMPT[REQUEST[`contents`].length][`images`][MESSAGE[`parts`].length]); let MESSAGE_IMAGE = await fileToGenerativePart(PROMPT[REQUEST[`contents`].length][`images`][MESSAGE[`parts`].length]);
if (MESSAGE_IMAGE) { if (MESSAGE_IMAGE) {