I am currently working on creating a chatbot using Dialogflow integrated with OpenAI in Firebase Cloud Function. However, I am facing an issue where I cannot access the /dialogflow endpoint and keep receiving the error message: "Cannot GET /dialogflow". My project is built using Node.js and all other routes seem to be functioning properly. I have split my code into three files - app.js, server.js, and index.js. Any assistance with this would be greatly appreciated. Here is the snippet of my code:
const functions = require("firebase-functions");
const {Configuration, OpenAIApi} = require("openai");
require("dotenv").config();
const configuration = new Configuration({
apiKey: process.env.OPENAI_API_KEY,
});
const openai = new OpenAIApi(configuration);
const express = require("express");
const {WebhookClient} = require("dialogflow-fulfillment");
const app = express();
app.get("/", (req, res) => res.send("online"));
app.post("/dialogflow", express.json(), (req, res) => {
const prompt = new WebhookClient({request: req, response: res});
/**
* A function to welcome the user to the agent.
* @param {Object} agent - The Dialogflow agent object.
*/
function welcome() {
prompt.add("Welcome to my agent!");
}
async function queryGPT(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: "",
};
}
}
const intentMap = new Map();
intentMap.set("Default Welcome Intent", welcome);
intentMap.set("Default Fallback Intent", queryGPT);
prompt.handleRequest(intentMap);
});
module.exports = app;
Code snippet for server.js :
const app = require("./app");
app.listen(process.env.PORT || 8080);
Code snippet for Index.js
const functions = require("firebase-functions");
const app = require("./app");
exports.app = functions.https.onRequest(app);