-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsample.js
More file actions
50 lines (44 loc) · 1.83 KB
/
Copy pathsample.js
File metadata and controls
50 lines (44 loc) · 1.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
const dotenv = require('dotenv');
const fs = require('fs');
const {google} = require('googleapis');
const mime = require('mime-types');
// Load environment variables from .env file
dotenv.config({ path: '.env' });
const API_KEY = process.env.GOOGLE_API_KEY;
const GENAI_DISCOVERY_URL = `https://generativelanguage.googleapis.com/$discovery/rest?version=v1beta&key=${API_KEY}`;
async function run(filePath, fileDisplayName) {
// Initialize API Client
const genaiService = await google.discoverAPI({url: GENAI_DISCOVERY_URL});
const auth = new google.auth.GoogleAuth().fromAPIKey(API_KEY);
// Prepare file to upload to GenAI File API
const media = {
mimeType: mime.lookup(filePath),
body: fs.createReadStream(filePath),
};
var body = {"file": {"displayName": fileDisplayName}};
try {
// Upload the file
const createFileResponse = await genaiService.media.upload({
media: media, auth: auth, requestBody:body});
const file = createFileResponse.data.file;
const fileUri = file.uri;
console.log("Uploaded file: " + fileUri);
// Make Gemini 1.5 API LLM call
const prompt = "Describe the image with a Health Doctor description for medical conditions";
const model = "models/gemini-1.5-pro-latest";
const contents = {'contents': [{
'parts':[
{'text': prompt},
{'file_data': {'file_uri': fileUri, 'mime_type': file.mimeType}}]
}]}
const generateContentResponse = await genaiService.models.generateContent({
model: model, requestBody: contents, auth: auth});
console.log(JSON.stringify(generateContentResponse.data));
}
catch (err) {
throw err;
}
}
filePath = "sample_data/gemini_logo.png";
fileDisplayName = "Gemini logo";
run(filePath, fileDisplayName);