@onepunya/ez-gemini
Advanced tools
Comparing version 1.0.7-saitekina to 1.0.8-saitekina
@@ -1,58 +0,73 @@ | ||
import axios from "axios"; | ||
import fetch from "node-fetch"; | ||
import jimp from "jimp"; | ||
const defaultApiKey = "AIzaSyAY8DjFZHICDZ-TeHNN6lnEFoB-qczmXxE"; | ||
import axios from 'axios'; | ||
import fetch from 'node-fetch'; | ||
import jimp from 'jimp'; | ||
const defaultApiKey = 'AIzaSyAY8DjFZHICDZ-TeHNN6lnEFoB-qczmXxE'; | ||
class Gemini { | ||
constructor(e = defaultApiKey) { | ||
this.apiKey = e | ||
} | ||
async pro(e) { | ||
const t = `https://generativelanguage.googleapis.com/v1beta/models/gemini-pro:generateContent?key=${this.apiKey}`, | ||
a = { | ||
contents: [{ | ||
parts: [{ | ||
text: e | ||
}] | ||
}] | ||
}; | ||
return (await axios.post(t, a, { | ||
headers: { | ||
"Content-Type": "application/json" | ||
} | ||
})).data.candidates[0].content.parts[0].text | ||
} | ||
async vision(e, t) { | ||
const a = await this.bufferlah(e), | ||
i = { | ||
contents: [{ | ||
parts: [{ | ||
text: t | ||
}, { | ||
inline_data: { | ||
mime_type: "image/jpeg", | ||
data: (await this.resize(a)).toString("base64") | ||
} | ||
}] | ||
}] | ||
}, | ||
n = await fetch(`https://generativelanguage.googleapis.com/v1beta/models/gemini-pro-vision:generateContent?key=${this.apiKey}`, { | ||
method: "POST", | ||
headers: { | ||
"Content-Type": "application/json" | ||
}, | ||
body: JSON.stringify(i) | ||
}); | ||
return (await n.json()).candidates[0].content.parts[0].text | ||
} | ||
async bufferlah(e) { | ||
const t = await axios.get(e, { | ||
responseType: "arraybuffer" | ||
}); | ||
return Buffer.from(t.data, "binary") | ||
} | ||
async resize(e) { | ||
const t = await jimp.read(e); | ||
return await t.resize(512, 512).getBufferAsync(jimp.MIME_JPEG) | ||
} | ||
constructor(apiKey = defaultApiKey) { | ||
this.apiKey = apiKey; | ||
} | ||
async pro(inputText) { | ||
const url = `https://generativelanguage.googleapis.com/v1beta/models/gemini-pro:generateContent?key=${this.apiKey}`; | ||
const headers = { | ||
'Content-Type': 'application/json' | ||
}; | ||
const data = { | ||
contents: [{ | ||
parts: [{ | ||
text: inputText | ||
}] | ||
}] | ||
}; | ||
const response = await axios.post(url, data, { headers }); | ||
return response.data.candidates[0].content.parts[0].text; | ||
} | ||
async vision(imageUrl, inputText) { | ||
const buff = await this.bufferlah(imageUrl); | ||
const resizedBuff = await this.resize(buff); | ||
const requestBody = { | ||
"contents": [ | ||
{ | ||
"parts": [ | ||
{"text": inputText}, | ||
{ | ||
"inline_data": { | ||
"mime_type": "image/jpeg", | ||
"data": resizedBuff.toString('base64') | ||
} | ||
} | ||
] | ||
} | ||
] | ||
}; | ||
const response = await fetch(`https://generativelanguage.googleapis.com/v1beta/models/gemini-pro-vision:generateContent?key=${this.apiKey}`, { | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/json' | ||
}, | ||
body: JSON.stringify(requestBody) | ||
}); | ||
const data = await response.json(); | ||
return data.candidates[0].content.parts[0].text; | ||
} | ||
async bufferlah(hm) { | ||
const response = await axios.get(hm, { responseType: 'arraybuffer' }); | ||
const buffer = Buffer.from(response.data, 'binary'); | ||
return buffer; | ||
} | ||
async resize(buffer) { | ||
const oyy = await jimp.read(buffer); | ||
const kiyomasa = await oyy.resize(512, 512).getBufferAsync(jimp.MIME_JPEG); | ||
return kiyomasa; | ||
} | ||
} | ||
export default Gemini; | ||
export default Gemini; |
{ | ||
"name": "@onepunya/ez-gemini", | ||
"version": "1.0.7-saitekina", | ||
"version": "1.0.8-saitekina", | ||
"main": "index.cjs", | ||
@@ -5,0 +5,0 @@ "module": "index.js", |
Sorry, the diff of this file is not supported yet
7426
7
128