gpt4 book ai didi

node.js - GPT 3.5 Turbo 与 Dialogflow 集成

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

我正在尝试使用谷歌云功能在 Node.js 中将 Dialogflow 机器人与新的 openai API GPT 3.5 Turbo 集成。我有下面的代码抛出错误“完成状态代码:404”。我还有另外两个文件。我尝试使用文本 davinci 003,它工作正常。有人可以帮忙吗?

应用程序.js

const express = require("express");
const axios = require("axios");
const {Configuration, OpenAIApi} = require("openai");
require("dotenv").config();
const bodyParser = require("body-parser");


const configuration = new Configuration({
apiKey: process.env.OPENAI_API_KEY,
});
const openai = new OpenAIApi(configuration);

const textGeneration = async (prompt) => {
try {
const response = await openai.createChatCompletion({
model: "gpt-3.5-turbo",
prompt: `Human: ${prompt}\nAI: `,
messages: [
{"role": "user", "content": prompt},
],
temperature: 0.5,
max_tokens: 100,
top_p: 1,
frequency_penalty: 0,
presence_penalty: 0.6,
stop: ["Human:", "AI:"],
});
return {
status: 1,
response: `${response.data.choices[0].message.content}`,
};
} catch (error) {
return {
status: 0,
response: "",
};
}
};


const app = express();

app.use(express.urlencoded({extended: true}));
app.use(express.json());
app.use(
bodyParser.json({
limit: "250mb",
}),
),
app.use(
bodyParser.urlencoded({
limit: "250mb",
extended: true,
parameterLimit: 250000,
}),
),


app.use((req, res, next) => {
console.log(`Path ${req.path} with Method ${req.method}`);
next();
});


app.get("/", (req, res) => {
res.sendStatus(200);
});


app.get("/dialogflow", async (req, res) => {
const action = req.body.queryResult.action;
const queryText = req.body.queryResult.queryText;

if (action === "input.unknown") {
const result = await textGeneration(queryText);
if (result.status == 1) {
res.send(
{
fulfillmentText: result.response,
},
);
} else {
res.send(
{
fulfillmentText: `Sorry, I'm not able to help with that.`,
},
);
}
} else {
res.send(
{
fulfillmentText: `No handler for the action ${action}.`,
},
);
}
});

module.exports =app;

索引.js

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

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

服务器.js

const app = require("./app");
app.listen(process.env.PORT || 8080);

最佳答案

我遇到了同样的问题,自 5 天前以来我一直在努力解决这个问题。适合我从“text-davinci-003”切换到“gpt-turbo”的解决方案如下:

npm i @maxijonson/gpt-turbo

import express from 'express';
import * as dotenv from 'dotenv';
import cors from 'cors';
import { Conversation } from '@maxijonson/gpt-turbo';
const port = process.env.PORT || 5000;

dotenv.config();


const app = express();
app.use(cors());
app.use(express.json());

app.get('/', async (req, res) => {
res.status(200).send({
message: 'Welcome to Genus SMART AI!'
})
});

app.post('/', async (req, res) => {
try {
const prompt = req.body.prompt;
const conversation = new Conversation({
apiKey: process.env.OPENAI_API_KEY,
});
const response = await conversation.prompt(`${prompt}`);
res.status(200).send({
bot: response,
});

} catch (error) {
console.error(error)
res.status(500).send(error || 'Something went wrong');
}
});
app.listen(port, () => console.log(`Server started on port htt

p://localhost:${port}`));conversation. Prompt

关于node.js - GPT 3.5 Turbo 与 Dialogflow 集成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/75657590/

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