Gemini ProのAPIが提供されたので試してみる

## 背景

https://blog.google/technology/ai/gemini-api-developers-cloud/

## Google AI Studioで、APIKeyを手に入れる

### Google AI Studioヘアクセスする

サイトへアクセスし、「Get API key in Google AI Studio」でGoggleAIStudioへアクセス。

https://ai.google.dev/

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/3589058/21d9a2cf-793b-4846-bf05-24621ab968a7.png)

### 利用規約へ同意する

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/3589058/90fdbf17-196e-f138-e247-8f9da8c90190.png)


### API Keyを手に入れる

サイドバーの「Get API Key」へアクセス。

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/3589058/94a01b87-761d-249b-d54b-0774ad04a6b4.png)

「Create API key in new project」をクリック

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/3589058/f1ad6c51-2947-ccab-9291-9203dc82c6f4.png)

しばらく待つと、下記のモーダルにAPI keyが表示されるので、Copyを押して保存しておきます。

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/3589058/8c433e13-696d-f017-46fa-1841d361a355.png)


### 簡単にAPIKeyを試す。

APIKey生成後に、下記の簡単にテストできるcurlが表示されるので、こちらでAPIKeyが正常に動くか試します。

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/3589058/e32009cd-ccd6-6771-2d1c-117db02468b4.png)

ターミナルで下記を叩いてみます。

```sh
curl \
-H 'Content-Type: application/json' \
-d '{ "prompt": { "text": "Write a story about a magic backpack"} }' \
"https://generativelanguage.googleapis.com/v1beta3/models/text-bison-001:generateText?key=コピーしたKey"
```

APIKeyが正しければ、下記のようにAIが考えた物語が帰ってきます。

```sh
{
  "candidates": [
    {
      "output": "Once upon a time, there was a young boy named Billy who loved to go on adventures. He would often explore the woods near his house, looking for new and exciting things to see. One day, Billy was exploring the woods when he came across a strange backpack. It was sitting on a rock, and it looked like it had been abandoned. Billy picked up the backpack and examined it. It was made of a strange material that he didn't recognize, and it had a few straps and a zipper. Billy opened the zipper and peered inside. The backpack was filled with all sorts of strange and wonderful things. There were maps, compasses, telescopes, binoculars, and even a few magic items. Billy was amazed. He had never seen anything like it before.\n\nBilly decided to take the backpack home with him. He couldn't wait to show his friends all the amazing things he had found. When he got home, he showed his friends the backpack. They were all amazed by it, too. They started to play with all the different items, and they had a lot of fun.\n\nOne of the items in the backpack was a magic map. The map showed all the different places in the world that Billy could visit. Billy decided that he wanted to use the map to go on an adventure. He packed his things and set off on his journey.\n\nBilly traveled all over the world. He visited different countries, met new people, and had many exciting experiences. He used the magic items in the backpack to help him on his adventures. He used the maps to find his way, the compasses to stay on course, and the telescopes and binoculars to see faraway places.\n\nBilly had the time of his life on his adventures. He learned a lot about the world, and he made many new friends. He also learned a lot about himself. He learned that he was braver and more resourceful than he thought he was.\n\nWhen Billy finally returned home, he was a different person. He was more confident and more adventurous. He was also more grateful for the things he had. He knew that he had been lucky to have the opportunity to go on such an amazing journey.\n\nBilly continued to use the magic backpack on his future adventures. He went to all sorts of amazing places and had many more exciting experiences. He learned a lot about the world, and he made many more friends. He also learned a lot about himself. He learned that he was capable of anything he set his mind to.\n\nThe magic backpack was a gift that Billy would never forget. It gave him the opportunity to have the adventure of a lifetime. It also taught him a lot about himself and the world around him.",
      "safetyRatings": [
        {
          "category": "HARM_CATEGORY_DEROGATORY",
          "probability": "NEGLIGIBLE"
        },
        {
          "category": "HARM_CATEGORY_TOXICITY",
          "probability": "NEGLIGIBLE"
        },
        {
          "category": "HARM_CATEGORY_VIOLENCE",
          "probability": "NEGLIGIBLE"
        },
        {
          "category": "HARM_CATEGORY_SEXUAL",
          "probability": "NEGLIGIBLE"
        },
        {
          "category": "HARM_CATEGORY_MEDICAL",
          "probability": "NEGLIGIBLE"
        },
        {
          "category": "HARM_CATEGORY_DANGEROUS",
          "probability": "NEGLIGIBLE"
        }
      ]
    }
  ]
}
```

これで無事、APIKeyを手に入れて、curlからGeminiProのAPIを叩くことができました。