如何使用curl从gemini-1.5-pro-001获取JSON输出

问题描述 投票:0回答:1

如何使用curl从Gemini 1.5 API检索JSON格式的数据? 下面的代码可以正确运行:

curl -H 'Content-Type: application/json'      -H "x-goog-api-key: ${API_KEY}"      -d '{"contents":[
            {"role": "user",
              "parts":[{"text": "Give me five subcategories of jazz?"}]}]}'      "https://generativelanguage.googleapis.com/v1/models/gemini-pro:generateContent"

但是当我尝试添加时:

,"generation_config": {"response_mime_type": "application/json"}}

给予:

curl -H 'Content-Type: application/json' -H "x-goog-api-key: ${API_KEY}" -d '{"contents":[
            {"role": "user",
              "parts":[{"text": "Give me five subcategories of jazz?"}]},"generation_config": {"response_mime_type": "application/json"},]'      "https://generativelanguage.googleapis.com/v1/models/gemini-1.5-flash:generateContent"

我收到此错误响应:

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

我尝试过几种不同的模型(例如

gemini-1.5-pro-latest
),但结果总是相同。

json curl google-gemini
1个回答
0
投票

试试这个。

curl https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash:generateContent?key=$GOOGLE_API_KEY \
    -H 'Content-Type: application/json' \
    -X POST \
    -d '{
        "contents": [{
            "parts":[
                {"text": "Write a story about a magic backpack."}
            ]
        }],
        "safetySettings": [
            {
                "category": "HARM_CATEGORY_DANGEROUS_CONTENT",
                "threshold": "BLOCK_ONLY_HIGH"
            }
        ],
        "generationConfig": {
            "stopSequences": [
                "Title"
            ],
            "temperature": 1.0,
            "maxOutputTokens": 800,
            "topP": 0.8,
            "topK": 10,
            "responseMimeType": "application/json"
        }
    }'  2> /dev/null | grep "text"
© www.soinside.com 2019 - 2024. All rights reserved.