From 342f94d16bc52392ba01889b4e222608d4ce35d9 Mon Sep 17 00:00:00 2001 From: Shadow Date: Mon, 5 Aug 2024 18:47:12 +0000 Subject: [PATCH] Hacky temporary migration to GPT 4O-Mini (#820) * migrate to gpt 4o mini * fix(ai): migrate to new openai version * remove redundant comment * fix(ai): fix 4o mini pricing --------- Co-authored-by: LunaUrsa <1836049+LunaUrsa@users.noreply.github.com> --- package-lock.json | 60 ++++------------------------- package.json | 4 +- src/discord/commands/global/d.ai.ts | 6 +-- src/global/commands/g.ai.ts | 21 +++++----- 4 files changed, 24 insertions(+), 67 deletions(-) diff --git a/package-lock.json b/package-lock.json index 71b8d477e..2f4af8ded 100644 --- a/package-lock.json +++ b/package-lock.json @@ -39,7 +39,7 @@ "node-os-utils": "^1.3.7", "objection": "^3.1.3", "octokit": "^3.1.2", - "openai": "^4.25.0", + "openai": "^4.53.2", "rollbar": "^2.26.2", "rss-parser": "^3.13.0", "source-map-support": "^0.5.21", @@ -3466,11 +3466,6 @@ "resolved": "https://registry.npmjs.org/balanced-match/-/balanced-match-1.0.2.tgz", "integrity": "sha512-3oSeUO0TMV67hN1AmbXsK4yaqU7tjiHlbxRDZOpH0KW9+CeX4bRAaX0Anxt0tx2MrpRpWwQaPwIlISEJhYU5Pw==" }, - "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/basic-auth": { "version": "2.0.1", "resolved": "https://registry.npmjs.org/basic-auth/-/basic-auth-2.0.1.tgz", @@ -3763,14 +3758,6 @@ "node": ">=10" } }, - "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/chownr": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/chownr/-/chownr-2.0.0.tgz", @@ -4093,14 +4080,6 @@ "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/dashdash": { "version": "1.14.1", "resolved": "https://registry.npmjs.org/dashdash/-/dashdash-1.14.1.tgz", @@ -4327,15 +4306,6 @@ "node": "^14.15.0 || ^16.10.0 || >=18.0.0" } }, - "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/dir-glob": { "version": "3.0.1", "resolved": "https://registry.npmjs.org/dir-glob/-/dir-glob-3.0.1.tgz", @@ -7808,21 +7778,6 @@ "integrity": "sha512-CkYQrPYZfWnu/DAmVCpTSX/xHpKZ80eKh2lAkyA6AJTef6bW+6JpbQZN5rofum7da+SyN1bi5ctTm+lTfcCW3g==", "dev": true }, - "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/md5/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/media-typer": { "version": "0.3.0", "resolved": "https://registry.npmjs.org/media-typer/-/media-typer-0.3.0.tgz", @@ -8388,19 +8343,18 @@ } }, "node_modules/openai": { - "version": "4.25.0", - "resolved": "https://registry.npmjs.org/openai/-/openai-4.25.0.tgz", - "integrity": "sha512-qLMFOizjxKuDfQkBrczZPYo6XVL4bdcuz9MR11Q+M91kGcs8dQw+O90nRcC+qWuhaGphQkfXQJMn4cd7Yew3Kg==", + "version": "4.53.2", + "resolved": "https://registry.npmjs.org/openai/-/openai-4.53.2.tgz", + "integrity": "sha512-ohYEv6OV3jsFGqNrgolDDWN6Ssx1nFg6JDJQuaBFo4SL2i+MBoOQ16n2Pq1iBF5lH1PKnfCIOfqAGkmzPvdB9g==", + "license": "Apache-2.0", "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" + "node-fetch": "^2.6.7" }, "bin": { "openai": "bin/cli" @@ -10460,6 +10414,8 @@ "version": "3.2.1", "resolved": "https://registry.npmjs.org/web-streams-polyfill/-/web-streams-polyfill-3.2.1.tgz", "integrity": "sha512-e0MO3wdXWKrLbL0DgGnUV7WHVuw9OUvL4hjgnPkIeEvESk74gAITi5G606JtZPp39cd8HA9VQzCIvA49LpPN5Q==", + "optional": true, + "peer": true, "engines": { "node": ">= 8" } diff --git a/package.json b/package.json index 4103185e8..26f457204 100644 --- a/package.json +++ b/package.json @@ -87,7 +87,7 @@ "node-os-utils": "^1.3.7", "objection": "^3.1.3", "octokit": "^3.1.2", - "openai": "^4.25.0", + "openai": "^4.53.2", "rollbar": "^2.26.2", "rss-parser": "^3.13.0", "source-map-support": "^0.5.21", @@ -130,4 +130,4 @@ "tsc-watch": "^6.0.4", "typescript": "^5.3.3" } -} \ No newline at end of file +} diff --git a/src/discord/commands/global/d.ai.ts b/src/discord/commands/global/d.ai.ts index 7242856a4..961d7073c 100644 --- a/src/discord/commands/global/d.ai.ts +++ b/src/discord/commands/global/d.ai.ts @@ -64,9 +64,9 @@ const tripbotUAT = '@TripBot UAT (Moonbear)'; // Costs per 1k tokens const aiCosts = { - GPT_3_5_TURBO: { - input: 0.0005, - output: 0.0015, + GPT_3_5_TURBO: { // LAZY TEMP FIX - CHANGE NAME THESE PRICES ARE FOR 4o MINI NOW + input: 0.00015, + output: 0.00060, }, // GPT_3_5_TURBO_1106: { // input: 0.001, diff --git a/src/global/commands/g.ai.ts b/src/global/commands/g.ai.ts index aee01a454..b2cec60ca 100644 --- a/src/global/commands/g.ai.ts +++ b/src/global/commands/g.ai.ts @@ -4,9 +4,9 @@ import OpenAI from 'openai'; import { ai_personas } from '@prisma/client'; import { ImagesResponse, ModerationCreateResponse } from 'openai/resources'; -import { Assistant } from 'openai/resources/beta/assistants/assistants'; +import { Assistant } from 'openai/resources/beta/assistants'; import { ThreadDeleted } from 'openai/resources/beta/threads/threads'; -import { MessageContentText } from 'openai/resources/beta/threads/messages/messages'; +import { TextContentBlock } from 'openai/resources/beta/threads/messages'; import { GoogleGenerativeAI, HarmCategory, HarmBlockThreshold, GenerationConfig, SafetySetting, Part, InputContent, GenerateContentResult, @@ -99,7 +99,7 @@ const aiFunctions = [ }, }, }, -] as Assistant.Function[]; +]; export async function aiModerateReport( message: string, @@ -139,8 +139,9 @@ export async function getAssistant(name: string):Promise { name, }, }); - - const modelName = personaData.ai_model.toLowerCase() === 'gpt_3_5_turbo' ? 'gpt-3.5-turbo-1106' : 'gpt-4-turbo-preview'; + // LAZY TEMP FIX + // eslint-disable-next-line sonarjs/no-all-duplicated-branches + const modelName = personaData.ai_model.toLowerCase() === 'gpt-3.5-turbo-1106' ? 'gpt-4o-mini' : 'gpt-4o-mini'; // Upload a file with an "assistants" purpose // const combinedDb = await openAi.files.create({ @@ -153,13 +154,13 @@ export async function getAssistant(name: string):Promise { model: modelName, name: personaData.name, description: personaData.description, - instructions: `${objectiveTruths}\n${personaData.prompt}`, + instructions: `${objectiveTruths}\n${personaData.prompt}`, // LAZY TEMP FIX. https://platform.openai.com/docs/assistants/migration/what-has-changed - UPDATE to version 4.52.7 in package.json tools: [ // { type: 'code_interpreter' }, // { type: 'retrieval' }, // ...aiFunctions, ], - file_ids: [], + // file_ids: [], metadata: {}, } as Omit; @@ -536,7 +537,7 @@ async function openAiWaitForRun( await openAi.beta.threads.messages.list(thread.id, { limit: 1 }) ).data[0].content[0]; // log.debug(F, `messageContent: ${JSON.stringify(messageContent, null, 2)}`); - return { response: (messageContent as MessageContentText).text.value.slice(0, 2000), promptTokens, completionTokens }; + return { response: (messageContent as TextContentBlock).text.value.slice(0, 2000), promptTokens, completionTokens }; } case 'requires_action': { log.debug(F, 'requires_action'); @@ -757,7 +758,7 @@ async function openAiChat( let model = aiPersona.ai_model.toLowerCase(); // Convert ai models into proper names if (aiPersona.ai_model === 'GPT_3_5_TURBO') { - model = 'gpt-3.5-turbo-1106'; + model = 'gpt-4o-mini'; // LAZY TEMP FIX } // This message list is sent to the API @@ -906,7 +907,7 @@ export async function aiFlairMod( let model = aiPersona.ai_model.toLowerCase(); // Convert ai models into proper names if (aiPersona.ai_model === 'GPT_3_5_TURBO') { - model = 'gpt-3.5-turbo-1106'; + model = 'gpt-4o-mini'; // LAZY TEMP FIX } // This message list is sent to the API const chatCompletionMessages = [{