I working on a project that involves connecting chatgpt OpenAI to an existing Dialogflow Chatbot. So I decided to create a demo before starting on research I encountered a way to do this with node.js. But the code does not quiet work yet it should I have tried but not sure. I mostly got the idea and code online. So i need help in knowing how the code could work for the demo if it is correct? what should I do better.
code:
const dialogflow = require("@google-cloud/dialogflow");
const { WebhookClient, Payload } = require("dialogflow-fulfillment");
const express = require("express");
const twilio = require("twilio");
const axios = require("axios");
// Step 1: Create a client for handling Dialogflow sessions
const sessionClient = new dialogflow.SessionsClient();
// Step 2: Load the OpenAI configuration and API key from environment variables
const OpenAI = require("openai");
// import { Configuration, OpenAIApi } from "openai";
//step 2.1 Load environment variables
require("dotenv").config();
//OpenAI configuration
const openai = new OpenAI({
apiKey: process.env.OPENAI_API_KEY,
});
// Step 3: Initialize OpenAI API with the loaded configuration
// const openai = new OpenAI(configuration);
// Step 4: Define a function for text generation using OpenAI's GPT-3.5 Turbo model
const textGeneration = async (prompt) => {
try {
const response = await openai.chat.completions.create({
model: "gpt-3.5-turbo",
messages: [
{
role: "user",
content: `Human: ${prompt}`, // You should provide a user message or prompt here.
},
],
temperature: 1, // Control the randomness of the output.
max_tokens: 256, // Set the maximum length of the generated text.
top_p: 1, // Control the probability of the generated tokens.
frequency_penalty: 0, // Adjust the penalty for using frequent tokens.
presence_penalty: 0, // Adjust the penalty for using new tokens.
});
} catch (error) {
// Step 5: Handle any errors that may occur during API call
return {
status: 0, // Indicate failure status if there's an error
response: "", // Provide an empty response in case of an error
};
}
};
//initialise express
const webApp = express();
const PORT = process.env.PORT || 5001;
webApp.use(
express.urlencoded({
extended: true,
})
);
webApp.use(express.json());
webApp.use((req, res, next) => {
console.log(`Path ${req.path} with Method ${req.method}`);
next();
});
webApp.get("/", (req, res) => {
res.sendStatus(200);
res.send("Status Okay");
});
webApp.post("/dialogflow", async (req, res) => {
let id = res.req.body.session.substr(43);
console.log(id);
const agent = new WebhookClient({
request: req,
response: res,
});
//The main section as this part handles the dialogflow standpoint
async function fallback() {
let action = req.body.queryResult.action;
let queryText = req.body.queryResult.queryText;
if (action === "input.unknown") {
let result = await textGeneration(queryText);
if (result.status === 1) {
agent.add(result.response);
} else {
agent.add("Sorry, I am not able to help");
}
}
}
function hi(agent) {
console.log("Intent => hi");
agent.add("Hi, I am your virtual assistant. Tell me how can I help you");
}
let intentMap = new Map();
intentMap.set("Default Welcome Intent", hi);
intentMap.set("Default Fallback Intent", fallback);
agent.handleRequest(intentMap);
});
webApp.listen(PORT, () => {
console.log(`Server is up and running at http://localhost:${PORT}`);
});