I'm trying to get support for the file upload endpoint that is part of the betav1 version. Which hasn't been added to the generative ai js library yet.
I can load the discovery doc and call the file endpoint
require('dotenv').config();
const API_KEY = process.env.API_KEY; // Get the api key from env
const MODEL_NAME = process.env.TEXT_MODEL_NAME_LATEST; // Get the model name from env
// Importing the GoogleGenerativeAI class from the "@google/generative-ai" package
const { GoogleGenerativeAI } = require("@google/generative-ai");
// Access your API key as an environment variable (see "Set up your API key" above)
const genAI = new GoogleGenerativeAI(process.env.API_KEY);
async function run() {
// For text-only input, use the gemini-pro model
const model = genAI.getGenerativeModel({ model: MODEL_NAME},{apiVersion: 'v1beta',});
const prompt = "Write a story about a magic backpack."
const result = await model.generateContent(prompt);
const response = await result.response;
const text = response.text();
console.log(text);
}
run();
Its resulting in an error as its not adding the api key.
errors: [
{
message: "Method doesn't allow unregistered callers (callers without established identity). Please use API Key or other form of API consumer identity to call this API.",
domain: 'global',
reason: 'forbidden'
}
],
Any one know how to add the api key to this?
After a lot of back and forth it seams you can load the key directly into the serivce
const service = google.discoverAPI(discoveryUrl, { auth : apiKey});
const { google } = require('googleapis');
require('dotenv').config();
const API_KEY = process.env.API_KEY; // Get the api key from env
async function createService(apiKey) {
const discoveryUrl = `https://generativelanguage.googleapis.com/$discovery/rest?version=v1beta&key=${apiKey}`;
try {
const service = google.discoverAPI(discoveryUrl, { auth : apiKey});
return service;
} catch (error) {
console.error('Error fetching discovery document:', error);
throw error;
}
}
// Usage
createService(API_KEY)
.then(async service => {
// You can use the service object here
const res = await service.files.list({
pageSize: 100,
});
console.log(res)
const files = res.data.files;
if (files.length === 0) {
console.log('No files found.');
return;
}
console.log('Files:');
files.map((file) => {
console.log(`${file.name} (${file.id})`);
});
//console.log('Service created:', service);
})
.catch(error => {
console.error('Error creating service:', error);
});