I am building a project where I’m using chat engine and Open AI’s gpt-3.5. Basically this is web based chat application where you can chat with normal user and there will be a option called chat with AI . After using chatgpt-3.5-turbo , I got to know that , the credit points for tokens are not enough to experiment. So I use reverse proxy method to use it for free. Below I’m sharing my code =>
import express from "express";
import axios from "axios";
import dotenv from "dotenv";
import { openai } from "../index.js";
dotenv.config();
const router = express.Router();
router.post("/text", async(req,res) => {
try {
const { text,activeChatId } = req.body;
console.log(" ~ router.post ~ req.body:", req.body)
const response = await openai.chat.completions.create({
model:"gpt-3.5-turbo" ,
prompt: text,
temperature: 0.7,
max_tokens: 2048,
top_p: 1,
frequency_penalty: 0.5,
presence_penalty: 0,
stream: true,
});
await axios.post(
`https://api.openai.com/chats/${activeChatId}/messages/`,
{ text: response.choices[0].message.content },
{
headers: {
"Project-ID": process.env.PROJECT_ID,
"User-Name": process.env.BOT_USER_NAME,
"User-Secret": process.env.BOT_USER_SECRET,
}
}
);
res.status(200).json({ text: response.choices[0].message.content })
} catch (error) {
console.error("1st error",error);
res.status(500).json({ error: error.message })
}
})
export default router;
my index.js file =>
import express from "express";
import bodyParser from "body-parser";
import cors from "cors";
import dotenv from "dotenv";
import helmet from "helmet";
import morgan from "morgan";
import OpenAI from "openai";
import openAiRoutes from "./routes/openai.js";
/*CONFIGURATION*/
dotenv.config();
const app = express();
app.use(express.json());
app.use(helmet());
app.use(helmet.crossOriginResourcePolicy({ policy: "cross-origin"}))
app.use(morgan("common"));
app.use(bodyParser.json({ limit:"30mb", extended:"true" }));
app.use(bodyParser.urlencoded({ limit:"30mb", extended: "true" }));
app.use(cors());
// OPENAI CONFIGURATION
export const openai = new OpenAI({
apiKey: process.env.OPEN_API_KEY,
baseURL: "http://localhost:3040/v1",
});
app.use("/openai",openAiRoutes);
const PORT = process.env.PORT || 9000;
app.listen(PORT, () => {
console.log(`It is running on Port: ${PORT}`);
});
When i try to run this code, it’s giving me this error =>
::1 - - [26/May/2024:20:00:08 +0000] "OPTIONS /openai/text HTTP/1.1" 204 0
~ router.post ~ req.body: {
attachments: [],
created: '2024-05-26 20:00:08.331885+00:00',
sender_username: 'testid',
text: 'hi',
activeChatId: 252894
}
1st error TypeError: Cannot read properties of undefined (reading '0')
at file:///D:/Web%20D/Chat-app/server/routes/openai.js:26:37
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
::1 - - [26/May/2024:20:00:10 +0000] "POST /openai/text HTTP/1.1" 500 61
Can anybody give me any solution for this problem.
TOM Mark is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.