Google Gemini接口调用(node版)

发布时间:2024年01月03日

一、打开Google AI Studio

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

二、在国外服务器上部署一个接口用于真正的请求

const sdAxiosOnAzure = async (req, res) => {
  let {
    config = {
      url: 'https://sinkin.ai/api/inference',
      method: 'post',
      data: {},
      timeout: 30 * 60 * 1000,
    },
    apiKey = '',
  } = req.body

  if (apiKey === 'XXX') {
    if (
      config.headers &&
      config.headers['Content-Type'] &&
      config.headers['Content-Type'].includes('multipart/form-data')
    ) {
      const data = new FormData()
      let tempData = config.data
      for (let key in tempData) {
        data.append(key, tempData[key])
      }
      config.data = data
      config.headers['Content-Type'] =
        'multipart/form-data; boundary=' + data.getBoundary()
    }
    axios({
      ...config,
    })
      .then((response) => {
        res.send({
          code: 200,
          data: {
            response: response.data,
          },
          message: '成功',
        })
      })
      .catch((err) => {
        res.send({
          code: 400,
          data: {
            err,
          },
          message: '失败',
        })
      })
  } else {
    res.send({
      code: 400,
      message: '失败:参数apiKey',
    })
  }
}

三、在postman上发起请求

?

请求:

{
    "config": {
        "url": "https://generativelanguage.googleapis.com/v1beta/models/gemini-pro:generateContent?key=xxx",
        "data": {
            "contents": [
                {
                    "role": "user",
                    "parts": [
                        {
                            "text": "北京有多少人?"
                        }
                    ]
                }
            ]
        },
        "method": "post"
    },
    "apiKey": "XXX"
}

响应:

{
    "code": 200,
    "data": {
        "response": {
            "candidates": [
                {
                    "content": {
                        "parts": [
                            {
                                "text": "截至 2023 年 3 月,北京的人口总数约为 2,154 万。然而,这一数字可能因人口流动和其他因素而变化。因此,您最好查阅更接近实时的数据源,例如北京市政府或国家统计局网站,以获取最准确的北京人口信息。"
                            }
                        ],
                        "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"
                    }
                ]
            }
        }
    },
    "message": "成功"
}

四、在node里发起请求

const { GoogleGenerativeAI } = require('@google/generative-ai')

const genAI = new GoogleGenerativeAI('你的key')

const chatVertexaiOnAzure = async (req, res) => {
  let { prompt = 'Write a story about a magic backpack.', apiKey = 'sk-xxx' } =
    req.body

  if (apiKey === apiKeyOnServer) {

    const model = genAI.getGenerativeModel({ model: 'gemini-pro' })

    const result = await model.generateContent(prompt)
    const response = await result.response
    const text = response.text()

    res.send({
      code: 200,
      data: {
        text,
      },
      message: '成功',
    })
  } else {
    res.send({
      code: 400,
      message: '失败:参数apiKey',
    })
  }
}

参考链接:

https://chat.xutongbao.top/

文章来源:https://blog.csdn.net/xutongbao/article/details/135368464
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。