Implementazione di CHATGPT con Node js
Che cos'è ChatGPT :
ChatGPT è un chatbot di intelligenza artificiale sviluppato da OpenAI e lanciato nel novembre 2022. È costruito sulla base della famiglia GPT-3 di OpenAI di modelli linguistici di grandi dimensioni ed è stato messo a punto utilizzando sia il metodo di apprendimento automatico supervisionato in cui i dati di addestramento hanno la risposta chiave con esso in modo che il modello venga addestrato con la risposta corretta stessa ] e rinforzo [metodo di apprendimento automatico in cui un agente intelligente (programma per computer) interagisce con l'ambiente e impara ad agire all'interno di esso] tecniche di apprendimento.
In parole semplici, possiamo dire che "ChatGPT è un chatbot che chatta con gli utenti in linguaggio umano".
Implementare nell'applicazione Node js
Per utilizzare ChatGPT nella nostra applicazione node js utilizzeremo OpenAI apis . Per utilizzare le API openAI, dobbiamo ottenere una chiave API da openAI e utilizzare una libreria client API, come il pacchetto npm openai, per effettuare richieste all'API dalla nostra applicazione node js.
Ottieni la chiave API dal sito web di OpenAI:
Vai al sito Web openAI ( https://openai.com/ ) e dopo la registrazione cerca la sezione "API" nelle impostazioni del tuo account e genera la chiave API. Non dimenticare di mantenere segreta e sicura la tua chiave API.
Crea un'applicazione nodo di base:
Vai prima al terminale e crea il progetto usando
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}`);
});
Vai al postino e premi una richiesta POST "http://localhost:5003/factual-answering"
Puoi anche clonare il mio repository " https://github.com/aditiipriya/chatgptImplementation ".
Spero che aiuti .. Grazie !! :)

![Che cos'è un elenco collegato, comunque? [Parte 1]](https://post.nghiatu.com/assets/images/m/max/724/1*Xokk6XOjWyIGCBujkJsCzQ.jpeg)



































