skip to Main Content

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.

2

Answers


  1. 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.

    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.

    Login or Signup to reply.
  2. 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 standart 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));
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search