├── .gitignore ├── config └── open-ai.js ├── index.js ├── package-lock.json ├── package.json └── readme.md /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | .env 3 | -------------------------------------------------------------------------------- /config/open-ai.js: -------------------------------------------------------------------------------- 1 | import { Configuration, OpenAIApi } from 'openai'; 2 | import dotenv from 'dotenv'; 3 | dotenv.config(); 4 | 5 | const configuration = new Configuration({ 6 | apiKey: process.env.OPENAI_API_KEY, 7 | }); 8 | 9 | const openai = new OpenAIApi(configuration); 10 | 11 | export default openai; 12 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | import openai from './config/open-ai.js'; 2 | import readlineSync from 'readline-sync'; 3 | import colors from 'colors'; 4 | 5 | async function main() { 6 | console.log(colors.bold.green('Welcome to the Chatbot Program!')); 7 | console.log(colors.bold.green('You can start chatting with the bot.')); 8 | 9 | const chatHistory = []; // Store conversation history 10 | 11 | while (true) { 12 | const userInput = readlineSync.question(colors.yellow('You: ')); 13 | 14 | try { 15 | // Construct messages by iterating over the history 16 | const messages = chatHistory.map(([role, content]) => ({ 17 | role, 18 | content, 19 | })); 20 | 21 | // Add latest user input 22 | messages.push({ role: 'user', content: userInput }); 23 | 24 | // Call the API with user input & history 25 | const completion = await openai.createChatCompletion({ 26 | model: 'gpt-3.5-turbo', 27 | messages: messages, 28 | }); 29 | 30 | // Get completion text/content 31 | const completionText = completion.data.choices[0].message.content; 32 | 33 | if (userInput.toLowerCase() === 'exit') { 34 | console.log(colors.green('Bot: ') + completionText); 35 | return; 36 | } 37 | 38 | console.log(colors.green('Bot: ') + completionText); 39 | 40 | // Update history with user input and assistant response 41 | chatHistory.push(['user', userInput]); 42 | chatHistory.push(['assistant', completionText]); 43 | } catch (error) { 44 | if (error.response) { 45 | console.error(colors.red(error.response.data.error.code)); 46 | console.error(colors.red(error.response.data.error.message)); 47 | return; 48 | } 49 | console.error(colors.red(error)); 50 | return; 51 | } 52 | } 53 | } 54 | 55 | main(); 56 | -------------------------------------------------------------------------------- /package-lock.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "chatbot", 3 | "version": "1.0.0", 4 | "lockfileVersion": 3, 5 | "requires": true, 6 | "packages": { 7 | "": { 8 | "name": "chatbot", 9 | "version": "1.0.0", 10 | "license": "MIT", 11 | "dependencies": { 12 | "colors": "^1.4.0", 13 | "dotenv": "^16.3.1", 14 | "openai": "^3.3.0", 15 | "readline-sync": "^1.4.10" 16 | } 17 | }, 18 | "node_modules/asynckit": { 19 | "version": "0.4.0", 20 | "resolved": "https://registry.npmjs.org/asynckit/-/asynckit-0.4.0.tgz", 21 | "integrity": "sha512-Oei9OH4tRh0YqU3GxhX79dM/mwVgvbZJaSNaRk+bshkj0S5cfHcgYakreBjrHwatXKbz+IoIdYLxrKim2MjW0Q==" 22 | }, 23 | "node_modules/axios": { 24 | "version": "0.26.1", 25 | "resolved": "https://registry.npmjs.org/axios/-/axios-0.26.1.tgz", 26 | "integrity": "sha512-fPwcX4EvnSHuInCMItEhAGnaSEXRBjtzh9fOtsE6E1G6p7vl7edEeZe11QHf18+6+9gR5PbKV/sGKNaD8YaMeA==", 27 | "dependencies": { 28 | "follow-redirects": "^1.14.8" 29 | } 30 | }, 31 | "node_modules/colors": { 32 | "version": "1.4.0", 33 | "resolved": "https://registry.npmjs.org/colors/-/colors-1.4.0.tgz", 34 | "integrity": "sha512-a+UqTh4kgZg/SlGvfbzDHpgRu7AAQOmmqRHJnxhRZICKFUT91brVhNNt58CMWU9PsBbv3PDCZUHbVxuDiH2mtA==", 35 | "engines": { 36 | "node": ">=0.1.90" 37 | } 38 | }, 39 | "node_modules/combined-stream": { 40 | "version": "1.0.8", 41 | "resolved": "https://registry.npmjs.org/combined-stream/-/combined-stream-1.0.8.tgz", 42 | "integrity": "sha512-FQN4MRfuJeHf7cBbBMJFXhKSDq+2kAArBlmRBvcvFE5BB1HZKXtSFASDhdlz9zOYwxh8lDdnvmMOe/+5cdoEdg==", 43 | "dependencies": { 44 | "delayed-stream": "~1.0.0" 45 | }, 46 | "engines": { 47 | "node": ">= 0.8" 48 | } 49 | }, 50 | "node_modules/delayed-stream": { 51 | "version": "1.0.0", 52 | "resolved": "https://registry.npmjs.org/delayed-stream/-/delayed-stream-1.0.0.tgz", 53 | "integrity": "sha512-ZySD7Nf91aLB0RxL4KGrKHBXl7Eds1DAmEdcoVawXnLD7SDhpNgtuII2aAkg7a7QS41jxPSZ17p4VdGnMHk3MQ==", 54 | "engines": { 55 | "node": ">=0.4.0" 56 | } 57 | }, 58 | "node_modules/dotenv": { 59 | "version": "16.3.1", 60 | "resolved": "https://registry.npmjs.org/dotenv/-/dotenv-16.3.1.tgz", 61 | "integrity": "sha512-IPzF4w4/Rd94bA9imS68tZBaYyBWSCE47V1RGuMrB94iyTOIEwRmVL2x/4An+6mETpLrKJ5hQkB8W4kFAadeIQ==", 62 | "engines": { 63 | "node": ">=12" 64 | }, 65 | "funding": { 66 | "url": "https://github.com/motdotla/dotenv?sponsor=1" 67 | } 68 | }, 69 | "node_modules/follow-redirects": { 70 | "version": "1.15.2", 71 | "resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.15.2.tgz", 72 | "integrity": "sha512-VQLG33o04KaQ8uYi2tVNbdrWp1QWxNNea+nmIB4EVM28v0hmP17z7aG1+wAkNzVq4KeXTq3221ye5qTJP91JwA==", 73 | "funding": [ 74 | { 75 | "type": "individual", 76 | "url": "https://github.com/sponsors/RubenVerborgh" 77 | } 78 | ], 79 | "engines": { 80 | "node": ">=4.0" 81 | }, 82 | "peerDependenciesMeta": { 83 | "debug": { 84 | "optional": true 85 | } 86 | } 87 | }, 88 | "node_modules/form-data": { 89 | "version": "4.0.0", 90 | "resolved": "https://registry.npmjs.org/form-data/-/form-data-4.0.0.tgz", 91 | "integrity": "sha512-ETEklSGi5t0QMZuiXoA/Q6vcnxcLQP5vdugSpuAyi6SVGi2clPPp+xgEhuMaHC+zGgn31Kd235W35f7Hykkaww==", 92 | "dependencies": { 93 | "asynckit": "^0.4.0", 94 | "combined-stream": "^1.0.8", 95 | "mime-types": "^2.1.12" 96 | }, 97 | "engines": { 98 | "node": ">= 6" 99 | } 100 | }, 101 | "node_modules/mime-db": { 102 | "version": "1.52.0", 103 | "resolved": "https://registry.npmjs.org/mime-db/-/mime-db-1.52.0.tgz", 104 | "integrity": "sha512-sPU4uV7dYlvtWJxwwxHD0PuihVNiE7TyAbQ5SWxDCB9mUYvOgroQOwYQQOKPJ8CIbE+1ETVlOoK1UC2nU3gYvg==", 105 | "engines": { 106 | "node": ">= 0.6" 107 | } 108 | }, 109 | "node_modules/mime-types": { 110 | "version": "2.1.35", 111 | "resolved": "https://registry.npmjs.org/mime-types/-/mime-types-2.1.35.tgz", 112 | "integrity": "sha512-ZDY+bPm5zTTF+YpCrAU9nK0UgICYPT0QtT1NZWFv4s++TNkcgVaT0g6+4R2uI4MjQjzysHB1zxuWL50hzaeXiw==", 113 | "dependencies": { 114 | "mime-db": "1.52.0" 115 | }, 116 | "engines": { 117 | "node": ">= 0.6" 118 | } 119 | }, 120 | "node_modules/openai": { 121 | "version": "3.3.0", 122 | "resolved": "https://registry.npmjs.org/openai/-/openai-3.3.0.tgz", 123 | "integrity": "sha512-uqxI/Au+aPRnsaQRe8CojU0eCR7I0mBiKjD3sNMzY6DaC1ZVrc85u98mtJW6voDug8fgGN+DIZmTDxTthxb7dQ==", 124 | "dependencies": { 125 | "axios": "^0.26.0", 126 | "form-data": "^4.0.0" 127 | } 128 | }, 129 | "node_modules/readline-sync": { 130 | "version": "1.4.10", 131 | "resolved": "https://registry.npmjs.org/readline-sync/-/readline-sync-1.4.10.tgz", 132 | "integrity": "sha512-gNva8/6UAe8QYepIQH/jQ2qn91Qj0B9sYjMBBs3QOB8F2CXcKgLxQaJRP76sWVRQt+QU+8fAkCbCvjjMFu7Ycw==", 133 | "engines": { 134 | "node": ">= 0.8.0" 135 | } 136 | } 137 | } 138 | } 139 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "chatbot", 3 | "version": "1.0.0", 4 | "description": "Chatbot powered by ChatGPT", 5 | "type": "module", 6 | "main": "index.js", 7 | "scripts": { 8 | "start": "node index.js" 9 | }, 10 | "author": "Brad Traversy", 11 | "license": "MIT", 12 | "dependencies": { 13 | "colors": "^1.4.0", 14 | "dotenv": "^16.3.1", 15 | "openai": "^3.3.0", 16 | "readline-sync": "^1.4.10" 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # ChatGPT Chatbot 2 | 3 | This is a ChatGPT chatbot written in Node.js that runs in the terminal 4 | 5 | ## Usage 6 | 7 | Get your OpenAI API key at https://platform.openai.com/account/api-keys 8 | 9 | Create a file named `.env` and add the following: 10 | 11 | ``` 12 | OPENAI_API_KEY=YOURKEY 13 | ``` 14 | 15 | Install dependencies: 16 | 17 | ```bash 18 | npm Install 19 | ``` 20 | 21 | Run the bot: 22 | 23 | ```bash 24 | npm start 25 | ``` 26 | 27 | --------------------------------------------------------------------------------