生成 AI の Gemini を curl で試してみた

Bard が Gemini に名称変更されました。ChatGPT は試した事があったので、Gemini もまずは curl から試してみました。

API キーを取得

https://makersuite.google.com/app/apikey

こちらから API キーを取得します。

サンプル通りにまずは試す

API_KEY=<your api key>

curl "https://generativelanguage.googleapis.com/v1/models/gemini-pro:generateContent?key=$API_KEY" \
    -H 'Content-Type: application/json' \
    -X POST \
    -d '{ "contents":[
      { "parts":[{"text": "Write a story about a magic backpack"}]}
    ]
}'

実行結果の出力

{
  "candidates": [
    {
      "content": {
        "parts": [
          {
            "text": "Once upon a time, in a small town nestled amidst rolling green hills, there lived a young girl named Alice. Alice was a curious and adventurous child, always seeking out new experiences and treasures. One day, while exploring the attic of her grandmother's old house, she stumbled upon a dusty old backpack hidden away in a trunk.\n\nIntrigued, Alice opened the backpack and found it filled with an assortment of peculiar items: a shimmering crystal, a worn leather-bound book, a compass with a needle that spun erratically, and a small, silver key. As she examined each object, a gentle hum filled the air, and the backpack began to glow with a soft, ethereal light.\n\nAlice realized that this was no ordinary backpack. It was a magic backpack, filled with secrets and wonders beyond her imagination. With a trembling hand, she fastened the backpack onto her shoulders and stepped out into the world, ready for her journey of discovery.\n\nHer first adventure led her to a nearby forest, where she followed the compass's erratic movements until she reached a hidden grove. Surrounded by ancient trees, she found a gnarled old oak with a hollow trunk. Using the silver key, she unlocked a secret compartment within the tree, revealing a collection of ancient scrolls and maps that hinted at hidden treasures and forgotten lands.\n\nThroughout the following weeks, Alice embarked on countless adventures with her magic backpack as her faithful companion. She traversed treacherous mountains, explored uncharted territories, and encountered extraordinary creatures. The backpack seemed to possess an innate sense of direction, guiding her towards hidden wonders and magical realms.\n\nShe learned to navigate by the stars using the compass, read ancient languages from the leather-bound book, and harness the power of the shimmering crystal to solve puzzles and overcome obstacles. Each journey brought new challenges and discoveries, and Alice's knowledge and skills grew with every passing day.\n\nAs Alice's reputation as a fearless adventurer spread throughout the land, she attracted the attention of a wise and benevolent wizard named Merlín. Impressed by her unwavering curiosity and unwavering spirit, Merlín offered to become her mentor and guide her on her magical quests.\n\nTogether, they embarked on a journey to uncover the secrets of the lost city of Atlantis, where they faced perilous trials and encountered formidable guardians. With her magic backpack and Merlín's guidance, Alice unlocked the secrets of the city and discovered a treasure trove of ancient knowledge and artifacts.\n\nAlice's adventures continued far and wide, leaving a trail of legends and tales of wonder wherever she went. She became known as the \"Girl With the Magic Backpack,\" inspiring others to seek out adventure and explore the unknown.\n\nAnd as the years passed, the magic backpack became a symbol of hope, courage, and the boundless power of curiosity. It reminded people that the world was filled with wonders waiting to be discovered, and that anything was possible if they dared to dream and embark on the path of adventure."
          }
        ],
        "role": "model"
      },
      "finishReason": "STOP",
      "index": 0,
      "safetyRatings": [
        {
          "category": "HARM_CATEGORY_SEXUALLY_EXPLICIT",
          "probability": "NEGLIGIBLE"
        },
        {
          "category": "HARM_CATEGORY_HATE_SPEECH",
          "probability": "NEGLIGIBLE"
        },
        {
          "category": "HARM_CATEGORY_HARASSMENT",
          "probability": "NEGLIGIBLE"
        },
        {
          "category": "HARM_CATEGORY_DANGEROUS_CONTENT",
          "probability": "NEGLIGIBLE"
        }
      ]
    }
  ],
  "promptFeedback": {
    "safetyRatings": [
      {
        "category": "HARM_CATEGORY_SEXUALLY_EXPLICIT",
        "probability": "NEGLIGIBLE"
      },
      {
        "category": "HARM_CATEGORY_HATE_SPEECH",
        "probability": "NEGLIGIBLE"
      },
      {
        "category": "HARM_CATEGORY_HARASSMENT",
        "probability": "NEGLIGIBLE"
      },
      {
        "category": "HARM_CATEGORY_DANGEROUS_CONTENT",
        "probability": "NEGLIGIBLE"
      }
    ]
  }
}

