├── .gitignore ├── LICENSE ├── package-lock.json ├── package.json └── script.js /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | .env -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2023 WebDevSimplified 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /package-lock.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "chat-gpt-api", 3 | "version": "1.0.0", 4 | "lockfileVersion": 3, 5 | "requires": true, 6 | "packages": { 7 | "": { 8 | "name": "chat-gpt-api", 9 | "version": "1.0.0", 10 | "license": "ISC", 11 | "dependencies": { 12 | "dotenv": "^16.0.3", 13 | "openai": "^3.2.1" 14 | } 15 | }, 16 | "node_modules/asynckit": { 17 | "version": "0.4.0", 18 | "resolved": "https://registry.npmjs.org/asynckit/-/asynckit-0.4.0.tgz", 19 | "integrity": "sha512-Oei9OH4tRh0YqU3GxhX79dM/mwVgvbZJaSNaRk+bshkj0S5cfHcgYakreBjrHwatXKbz+IoIdYLxrKim2MjW0Q==" 20 | }, 21 | "node_modules/axios": { 22 | "version": "0.26.1", 23 | "resolved": "https://registry.npmjs.org/axios/-/axios-0.26.1.tgz", 24 | "integrity": "sha512-fPwcX4EvnSHuInCMItEhAGnaSEXRBjtzh9fOtsE6E1G6p7vl7edEeZe11QHf18+6+9gR5PbKV/sGKNaD8YaMeA==", 25 | "dependencies": { 26 | "follow-redirects": "^1.14.8" 27 | } 28 | }, 29 | "node_modules/combined-stream": { 30 | "version": "1.0.8", 31 | "resolved": "https://registry.npmjs.org/combined-stream/-/combined-stream-1.0.8.tgz", 32 | "integrity": "sha512-FQN4MRfuJeHf7cBbBMJFXhKSDq+2kAArBlmRBvcvFE5BB1HZKXtSFASDhdlz9zOYwxh8lDdnvmMOe/+5cdoEdg==", 33 | "dependencies": { 34 | "delayed-stream": "~1.0.0" 35 | }, 36 | "engines": { 37 | "node": ">= 0.8" 38 | } 39 | }, 40 | "node_modules/delayed-stream": { 41 | "version": "1.0.0", 42 | "resolved": "https://registry.npmjs.org/delayed-stream/-/delayed-stream-1.0.0.tgz", 43 | "integrity": "sha512-ZySD7Nf91aLB0RxL4KGrKHBXl7Eds1DAmEdcoVawXnLD7SDhpNgtuII2aAkg7a7QS41jxPSZ17p4VdGnMHk3MQ==", 44 | "engines": { 45 | "node": ">=0.4.0" 46 | } 47 | }, 48 | "node_modules/dotenv": { 49 | "version": "16.0.3", 50 | "resolved": "https://registry.npmjs.org/dotenv/-/dotenv-16.0.3.tgz", 51 | "integrity": "sha512-7GO6HghkA5fYG9TYnNxi14/7K9f5occMlp3zXAuSxn7CKCxt9xbNWG7yF8hTCSUchlfWSe3uLmlPfigevRItzQ==", 52 | "engines": { 53 | "node": ">=12" 54 | } 55 | }, 56 | "node_modules/follow-redirects": { 57 | "version": "1.15.2", 58 | "resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.15.2.tgz", 59 | "integrity": "sha512-VQLG33o04KaQ8uYi2tVNbdrWp1QWxNNea+nmIB4EVM28v0hmP17z7aG1+wAkNzVq4KeXTq3221ye5qTJP91JwA==", 60 | "funding": [ 61 | { 62 | "type": "individual", 63 | "url": "https://github.com/sponsors/RubenVerborgh" 64 | } 65 | ], 66 | "engines": { 67 | "node": ">=4.0" 68 | }, 69 | "peerDependenciesMeta": { 70 | "debug": { 71 | "optional": true 72 | } 73 | } 74 | }, 75 | "node_modules/form-data": { 76 | "version": "4.0.0", 77 | "resolved": "https://registry.npmjs.org/form-data/-/form-data-4.0.0.tgz", 78 | "integrity": "sha512-ETEklSGi5t0QMZuiXoA/Q6vcnxcLQP5vdugSpuAyi6SVGi2clPPp+xgEhuMaHC+zGgn31Kd235W35f7Hykkaww==", 79 | "dependencies": { 80 | "asynckit": "^0.4.0", 81 | "combined-stream": "^1.0.8", 82 | "mime-types": "^2.1.12" 83 | }, 84 | "engines": { 85 | "node": ">= 6" 86 | } 87 | }, 88 | "node_modules/mime-db": { 89 | "version": "1.52.0", 90 | "resolved": "https://registry.npmjs.org/mime-db/-/mime-db-1.52.0.tgz", 91 | "integrity": "sha512-sPU4uV7dYlvtWJxwwxHD0PuihVNiE7TyAbQ5SWxDCB9mUYvOgroQOwYQQOKPJ8CIbE+1ETVlOoK1UC2nU3gYvg==", 92 | "engines": { 93 | "node": ">= 0.6" 94 | } 95 | }, 96 | "node_modules/mime-types": { 97 | "version": "2.1.35", 98 | "resolved": "https://registry.npmjs.org/mime-types/-/mime-types-2.1.35.tgz", 99 | "integrity": "sha512-ZDY+bPm5zTTF+YpCrAU9nK0UgICYPT0QtT1NZWFv4s++TNkcgVaT0g6+4R2uI4MjQjzysHB1zxuWL50hzaeXiw==", 100 | "dependencies": { 101 | "mime-db": "1.52.0" 102 | }, 103 | "engines": { 104 | "node": ">= 0.6" 105 | } 106 | }, 107 | "node_modules/openai": { 108 | "version": "3.2.1", 109 | "resolved": "https://registry.npmjs.org/openai/-/openai-3.2.1.tgz", 110 | "integrity": "sha512-762C9BNlJPbjjlWZi4WYK9iM2tAVAv0uUp1UmI34vb0CN5T2mjB/qM6RYBmNKMh/dN9fC+bxqPwWJZUTWW052A==", 111 | "dependencies": { 112 | "axios": "^0.26.0", 113 | "form-data": "^4.0.0" 114 | } 115 | } 116 | } 117 | } 118 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "chat-gpt-api", 3 | "version": "1.0.0", 4 | "description": "", 5 | "main": "script.js", 6 | "type": "module", 7 | "scripts": { 8 | "test": "echo \"Error: no test specified\" && exit 1" 9 | }, 10 | "keywords": [], 11 | "author": "", 12 | "license": "ISC", 13 | "dependencies": { 14 | "dotenv": "^16.0.3", 15 | "openai": "^3.2.1" 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /script.js: -------------------------------------------------------------------------------- 1 | import { config } from "dotenv" 2 | config() 3 | 4 | import { Configuration, OpenAIApi } from "openai" 5 | import readline from "readline" 6 | 7 | const openAi = new OpenAIApi( 8 | new Configuration({ 9 | apiKey: process.env.OPEN_AI_API_KEY, 10 | }) 11 | ) 12 | 13 | const userInterface = readline.createInterface({ 14 | input: process.stdin, 15 | output: process.stdout, 16 | }) 17 | 18 | userInterface.prompt() 19 | userInterface.on("line", async input => { 20 | const response = await openAi.createChatCompletion({ 21 | model: "gpt-3.5-turbo", 22 | messages: [{ role: "user", content: input }], 23 | }) 24 | console.log(response.data.choices[0].message.content) 25 | userInterface.prompt() 26 | }) 27 | --------------------------------------------------------------------------------