Open3

Gemini APIキーの取得についてcurlがないとき代替手段で接続テストを実施する

mars2nicomars2nico

標準のテストはcurlを使用すること前提になっている。

curl "https://generativelanguage.googleapis.com/v1beta/models/gemini-2.0-flash:generateContent?key=GEMINI_API_KEY" \
-H 'Content-Type: application/json' \
-X POST \
-d '{
  "contents": [{
    "parts":[{"text": "Explain how AI works"}]
    }]
   }'

これを代替手段で実施する。

mars2nicomars2nico

Powershellの場合

Invoke-WebRequest "https://generativelanguage.googleapis.com/v1beta/models/gemini-2.0-flash:generateContent?key=GEMINI_API_KEY" `
-Header @{ 'Content-Type' = 'application/json' } `
-Method Post `
-Body '{
  "contents": [{
    "parts": [{"text": "Explain how AI works"}]
  }]
}'
レスポンスの例
  • textが長いので一部省略
  • 末尾のメタデータは省略
{
  "candidates": [
    {
      "content": {
        "parts": [
          {
            "text": "Okay, let's break down how AI works in a way that's understandable.  It's a broad field, so we'll focus on the core concepts and key techniques.\n\n**What is AI?**\n\nAt its most basic, (略) **In Summary:**\n\nAI is a powerful set of technologies that allows machines to perform tasks that typically require human intelligence. It relies on algorithms and data to learn, reason, and solve problems. Machine learning, deep learning, natural language processing, and computer vision are some of the key techniques used in AI. While AI has the potential to transform many industries, it's important to be aware of its limitations and ethical implications.\n\nThis explanation provides a high-level overview. Each of these areas has its own complexities and nuances, and there's a vast amount of research and development ongoing in the field.\n"
          }
        ],
        "role": "model"
      },
      "finishReason": "STOP",
      "citationMetadata": {
        "citationSources": [
          {
            "startIndex": 211,
            "endIndex": 341,
            "uri": "https://audio-consultants.com/ai-definity-redefining-intelligence/"
          },
          {
            "startIndex": 3584,
            "endIndex": 3721,
            "uri": "https://github.com/Amandeep404/Data-Science-Master"
          },
          {
            "startIndex": 3774,
            "endIndex": 3906,
            "uri": "https://technofytimes.com/from-zero-to-hero-your-roadmap-to-becoming/"
          },
          {
            "startIndex": 4480,
            "endIndex": 4617,
            "uri": "https://itinai.com/cohere-ai-releases-aya23-models-transformative-multilingual-nlp-with-8b-and-35b-parameter-models/"
          },
          {
            "startIndex": 4719,
            "endIndex": 4858,
            "uri": "https://serp.ai/bidirectional-lstm/"
          },
          {
            "startIndex": 6679,
            "endIndex": 6805,
            "uri": "https://samuelsum.com/understanding-the-proper-and-best-practices-for-using-generative-ai/"
          }
        ]
      },