user27053061
user27053061

Reputation:

Error 400 invalid JSON Payload Unknown name generationConfig on an AI API Curl command

I'm trying to use Generative Language API (Gemini API) in my React Native so I'm first trying with a curl. Someone gave me this command :

`

API_KEY="YOUR_API_KEY"



# Adjust safety settings in generationConfig below.

# See https://ai.google.dev/gemini-api/docs/safety-settings

curl \

  -X POST https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash:generateContent?key=${API_KEY} \

  -H 'Content-Type: application/json' \

  -d @<(echo '{

  "contents": [

    {

      "role": "user",

      "parts": [

        {

          "text": "input: "

        },

        {

          "text": "output: "

        }

      ]

    }

  ],

  "generationConfig": {

    "temperature": 1,

    "topK": 64,

    "topP": 0.95,

    "maxOutputTokens": 8192,

    "responseMimeType": "text/plain"

  }

}')

`

But I got this error :

"error": { "code": 400, "message": "Invalid JSON payload received. Unknown name \"generationConfig\": Cannot find field.", "status": "INVALID_ARGUMENT", "details": [ { "@type": "type.googleapis.com/google.rpc.BadRequest", "fieldViolations": [ { "description": "Invalid JSON payload received. Unknown name \"generationConfig\": Cannot find field." } ] } ] } }'

Is there something other than the API key to adjust ? I'm trying to use Generative Language API (Gemini API) in my React Native so I'm first trying with a curl. I'd like to have an response from the AI.

Upvotes: 1

Views: 641

Answers (2)

Vladlen Kaveev
Vladlen Kaveev

Reputation: 86

It would be easier and better to convert the сURL and use Axios or Fetch:

Using Axios:

npm install axios

import axios from 'axios';

const API_KEY = 'YOUR_API_KEY';
const url = `https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash:generateContent?key=${API_KEY}`;

const requestData = {
  contents: [
    {
      role: 'user',
      parts: [
        { text: 'input: ' },
        { text: 'output: ' }
      ]
    }
  ],
  generationConfig: {
    temperature: 1,
    topK: 64,
    topP: 0.95,
    maxOutputTokens: 8192,
    responseMimeType: 'text/plain'
  }
};

axios.post(url, requestData, {
  headers: {
    'Content-Type': 'application/json'
  }
})
.then(response => {
  console.log(response.data);
})
.catch(error => {
  console.error('Error:', error);
});

Using standard Fetch:

const API_KEY = 'YOUR_API_KEY';
const url = `https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash:generateContent?key=${API_KEY}`;

const requestData = {
  contents: [
    {
      role: 'user',
      parts: [
        { text: 'input: ' },
        { text: 'output: ' }
      ]
    }
  ],
  generationConfig: {
    temperature: 1,
    topK: 64,
    topP: 0.95,
    maxOutputTokens: 8192,
    responseMimeType: 'text/plain'
  }
};

fetch(url, {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json'
  },
  body: JSON.stringify(requestData)
})
  .then(response => response.json())
  .then(data => console.log(data))
  .catch(error => console.error('Error:', error));

Upvotes: 0

Mark McDonald
Mark McDonald

Reputation: 8200

The command you used worked fine for me, and it looks like it should work.

I also tried tweaking the max_output_tokens field (down to 128) and that worked, giving me a shorter response.

If you want to use curl, double-check that you are entering the command in correctly. And maybe provide some more info on what you did (e.g. is this in bash? on a mac? windows? powershell? these kinds of changes might impact how the command is interpreted). I successfully ran the command from bash on a linux machine.

If you want to use JS directly, skipping the curl test, take a look at the official JS SDK.

import { GoogleGenerativeAI } from "@google/generative-ai";
 
const model = genAI.getGenerativeModel({
  model: "gemini-1.5-flash",
  generationConfig: {
    temperature: 1,
    topK: 64,
    topP: 0.95,
    maxOutputTokens: 8192,
    responseMimeType: 'text/plain'
  },
});

const result = await model.generateContent(["input: ", "output: "]);
console.log(result.response.text());

Upvotes: 0

Related Questions