Oracle Digital Assistant (ODA) provides a comprehensive platform for creating conversational interfaces. This article will guide you through integrating ChatGPT with ODA using the bots-node-sdk and openai libraries.
Prerequisites:
- Oracle Digital Assistant instance
- ChatGPT API key
- Node.js environment
Configuration:
Create a new file named services.js and add the following code:
const OracleBot = require('@oracle/bots-node-sdk');
const { WebhookClient, WebhookEvent } = OracleBot.Middleware;
const express = require('express');
const { Configuration, OpenIApi } = require("openai");
const configuration = new Configuration({
apiKey: "YOUR_CHATGPT_API_KEY",
});
const openai = new OpenIApi(configuration);
const textGeneration = async (prompt) => {
try {
const response = await openai.createCompletion({
model: 'text-davinci-003',
prompt: `Human: ${prompt}\nAI: `,
temperature: 0.9,
max_tokens: 500,
top_p: 1,
frequency_penalty: 0,
presence_penalty: 0.6,
stop: ['Human:', 'AI:'],
});
return {
status: 1,
response: `${response.data.choices[0].text}`,
};
} catch (error) {
return {
status: 0,
response: '',
};
}
};
module.exports = (app) => {
const logger = console;
// Initialize Oracle Digital Assistant
OracleBot.init(app, {
logger,
});
// Set up webhook integration
const webhook = new WebhookClient({
channel: {
url: "YOUR_ODA_WEBHOOK_URL",
secret: "YOUR_ODA_WEBHOOK_SECRET",
},
});
// Handle incoming messages
webhook.on(WebhookEvent.MESSAGE_RECEIVED, (message) => {
const action = message.queryResult.action;
const queryText = message.queryResult.queryText;
if (action === 'input.unknown') {
textGeneration(queryText).then((result) => {
if (result.status === 1) {
res.send({
fulfillmentMessages: [
{
text: {
text: [result.response],
},
},
],
});
} else {
res.send({
fulfillmentMessages: [
{
text: {
text: ["Sorry, I'm not able to help with that."],
},
},
],
});
}
});
} else {
res.send({
fulfillmentMessages: [
{
text: {
text: [`No handler for action ${action}`],
},
},
],
});
}
});
// Set up endpoint for incoming messages
app.post('/bot/message', (req, res) => {
const message = req.body;
webhook.send(message).then(() => res.send('ok'));
});
};
- Replace YOUR_CHATGPT_API_KEY with your actual ChatGPT API key.
- Replace YOUR_ODA_WEBHOOK_URL and YOUR_ODA_WEBHOOK_SECRET with your actual Oracle Digital Assistant webhook URL and secret.
Dialog Flow Integration
To integrate the ChatGPT service with your Oracle Digital Assistant dialog flow, follow these steps:
Create a new intent in your dialog flow with the action input.unknown.
Add a new fulfillment to the intent with the following settings:
Fulfillment type: Webhook
Webhook URL: YOUR_APP_URL/bot/message (replace with your actual app URL)
HTTP method: POST
Save and deploy your dialog flow.
Testing
Test your integration by sending a message to your Oracle Digital Assistant instance. The message should be routed to the ChatGPT service, which will generate a response. The response will then be sent back to the user.
Note: Make sure to replace the placeholder values with your actual credentials and URLs.
No comments:
Post a Comment