From adc58c201f8ade4f4308e0be1d439312b1b6ed2c Mon Sep 17 00:00:00 2001 From: Ryan Xin Date: Mon, 12 Aug 2024 11:07:12 +0100 Subject: [PATCH] release: v1.3.2 Update gpt-4o model Set max model context in env --- package.json | 2 +- service/.env.example | 3 +++ service/package.json | 4 ++-- service/pnpm-lock.yaml | 22 ++++++++++------------ service/src/chatgpt/index.ts | 15 +++++++++------ service/src/chatgpt/types.ts | 3 ++- service/src/middleware/logger.ts | 6 +++--- 7 files changed, 30 insertions(+), 25 deletions(-) diff --git a/package.json b/package.json index 1b5403e..9f835f8 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "another-chatgpt-web", - "version": "1.3.1", + "version": "1.3.2", "private": false, "description": "Another ChatGPT Web", "author": "Flat 2", diff --git a/service/.env.example b/service/.env.example index 7a5d302..ab806b9 100644 --- a/service/.env.example +++ b/service/.env.example @@ -7,3 +7,6 @@ SERVER_PORT=3002 # boolean DEBUG_MODE=false + +# number +MAX_CONTEXT_TOKENS=127000 diff --git a/service/package.json b/service/package.json index 424fce2..beca3bf 100644 --- a/service/package.json +++ b/service/package.json @@ -1,6 +1,6 @@ { "name": "another-chatgpt-web-service", - "version": "1.3.1", + "version": "1.3.2", "private": false, "description": "Another ChatGPT Web Service", "author": "Flat 2", @@ -23,7 +23,7 @@ "dependencies": { "dotenv": "^16.4.5", "express": "^4.19.2", - "openai": "^4.52.2", + "openai": "^4.55.4", "tiktoken": "^1.0.15" }, "devDependencies": { diff --git a/service/pnpm-lock.yaml b/service/pnpm-lock.yaml index b8b6639..7a6a04f 100644 --- a/service/pnpm-lock.yaml +++ b/service/pnpm-lock.yaml @@ -15,8 +15,8 @@ importers: specifier: ^4.19.2 version: 4.19.2 openai: - specifier: ^4.52.2 - version: 4.52.2 + specifier: ^4.55.4 + version: 4.55.4 tiktoken: specifier: ^1.0.15 version: 1.0.15 @@ -885,9 +885,14 @@ packages: resolution: {integrity: sha512-kbpaSSGJTWdAY5KPVeMOKXSrPtr8C8C7wodJbcsd51jRnmD+GZu8Y0VoU6Dm5Z4vWr0Ig/1NKuWRKf7j5aaYSg==} engines: {node: '>=6'} - openai@4.52.2: - resolution: {integrity: sha512-mMc0XgFuVSkcm0lRIi8zaw++otC82ZlfkCur1qguXYWPETr/+ZwL9A/vvp3YahX+shpaT6j03dwsmUyLAfmEfg==} + openai@4.55.4: + resolution: {integrity: sha512-TEC75Y6U/OKIJp9fHao3zkTYfKLYGqXdD2TI+xN2Zd5W8KNKvv6E4/OBTOW7jg7fySfrBrhy5fYzBbyBcdHEtQ==} hasBin: true + peerDependencies: + zod: ^3.23.8 + peerDependenciesMeta: + zod: + optional: true package-json-from-dist@1.0.0: resolution: {integrity: sha512-dATvCeZN/8wQsGywez1mzHtTlP22H8OEfPrVMLNr4/eGa+ijtLn/6M5f0dY8UKNrC2O9UCU6SSoG3qRKnt7STw==} @@ -1135,10 +1140,6 @@ packages: resolution: {integrity: sha512-BNGbWLfd0eUPabhkXUVm0j8uuvREyTh5ovRa/dyow/BqAbZJyC+5fU+IzQOzmAKzYqYRAISoRhdQr3eIZ/PXqg==} engines: {node: '>= 0.8'} - web-streams-polyfill@3.3.3: - resolution: {integrity: sha512-d2JWLCivmZYTSIoge9MsgFCZrt571BikcWGYkjC1khllbTeDlGqZ2D8vD8E/lJa8WGWbb7Plm8/XJYV7IJHZZw==} - engines: {node: '>= 8'} - web-streams-polyfill@4.0.0-beta.3: resolution: {integrity: sha512-QW95TCTaHmsYfHDybGMwO5IJIM93I/6vTRk+daHTWFPhwh+C8Cg7j7XyKrwrj8Ib6vYXe0ocYNrmzY4xAAN6ug==} engines: {node: '>= 14'} @@ -1925,7 +1926,7 @@ snapshots: dependencies: mimic-fn: 2.1.0 - openai@4.52.2: + openai@4.55.4: dependencies: '@types/node': 18.19.39 '@types/node-fetch': 2.6.11 @@ -1934,7 +1935,6 @@ snapshots: form-data-encoder: 1.7.2 formdata-node: 4.4.1 node-fetch: 2.7.0 - web-streams-polyfill: 3.3.3 transitivePeerDependencies: - encoding @@ -2175,8 +2175,6 @@ snapshots: vary@1.1.2: {} - web-streams-polyfill@3.3.3: {} - web-streams-polyfill@4.0.0-beta.3: {} webidl-conversions@3.0.1: {} diff --git a/service/src/chatgpt/index.ts b/service/src/chatgpt/index.ts index e8a1971..3bccf96 100644 --- a/service/src/chatgpt/index.ts +++ b/service/src/chatgpt/index.ts @@ -11,17 +11,20 @@ dotenv.config({ override: true }) const OPENAI_API_KEY: string = process.env.OPENAI_API_KEY ?? '' const DEBUG_MODE: boolean = process.env.DEBUG_MODE === 'true' +const MAX_CONTEXT_TOKENS: number = parseInt(process.env.MAX_CONTEXT_TOKENS) || 999999 if (!isNotEmptyString(OPENAI_API_KEY)) throw new Error('Missing OPENAI_API_KEY environment variable') const model_contexts: { [model in Model]: ModelContext } = { 'gpt-4o': { - max_context_tokens: 127000, + model_name: 'gpt-4o-2024-08-06', + max_context_tokens: Math.min(MAX_CONTEXT_TOKENS, 127000), max_response_tokens: 4000, }, 'gpt-4o-mini': { - max_context_tokens: 127000, + model_name: 'gpt-4o-mini-2024-07-18', + max_context_tokens: Math.min(MAX_CONTEXT_TOKENS, 127000), max_response_tokens: 16000, }, } @@ -62,13 +65,13 @@ export async function chatReplyProcess(options: RequestOptions) { if (!(model in model_contexts)) { return sendResponse({ type: 'Fail', message: 'Invalid model requested.' }) } - const { max_context_tokens, max_response_tokens } = model_contexts[model] + const { model_name, max_context_tokens, max_response_tokens } = model_contexts[model] let estimated_tokens: number ({ messages, estimated_tokens } = filterMessagesByTokenCount(messages, max_context_tokens - max_response_tokens)) if (DEBUG_MODE) { global.console.log('-'.repeat(30)) global.console.log(`Time: ${new Date().toISOString()}`) - global.console.log(`Model: ${model}`) + global.console.log(`Model: ${model_name}`) global.console.log(`Temperature: ${temperature}`) global.console.log(`Top P: ${top_p}`) global.console.log(`Estimated tokens: ${estimated_tokens}`) @@ -76,7 +79,7 @@ export async function chatReplyProcess(options: RequestOptions) { } try { const stream = await openai.chat.completions.create({ - model, + model: model_name, messages, max_tokens: max_response_tokens, stream: true, @@ -95,7 +98,7 @@ export async function chatReplyProcess(options: RequestOptions) { if (DEBUG_MODE) { global.console.log(`Usage: ${JSON.stringify(usage, null, 2)}`) } - await logUsage(model, usage, user) + await logUsage(model_name, usage, user) return sendResponse({ type: 'Success' }) } catch (error: any) { global.console.error(error) diff --git a/service/src/chatgpt/types.ts b/service/src/chatgpt/types.ts index 05d7ca3..c78138a 100644 --- a/service/src/chatgpt/types.ts +++ b/service/src/chatgpt/types.ts @@ -1,4 +1,4 @@ -import type { ChatCompletionChunk } from 'openai/src/resources/chat' +import type { ChatCompletionChunk, ChatModel } from 'openai/src/resources/chat' export type Model = 'gpt-4o' | 'gpt-4o-mini' @@ -17,6 +17,7 @@ export interface RequestOptions { } export interface ModelContext { + model_name: ChatModel max_context_tokens: number max_response_tokens: number } diff --git a/service/src/middleware/logger.ts b/service/src/middleware/logger.ts index 4683716..f708a2e 100644 --- a/service/src/middleware/logger.ts +++ b/service/src/middleware/logger.ts @@ -1,11 +1,11 @@ import fs from 'fs/promises' import type { CompletionUsage } from 'openai/src/resources/completions' -import type { Model } from '../chatgpt/types' +import type { ChatModel } from 'openai/src/resources/chat' interface UsageRecord { [date: string]: { [user: string]: { - [model in Model]?: { + [model in ChatModel]?: { prompt_tokens: number completion_tokens: number } @@ -13,7 +13,7 @@ interface UsageRecord { } } -export async function logUsage(model: Model, usage: CompletionUsage, user?: string) { +export async function logUsage(model: ChatModel, usage: CompletionUsage, user?: string) { user = user ?? '__default__' const date = new Date() const monthKey = `${date.getFullYear()}_${(date.getMonth() + 1).toString().padStart(2, '0')}`