Triển khai CHATGPT với Node js
ChatGPT là gì :
ChatGPT là một chatbot trí tuệ nhân tạo do OpenAI phát triển và ra mắt vào tháng 11 năm 2022. Nó được xây dựng dựa trên các mô hình ngôn ngữ lớn GPT-3 của OpenAI và đã được tinh chỉnh bằng cả hai phương pháp học máy được giám sát trong đó dữ liệu đào tạo có câu trả lời chính với nó để mô hình được đào tạo với chính câu trả lời đúng ] và củng cố [ phương pháp học máy trong đó một tác nhân thông minh (chương trình máy tính) tương tác với môi trường và học cách hành động trong đó ] các kỹ thuật học.
Nói một cách đơn giản, chúng ta có thể nói rằng “ ChatGPT là một chatbot trò chuyện với người dùng bằng ngôn ngữ của con người “.
Triển khai trong Ứng dụng Node js
Để sử dụng ChatGPT trong ứng dụng nút js của chúng tôi, chúng tôi sẽ sử dụng OpenAI apis . Để sử dụng apis openAI, chúng tôi cần lấy khóa API từ openAI và sử dụng thư viện máy khách API, chẳng hạn như gói openai npm, để thực hiện các yêu cầu đối với API từ ứng dụng nút js của chúng tôi.
Nhận Khóa API từ trang web OpenAI :
Truy cập trang web openAI ( https://openai.com/ ) và sau khi đăng ký, tìm kiếm phần “API” trong cài đặt tài khoản của bạn và tạo Khóa API. Đừng quên giữ bí mật và an toàn khóa Api của bạn.
Tạo ứng dụng nút cơ bản:
Đi đến thiết bị đầu cuối trước và tạo dự án bằng cách sử dụng
mkdir chatgpt-nodeproject
cd chatgpt-nodeproject
code .
npm init -y
npm install express openai dotenv
touch server.js
touch .env
touch config.js
PORT=5003
OPEN_AI_KEY="PASTE YOUR API KEY HERE"
require('dotenv').config({ debug: process.env.DEBUG });
let cfg = {
port: process.env.PORT,
openaikey:process.env.OPEN_AI_KEY
}
module.exports = {
cfg
}
const express = require('express');
const config = require("./config").cfg;
const app = express();
app.use(express.json());
app.listen(config.port, () => {
console.log(`Example app listening at http://localhost:${config.port}`);
});
const { Configuration, OpenAIApi } = require("openai");
const configuration = new Configuration({
apiKey: config.openaikey,
});
const openai = new OpenAIApi(configuration);
app.post("/find-complexity", async(req,res)=> {
try{
const {prompt} = req.body;
const response = openai.createCompletion({
model: "text-davinci-003",
prompt: `${prompt} The complexity of this function is `, //{"prompt": "const example = (arr)=> { arr.map((item)=> { console.log(item)});}"}
});
return res.status(200).json({success:true,data:(await response).data.choices[0].text})
}catch(error){console.log(error,"error....")}
})
const express = require('express');
const config = require("./config").cfg;
const { Configuration, OpenAIApi } = require("openai");
const configuration = new Configuration({
apiKey: config.openaikey,
});
const openai = new OpenAIApi(configuration);
const app = express();
app.use(express.json());
app.post("/factual-answering", async(req,res)=> {
try{
const {prompt} = req.body;
const response = await openai.createCompletion({
model: "text-davinci-003",
prompt: `${prompt}`, // {"prompt": "Q: Pros and cons of callback and Promise\nA:"}
max_tokens: 100,
});
return res.status(200).json({success:true,data:(await response).data.choices[0].text})
}catch(error){console.log(error,"error....")}
})
app.post("/find-complexity", async(req,res)=> {
try{
const {prompt} = req.body;
const response = openai.createCompletion({
model: "text-davinci-003",
prompt: `${prompt} The complexity of this function is `, //{"prompt": "const example = (arr)=> { arr.map((item)=> { console.log(item)});}"}
});
return res.status(200).json({success:true,data:(await response).data.choices[0].text})
}catch(error){console.log(error,"error....")}
})
app.post("/imageGenerator-from-text", async(req,res)=> {
try{
const {prompt} = req.body;
const response = await openai.createImage({
prompt: `${prompt}`, //{"prompt": "kasoli in winter"}
n: 2, // number of images to create
size: "1024x1024",
});
return res.status(200).json({success:true,data:(await response).data})
}catch(error){console.log(error,"error....")}
})
app.post("/interview-questions", async(req,res)=> {
try{
const {prompt} = req.body;
const response = await openai.createCompletion({
model: "text-davinci-003",
prompt: `${prompt}`,//{ "prompt": "Create a list of 8 questions for interview in MERN:"}
max_tokens : 150
});
return res.status(200).json({success:true,data:(await response).data.choices[0].text})
}catch(error){console.log(error,"error....")}
})
app.listen(config.port, () => {
console.log(`Example app listening at http://localhost:${config.port}`);
});
Đi đến người đưa thư và nhấn một yêu cầu POST “http://localhost:5003/factual-answering”
Bạn cũng có thể sao chép repo của tôi “ https://github.com/aditiipriya/chatgptImplementation ”.
Hy vọng nó sẽ giúp .. Cảm ơn bạn !! :)

![Dù sao thì một danh sách được liên kết là gì? [Phần 1]](https://post.nghiatu.com/assets/images/m/max/724/1*Xokk6XOjWyIGCBujkJsCzQ.jpeg)



































