无法命中firebase云函数中的路由对话流

vkc1a9a2  于 2023-03-09  发布在  其他
关注(0)|答案(1)|浏览(107)

我正在尝试制作一个GC聊天集成的对话流机器人与openAI集成在firebase云函数。我无法点击/dialogflow,我得到这个错误:“无法获取/对话流”。我使用的是Node.js。所有其他路由都工作正常。下面是app.js中的代码,我有另外两个文件server.js和index.js。请帮助。以下是我的代码:

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!");
  }
  /**
 * Generates text using the OpenAI API.
 *
 * @async
 * @function
 * @param {string} prompt - The text prompt to generate a response for.
 * @return {Promise<{status: number, response: string}>} - A promise that resolves to an object with a status code and response text.
 */
  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;

我的server.js代码:

const app = require("./app");

app.listen(process.env.PORT || 8080);

我的Index.js代码

const functions = require("firebase-functions");
const app = require("./app");

exports.app = functions.https.onRequest(app);
7kqas0il

7kqas0il1#

您的/dialogflow路由已设置为响应POST请求:

app.post("/dialogflow", express.json(), (req, res) => { ... }

它不适用于GET。

相关问题