generative part is in blob
This commit is contained in:
parent
2809a15738
commit
a61bdac6b6
1 changed files with 13 additions and 16 deletions
|
@ -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();
|
const reader = new FileReader();
|
||||||
|
image[`base64`] = await new Promise((resolve) => {
|
||||||
reader.onloadend = () => resolve(reader.result.split(',')[1]);
|
reader.onloadend = () => resolve(reader.result.split(',')[1]);
|
||||||
reader.readAsDataURL(file);
|
reader.readAsDataURL(image[`blob`]);
|
||||||
});
|
});
|
||||||
return {
|
|
||||||
inlineData: { data: await base64EncodedDataPromise, mimeType: file.type },
|
return {inlineData: { data: image[`base64`], mimeType: image[`type`] }};
|
||||||
};
|
|
||||||
} else {
|
|
||||||
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) {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue