My code was working fine but now when i tried to call chatgpt Alexa keeps reprompting the intent and do not send request to openIA.
I tested the API_Key in with postman and it´s working good. Below follow the code I used to make integration in alexa developer console.
When a person call the intent it shows an image in alexa echo show and says an welcome message then user says the prompt question it sends to chatgpt limited by the systemrole rules and return with the answer.
But as I said it now keeps reprompting the welcome message.
// Handler para o ChatGPTIntent
const ChatGPTIntentHandler = {
canHandle(handlerInput) {
return Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest'
&& Alexa.getIntentName(handlerInput.requestEnvelope) === 'ChatGPTIntent';
},
handle(handlerInput) {
// Mensagem de boas-vindas que a Alexa irá falar
const speakOutput = '<voice name="Ricardo"> Oi, eu sou o IÁ-glô, assistente virtual do suporte. O que você gostaria de saber?</voice>';
// Definição do documento APL para a tela inicial
const document = {
"type": "APL",
"version": "2024.1",
"license": "Copyright 2024 Amazon.com, Inc. ou suas afiliadas. Todos os direitos reservados.nSPDX-License-Identifier: LicenseRef-.amazon.com.-AmznSL-1.0nLicenciado sob a Licença de Software da Amazon http://aws.amazon.com/asl/",
"theme": "dark",
"import": [
{
"name": "alexa-layouts",
"version": "1.7.0"
}
],
"mainTemplate": {
"parameters": [
"backgroundImageExampleData"
],
"items": [
{
"type": "Container",
"width": "100%",
"height": "100%",
"items": [
{
"type": "AlexaBackground",
"backgroundImageSource": "https://i.imgur.com/Pd42cgt.png"
}
]
}
]
}
};
// Datasources vazias (sem dados adicionais)
const datasources = {};
// Retorna a resposta da Alexa, incluindo o documento APL
return handlerInput.responseBuilder
.speak (speakOutput)
.reprompt(speakOutput)
.addDirective({
type: 'Alexa.Presentation.APL.RenderDocument',
token: 'documentToken',
document: document,
datasources: datasources
})
.getResponse();
}
};
const IntegrarChatGPTIntentHandler = {
canHandle(handlerInput) {
return Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest'
&& Alexa.getIntentName(handlerInput.requestEnvelope) === 'IntegrarChatGPTIntent';
},
async handle(handlerInput) {
const userQuery = handlerInput.requestEnvelope.request.intent.slots.query.value; // Recebe a pergunta do usuário
console.log(`Recebendo consulta do usuário: ${userQuery}`);
const systemRole = "Você é um especialista do helpdesk e deve responder perguntas sobre o Media Composer. Finalize dizendo que, caso a pessoa precise, ela pode abrir um chamado no Globo Service para o suporte parceria.";
try {
const response = await axios.post('https://api.openai.com/v1/chat/completions', {
model: 'gpt-3.5-turbo',
messages: [
{ role: 'system', content: systemRole },
{ role: 'user', content: userQuery }
],
max_tokens: 250
}, {
headers: {
'Authorization': `Bearer ${API_KEY}`
}
});
const gptResponse = response.data.choices[0].message.content.trim();
console.log(`Resposta do GPT: ${gptResponse}`);
const speakOutput = `<voice name="Ricardo">${gptResponse}</voice>`;
return handlerInput.responseBuilder
.speak(speakOutput)
.reprompt('')
.getResponse();
} catch (error) {
console.error(`Erro ao chamar o ChatGPT: ${error}`);
return handlerInput.responseBuilder
.speak('Desculpe, ocorreu um erro ao obter a resposta. Por favor, tente novamente mais tarde.')
.reprompt('')
.getResponse();
}
}
};
Lucas Oliveira is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.