From e5c1271a29232a0c253f03f0070610e90f699163 Mon Sep 17 00:00:00 2001 From: Sergiy Date: Thu, 22 Aug 2024 11:55:54 +0300 Subject: [PATCH 01/11] OV-39: + openai package --- backend/package.json | 1 + package-lock.json | 170 ++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 168 insertions(+), 3 deletions(-) diff --git a/backend/package.json b/backend/package.json index 084b4118d..3e19ba323 100644 --- a/backend/package.json +++ b/backend/package.json @@ -36,6 +36,7 @@ "fastify": "4.28.1", "knex": "3.1.0", "objection": "3.1.4", + "openai": "4.56.0", "pg": "8.12.0", "pino": "9.3.2", "pino-pretty": "10.3.1", diff --git a/package-lock.json b/package-lock.json index 711edfde0..01ff5dced 100644 --- a/package-lock.json +++ b/package-lock.json @@ -48,6 +48,7 @@ "fastify": "4.28.1", "knex": "3.1.0", "objection": "3.1.4", + "openai": "4.56.0", "pg": "8.12.0", "pino": "9.3.2", "pino-pretty": "10.3.1", @@ -3623,11 +3624,19 @@ "version": "22.3.0", "resolved": "https://registry.npmjs.org/@types/node/-/node-22.3.0.tgz", "integrity": "sha512-nrWpWVaDZuaVc5X84xJ0vNrLvomM205oQyLsRt7OHNZbSHslcWsvgFR7O7hire2ZonjLrWBbedmotmIlJDVd6g==", - "dev": true, "dependencies": { "undici-types": "~6.18.2" } }, + "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/@types/normalize-package-data": { "version": "2.4.4", "resolved": "https://registry.npmjs.org/@types/normalize-package-data/-/normalize-package-data-2.4.4.tgz", @@ -4142,6 +4151,17 @@ "node": ">= 6.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": "8.17.1", "resolved": "https://registry.npmjs.org/ajv/-/ajv-8.17.1.tgz", @@ -4458,6 +4478,11 @@ "retry": "0.12.0" } }, + "node_modules/asynckit": { + "version": "0.4.0", + "resolved": "https://registry.npmjs.org/asynckit/-/asynckit-0.4.0.tgz", + "integrity": "sha512-Oei9OH4tRh0YqU3GxhX79dM/mwVgvbZJaSNaRk+bshkj0S5cfHcgYakreBjrHwatXKbz+IoIdYLxrKim2MjW0Q==" + }, "node_modules/atomic-sleep": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/atomic-sleep/-/atomic-sleep-1.0.0.tgz", @@ -5048,6 +5073,17 @@ "node": ">=0.1.90" } }, + "node_modules/combined-stream": { + "version": "1.0.8", + "resolved": "https://registry.npmjs.org/combined-stream/-/combined-stream-1.0.8.tgz", + "integrity": "sha512-FQN4MRfuJeHf7cBbBMJFXhKSDq+2kAArBlmRBvcvFE5BB1HZKXtSFASDhdlz9zOYwxh8lDdnvmMOe/+5cdoEdg==", + "dependencies": { + "delayed-stream": "~1.0.0" + }, + "engines": { + "node": ">= 0.8" + } + }, "node_modules/commander": { "version": "2.20.3", "resolved": "https://registry.npmjs.org/commander/-/commander-2.20.3.tgz", @@ -5578,6 +5614,14 @@ "url": "https://github.com/sponsors/ljharb" } }, + "node_modules/delayed-stream": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/delayed-stream/-/delayed-stream-1.0.0.tgz", + "integrity": "sha512-ZySD7Nf91aLB0RxL4KGrKHBXl7Eds1DAmEdcoVawXnLD7SDhpNgtuII2aAkg7a7QS41jxPSZ17p4VdGnMHk3MQ==", + "engines": { + "node": ">=0.4.0" + } + }, "node_modules/delegates": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/delegates/-/delegates-1.0.0.tgz", @@ -7029,6 +7073,36 @@ "url": "https://github.com/sponsors/isaacs" } }, + "node_modules/form-data": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/form-data/-/form-data-4.0.0.tgz", + "integrity": "sha512-ETEklSGi5t0QMZuiXoA/Q6vcnxcLQP5vdugSpuAyi6SVGi2clPPp+xgEhuMaHC+zGgn31Kd235W35f7Hykkaww==", + "dependencies": { + "asynckit": "^0.4.0", + "combined-stream": "^1.0.8", + "mime-types": "^2.1.12" + }, + "engines": { + "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/formik": { "version": "2.4.6", "resolved": "https://registry.npmjs.org/formik/-/formik-2.4.6.tgz", @@ -7776,6 +7850,14 @@ "node": ">=16.17.0" } }, + "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/hyperlinker": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/hyperlinker/-/hyperlinker-1.0.0.tgz", @@ -9213,6 +9295,25 @@ "node": ">=10.0.0" } }, + "node_modules/mime-db": { + "version": "1.52.0", + "resolved": "https://registry.npmjs.org/mime-db/-/mime-db-1.52.0.tgz", + "integrity": "sha512-sPU4uV7dYlvtWJxwwxHD0PuihVNiE7TyAbQ5SWxDCB9mUYvOgroQOwYQQOKPJ8CIbE+1ETVlOoK1UC2nU3gYvg==", + "engines": { + "node": ">= 0.6" + } + }, + "node_modules/mime-types": { + "version": "2.1.35", + "resolved": "https://registry.npmjs.org/mime-types/-/mime-types-2.1.35.tgz", + "integrity": "sha512-ZDY+bPm5zTTF+YpCrAU9nK0UgICYPT0QtT1NZWFv4s++TNkcgVaT0g6+4R2uI4MjQjzysHB1zxuWL50hzaeXiw==", + "dependencies": { + "mime-db": "1.52.0" + }, + "engines": { + "node": ">= 0.6" + } + }, "node_modules/mimic-fn": { "version": "4.0.0", "resolved": "https://registry.npmjs.org/mimic-fn/-/mimic-fn-4.0.0.tgz", @@ -9369,6 +9470,24 @@ "integrity": "sha512-qN8v/s2PAJwGUtr1/hYTpNKlD6Y9rc4p8KSmJXyGdYGZsDGKXrGThikLFP9OCHFeLeEpQzPwiAtdIvBLqm//Hw==", "dev": true }, + "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", @@ -9709,6 +9828,44 @@ "url": "https://github.com/sponsors/sindresorhus" } }, + "node_modules/openai": { + "version": "4.56.0", + "resolved": "https://registry.npmjs.org/openai/-/openai-4.56.0.tgz", + "integrity": "sha512-zcag97+3bG890MNNa0DQD9dGmmTWL8unJdNkulZzWRXrl+QeD+YkBI4H58rJcwErxqGK6a0jVPZ4ReJjhDGcmw==", + "dependencies": { + "@types/node": "^18.11.18", + "@types/node-fetch": "^2.6.4", + "abort-controller": "^3.0.0", + "agentkeepalive": "^4.2.1", + "form-data-encoder": "1.7.2", + "formdata-node": "^4.3.2", + "node-fetch": "^2.6.7" + }, + "bin": { + "openai": "bin/cli" + }, + "peerDependencies": { + "zod": "^3.23.8" + }, + "peerDependenciesMeta": { + "zod": { + "optional": true + } + } + }, + "node_modules/openai/node_modules/@types/node": { + "version": "18.19.45", + "resolved": "https://registry.npmjs.org/@types/node/-/node-18.19.45.tgz", + "integrity": "sha512-VZxPKNNhjKmaC1SUYowuXSRSMGyQGmQjvvA1xE4QZ0xce2kLtEhPDS+kqpCPBZYgqblCLQ2DAjSzmgCM5auvhA==", + "dependencies": { + "undici-types": "~5.26.4" + } + }, + "node_modules/openai/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==" + }, "node_modules/openapi-types": { "version": "12.1.3", "resolved": "https://registry.npmjs.org/openapi-types/-/openapi-types-12.1.3.tgz", @@ -12566,8 +12723,7 @@ "node_modules/undici-types": { "version": "6.18.2", "resolved": "https://registry.npmjs.org/undici-types/-/undici-types-6.18.2.tgz", - "integrity": "sha512-5ruQbENj95yDYJNS3TvcaxPMshV7aizdv/hWYjGIKoANWKjhWNBsr2YEuYZKodQulB1b8l7ILOuDQep3afowQQ==", - "dev": true + "integrity": "sha512-5ruQbENj95yDYJNS3TvcaxPMshV7aizdv/hWYjGIKoANWKjhWNBsr2YEuYZKodQulB1b8l7ILOuDQep3afowQQ==" }, "node_modules/unicorn-magic": { "version": "0.1.0", @@ -13168,6 +13324,14 @@ "@esbuild/win32-x64": "0.21.5" } }, + "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/webidl-conversions": { "version": "3.0.1", "resolved": "https://registry.npmjs.org/webidl-conversions/-/webidl-conversions-3.0.1.tgz", From 3559624a5f005df0b0dd78c7f6b5510d7982f887 Mon Sep 17 00:00:00 2001 From: Sergiy Date: Thu, 22 Aug 2024 11:57:24 +0300 Subject: [PATCH 02/11] OV-39: * environment schema --- backend/src/common/config/base-config.package.ts | 6 ++++++ backend/src/common/config/types/environment-schema.type.ts | 1 + 2 files changed, 7 insertions(+) diff --git a/backend/src/common/config/base-config.package.ts b/backend/src/common/config/base-config.package.ts index a89d47525..ef47737b3 100644 --- a/backend/src/common/config/base-config.package.ts +++ b/backend/src/common/config/base-config.package.ts @@ -41,6 +41,12 @@ class BaseConfig implements Config { env: 'PORT', default: null, }, + OPEN_AI_KEY: { + doc: 'Key for Open Ai', + format: String, + env: 'OPEN_AI_KEY', + default: null, + }, }, DB: { CONNECTION_STRING: { diff --git a/backend/src/common/config/types/environment-schema.type.ts b/backend/src/common/config/types/environment-schema.type.ts index f3e503f0f..ce22834aa 100644 --- a/backend/src/common/config/types/environment-schema.type.ts +++ b/backend/src/common/config/types/environment-schema.type.ts @@ -5,6 +5,7 @@ type EnvironmentSchema = { APP: { PORT: number; ENVIRONMENT: ValueOf; + OPEN_AI_KEY: string; }; DB: { CONNECTION_STRING: string; From cf313687e0bd3a285bf0a7449758df946c915926 Mon Sep 17 00:00:00 2001 From: Sergiy Date: Thu, 22 Aug 2024 11:58:20 +0300 Subject: [PATCH 03/11] OV-39: + generated text type --- .../services/open-ai/libs/types/generated-text.type.ts | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 backend/src/common/services/open-ai/libs/types/generated-text.type.ts diff --git a/backend/src/common/services/open-ai/libs/types/generated-text.type.ts b/backend/src/common/services/open-ai/libs/types/generated-text.type.ts new file mode 100644 index 000000000..895720bf7 --- /dev/null +++ b/backend/src/common/services/open-ai/libs/types/generated-text.type.ts @@ -0,0 +1,5 @@ +type GeneratedText = { + generatedText: string; +}; + +export { type GeneratedText }; From c7d17e1164f21a997d39da82c6bd4af558227e08 Mon Sep 17 00:00:00 2001 From: Sergiy Date: Thu, 22 Aug 2024 11:59:26 +0300 Subject: [PATCH 04/11] OV-39: + enum --- backend/src/common/services/open-ai/libs/enums/enums.ts | 1 + .../services/open-ai/libs/enums/open-ai-role.enum.ts | 7 +++++++ 2 files changed, 8 insertions(+) create mode 100644 backend/src/common/services/open-ai/libs/enums/enums.ts create mode 100644 backend/src/common/services/open-ai/libs/enums/open-ai-role.enum.ts diff --git a/backend/src/common/services/open-ai/libs/enums/enums.ts b/backend/src/common/services/open-ai/libs/enums/enums.ts new file mode 100644 index 000000000..58dc0594a --- /dev/null +++ b/backend/src/common/services/open-ai/libs/enums/enums.ts @@ -0,0 +1 @@ +export { OpenAIRole } from './open-ai-role.enum.js'; diff --git a/backend/src/common/services/open-ai/libs/enums/open-ai-role.enum.ts b/backend/src/common/services/open-ai/libs/enums/open-ai-role.enum.ts new file mode 100644 index 000000000..b4ad8c85a --- /dev/null +++ b/backend/src/common/services/open-ai/libs/enums/open-ai-role.enum.ts @@ -0,0 +1,7 @@ +const OpenAIRole = { + USER: 'user', + SYSTEM: 'system', + ASSISTANT: 'assistant', +} as const; + +export { OpenAIRole }; From 0d6f0097e0997d3479eed1f05b5e030e135e222c Mon Sep 17 00:00:00 2001 From: Sergiy Date: Thu, 22 Aug 2024 12:00:53 +0300 Subject: [PATCH 05/11] OV-39: + constants --- .../services/open-ai/libs/constants/chat-model.constant.ts | 3 +++ .../src/common/services/open-ai/libs/constants/constants.ts | 2 ++ .../services/open-ai/libs/constants/max-token.constant.ts | 3 +++ 3 files changed, 8 insertions(+) create mode 100644 backend/src/common/services/open-ai/libs/constants/chat-model.constant.ts create mode 100644 backend/src/common/services/open-ai/libs/constants/constants.ts create mode 100644 backend/src/common/services/open-ai/libs/constants/max-token.constant.ts diff --git a/backend/src/common/services/open-ai/libs/constants/chat-model.constant.ts b/backend/src/common/services/open-ai/libs/constants/chat-model.constant.ts new file mode 100644 index 000000000..76241089d --- /dev/null +++ b/backend/src/common/services/open-ai/libs/constants/chat-model.constant.ts @@ -0,0 +1,3 @@ +const CHAT_MODEL = 'gpt-4o-mini' as const; + +export { CHAT_MODEL }; diff --git a/backend/src/common/services/open-ai/libs/constants/constants.ts b/backend/src/common/services/open-ai/libs/constants/constants.ts new file mode 100644 index 000000000..b470965ed --- /dev/null +++ b/backend/src/common/services/open-ai/libs/constants/constants.ts @@ -0,0 +1,2 @@ +export { CHAT_MODEL } from './chat-model.constant.js'; +export { MAX_TOKEN } from './max-token.constant.js'; diff --git a/backend/src/common/services/open-ai/libs/constants/max-token.constant.ts b/backend/src/common/services/open-ai/libs/constants/max-token.constant.ts new file mode 100644 index 000000000..886179719 --- /dev/null +++ b/backend/src/common/services/open-ai/libs/constants/max-token.constant.ts @@ -0,0 +1,3 @@ +const MAX_TOKEN = 2048 as const; + +export { MAX_TOKEN }; From 28ed82211e24189657f068b4f3f74d8f7f01f73a Mon Sep 17 00:00:00 2001 From: Sergiy Date: Thu, 22 Aug 2024 12:01:39 +0300 Subject: [PATCH 06/11] OV-39: + export types --- backend/src/common/services/open-ai/libs/types/types.ts | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 backend/src/common/services/open-ai/libs/types/types.ts diff --git a/backend/src/common/services/open-ai/libs/types/types.ts b/backend/src/common/services/open-ai/libs/types/types.ts new file mode 100644 index 000000000..d3de0182b --- /dev/null +++ b/backend/src/common/services/open-ai/libs/types/types.ts @@ -0,0 +1,3 @@ +export { type GeneratedText } from './generated-text.type.js'; +export { type Message } from './message.type.js'; +export { type OpenAIService } from './open-ai-service.type.js'; From c51b15edaca868cfc51e97d7cd13bccc88b303da Mon Sep 17 00:00:00 2001 From: Sergiy Date: Thu, 22 Aug 2024 12:02:16 +0300 Subject: [PATCH 07/11] OV-39: + types --- .../common/services/open-ai/libs/types/message.type.ts | 10 ++++++++++ .../open-ai/libs/types/open-ai-service.type.ts | 8 ++++++++ 2 files changed, 18 insertions(+) create mode 100644 backend/src/common/services/open-ai/libs/types/message.type.ts create mode 100644 backend/src/common/services/open-ai/libs/types/open-ai-service.type.ts diff --git a/backend/src/common/services/open-ai/libs/types/message.type.ts b/backend/src/common/services/open-ai/libs/types/message.type.ts new file mode 100644 index 000000000..1bd4ee658 --- /dev/null +++ b/backend/src/common/services/open-ai/libs/types/message.type.ts @@ -0,0 +1,10 @@ +import { type ValueOf } from 'shared'; + +import { type OpenAIRole } from '../enums/enums.js'; + +type Message = { + role: ValueOf; + content: string; +}; + +export { type Message }; diff --git a/backend/src/common/services/open-ai/libs/types/open-ai-service.type.ts b/backend/src/common/services/open-ai/libs/types/open-ai-service.type.ts new file mode 100644 index 000000000..313148017 --- /dev/null +++ b/backend/src/common/services/open-ai/libs/types/open-ai-service.type.ts @@ -0,0 +1,8 @@ +import { type GeneratedText } from './generated-text.type.js'; +import { type Message } from './message.type.js'; + +type OpenAIService = { + generateText(messages: Message[]): Promise; +}; + +export { type OpenAIService }; From 2b5a50717fb008601d0625fbe219190ff8d91786 Mon Sep 17 00:00:00 2001 From: Sergiy Date: Thu, 22 Aug 2024 12:03:31 +0300 Subject: [PATCH 08/11] OV-39: + open-ai service --- .../services/open-ai/open-ai.service.ts | 43 +++++++++++++++++++ backend/src/common/services/services.ts | 5 +++ 2 files changed, 48 insertions(+) create mode 100644 backend/src/common/services/open-ai/open-ai.service.ts diff --git a/backend/src/common/services/open-ai/open-ai.service.ts b/backend/src/common/services/open-ai/open-ai.service.ts new file mode 100644 index 000000000..b89b0e179 --- /dev/null +++ b/backend/src/common/services/open-ai/open-ai.service.ts @@ -0,0 +1,43 @@ +import OpenAI, { type OpenAI as OpenAIType } from 'openai'; + +import { type BaseConfig } from '~/common/config/base-config.package.js'; + +import { CHAT_MODEL, MAX_TOKEN } from './libs/constants/constants.js'; +import { + type GeneratedText, + type Message, + type OpenAIService as OpenAIServiceModule, +} from './libs/types/types.js'; + +type Constructor = { + config: BaseConfig; +}; + +class OpenAIService implements OpenAIServiceModule { + private config: BaseConfig; + private openAi: OpenAIType; + + public constructor({ config }: Constructor) { + this.config = config; + this.openAi = this.initOpenAi(); + } + + private initOpenAi = (): OpenAIType => { + return new OpenAI({ + apiKey: this.config.ENV.APP.OPEN_AI_KEY, + }); + }; + + public async generateText(messages: Message[]): Promise { + const completion = await this.openAi.chat.completions.create({ + messages, + model: CHAT_MODEL, + max_tokens: MAX_TOKEN, + }); + + const generatedText = completion.choices[0]?.message.content || ''; + return { generatedText }; + } +} + +export { OpenAIService }; diff --git a/backend/src/common/services/services.ts b/backend/src/common/services/services.ts index 2be4c1278..6d79653eb 100644 --- a/backend/src/common/services/services.ts +++ b/backend/src/common/services/services.ts @@ -1,5 +1,10 @@ +import { config } from '~/common/config/config.js'; + import { CryptService } from './crypt/crypt.service.js'; +import { OpenAIService } from './open-ai/open-ai.service.js'; +const openAIService = new OpenAIService({ config }); const cryptService = new CryptService(); export { cryptService }; +export { openAIService }; From 316a28eb4791feec3905084edeac8ced675171b0 Mon Sep 17 00:00:00 2001 From: Sergiy Date: Thu, 22 Aug 2024 23:40:36 +0300 Subject: [PATCH 09/11] OV-39: + move files --- .../open-ai/libs/constants/chat-model.constant.ts | 0 .../services => bundles}/open-ai/libs/constants/constants.ts | 0 .../open-ai/libs/constants/max-token.constant.ts | 0 .../{common/services => bundles}/open-ai/libs/enums/enums.ts | 0 .../open-ai/libs/enums/open-ai-role.enum.ts | 0 .../open-ai/libs/types/generated-text.type.ts | 0 .../services => bundles}/open-ai/libs/types/message.type.ts | 0 .../open-ai/libs/types/open-ai-service.type.ts | 0 .../{common/services => bundles}/open-ai/libs/types/types.ts | 0 .../{common/services => bundles}/open-ai/open-ai.service.ts | 0 backend/src/common/services/services.ts | 5 ----- 11 files changed, 5 deletions(-) rename backend/src/{common/services => bundles}/open-ai/libs/constants/chat-model.constant.ts (100%) rename backend/src/{common/services => bundles}/open-ai/libs/constants/constants.ts (100%) rename backend/src/{common/services => bundles}/open-ai/libs/constants/max-token.constant.ts (100%) rename backend/src/{common/services => bundles}/open-ai/libs/enums/enums.ts (100%) rename backend/src/{common/services => bundles}/open-ai/libs/enums/open-ai-role.enum.ts (100%) rename backend/src/{common/services => bundles}/open-ai/libs/types/generated-text.type.ts (100%) rename backend/src/{common/services => bundles}/open-ai/libs/types/message.type.ts (100%) rename backend/src/{common/services => bundles}/open-ai/libs/types/open-ai-service.type.ts (100%) rename backend/src/{common/services => bundles}/open-ai/libs/types/types.ts (100%) rename backend/src/{common/services => bundles}/open-ai/open-ai.service.ts (100%) diff --git a/backend/src/common/services/open-ai/libs/constants/chat-model.constant.ts b/backend/src/bundles/open-ai/libs/constants/chat-model.constant.ts similarity index 100% rename from backend/src/common/services/open-ai/libs/constants/chat-model.constant.ts rename to backend/src/bundles/open-ai/libs/constants/chat-model.constant.ts diff --git a/backend/src/common/services/open-ai/libs/constants/constants.ts b/backend/src/bundles/open-ai/libs/constants/constants.ts similarity index 100% rename from backend/src/common/services/open-ai/libs/constants/constants.ts rename to backend/src/bundles/open-ai/libs/constants/constants.ts diff --git a/backend/src/common/services/open-ai/libs/constants/max-token.constant.ts b/backend/src/bundles/open-ai/libs/constants/max-token.constant.ts similarity index 100% rename from backend/src/common/services/open-ai/libs/constants/max-token.constant.ts rename to backend/src/bundles/open-ai/libs/constants/max-token.constant.ts diff --git a/backend/src/common/services/open-ai/libs/enums/enums.ts b/backend/src/bundles/open-ai/libs/enums/enums.ts similarity index 100% rename from backend/src/common/services/open-ai/libs/enums/enums.ts rename to backend/src/bundles/open-ai/libs/enums/enums.ts diff --git a/backend/src/common/services/open-ai/libs/enums/open-ai-role.enum.ts b/backend/src/bundles/open-ai/libs/enums/open-ai-role.enum.ts similarity index 100% rename from backend/src/common/services/open-ai/libs/enums/open-ai-role.enum.ts rename to backend/src/bundles/open-ai/libs/enums/open-ai-role.enum.ts diff --git a/backend/src/common/services/open-ai/libs/types/generated-text.type.ts b/backend/src/bundles/open-ai/libs/types/generated-text.type.ts similarity index 100% rename from backend/src/common/services/open-ai/libs/types/generated-text.type.ts rename to backend/src/bundles/open-ai/libs/types/generated-text.type.ts diff --git a/backend/src/common/services/open-ai/libs/types/message.type.ts b/backend/src/bundles/open-ai/libs/types/message.type.ts similarity index 100% rename from backend/src/common/services/open-ai/libs/types/message.type.ts rename to backend/src/bundles/open-ai/libs/types/message.type.ts diff --git a/backend/src/common/services/open-ai/libs/types/open-ai-service.type.ts b/backend/src/bundles/open-ai/libs/types/open-ai-service.type.ts similarity index 100% rename from backend/src/common/services/open-ai/libs/types/open-ai-service.type.ts rename to backend/src/bundles/open-ai/libs/types/open-ai-service.type.ts diff --git a/backend/src/common/services/open-ai/libs/types/types.ts b/backend/src/bundles/open-ai/libs/types/types.ts similarity index 100% rename from backend/src/common/services/open-ai/libs/types/types.ts rename to backend/src/bundles/open-ai/libs/types/types.ts diff --git a/backend/src/common/services/open-ai/open-ai.service.ts b/backend/src/bundles/open-ai/open-ai.service.ts similarity index 100% rename from backend/src/common/services/open-ai/open-ai.service.ts rename to backend/src/bundles/open-ai/open-ai.service.ts diff --git a/backend/src/common/services/services.ts b/backend/src/common/services/services.ts index 6d79653eb..2be4c1278 100644 --- a/backend/src/common/services/services.ts +++ b/backend/src/common/services/services.ts @@ -1,10 +1,5 @@ -import { config } from '~/common/config/config.js'; - import { CryptService } from './crypt/crypt.service.js'; -import { OpenAIService } from './open-ai/open-ai.service.js'; -const openAIService = new OpenAIService({ config }); const cryptService = new CryptService(); export { cryptService }; -export { openAIService }; From 71e4c8e76eb085b4ffa1a6c3bd7c0a5282e0931a Mon Sep 17 00:00:00 2001 From: Sergiy Date: Tue, 27 Aug 2024 16:06:42 +0300 Subject: [PATCH 10/11] OV-39: - generated text type --- .../src/bundles/open-ai/libs/types/generated-text.type.ts | 5 ----- .../{bundles => common/services}/open-ai/libs/types/types.ts | 1 - 2 files changed, 6 deletions(-) delete mode 100644 backend/src/bundles/open-ai/libs/types/generated-text.type.ts rename backend/src/{bundles => common/services}/open-ai/libs/types/types.ts (64%) diff --git a/backend/src/bundles/open-ai/libs/types/generated-text.type.ts b/backend/src/bundles/open-ai/libs/types/generated-text.type.ts deleted file mode 100644 index 895720bf7..000000000 --- a/backend/src/bundles/open-ai/libs/types/generated-text.type.ts +++ /dev/null @@ -1,5 +0,0 @@ -type GeneratedText = { - generatedText: string; -}; - -export { type GeneratedText }; diff --git a/backend/src/bundles/open-ai/libs/types/types.ts b/backend/src/common/services/open-ai/libs/types/types.ts similarity index 64% rename from backend/src/bundles/open-ai/libs/types/types.ts rename to backend/src/common/services/open-ai/libs/types/types.ts index d3de0182b..4b69fcdc4 100644 --- a/backend/src/bundles/open-ai/libs/types/types.ts +++ b/backend/src/common/services/open-ai/libs/types/types.ts @@ -1,3 +1,2 @@ -export { type GeneratedText } from './generated-text.type.js'; export { type Message } from './message.type.js'; export { type OpenAIService } from './open-ai-service.type.js'; From 1ed41d0d28ec6cae40f707571a2b70af26d083bd Mon Sep 17 00:00:00 2001 From: Sergiy Date: Tue, 27 Aug 2024 16:08:07 +0300 Subject: [PATCH 11/11] OV-39: * move files --- .../open-ai/libs/constants/chat-model.constant.ts | 3 --- .../open-ai/libs/constants/max-token.constant.ts | 3 --- .../open-ai/libs/types/open-ai-service.type.ts | 8 -------- .../open-ai/libs/constants/chat-model.constant.ts | 3 +++ .../services}/open-ai/libs/constants/constants.ts | 0 .../open-ai/libs/constants/max-token.constant.ts | 3 +++ .../services}/open-ai/libs/enums/enums.ts | 0 .../open-ai/libs/enums/open-ai-role.enum.ts | 0 .../services}/open-ai/libs/types/message.type.ts | 0 .../open-ai/libs/types/open-ai-service.type.ts | 7 +++++++ .../services}/open-ai/open-ai.service.ts | 12 +++++------- 11 files changed, 18 insertions(+), 21 deletions(-) delete mode 100644 backend/src/bundles/open-ai/libs/constants/chat-model.constant.ts delete mode 100644 backend/src/bundles/open-ai/libs/constants/max-token.constant.ts delete mode 100644 backend/src/bundles/open-ai/libs/types/open-ai-service.type.ts create mode 100644 backend/src/common/services/open-ai/libs/constants/chat-model.constant.ts rename backend/src/{bundles => common/services}/open-ai/libs/constants/constants.ts (100%) create mode 100644 backend/src/common/services/open-ai/libs/constants/max-token.constant.ts rename backend/src/{bundles => common/services}/open-ai/libs/enums/enums.ts (100%) rename backend/src/{bundles => common/services}/open-ai/libs/enums/open-ai-role.enum.ts (100%) rename backend/src/{bundles => common/services}/open-ai/libs/types/message.type.ts (100%) create mode 100644 backend/src/common/services/open-ai/libs/types/open-ai-service.type.ts rename backend/src/{bundles => common/services}/open-ai/open-ai.service.ts (70%) diff --git a/backend/src/bundles/open-ai/libs/constants/chat-model.constant.ts b/backend/src/bundles/open-ai/libs/constants/chat-model.constant.ts deleted file mode 100644 index 76241089d..000000000 --- a/backend/src/bundles/open-ai/libs/constants/chat-model.constant.ts +++ /dev/null @@ -1,3 +0,0 @@ -const CHAT_MODEL = 'gpt-4o-mini' as const; - -export { CHAT_MODEL }; diff --git a/backend/src/bundles/open-ai/libs/constants/max-token.constant.ts b/backend/src/bundles/open-ai/libs/constants/max-token.constant.ts deleted file mode 100644 index 886179719..000000000 --- a/backend/src/bundles/open-ai/libs/constants/max-token.constant.ts +++ /dev/null @@ -1,3 +0,0 @@ -const MAX_TOKEN = 2048 as const; - -export { MAX_TOKEN }; diff --git a/backend/src/bundles/open-ai/libs/types/open-ai-service.type.ts b/backend/src/bundles/open-ai/libs/types/open-ai-service.type.ts deleted file mode 100644 index 313148017..000000000 --- a/backend/src/bundles/open-ai/libs/types/open-ai-service.type.ts +++ /dev/null @@ -1,8 +0,0 @@ -import { type GeneratedText } from './generated-text.type.js'; -import { type Message } from './message.type.js'; - -type OpenAIService = { - generateText(messages: Message[]): Promise; -}; - -export { type OpenAIService }; diff --git a/backend/src/common/services/open-ai/libs/constants/chat-model.constant.ts b/backend/src/common/services/open-ai/libs/constants/chat-model.constant.ts new file mode 100644 index 000000000..385b505d5 --- /dev/null +++ b/backend/src/common/services/open-ai/libs/constants/chat-model.constant.ts @@ -0,0 +1,3 @@ +const CHAT_MODEL = 'gpt-4o-mini'; + +export { CHAT_MODEL }; diff --git a/backend/src/bundles/open-ai/libs/constants/constants.ts b/backend/src/common/services/open-ai/libs/constants/constants.ts similarity index 100% rename from backend/src/bundles/open-ai/libs/constants/constants.ts rename to backend/src/common/services/open-ai/libs/constants/constants.ts diff --git a/backend/src/common/services/open-ai/libs/constants/max-token.constant.ts b/backend/src/common/services/open-ai/libs/constants/max-token.constant.ts new file mode 100644 index 000000000..e403fe4b1 --- /dev/null +++ b/backend/src/common/services/open-ai/libs/constants/max-token.constant.ts @@ -0,0 +1,3 @@ +const MAX_TOKEN = 16_000; + +export { MAX_TOKEN }; diff --git a/backend/src/bundles/open-ai/libs/enums/enums.ts b/backend/src/common/services/open-ai/libs/enums/enums.ts similarity index 100% rename from backend/src/bundles/open-ai/libs/enums/enums.ts rename to backend/src/common/services/open-ai/libs/enums/enums.ts diff --git a/backend/src/bundles/open-ai/libs/enums/open-ai-role.enum.ts b/backend/src/common/services/open-ai/libs/enums/open-ai-role.enum.ts similarity index 100% rename from backend/src/bundles/open-ai/libs/enums/open-ai-role.enum.ts rename to backend/src/common/services/open-ai/libs/enums/open-ai-role.enum.ts diff --git a/backend/src/bundles/open-ai/libs/types/message.type.ts b/backend/src/common/services/open-ai/libs/types/message.type.ts similarity index 100% rename from backend/src/bundles/open-ai/libs/types/message.type.ts rename to backend/src/common/services/open-ai/libs/types/message.type.ts diff --git a/backend/src/common/services/open-ai/libs/types/open-ai-service.type.ts b/backend/src/common/services/open-ai/libs/types/open-ai-service.type.ts new file mode 100644 index 000000000..9963d9ccd --- /dev/null +++ b/backend/src/common/services/open-ai/libs/types/open-ai-service.type.ts @@ -0,0 +1,7 @@ +import { type Message } from './message.type.js'; + +type OpenAIService = { + generateText(messages: Message[]): Promise; +}; + +export { type OpenAIService }; diff --git a/backend/src/bundles/open-ai/open-ai.service.ts b/backend/src/common/services/open-ai/open-ai.service.ts similarity index 70% rename from backend/src/bundles/open-ai/open-ai.service.ts rename to backend/src/common/services/open-ai/open-ai.service.ts index b89b0e179..99a7dfedb 100644 --- a/backend/src/bundles/open-ai/open-ai.service.ts +++ b/backend/src/common/services/open-ai/open-ai.service.ts @@ -1,10 +1,9 @@ -import OpenAI, { type OpenAI as OpenAIType } from 'openai'; +import OpenAI from 'openai'; import { type BaseConfig } from '~/common/config/base-config.package.js'; import { CHAT_MODEL, MAX_TOKEN } from './libs/constants/constants.js'; import { - type GeneratedText, type Message, type OpenAIService as OpenAIServiceModule, } from './libs/types/types.js'; @@ -15,28 +14,27 @@ type Constructor = { class OpenAIService implements OpenAIServiceModule { private config: BaseConfig; - private openAi: OpenAIType; + private openAi: OpenAI; public constructor({ config }: Constructor) { this.config = config; this.openAi = this.initOpenAi(); } - private initOpenAi = (): OpenAIType => { + private initOpenAi = (): OpenAI => { return new OpenAI({ apiKey: this.config.ENV.APP.OPEN_AI_KEY, }); }; - public async generateText(messages: Message[]): Promise { + public async generateText(messages: Message[]): Promise { const completion = await this.openAi.chat.completions.create({ messages, model: CHAT_MODEL, max_tokens: MAX_TOKEN, }); - const generatedText = completion.choices[0]?.message.content || ''; - return { generatedText }; + return completion.choices[0]?.message.content || ''; } }