Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ja jp #67

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open

Ja jp #67

wants to merge 6 commits into from

Conversation

kexuanZhu09
Copy link

const Alexa = require('ask-sdk-core');
const axios = require('axios');

const CHATGPT_API_URL = 'https://api.openai.com/v1/engines/davinci-codex/completions';
const CHATGPT_API_KEY = 'YOUR_OPENAI_API_KEY';

const LaunchRequestHandler = {
canHandle(handlerInput) {
return Alexa.getRequestType(handlerInput.requestEnvelope) === 'LaunchRequest';
},
async handle(handlerInput) {
const speakOutput = 'Welcome to ChatGPT. How can I assist you today?';
return handlerInput.responseBuilder
.speak(speakOutput)
.reprompt(speakOutput)
.getResponse();
}
};

const ChatGPTIntentHandler = {
canHandle(handlerInput) {
return Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest'
&& Alexa.getIntentName(handlerInput.requestEnvelope) === 'ChatGPTIntent';
},
async handle(handlerInput) {
const { request } = handlerInput.requestEnvelope;
const query = request.intent.slots.query.value;

const response = await axios.post(CHATGPT_API_URL, {
  prompt: query,
  max_tokens: 100,
  n: 1,
  stop: '\n',
  temperature: 0.5
}, {
  headers: {
    'Authorization': `Bearer ${CHATGPT_API_KEY}`,
    'Content-Type': 'application/json'
  }
});

const message = response.data.choices[0].text.trim();
const speakOutput = `ChatGPT says ${message}`;

return handlerInput.responseBuilder
  .speak(speakOutput)
  .getResponse();

}
};

const ErrorHandler = {
canHandle() {
return true;
},
handle(handlerInput, error) {
console.error(Error: ${error.message});

const speakOutput = 'Sorry, there was an error. Please try again later.';

return handlerInput.responseBuilder
  .speak(speakOutput)
  .getResponse();

},
};

exports.handler = Alexa.SkillBuilders.custom()
.addRequestHandlers(
LaunchRequestHandler,
ChatGPTIntentHandler
)
.addErrorHandlers(ErrorHandler)
.lambda();

Justin and others added 6 commits January 10, 2018 10:21
Add display directives for multi-modal experience
Japanese localized version of Quiz Game template
Update ja-JP branch based on the newest en-US branch
Fix the issue that the image shouldn't be appeared on it's own line.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants