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',
})
}
}
?
请求:
{
"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": "成功"
}
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',
})
}
}
参考链接: