Genini Api calls

Hello guys seem to have an issue with calling my tuned model below is my code

const fs = require('fs').promises;
const path = require('path');
const { GoogleAuth } = require('google-auth-library');
const axios = require('axios');

async function run() {
    // Load the service account key JSON file
    const keyFile = await fs.readFile(path.join(__dirname, 'secret.json'), 'utf8');
    const keys = JSON.parse(keyFile);

    // Initialize GoogleAuth for OAuth 2.0
    const auth = new GoogleAuth({
        credentials: keys,
        scopes: ['https://www.googleapis.com/auth/generative-language']
    });

    const client = await auth.getClient();

    // Get the access token
    const accessToken = await client.getAccessToken();

    const generationConfig = {
        temperature: 0.9,
        topP: 1,
        maxOutputTokens: 8192,
    };

    const requestBody = {
        contents: [{
            role: 'user',
            parts: [{ text: 'input: ' }, { text: 'output: ' }]
        }],
        generationConfig,
    };

    try {
        const response = await axios.post(
            'https://generativelanguage.googleapis.com/v1beta/tunedModels/copy-of-plan-house-model-a5iltlw1z6df:generateContent',
            requestBody, {
                headers: {
                    'Authorization': `Bearer ${accessToken.token}`,
                    'Content-Type': 'application/json',
                }
            }
        );

        console.log('Generated content:', response.data.candidates[0].content.parts[0].text);
    } catch (error) {
        console.error('Error generating content:', error.response ? error.response.data : error.message);
    }
}

run();

i get error
Error generating content: {
error: {
code: 403,
message: ‘You do not have permission to access tuned model tunedModels/copy-of-plan-house-model-a5iltlw1z6df.’,
status: ‘PERMISSION_DENIED’
}
}

Your comment in the code indicates you’re using a service account.

How have you permitted the service account to the trained model?

  • Editor

  • Owner

  • Service Account Admin

For Service Account it’s usually enough if you have Owner permission without any other permission.

Have you made a successful request with your service account before because if am not wrong you need to pass the Project ID in the header as well.

can you show me an example code you use please in any language you used

So you’ve permitted the service account to the project, but not to the model?

That’s the problem, I think. The model maintains its own permission structure that permit it to a user.

So you’ll need to permit it (by its service account email address) to the model itself. Which require you to authenticate as yourself first to grant the permission. See herefor more info.

thanks alot man you are a live saver

1 Like