gpt4 book ai didi

javascript - 无法在 firebase 云功能中点击路由对话流

转载 作者:行者123 更新时间:2023-12-02 22:45:52 25 4
gpt4 key购买 nike

我正在尝试在 firebase 云功能中制作一个与 openAI 集成的 GC 聊天集成 Dialogflow 机器人。我无法点击/dialogflow,出现此错误:“无法获取/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);

最佳答案

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

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

它不适用于 GET。

关于javascript - 无法在 firebase 云功能中点击路由对话流,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/75561160/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com