From 186ecd7bc14520a08b47c281a0a15d4071120342 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Sat, 23 Mar 2024 05:00:56 +0000 Subject: [PATCH 1/8] Bump axios and openai Removes [axios](https://github.com/axios/axios). It's no longer used after updating ancestor dependency [openai](https://github.com/openai/openai-node). These dependencies need to be updated together. Removes `axios` Updates `openai` from 3.3.0 to 4.29.2 - [Release notes](https://github.com/openai/openai-node/releases) - [Changelog](https://github.com/openai/openai-node/blob/master/CHANGELOG.md) - [Commits](https://github.com/openai/openai-node/compare/v3.3.0...v4.29.2) --- updated-dependencies: - dependency-name: axios dependency-type: indirect - dependency-name: openai dependency-type: direct:production ... Signed-off-by: dependabot[bot] --- package-lock.json | 245 +++++++++++++++++++++++++++++++++++++++------- package.json | 2 +- 2 files changed, 208 insertions(+), 39 deletions(-) diff --git a/package-lock.json b/package-lock.json index 4a3f6b3..5f32952 100644 --- a/package-lock.json +++ b/package-lock.json @@ -11,7 +11,7 @@ "dependencies": { "dotenv": "16.3.1", "express": "4.18.2", - "openai": "3.3.0" + "openai": "4.29.2" }, "devDependencies": { "@types/node": "^20.3.1", @@ -177,17 +177,36 @@ "version": "20.11.30", "resolved": "https://registry.npmjs.org/@types/node/-/node-20.11.30.tgz", "integrity": "sha512-dHM6ZxwlmuZaRmUPfv1p+KrdD1Dci04FbdEm/9wEMouFqxYoFl5aMkt0VMAUtYRQDyYvD41WJLukhq/ha3YuTw==", - "dev": true, "dependencies": { "undici-types": "~5.26.4" } }, + "node_modules/@types/node-fetch": { + "version": "2.6.11", + "resolved": "https://registry.npmjs.org/@types/node-fetch/-/node-fetch-2.6.11.tgz", + "integrity": "sha512-24xFj9R5+rfQJLRyM56qh+wnVSYhyXC2tkoBndtY0U+vubqNsYXGjufB2nn8Q6gt0LrARwL6UBtMCSVCwl4B1g==", + "dependencies": { + "@types/node": "*", + "form-data": "^4.0.0" + } + }, "node_modules/@ungap/structured-clone": { "version": "1.2.0", "resolved": "https://registry.npmjs.org/@ungap/structured-clone/-/structured-clone-1.2.0.tgz", "integrity": "sha512-zuVdFrMJiuCDQUMCzQaD6KL28MjnqqN8XnAqiEq9PNm/hCPTSGfrXCOfwj1ow4LFb/tNymJPwsNbVePc1xFqrQ==", "dev": true }, + "node_modules/abort-controller": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/abort-controller/-/abort-controller-3.0.0.tgz", + "integrity": "sha512-h8lQ8tacZYnR3vNQTgibj+tODHI5/+l06Au2Pcriv/Gmet0eaj4TwWH41sO9wnHDiQsEj19q0drzdWdeAHtweg==", + "dependencies": { + "event-target-shim": "^5.0.0" + }, + "engines": { + "node": ">=6.5" + } + }, "node_modules/accepts": { "version": "1.3.8", "resolved": "https://registry.npmjs.org/accepts/-/accepts-1.3.8.tgz", @@ -221,6 +240,17 @@ "acorn": "^6.0.0 || ^7.0.0 || ^8.0.0" } }, + "node_modules/agentkeepalive": { + "version": "4.5.0", + "resolved": "https://registry.npmjs.org/agentkeepalive/-/agentkeepalive-4.5.0.tgz", + "integrity": "sha512-5GG/5IbQQpC9FpkRGsSvZI5QYeSCzlJHdpBQntCsuTOxhKD8lqKhrleg2Yi7yvMIf82Ycmmqln9U8V9qwEiJew==", + "dependencies": { + "humanize-ms": "^1.2.1" + }, + "engines": { + "node": ">= 8.0.0" + } + }, "node_modules/ajv": { "version": "6.12.6", "resolved": "https://registry.npmjs.org/ajv/-/ajv-6.12.6.tgz", @@ -451,20 +481,17 @@ "url": "https://github.com/sponsors/ljharb" } }, - "node_modules/axios": { - "version": "0.26.1", - "resolved": "https://registry.npmjs.org/axios/-/axios-0.26.1.tgz", - "integrity": "sha512-fPwcX4EvnSHuInCMItEhAGnaSEXRBjtzh9fOtsE6E1G6p7vl7edEeZe11QHf18+6+9gR5PbKV/sGKNaD8YaMeA==", - "dependencies": { - "follow-redirects": "^1.14.8" - } - }, "node_modules/balanced-match": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/balanced-match/-/balanced-match-1.0.2.tgz", "integrity": "sha512-3oSeUO0TMV67hN1AmbXsK4yaqU7tjiHlbxRDZOpH0KW9+CeX4bRAaX0Anxt0tx2MrpRpWwQaPwIlISEJhYU5Pw==", "dev": true }, + "node_modules/base-64": { + "version": "0.1.0", + "resolved": "https://registry.npmjs.org/base-64/-/base-64-0.1.0.tgz", + "integrity": "sha512-Y5gU45svrR5tI2Vt/X9GPd3L0HNIKzGu202EjxrXMpuc2V2CiKgemAbUUsqYmZJvPtCXoUKjNZwBJzsNScUbXA==" + }, "node_modules/body-parser": { "version": "1.20.1", "resolved": "https://registry.npmjs.org/body-parser/-/body-parser-1.20.1.tgz", @@ -586,6 +613,14 @@ "url": "https://github.com/chalk/chalk?sponsor=1" } }, + "node_modules/charenc": { + "version": "0.0.2", + "resolved": "https://registry.npmjs.org/charenc/-/charenc-0.0.2.tgz", + "integrity": "sha512-yrLQ/yVUFXkzg7EDQsPieE/53+0RlaWTs+wBrvW36cyilJ2SaDWfl4Yj7MtLTXleV9uEKefbAGUPv2/iWSooRA==", + "engines": { + "node": "*" + } + }, "node_modules/cliui": { "version": "8.0.1", "resolved": "https://registry.npmjs.org/cliui/-/cliui-8.0.1.tgz", @@ -723,6 +758,14 @@ "node": ">= 8" } }, + "node_modules/crypt": { + "version": "0.0.2", + "resolved": "https://registry.npmjs.org/crypt/-/crypt-0.0.2.tgz", + "integrity": "sha512-mCxBlsHFYh9C+HVpiEacem8FEBnMXgU9gy4zmNC+SXAZNB/1idgp/aulFJ4FgCi7GPEVbfyng092GqL2k2rmow==", + "engines": { + "node": "*" + } + }, "node_modules/data-view-buffer": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/data-view-buffer/-/data-view-buffer-1.0.1.tgz", @@ -871,6 +914,15 @@ "npm": "1.2.8000 || >= 1.4.16" } }, + "node_modules/digest-fetch": { + "version": "1.3.0", + "resolved": "https://registry.npmjs.org/digest-fetch/-/digest-fetch-1.3.0.tgz", + "integrity": "sha512-CGJuv6iKNM7QyZlM2T3sPAdZWd/p9zQiRNS9G+9COUCwzWFTs0Xp8NF5iePx7wtvhDykReiRRrSeNb4oMmB8lA==", + "dependencies": { + "base-64": "^0.1.0", + "md5": "^2.3.0" + } + }, "node_modules/doctrine": { "version": "3.0.0", "resolved": "https://registry.npmjs.org/doctrine/-/doctrine-3.0.0.tgz", @@ -1588,6 +1640,14 @@ "node": ">= 0.6" } }, + "node_modules/event-target-shim": { + "version": "5.0.1", + "resolved": "https://registry.npmjs.org/event-target-shim/-/event-target-shim-5.0.1.tgz", + "integrity": "sha512-i/2XbnSz/uxRCU6+NdVJgKWDTM427+MqYbkQzD321DuCQJUqOuJKIA0IM2+W2xtYHdKOmZ4dR6fExsd4SXL+WQ==", + "engines": { + "node": ">=6" + } + }, "node_modules/express": { "version": "4.18.2", "resolved": "https://registry.npmjs.org/express/-/express-4.18.2.tgz", @@ -1747,25 +1807,6 @@ "integrity": "sha512-X8cqMLLie7KsNUDSdzeN8FYK9rEt4Dt67OsG/DNGnYTSDBG4uFAJFBnUeiV+zCVAvwFy56IjM9sH51jVaEhNxw==", "dev": true }, - "node_modules/follow-redirects": { - "version": "1.15.6", - "resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.15.6.tgz", - "integrity": "sha512-wWN62YITEaOpSK584EZXJafH1AGpO8RVgElfkuXbTOrPX4fIfOyEpW/CsiNd8JdYrAoOvafRTOEnvsO++qCqFA==", - "funding": [ - { - "type": "individual", - "url": "https://github.com/sponsors/RubenVerborgh" - } - ], - "engines": { - "node": ">=4.0" - }, - "peerDependenciesMeta": { - "debug": { - "optional": true - } - } - }, "node_modules/for-each": { "version": "0.3.3", "resolved": "https://registry.npmjs.org/for-each/-/for-each-0.3.3.tgz", @@ -1788,6 +1829,31 @@ "node": ">= 6" } }, + "node_modules/form-data-encoder": { + "version": "1.7.2", + "resolved": "https://registry.npmjs.org/form-data-encoder/-/form-data-encoder-1.7.2.tgz", + "integrity": "sha512-qfqtYan3rxrnCk1VYaA4H+Ms9xdpPqvLZa6xmMgFvhO32x7/3J/ExcTd6qpxM0vH2GdMI+poehyBZvqfMTto8A==" + }, + "node_modules/formdata-node": { + "version": "4.4.1", + "resolved": "https://registry.npmjs.org/formdata-node/-/formdata-node-4.4.1.tgz", + "integrity": "sha512-0iirZp3uVDjVGt9p49aTaqjk84TrglENEDuqfdlZQ1roC9CWlPk6Avf8EEnZNcAqPonwkG35x4n3ww/1THYAeQ==", + "dependencies": { + "node-domexception": "1.0.0", + "web-streams-polyfill": "4.0.0-beta.3" + }, + "engines": { + "node": ">= 12.20" + } + }, + "node_modules/formdata-node/node_modules/web-streams-polyfill": { + "version": "4.0.0-beta.3", + "resolved": "https://registry.npmjs.org/web-streams-polyfill/-/web-streams-polyfill-4.0.0-beta.3.tgz", + "integrity": "sha512-QW95TCTaHmsYfHDybGMwO5IJIM93I/6vTRk+daHTWFPhwh+C8Cg7j7XyKrwrj8Ib6vYXe0ocYNrmzY4xAAN6ug==", + "engines": { + "node": ">= 14" + } + }, "node_modules/forwarded": { "version": "0.2.0", "resolved": "https://registry.npmjs.org/forwarded/-/forwarded-0.2.0.tgz", @@ -2078,6 +2144,14 @@ "node": ">= 0.8" } }, + "node_modules/humanize-ms": { + "version": "1.2.1", + "resolved": "https://registry.npmjs.org/humanize-ms/-/humanize-ms-1.2.1.tgz", + "integrity": "sha512-Fl70vYtsAFb/C06PTS9dZBo7ihau+Tu/DNCk/OyHhea07S+aeMWpFFkUaXRa8fI+ScZbEI8dfSxwY7gxZ9SAVQ==", + "dependencies": { + "ms": "^2.0.0" + } + }, "node_modules/iconv-lite": { "version": "0.4.24", "resolved": "https://registry.npmjs.org/iconv-lite/-/iconv-lite-0.4.24.tgz", @@ -2225,6 +2299,11 @@ "url": "https://github.com/sponsors/ljharb" } }, + "node_modules/is-buffer": { + "version": "1.1.6", + "resolved": "https://registry.npmjs.org/is-buffer/-/is-buffer-1.1.6.tgz", + "integrity": "sha512-NcdALwpXkTm5Zvvbk7owOUSvVvBKDgKP5/ewfXEznmQFfs4ZRmanOeKBTjRVjka3QFoN6XJ+9F3USqfHqTaU5w==" + }, "node_modules/is-callable": { "version": "1.2.7", "resolved": "https://registry.npmjs.org/is-callable/-/is-callable-1.2.7.tgz", @@ -2704,6 +2783,16 @@ "node": ">=10" } }, + "node_modules/md5": { + "version": "2.3.0", + "resolved": "https://registry.npmjs.org/md5/-/md5-2.3.0.tgz", + "integrity": "sha512-T1GITYmFaKuO91vxyoQMFETst+O71VUPEU3ze5GNzDm0OWdP8v1ziTaAEPUr/3kLsY3Sftgz242A1SetQiDL7g==", + "dependencies": { + "charenc": "0.0.2", + "crypt": "0.0.2", + "is-buffer": "~1.1.6" + } + }, "node_modules/media-typer": { "version": "0.3.0", "resolved": "https://registry.npmjs.org/media-typer/-/media-typer-0.3.0.tgz", @@ -2779,8 +2868,7 @@ "node_modules/ms": { "version": "2.1.2", "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.2.tgz", - "integrity": "sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w==", - "dev": true + "integrity": "sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w==" }, "node_modules/natural-compare": { "version": "1.4.0", @@ -2796,6 +2884,43 @@ "node": ">= 0.6" } }, + "node_modules/node-domexception": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/node-domexception/-/node-domexception-1.0.0.tgz", + "integrity": "sha512-/jKZoMpw0F8GRwl4/eLROPA3cfcXtLApP0QzLmUT/HuPCZWyB7IY9ZrMeKw2O/nFIqPQB3PVM9aYm0F312AXDQ==", + "funding": [ + { + "type": "github", + "url": "https://github.com/sponsors/jimmywarting" + }, + { + "type": "github", + "url": "https://paypal.me/jimmywarting" + } + ], + "engines": { + "node": ">=10.5.0" + } + }, + "node_modules/node-fetch": { + "version": "2.7.0", + "resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-2.7.0.tgz", + "integrity": "sha512-c4FRfUm/dbcWZ7U+1Wq0AwCyFL+3nt2bEw05wfxSz+DWpWsitgmSgYmy2dQdWyKC1694ELPqMs/YzUSNozLt8A==", + "dependencies": { + "whatwg-url": "^5.0.0" + }, + "engines": { + "node": "4.x || >=6.0.0" + }, + "peerDependencies": { + "encoding": "^0.1.0" + }, + "peerDependenciesMeta": { + "encoding": { + "optional": true + } + } + }, "node_modules/object-assign": { "version": "4.1.1", "resolved": "https://registry.npmjs.org/object-assign/-/object-assign-4.1.1.tgz", @@ -2941,12 +3066,30 @@ } }, "node_modules/openai": { - "version": "3.3.0", - "resolved": "https://registry.npmjs.org/openai/-/openai-3.3.0.tgz", - "integrity": "sha512-uqxI/Au+aPRnsaQRe8CojU0eCR7I0mBiKjD3sNMzY6DaC1ZVrc85u98mtJW6voDug8fgGN+DIZmTDxTthxb7dQ==", + "version": "4.29.2", + "resolved": "https://registry.npmjs.org/openai/-/openai-4.29.2.tgz", + "integrity": "sha512-cPkT6zjEcE4qU5OW/SoDDuXEsdOLrXlAORhzmaguj5xZSPlgKvLhi27sFWhLKj07Y6WKNWxcwIbzm512FzTBNQ==", + "dependencies": { + "@types/node": "^18.11.18", + "@types/node-fetch": "^2.6.4", + "abort-controller": "^3.0.0", + "agentkeepalive": "^4.2.1", + "digest-fetch": "^1.3.0", + "form-data-encoder": "1.7.2", + "formdata-node": "^4.3.2", + "node-fetch": "^2.6.7", + "web-streams-polyfill": "^3.2.1" + }, + "bin": { + "openai": "bin/cli" + } + }, + "node_modules/openai/node_modules/@types/node": { + "version": "18.19.26", + "resolved": "https://registry.npmjs.org/@types/node/-/node-18.19.26.tgz", + "integrity": "sha512-+wiMJsIwLOYCvUqSdKTrfkS8mpTp+MPINe6+Np4TAGFWWRWiBQ5kSq9nZGCSPkzx9mvT+uEukzpX4MOSCydcvw==", "dependencies": { - "axios": "^0.26.0", - "form-data": "^4.0.0" + "undici-types": "~5.26.4" } }, "node_modules/optionator": { @@ -3881,6 +4024,11 @@ "node": ">=0.6" } }, + "node_modules/tr46": { + "version": "0.0.3", + "resolved": "https://registry.npmjs.org/tr46/-/tr46-0.0.3.tgz", + "integrity": "sha512-N3WMsuqV66lT30CrXNbEjx4GEwlow3v6rr4mCcv6prnfwhS01rkgyFdjPNBYd9br7LpXV1+Emh01fHnq2Gdgrw==" + }, "node_modules/tree-kill": { "version": "1.2.2", "resolved": "https://registry.npmjs.org/tree-kill/-/tree-kill-1.2.2.tgz", @@ -4048,8 +4196,7 @@ "node_modules/undici-types": { "version": "5.26.5", "resolved": "https://registry.npmjs.org/undici-types/-/undici-types-5.26.5.tgz", - "integrity": "sha512-JlCMO+ehdEIKqlFxk6IfVoAUVmgz7cU7zD/h9XZ0qzeosSHmUJVOzSQvvYSYWXkFXC+IfLKSIffhv0sVZup6pA==", - "dev": true + "integrity": "sha512-JlCMO+ehdEIKqlFxk6IfVoAUVmgz7cU7zD/h9XZ0qzeosSHmUJVOzSQvvYSYWXkFXC+IfLKSIffhv0sVZup6pA==" }, "node_modules/unpipe": { "version": "1.0.0", @@ -4093,6 +4240,28 @@ "node": ">=0.10.48" } }, + "node_modules/web-streams-polyfill": { + "version": "3.3.3", + "resolved": "https://registry.npmjs.org/web-streams-polyfill/-/web-streams-polyfill-3.3.3.tgz", + "integrity": "sha512-d2JWLCivmZYTSIoge9MsgFCZrt571BikcWGYkjC1khllbTeDlGqZ2D8vD8E/lJa8WGWbb7Plm8/XJYV7IJHZZw==", + "engines": { + "node": ">= 8" + } + }, + "node_modules/webidl-conversions": { + "version": "3.0.1", + "resolved": "https://registry.npmjs.org/webidl-conversions/-/webidl-conversions-3.0.1.tgz", + "integrity": "sha512-2JAn3z8AR6rjK8Sm8orRC0h/bcl/DqL7tRPdGZ4I1CjdF+EaMLmYxBHyXuKL849eucPFhvBoxMsflfOb8kxaeQ==" + }, + "node_modules/whatwg-url": { + "version": "5.0.0", + "resolved": "https://registry.npmjs.org/whatwg-url/-/whatwg-url-5.0.0.tgz", + "integrity": "sha512-saE57nupxk6v3HY35+jzBwYa0rKSy0XR8JSxZPwgLr7ys0IBzhGviA1/TUGJLmSVqs8pb9AnvICXEuOHLprYTw==", + "dependencies": { + "tr46": "~0.0.3", + "webidl-conversions": "^3.0.0" + } + }, "node_modules/which": { "version": "2.0.2", "resolved": "https://registry.npmjs.org/which/-/which-2.0.2.tgz", diff --git a/package.json b/package.json index 404b283..a030021 100644 --- a/package.json +++ b/package.json @@ -31,6 +31,6 @@ "dependencies": { "dotenv": "16.3.1", "express": "4.18.2", - "openai": "3.3.0" + "openai": "4.29.2" } } From b86d478522ed6b80d1f3dbaf616c68a31ecb6751 Mon Sep 17 00:00:00 2001 From: Carlos Ochoa Date: Mon, 25 Mar 2024 00:17:04 +0000 Subject: [PATCH 2/8] env file template updated --- .env.template | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/.env.template b/.env.template index aea514c..36aae6f 100644 --- a/.env.template +++ b/.env.template @@ -1,14 +1,15 @@ -# OpenAI GPT available models: gpt-4-32k | gpt-4 | gpt-3.5-turbo | gpt-3.5-turbo-16k +# OpenAI GPT available models: https://platform.openai.com/docs/models # # IMPORTANT: Rename this file to .env before running the server # # PORT Where the server will be running -PORT = 3001 +PORT=3001 # Your OpenAI API key value -OPENAI_API_KEY = "sk-EtcEtcEtcEtcEtcEtcEtcEtcEtcEtcEtcEtcEtcEtcEtcEtc" -OPENAI_API_DEFAULT_MODEL = "gpt-3.5-turbo" -OPENAI_API_DEFAULT_MAX_TOKENS = "4000" -OPENAI_API_DEFAULT_TEMPERATURE = "0.25" -OPENAI_API_DEFAULT_TOP_P = "1" -OPENAI_API_DEFAULT_FREQUENCY_PENALTY = "0.5" -OPENAI_API_DEFAULT_PRESENCE_PENALTY = "0.5" \ No newline at end of file +OPENAI_API_KEY="sk-EtcEtcEtcEtcEtcEtcEtcEtcEtcEtcEtcEtcEtcEtcEtcEtc" +OPENAI_ORG_ID="org-EtcEtcEtcEtcEtcEtcEtcEtc" +OPENAI_API_DEFAULT_MODEL="gpt-3.5-turbo" +OPENAI_API_DEFAULT_MAX_TOKENS=4000 +OPENAI_API_DEFAULT_TEMPERATURE=0.25 +OPENAI_API_DEFAULT_TOP_P=1 +OPENAI_API_DEFAULT_FREQUENCY_PENALTY=0.5 +OPENAI_API_DEFAULT_PRESENCE_PENALTY=0.5 \ No newline at end of file From 78df84842ab9a110b20ef7b43d379298de0c02af Mon Sep 17 00:00:00 2001 From: Carlos Ochoa Date: Mon, 25 Mar 2024 00:17:46 +0000 Subject: [PATCH 3/8] remove unnecessary files --- src/types.d.ts | 15 --------------- src/utils/ResponseError.ts | 11 ----------- src/utils/estimateTokens.ts | 13 ------------- src/utils/isGPTMessage.ts | 12 ------------ 4 files changed, 51 deletions(-) delete mode 100644 src/types.d.ts delete mode 100644 src/utils/ResponseError.ts delete mode 100644 src/utils/estimateTokens.ts delete mode 100644 src/utils/isGPTMessage.ts diff --git a/src/types.d.ts b/src/types.d.ts deleted file mode 100644 index cdc87a7..0000000 --- a/src/types.d.ts +++ /dev/null @@ -1,15 +0,0 @@ -// define the interface for your expected data -export interface ChatCompletionParameters { - model?: string // optional - messages: string | Array // required, user can send a simple string or an array of GPTMessage (role, content) see types.d.ts (GPTMessage) - max_tokens?: number // optional - temperature?: number // optional - top_p?: number // optional - frequency_penalty?: number // optional - presence_penalty?: number // optional -} - -export type GPTMessage = { - role: 'system' | 'user' | 'assistant' - content: string -} diff --git a/src/utils/ResponseError.ts b/src/utils/ResponseError.ts deleted file mode 100644 index a115610..0000000 --- a/src/utils/ResponseError.ts +++ /dev/null @@ -1,11 +0,0 @@ -export class ResponseError extends Error { - statusCode: number - - constructor(statusCode: number, message: string) { - super(message) - this.statusCode = statusCode - - // This line is needed in TypeScript to correctly set up the prototype chain. - Object.setPrototypeOf(this, ResponseError.prototype) - } -} diff --git a/src/utils/estimateTokens.ts b/src/utils/estimateTokens.ts deleted file mode 100644 index 0963ac0..0000000 --- a/src/utils/estimateTokens.ts +++ /dev/null @@ -1,13 +0,0 @@ -export function estimateTokens(text: string): number { - const cleanedText = text.trim().replace(/(\r\n|\n|\r)/gm, ' ') - - const wordCount: number = cleanedText.split(' ').length - const charCount: number = cleanedText.length - - const tokensCountWordEst: number = wordCount / 0.75 - const tokensCountCharEst: number = charCount / 4.0 - - const output = Math.max(tokensCountWordEst, tokensCountCharEst) - - return Math.floor(output) -} diff --git a/src/utils/isGPTMessage.ts b/src/utils/isGPTMessage.ts deleted file mode 100644 index 9c9ee6c..0000000 --- a/src/utils/isGPTMessage.ts +++ /dev/null @@ -1,12 +0,0 @@ -import { GPTMessage } from '../types' - -export function isGPTMessage(obj: any): obj is GPTMessage { - return ( - 'role' in obj && - 'content' in obj && - (obj.role === 'system' || - obj.role === 'user' || - obj.role === 'assistant') && - obj.content.length > 0 - ) -} From f6b7e7acd099aaed07f9b4c37df26aad4dc11b5c Mon Sep 17 00:00:00 2001 From: Carlos Ochoa Date: Mon, 25 Mar 2024 00:18:14 +0000 Subject: [PATCH 4/8] refactor validateEnvType file --- src/utils/validateEnvTypes.ts | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/src/utils/validateEnvTypes.ts b/src/utils/validateEnvTypes.ts index 7a21831..cc2986c 100644 --- a/src/utils/validateEnvTypes.ts +++ b/src/utils/validateEnvTypes.ts @@ -1,6 +1,4 @@ -import { DotenvConfigOutput } from 'dotenv' - -export function validateEnvTypes(envValues: DotenvConfigOutput) { +export function validateEnvTypes() { const envVariables = { OPENAI_API_KEY: 'string', OPENAI_API_DEFAULT_MODEL: 'string', @@ -13,13 +11,22 @@ export function validateEnvTypes(envValues: DotenvConfigOutput) { } for (let variable in envVariables) { - const value = envValues.parsed?.[variable] || '' - if (value.length === 0) { + const value = process.env[variable] + if (!value) { throw new Error(`${variable} is not set, check your .env file`) } - - if (envVariables[variable] === 'number' && isNaN(Number(value))) { - throw new Error(`${variable} must be a number, check your .env file`) + if (envVariables[variable] === 'string') { + let valuePreview = value + if (valuePreview.length > 16) { + valuePreview = valuePreview.slice(0, 16) + '...' + } + console.log(`${variable}: ${valuePreview}`) // tetemp + } else if (envVariables[variable] === 'number') { + const numericValue = Number(value) + console.log(`${variable}: ${value}`) // tetemp + if (isNaN(numericValue)) { + throw new Error(`${variable} must be a number, check your .env file`) + } } } } From bf9c43016865af7d3120816ae58df59111e3ec03 Mon Sep 17 00:00:00 2001 From: Carlos Ochoa Date: Mon, 25 Mar 2024 00:24:53 +0000 Subject: [PATCH 5/8] update tsconfig --- tsconfig.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tsconfig.json b/tsconfig.json index a3bce78..ecf740a 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,6 +1,6 @@ { "compilerOptions": { - "target": "es5", + "target": "ES2015", "module": "commonjs", "outDir": "dist", "strict": true, From 52d2cb055f9493c6e6b38bada5428eb9e3dbb15d Mon Sep 17 00:00:00 2001 From: Carlos Ochoa Date: Mon, 25 Mar 2024 00:25:41 +0000 Subject: [PATCH 6/8] dependencies and scripts updated --- package-lock.json | 195 ++++++++++++++++++++++++++++++++-------------- package.json | 18 ++--- 2 files changed, 145 insertions(+), 68 deletions(-) diff --git a/package-lock.json b/package-lock.json index 5f32952..22ed161 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,25 +1,25 @@ { "name": "openai-node-server", - "version": "1.0.0", + "version": "1.1.0", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "openai-node-server", - "version": "1.0.0", + "version": "1.1.0", "license": "ISC", "dependencies": { - "dotenv": "16.3.1", - "express": "4.18.2", + "express": "4.19.1", "openai": "4.29.2" }, "devDependencies": { - "@types/node": "^20.3.1", + "@types/express": "^4.17.21", + "@types/node": "^20.11.30", "concurrently": "^8.2.2", - "eslint": "^8.43.0", - "prettier": "^2.8.8", + "eslint": "^8.57.0", + "prettier": "^3.2.5", "standard": "^17.1.0", - "typescript": "^5.1.3" + "typescript": "^5.4.3" } }, "node_modules/@aashutoshrathi/word-wrap": { @@ -167,12 +167,67 @@ "node": ">= 8" } }, + "node_modules/@types/body-parser": { + "version": "1.19.5", + "resolved": "https://registry.npmjs.org/@types/body-parser/-/body-parser-1.19.5.tgz", + "integrity": "sha512-fB3Zu92ucau0iQ0JMCFQE7b/dv8Ot07NI3KaZIkIUNXq82k4eBAqUaneXfleGY9JWskeS9y+u0nXMyspcuQrCg==", + "dev": true, + "dependencies": { + "@types/connect": "*", + "@types/node": "*" + } + }, + "node_modules/@types/connect": { + "version": "3.4.38", + "resolved": "https://registry.npmjs.org/@types/connect/-/connect-3.4.38.tgz", + "integrity": "sha512-K6uROf1LD88uDQqJCktA4yzL1YYAK6NgfsI0v/mTgyPKWsX1CnJ0XPSDhViejru1GcRkLWb8RlzFYJRqGUbaug==", + "dev": true, + "dependencies": { + "@types/node": "*" + } + }, + "node_modules/@types/express": { + "version": "4.17.21", + "resolved": "https://registry.npmjs.org/@types/express/-/express-4.17.21.tgz", + "integrity": "sha512-ejlPM315qwLpaQlQDTjPdsUFSc6ZsP4AN6AlWnogPjQ7CVi7PYF3YVz+CY3jE2pwYf7E/7HlDAN0rV2GxTG0HQ==", + "dev": true, + "dependencies": { + "@types/body-parser": "*", + "@types/express-serve-static-core": "^4.17.33", + "@types/qs": "*", + "@types/serve-static": "*" + } + }, + "node_modules/@types/express-serve-static-core": { + "version": "4.17.43", + "resolved": "https://registry.npmjs.org/@types/express-serve-static-core/-/express-serve-static-core-4.17.43.tgz", + "integrity": "sha512-oaYtiBirUOPQGSWNGPWnzyAFJ0BP3cwvN4oWZQY+zUBwpVIGsKUkpBpSztp74drYcjavs7SKFZ4DX1V2QeN8rg==", + "dev": true, + "dependencies": { + "@types/node": "*", + "@types/qs": "*", + "@types/range-parser": "*", + "@types/send": "*" + } + }, + "node_modules/@types/http-errors": { + "version": "2.0.4", + "resolved": "https://registry.npmjs.org/@types/http-errors/-/http-errors-2.0.4.tgz", + "integrity": "sha512-D0CFMMtydbJAegzOyHjtiKPLlvnm3iTZyZRSZoLq2mRhDdmLfIWOCYPfQJ4cu2erKghU++QvjcUjp/5h7hESpA==", + "dev": true + }, "node_modules/@types/json5": { "version": "0.0.29", "resolved": "https://registry.npmjs.org/@types/json5/-/json5-0.0.29.tgz", "integrity": "sha512-dRLjCWHYg4oaA77cxO64oO+7JwCwnIzkZPdrrC71jQmQtlhM556pwKo5bUzqvZndkVbeFLIIi+9TC40JNF5hNQ==", "dev": true }, + "node_modules/@types/mime": { + "version": "1.3.5", + "resolved": "https://registry.npmjs.org/@types/mime/-/mime-1.3.5.tgz", + "integrity": "sha512-/pyBZWSLD2n0dcHE3hq8s8ZvcETHtEuF+3E7XVt0Ig2nvsVQXdghHVcEkIWjy9A0wKfTn97a/PSDYohKIlnP/w==", + "dev": true + }, "node_modules/@types/node": { "version": "20.11.30", "resolved": "https://registry.npmjs.org/@types/node/-/node-20.11.30.tgz", @@ -190,6 +245,39 @@ "form-data": "^4.0.0" } }, + "node_modules/@types/qs": { + "version": "6.9.14", + "resolved": "https://registry.npmjs.org/@types/qs/-/qs-6.9.14.tgz", + "integrity": "sha512-5khscbd3SwWMhFqylJBLQ0zIu7c1K6Vz0uBIt915BI3zV0q1nfjRQD3RqSBcPaO6PHEF4ov/t9y89fSiyThlPA==", + "dev": true + }, + "node_modules/@types/range-parser": { + "version": "1.2.7", + "resolved": "https://registry.npmjs.org/@types/range-parser/-/range-parser-1.2.7.tgz", + "integrity": "sha512-hKormJbkJqzQGhziax5PItDUTMAM9uE2XXQmM37dyd4hVM+5aVl7oVxMVUiVQn2oCQFN/LKCZdvSM0pFRqbSmQ==", + "dev": true + }, + "node_modules/@types/send": { + "version": "0.17.4", + "resolved": "https://registry.npmjs.org/@types/send/-/send-0.17.4.tgz", + "integrity": "sha512-x2EM6TJOybec7c52BX0ZspPodMsQUd5L6PRwOunVyVUhXiBSKf3AezDL8Dgvgt5o0UfKNfuA0eMLr2wLT4AiBA==", + "dev": true, + "dependencies": { + "@types/mime": "^1", + "@types/node": "*" + } + }, + "node_modules/@types/serve-static": { + "version": "1.15.5", + "resolved": "https://registry.npmjs.org/@types/serve-static/-/serve-static-1.15.5.tgz", + "integrity": "sha512-PDRk21MnK70hja/YF8AHfC7yIsiQHn1rcXx7ijCFBX/k+XQJhQT/gw3xekXKJvx+5SXaMMS8oqQy09Mzvz2TuQ==", + "dev": true, + "dependencies": { + "@types/http-errors": "*", + "@types/mime": "*", + "@types/node": "*" + } + }, "node_modules/@ungap/structured-clone": { "version": "1.2.0", "resolved": "https://registry.npmjs.org/@ungap/structured-clone/-/structured-clone-1.2.0.tgz", @@ -493,12 +581,12 @@ "integrity": "sha512-Y5gU45svrR5tI2Vt/X9GPd3L0HNIKzGu202EjxrXMpuc2V2CiKgemAbUUsqYmZJvPtCXoUKjNZwBJzsNScUbXA==" }, "node_modules/body-parser": { - "version": "1.20.1", - "resolved": "https://registry.npmjs.org/body-parser/-/body-parser-1.20.1.tgz", - "integrity": "sha512-jWi7abTbYwajOytWCQc37VulmWiRae5RyTpaCyDcS5/lMdtwSz5lOpDE67srw/HYe35f1z3fDQw+3txg7gNtWw==", + "version": "1.20.2", + "resolved": "https://registry.npmjs.org/body-parser/-/body-parser-1.20.2.tgz", + "integrity": "sha512-ml9pReCu3M61kGlqoTm2umSXTlRTuGTx0bfYj+uIUKKYycG5NtSbeetV3faSU6R7ajOPw0g/J1PvK4qNy7s5bA==", "dependencies": { "bytes": "3.1.2", - "content-type": "~1.0.4", + "content-type": "~1.0.5", "debug": "2.6.9", "depd": "2.0.0", "destroy": "1.2.0", @@ -506,7 +594,7 @@ "iconv-lite": "0.4.24", "on-finished": "2.4.1", "qs": "6.11.0", - "raw-body": "2.5.1", + "raw-body": "2.5.2", "type-is": "~1.6.18", "unpipe": "1.0.0" }, @@ -613,6 +701,18 @@ "url": "https://github.com/chalk/chalk?sponsor=1" } }, + "node_modules/chalk/node_modules/supports-color": { + "version": "7.2.0", + "resolved": "https://registry.npmjs.org/supports-color/-/supports-color-7.2.0.tgz", + "integrity": "sha512-qpCAvRl9stuOHveKsn7HncJRvv501qIacKzQlO/+Lwxc9+0q2wLyv4Dfvt80/DPn2pqOBsJdDiogXGR9+OvwRw==", + "dev": true, + "dependencies": { + "has-flag": "^4.0.0" + }, + "engines": { + "node": ">=8" + } + }, "node_modules/charenc": { "version": "0.0.2", "resolved": "https://registry.npmjs.org/charenc/-/charenc-0.0.2.tgz", @@ -697,21 +797,6 @@ "url": "https://github.com/open-cli-tools/concurrently?sponsor=1" } }, - "node_modules/concurrently/node_modules/supports-color": { - "version": "8.1.1", - "resolved": "https://registry.npmjs.org/supports-color/-/supports-color-8.1.1.tgz", - "integrity": "sha512-MpUEN2OodtUzxvKQl72cUF7RQ5EiHsGvSsVG0ia9c5RbWGL2CI4C7EpPS8UTBIplnlzZiNuV56w+FuNxy3ty2Q==", - "dev": true, - "dependencies": { - "has-flag": "^4.0.0" - }, - "engines": { - "node": ">=10" - }, - "funding": { - "url": "https://github.com/chalk/supports-color?sponsor=1" - } - }, "node_modules/content-disposition": { "version": "0.5.4", "resolved": "https://registry.npmjs.org/content-disposition/-/content-disposition-0.5.4.tgz", @@ -732,9 +817,9 @@ } }, "node_modules/cookie": { - "version": "0.5.0", - "resolved": "https://registry.npmjs.org/cookie/-/cookie-0.5.0.tgz", - "integrity": "sha512-YZ3GUyn/o8gfKJlnlX7g7xq4gyO6OSuhGPKaaGssGB2qgDUS0gPgtTvoyZLTt9Ab6dC4hfc9dV5arkvc/OCmrw==", + "version": "0.6.0", + "resolved": "https://registry.npmjs.org/cookie/-/cookie-0.6.0.tgz", + "integrity": "sha512-U71cyTamuh1CRNCfpGY6to28lxvNwPG4Guz/EVjgf3Jmzv0vlDp1atT9eS5dDjMYHucpHbWns6Lwf3BKz6svdw==", "engines": { "node": ">= 0.6" } @@ -935,17 +1020,6 @@ "node": ">=6.0.0" } }, - "node_modules/dotenv": { - "version": "16.3.1", - "resolved": "https://registry.npmjs.org/dotenv/-/dotenv-16.3.1.tgz", - "integrity": "sha512-IPzF4w4/Rd94bA9imS68tZBaYyBWSCE47V1RGuMrB94iyTOIEwRmVL2x/4An+6mETpLrKJ5hQkB8W4kFAadeIQ==", - "engines": { - "node": ">=12" - }, - "funding": { - "url": "https://github.com/motdotla/dotenv?sponsor=1" - } - }, "node_modules/ee-first": { "version": "1.1.1", "resolved": "https://registry.npmjs.org/ee-first/-/ee-first-1.1.1.tgz", @@ -1649,16 +1723,16 @@ } }, "node_modules/express": { - "version": "4.18.2", - "resolved": "https://registry.npmjs.org/express/-/express-4.18.2.tgz", - "integrity": "sha512-5/PsL6iGPdfQ/lKM1UuielYgv3BUoJfz1aUwU9vHZ+J7gyvwdQXFEBIEIaxeGf0GIcreATNyBExtalisDbuMqQ==", + "version": "4.19.1", + "resolved": "https://registry.npmjs.org/express/-/express-4.19.1.tgz", + "integrity": "sha512-K4w1/Bp7y8iSiVObmCrtq8Cs79XjJc/RU2YYkZQ7wpUu5ZyZ7MtPHkqoMz4pf+mgXfNvo2qft8D9OnrH2ABk9w==", "dependencies": { "accepts": "~1.3.8", "array-flatten": "1.1.1", - "body-parser": "1.20.1", + "body-parser": "1.20.2", "content-disposition": "0.5.4", "content-type": "~1.0.4", - "cookie": "0.5.0", + "cookie": "0.6.0", "cookie-signature": "1.0.6", "debug": "2.6.9", "depd": "2.0.0", @@ -3321,15 +3395,15 @@ } }, "node_modules/prettier": { - "version": "2.8.8", - "resolved": "https://registry.npmjs.org/prettier/-/prettier-2.8.8.tgz", - "integrity": "sha512-tdN8qQGvNjw4CHbY+XXk0JgCXn9QiF21a55rBe5LJAU+kDyC4WQn4+awm2Xfk2lQMk5fKup9XgzTZtGkjBdP9Q==", + "version": "3.2.5", + "resolved": "https://registry.npmjs.org/prettier/-/prettier-3.2.5.tgz", + "integrity": "sha512-3/GWa9aOC0YeD7LUfvOG2NiDyhOWRvt1k+rcKhOuYnMY24iiCphgneUfJDyFXd6rZCAnuLBv6UeAULtrhT/F4A==", "dev": true, "bin": { - "prettier": "bin-prettier.js" + "prettier": "bin/prettier.cjs" }, "engines": { - "node": ">=10.13.0" + "node": ">=14" }, "funding": { "url": "https://github.com/prettier/prettier?sponsor=1" @@ -3410,9 +3484,9 @@ } }, "node_modules/raw-body": { - "version": "2.5.1", - "resolved": "https://registry.npmjs.org/raw-body/-/raw-body-2.5.1.tgz", - "integrity": "sha512-qqJBtEyVgS0ZmPGdCFPWJ3FreoqvG4MVQln/kCgF7Olq95IbOp0/BWyMwbdtn4VTvkM8Y7khCQ2Xgk/tcrCXig==", + "version": "2.5.2", + "resolved": "https://registry.npmjs.org/raw-body/-/raw-body-2.5.2.tgz", + "integrity": "sha512-8zGqypfENjCIqGhgXToC8aB2r7YrBX+AQAfIPs/Mlk+BtPTztOvTS01NRW/3Eh60J+a48lt8qsCzirQ6loCVfA==", "dependencies": { "bytes": "3.1.2", "http-errors": "2.0.0", @@ -3987,15 +4061,18 @@ } }, "node_modules/supports-color": { - "version": "7.2.0", - "resolved": "https://registry.npmjs.org/supports-color/-/supports-color-7.2.0.tgz", - "integrity": "sha512-qpCAvRl9stuOHveKsn7HncJRvv501qIacKzQlO/+Lwxc9+0q2wLyv4Dfvt80/DPn2pqOBsJdDiogXGR9+OvwRw==", + "version": "8.1.1", + "resolved": "https://registry.npmjs.org/supports-color/-/supports-color-8.1.1.tgz", + "integrity": "sha512-MpUEN2OodtUzxvKQl72cUF7RQ5EiHsGvSsVG0ia9c5RbWGL2CI4C7EpPS8UTBIplnlzZiNuV56w+FuNxy3ty2Q==", "dev": true, "dependencies": { "has-flag": "^4.0.0" }, "engines": { - "node": ">=8" + "node": ">=10" + }, + "funding": { + "url": "https://github.com/chalk/supports-color?sponsor=1" } }, "node_modules/supports-preserve-symlinks-flag": { diff --git a/package.json b/package.json index a030021..600f9fd 100644 --- a/package.json +++ b/package.json @@ -1,14 +1,14 @@ { "name": "openai-node-server", - "version": "1.0.0", + "version": "1.1.0", "description": "", "main": "index.js", "scripts": { "start": "echo Error: you should specify the environment, e.g. \"npm run start:dev\" or \"npm run start:prod\" && exit 1", "start:dev": "npm run dev", "start:prod": "npm run prod", - "server-dev": "nodemon node ./dist/index.js", - "server-prod": "node ./dist/index.js", + "server-dev": "nodemon node --env-file .env ./dist/index.js", + "server-prod": "node --env-file .env ./dist/index.js", "dev": "NODE_ENV=development npm run dev-cmd", "dev-cmd": "npm run build && concurrently \"npm run tsc-watch\" \"npm run server-dev\"", "prod": "NODE_ENV=production npm run prod-cmd", @@ -21,16 +21,16 @@ "author": "Carlos Ulises Ochoa", "license": "ISC", "devDependencies": { - "@types/node": "^20.3.1", + "@types/express": "^4.17.21", + "@types/node": "^20.11.30", "concurrently": "^8.2.2", - "eslint": "^8.43.0", - "prettier": "^2.8.8", + "eslint": "^8.57.0", + "prettier": "^3.2.5", "standard": "^17.1.0", - "typescript": "^5.1.3" + "typescript": "^5.4.3" }, "dependencies": { - "dotenv": "16.3.1", - "express": "4.18.2", + "express": "4.19.1", "openai": "4.29.2" } } From 7270de68acb3cf3b320118b586d292ffef26fb42 Mon Sep 17 00:00:00 2001 From: Carlos Ochoa Date: Mon, 25 Mar 2024 00:26:11 +0000 Subject: [PATCH 7/8] implement version 1.1.0 --- src/api/openai.ts | 73 +++++++++++++++++++++++++++++++++++++++ src/index.ts | 85 ++++------------------------------------------ src/utils/index.ts | 3 -- 3 files changed, 79 insertions(+), 82 deletions(-) create mode 100644 src/api/openai.ts diff --git a/src/api/openai.ts b/src/api/openai.ts new file mode 100644 index 0000000..e01d15c --- /dev/null +++ b/src/api/openai.ts @@ -0,0 +1,73 @@ +import { Request, Response } from 'express' +import OpenAI from 'openai' + +const openai = new OpenAI({ + // Don't worry apiKey and organization will be read automatically from .env file + // apiKey: process.env.OPENAI_API_KEY, + // organization: process.env.OPENAI_ORG_ID, + // More options: + // timeout?: number, + // httpAgent?: any, + // fetch?: Core.Fetch, + // maxRetries?: number, + // defaultHeaders?: Core.Headers, + // defaultQuery?: Core.DefaultQuery, + // dangerouslyAllowBrowser?: boolean, +}) + +async function handlePostRequest(req: Request, res: Response): Promise { + try { + const { message } = req.body + + let cleanMessage: string + + try { + cleanMessage = message.trim() + } catch (e) { + throw new Error('Invalid message') + } + + if (cleanMessage.length === 0) { + throw new Error('Empty message') + } + + let model = process.env.OPENAI_API_DEFAULT_MODEL || 'gpt-3.5-turbo' + + const response = await openai.chat.completions.create({ + model, + messages: [ + { + role: 'system', + content: 'You are a helpful assistant.', + }, + { + role: 'user', + content: cleanMessage, + }, + ], + }) + const content = response?.choices[0]?.message?.content + + if (!content || content.length === 0) { + throw new Error('Server response is empty') + } + + res.status(200).json({ success: true, response: content }) + } catch (error: unknown) { + if (error instanceof Error) { + res.status(500).json({ success: false, error: error.message || 'An unknown error occurred' }) + } else { + // If it's not an Error instance, you might want to handle it differently + res.status(500).json({ success: false, error: 'An unexpected error occurred' }) + } + } +} + +export default async function openAIHandler(req: Request, res: Response): Promise { + if (req.method === 'POST') { + await handlePostRequest(req, res) + } else { + res.setHeader('Allow', ['POST']) + res.status(405).end(`Method ${req.method} Not Allowed`) + } +} diff --git a/src/index.ts b/src/index.ts index b1d569f..ca12557 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,26 +1,16 @@ import express, { Request, Response } from 'express' -import { Configuration, OpenAIApi } from 'openai' -import { config } from 'dotenv' -import { ChatCompletionParameters, GPTMessage } from './types' // this is the interface for the expected data -import { ResponseError, validateEnvTypes, isGPTMessage } from './utils' +import { validateEnvTypes } from './utils' import startServer from './server' +import openAIHandler from './api/openai' -const envValues = config() // Load environment variables from .env file - -validateEnvTypes(envValues) // Validate environment variables +validateEnvTypes() // Validate environment variables console.log(`Trying to start server at port: ${process.env.PORT}...`) const app = express() app.use(express.json()) -const openaiConfig = new Configuration({ - apiKey: process.env.OPENAI_API_KEY as string, -}) - -const openai = new OpenAIApi(openaiConfig) - -const port = Number(envValues.parsed?.PORT) || 3001 +const port = Number(process.env.PORT) || 3001 startServer(app, port) @@ -29,71 +19,8 @@ startServer(app, port) // // Routes */ -app.post('/api/shane-gpt', async (req: Request, res: Response) => { - try { - const { - model, // optional, if not provided, it will use the environment variable value (.env file) - messages, // required, user can send a simple string or an array of GPTMessage (role, content) see types.d.ts (GPTMessage) - // stream, // we will set this to false due to this is a simple example - max_tokens, // optional, if not provided, it will use the environment variable value (.env file) - temperature, // optional, if not provided, it will use the environment variable value (.env file) - top_p, // optional, if not provided, it will use the environment variable value (.env file) - frequency_penalty, // optional, if not provided, it will use the environment variable value (.env file) - presence_penalty, // optional, if not provided, it will use the environment variable value (.env file) - } = req.body as ChatCompletionParameters - - let finalMessages: Array | undefined = undefined - - // lookup for the messages parameter, look for a single plain string or an array of GPTMessage (role, content) see types.d.ts (GPTMessage) - if (Array.isArray(messages) && messages.every((m) => isGPTMessage(m))) { - finalMessages = messages - } else if (typeof messages === 'string' && messages.length > 0) { - finalMessages = [{ role: 'user', content: messages }] - } else { - throw new ResponseError(400, `You sent an invalid "messages" parameter value. Please provide a valid value.`) - } - - let finalModel = process.env.OPENAI_API_DEFAULT_MODEL || 'gpt-3.5-turbo' - - if (model) { - finalModel = model - } - - const response = await openai.createChatCompletion({ - model: finalModel, - messages: finalMessages, - stream: false, - max_tokens: max_tokens || Number(process.env.OPENAI_API_DEFAULT_MAX_TOKENS), - temperature: temperature || Number(process.env.OPENAI_API_DEFAULT_TEMPERATURE), - top_p: top_p || Number(process.env.OPENAI_API_DEFAULT_TOP_P), - frequency_penalty: frequency_penalty || Number(process.env.OPENAI_API_DEFAULT_FREQUENCY_PENALTY), - presence_penalty: presence_penalty || Number(process.env.OPENAI_API_DEFAULT_PRESENCE_PENALTY), - }) - - if (response.status !== 200) { - throw new ResponseError(response.status, 'Unexpected error occurred, please try again later.') - } - - return res.status(200).json({ - success: true, - data: response.data?.choices[0]?.message?.content ?? `Sorry I can't answer right now.`, - }) - } catch (error: any) { - if (error instanceof ResponseError) { - return res.status(error.statusCode).json({ - success: false, - error: 'There is a problem on the OpenAI server. Please try again later.', - // error: error.message, - }) - } else { - return res.status(400).json({ - success: false, - error: 'There is a problem on the server. Please try again later.', - // error: error.response.data?.error?.message ?? 'There is a problem on the server. Please try again later.', - }) - } - } -}) +// This should be for example: localhost:3001/api/openai +app.post('/api/openai', openAIHandler) app.get('/', (req, res) => { res.send(`Hello, TypeScript in Express! NODE_ENV: ${process.env.NODE_ENV}`) diff --git a/src/utils/index.ts b/src/utils/index.ts index 5f31481..247d0c0 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -1,4 +1 @@ -export * from './estimateTokens' -export * from './isGPTMessage' -export * from './ResponseError' export * from './validateEnvTypes' From 8e490f9c93dcc3a2c5c7590ff9de1fd07d110c1d Mon Sep 17 00:00:00 2001 From: Carlos Ochoa Date: Mon, 25 Mar 2024 00:27:14 +0000 Subject: [PATCH 8/8] remove unnecessary comments to validateEnvTypes file --- src/utils/validateEnvTypes.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/utils/validateEnvTypes.ts b/src/utils/validateEnvTypes.ts index cc2986c..77721c2 100644 --- a/src/utils/validateEnvTypes.ts +++ b/src/utils/validateEnvTypes.ts @@ -20,10 +20,10 @@ export function validateEnvTypes() { if (valuePreview.length > 16) { valuePreview = valuePreview.slice(0, 16) + '...' } - console.log(`${variable}: ${valuePreview}`) // tetemp + console.log(`${variable}: ${valuePreview}`) } else if (envVariables[variable] === 'number') { const numericValue = Number(value) - console.log(`${variable}: ${value}`) // tetemp + console.log(`${variable}: ${value}`) if (isNaN(numericValue)) { throw new Error(`${variable} must be a number, check your .env file`) }