GoogleGenerative API 400 Bad Request
Всем привет, пишу бота для Discord, в который хотел бы внедрить настроенную модель на основе Gemini. Для того чтобы модель заработала, мне нужно было получить OAuth-ключ. Я пошёл изучать, как его получить через Postman. Похоже, у меня получилось, но при запросе в консоль выводится вот такое сообщение:
GoogleGenerativeAIFetchError: [GoogleGenerativeAI Error]: Error fetching from https://generativelanguage.googleapis.com/v1beta/tunedModels/nursultanai-enlfq4pioewv:generateContent: [400 Bad Request] API key not valid. Please pass a valid API key. [{"@type":"type.googleapis.com/google.rpc.ErrorInfo","reason":"API_KEY_INVALID","domain":"googleapis.com","metadata":{"service":"generativelanguage.googleapis.com"}}]
at _makeRequestInternal (/home/runner/ai-assistant/node_modules/@google/generative-ai/dist/index.js:353:19)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
at async generateContent (/home/runner/ai-assistant/node_modules/@google/generative-ai/dist/index.js:746:22)
at async generateResponse (/home/runner/ai-assistant/asi/neuro.js:255:18)
at async Client.<anonymous> (/home/runner/ai-assistant/asi/neuro.js:279:22)
Emitted 'error' event on Client instance at:
at emitUnhandledRejectionOrErr (node:events:401:10)
at process.processTicksAndRejections (node:internal/process/task_queues:84:21) {
status: 400,
statusText: 'Bad Request',
errorDetails: [
{
'@type': 'type.googleapis.com/google.rpc.ErrorInfo',
reason: 'API_KEY_INVALID',
domain: 'googleapis.com',
metadata: { service: 'generativelanguage.googleapis.com' }
}
]
}
В чём заключается проблема даже понять не могу, т.к раньше не работал с API Гугловским. Надеюсь, хоть кто-то попытается помочь такому чайнику - как я
const {
GoogleGenerativeAI,
HarmCategory,
HarmBlockThreshold,
} = require("@google/generative-ai");
const discord = require("discord.js");
const MODEL_NAME = "tunedModels/nursultanai-enlfq4pioewv";
const API_KEY = "ya29.a0AXooCgtwbtqF7T1XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX-XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
const DISCORD_TOKEN =
"MTIzNjc4MzMxMDUyOTYyNjE2MwXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
const client = new discord.Client({
intents: Object.keys(discord.GatewayIntentBits),
});
let history = {};
async function generateResponse(inputText, iduser) {
const genAI = new GoogleGenerativeAI(API_KEY);
const model = genAI.getGenerativeModel({ model: MODEL_NAME }, {apiVersion: 'v1beta'});
if (!history[iduser]) {
history[iduser] = [];
}
const generationConfig = {
temperature: 0.7,
//topK: 0,
//topP: 1,
//maxOutputTokens: 2048,
};
const safetySettings = [
{
category: HarmCategory.HARM_CATEGORY_HARASSMENT,
threshold: HarmBlockThreshold.BLOCK_ONLY_HIGH,
},
{
category: HarmCategory.HARM_CATEGORY_HATE_SPEECH,
threshold: HarmBlockThreshold.BLOCK_ONLY_HIGH,
},
{
category: HarmCategory.HARM_CATEGORY_SEXUALLY_EXPLICIT,
threshold: HarmBlockThreshold.BLOCK_ONLY_HIGH,
},
{
category: HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT,
threshold: HarmBlockThreshold.BLOCK_ONLY_HIGH,
},
];
history[iduser].push({ text: `input: ${inputText}` });
const parts = [
...history[iduser],
{ text: `input: ${inputText}` },
{ text: "output: " },
];
console.log(history[iduser])
const result = await model.generateContent({
contents: [{ role: "user", parts }],
generationConfig,
safetySettings,
});
history[iduser].push({ text: `output: ${result.response.text()}` });
return result.response.text();
}
client.on("messageCreate", async (message) => {
if (message.author.bot) {
return;
}
const channelCategory = message.channel.parent ? message.channel.parent.name.toLowerCase() : null;
if (channelCategory !== allowedCategoryName.toLowerCase()) {
return;
}
const content = message.content.toLowerCase()
const containsTriggerWord = triggerWords.some((word) => content.includes(word));
if (containsTriggerWord) {
message.channel.sendTyping();
const response = await generateResponse(message.content, message.author.id);
message.channel.send(response);
}
});
client.login(DISCORD_TOKEN);