Pelo visto funções não são mais declaradas da mesma forma
meu chat.js
import { chat, funcoes, inicializaChat } from './inicializaChat.js';
export async function executaChat(mensagem) {
if (!chat) {
inicializaChat();
}
console.log("Tamanho do histórico: " + (await chat.getHistory()).length);
const result = await chat.sendMessage(mensagem);
const response = await result.response;
const content = response.candidates[0].content;
const fc = content.parts[0].functionCall;
const text = content.parts.map(({ text }) => text).join("");
console.log(text);
console.log(fc);
if (fc) {
const { name, args } = fc;
const fn = funcoes[name];
if (!fn) {
throw new Error(`Unknown function "${name}"`);
}
const fr = {
functionResponse: {
name,
response: {
name,
content: funcoes[name](args),
}
},
}
console.log(fr)
const request2 = [fr];
const response2 = await chat.sendMessage(request2);
const result2 = response2.response;
return result2.text();
} else if (text) {
return text;
}
}
meu inicializaChat.ts
import { GoogleGenerativeAI, FunctionDeclarationSchemaType } from '@google/generative-ai';
const genAI = new GoogleGenerativeAI("AQ.Ab8RN6LSyXK0dxxiX7B5ZEmX4tOJQLAsLGBQ6V59DmRz4iW1Iw");
const funcoes = {
taxaJurosParcelamento: ({ value }) => {
const meses = typeof value === "string" ? parseInt(value) : value;
if (meses <= 6) {
return 3;
} else if (meses <= 12) {
return 5;
} else if (meses <= 24) {
return 7;
}
}
};
const tools = [
{
functionDeclarations: [
{
name: "taxaJurosParcelamento",
description: "Retorna a taxa de juros para parcelamento baseado na quantidade de meses",
parameters: {
type: FunctionDeclarationSchemaType.OBJECT,
properties: {
value: { type: FunctionDeclarationSchemaType.NUMBER },
},
required: ["value"],
}
}
]
}
];
const model = genAI.getGenerativeModel(
{ model: "gemini-3.5-flash-lite", tools},
{ apiVersion: "v1beta"});
let chat;
function inicializaChat() {
chat = model.startChat({
history: [
{
role: "user",
parts: [{ text: `Você é Jordi, um chatbot amigável que representa
a empresa Jornada Viagens, que vende pacotes turísticos para
destinos nacionais e internacionais. Você pode responder mensagens
que tenham relação com viagens.` }],
},
{
role: "model",
parts: [{ text: `Olá! Obrigado por entrar em contato com o Jornada Viagens.
Antes de começar a responder sobre suas dúvidas, preciso do seu nome e
endereço de e-mail.` }],
},
],
generationConfig: {
maxOutputTokens: 1000,
},
});
}
export { chat, funcoes, inicializaChat}
meu erro:
{
name: 'taxaJurosParcelamento',
args: { value: 4 },
id: 'call_1507556'
}
{
functionResponse: {
name: 'taxaJurosParcelamento',
response: { name: 'taxaJurosParcelamento', content: 3 }
}
}
Error no endpoint do chat: GoogleGenerativeAIError: [400 Bad Request] Role 'function' is not supported. Please use a valid role: SYSTEM, SYSTEM_1, USER, ASSISTANT, DEVELOPER, CONTEXT, USER_CONTEXT, MODEL, USER.
at _makeRequestInternal (file:///C:/Users/Refri/OneDrive/js/3752-gemini-com-node-main/node_modules/@google/generative-ai/dist/index.mjs:301:19)
at process.processTicksAndRejections (node:internal/process/task_queues:104:5)
at async generateContent (file:///C:/Users/Refri/OneDrive/js/3752-gemini-com-node-main/node_modules/@google/generative-ai/dist/index.mjs:690:22)
at async ChatSession.sendMessage (file:///C:/Users/Refri/OneDrive/js/3752-gemini-com-node-main/node_modules/@google/generative-ai/dist/index.mjs:951:9)
at async executaChat (file:///C:/Users/Refri/OneDrive/js/3752-gemini-com-node-main/chat.js:37:23)
at async file:///C:/Users/Refri/OneDrive/js/3752-gemini-com-node-main/app.js:29:22