日本語で試す

curl "https://generativelanguage.googleapis.com/v1/models/gemini-pro:generateContent?key=$API_KEY" \
    -H 'Content-Type: application/json' \
    -X POST \
    -d '{ "contents":[
      { "parts":[{"text": "魔法のバックパックについての話を書いて"}]}
    ]
}'

日本語の実行結果の出力

{
  "candidates": [
    {
      "content": {
        "parts": [
          {
            "text": "昔々、ある小さな村に、魔法のバックパックを持った少年がいました。そのバックパックは、持ち主が望めば何でも入れることができました。少年は、木々から果物を摘んで、バックパックに入れて食べました。また、川で魚を釣って、バックパックに入れて家で食べました。\n\nある日、少年は森の中で迷子になりました。彼は歩き回り続けましたが、家に戻る道を見つけることができませんでした。彼は疲れ果てて、泣きそうになりました。そのとき、彼は魔法のバックパックを思い出しました。\n\n少年はバックパックを開けて、何か食べられるものがないか探しました。すると、中にはパンとチーズが入っていました。彼はそれらを食べて、空腹を満たしました。そして、彼は再び歩き始めました。\n\n少年は歩き続けましたが、家に戻る道を見つけられませんでした。彼は疲れ果てて、また泣きそうになりました。そのとき、彼は魔法のバックパックを思い出しました。\n\n少年はバックパックを開けて、何か助けてくれるものがないか探しました。すると、中には地図が入っていました。彼は地図を広げて、自分の居場所を調べました。そして、彼は家に戻る道を見つけました。\n\n少年は、魔法のバックパックのおかげで、家に戻る道を見つけることができました。彼は、魔法のバックパックに感謝し、これからも大切にすることを誓いました。"
          }
        ],
        "role": "model"
      },
      "finishReason": "STOP",
      "index": 0,
      "safetyRatings": [
        {
          "category": "HARM_CATEGORY_SEXUALLY_EXPLICIT",
          "probability": "NEGLIGIBLE"
        },
        {
          "category": "HARM_CATEGORY_HATE_SPEECH",
          "probability": "NEGLIGIBLE"
        },
        {
          "category": "HARM_CATEGORY_HARASSMENT",
          "probability": "NEGLIGIBLE"
        },
        {
          "category": "HARM_CATEGORY_DANGEROUS_CONTENT",
          "probability": "NEGLIGIBLE"
        }
      ]
    }
  ],
  "promptFeedback": {
    "safetyRatings": [
      {
        "category": "HARM_CATEGORY_SEXUALLY_EXPLICIT",
        "probability": "NEGLIGIBLE"
      },
      {
        "category": "HARM_CATEGORY_HATE_SPEECH",
        "probability": "NEGLIGIBLE"
      },
      {
        "category": "HARM_CATEGORY_HARASSMENT",
        "probability": "NEGLIGIBLE"
      },
      {
        "category": "HARM_CATEGORY_DANGEROUS_CONTENT",
        "probability": "NEGLIGIBLE"
      }
    ]
  }
}

参考

https://ai.google.dev/tutorials/rest_quickstart

https://ai.google.dev/docs/gemini_api_overview?hl=ja#curl

タグ: