Empty response being returned on setting up too many dialogflow web integrations in the website

25 views Asked by At

I have 4 Dialogflow ES chatbots, all of them work and return a response from external APIs, however, when embedded in the website, the chatbot starts returning an empty response, when a single chatbot is embedded, the deployment server is started again the bot works fine.

How to resolve this issue? An example server-side code of one of the bots is here:

const express = require("express");
const axios = require('axios');
const { WebhookClient } = require("dialogflow-fulfillment");
const app = express();
const cors = require('cors');

const key = 'abcd';
app.use(cors());
// Array to store the conversation
let conversation = [];
let mentalHealthSummary = "";

app.post("/webhook", express.json(), (req, res) => {
    const agent = new WebhookClient({ request: req, response: res });
    let intentMap = new Map();
    intentMap.set("Default Welcome Intent", welcome);
    intentMap.set("Default Fallback Intent", queryGPT);
    agent.handleRequest(intentMap);

    function welcome(agent) {
        agent.add('Hi, I am your virtual personal mental health assistant. How are you doing today?');
    }

    async function queryGPT(agent) {
        const OPENAI_API_KEY = key;

        const instance = axios.create({
            baseURL: 'https://api.openai.com/v1/',
            headers: { Authorization: `Bearer ${OPENAI_API_KEY}` },
        });

        const dialog = [
            `The following is a conversation with an AI assistant that can have meaningful conversations with users. The assistant is helpful, empathic, and friendly. Its objective is to make the user feel better by feeling heard. With each response, the AI assistant prompts the user to continue the conversation in a natural way.`,
        ];

        let query = agent.query;
        console.log('querytext ', query)
        dialog.push(`User: ${query}`);
        dialog.push('AI:');

        const completionParams = {
            model: 'gpt-3.5-turbo',
            messages: dialog.map((message, index) => {
                return { role: index % 2 === 0 ? 'system' : 'user', content: message };
            }),
        };

        try {
            const result = await instance.post('/chat/completions', completionParams);
            const botResponse = result.data.choices[0].message.content.trim();
            agent.add(botResponse);

            // Add the message to the conversation array
            addToConversation(`User: ${query}`, `AI: ${botResponse}`);
            // const convojson = JSON.stringify(conversation)
            // mentalHealthGPT(convojson);
        } catch (err) {
            console.log(err);
            agent.add('Sorry. Something went wrong. Can you say that again?');
        }
    }

    // Function to add a message to the conversation array
    function addToConversation(userMessage, botMessage) {
        conversation.push(userMessage);
        conversation.push(botMessage);
    }

});

// Route to get the entire conversation
app.get('/conversation', (req, res) => {
    res.json({ conversation });
});


// Route to generate a mental health report
app.get('/mentalHealthReport', async (req, res) => {
    try {
        // Extract user messages from the conversation
        const userMessages = conversation.filter(message => message.role === 'user');
        
        // Create a text string of user messages
        const userQuery = userMessages.map(message => message.content).join('.');
        console.log("userMessages:", userMessages);

        // Use OpenAI API to generate a mental health report
        const OPENAI_API_KEY = key;
        const instance = axios.create({
            baseURL: 'https://api.openai.com/v1/',
            headers: { Authorization: `Bearer ${OPENAI_API_KEY}` },
        });

        const dialog = [
            `For the ${conversation} take the last 2-3 sentences, and generate a mental health report for the parent of the user whose report has been generated. Also, suggest tips for the parent to help the user, i.e., the kid, to deal with the situation. Write the entire thing in a paragraph, straightforward and to the point.`,
        ];

        const completionParams = {
            model: 'gpt-3.5-turbo',
            messages: dialog.map((message, index) => {
                return { role: 'system', content: message };
            }),
        };

        const result = await instance.post('/chat/completions', completionParams);
        const mentalHealthReport = result.data.choices[0].message.content.trim();

        // Send the generated mental health report as JSON response
        res.json({ mentalHealthReport });
    } catch (err) {
        console.log(err);
        res.status(500).json({ error: 'Internal Server Error' });
    }
});

const port = 3000;
app.listen(port, () => console.log(`App listening on port ${port}!`));
0

There are 0 answers