Skip to content

Commit

Permalink
release: v1.3.2
Browse files Browse the repository at this point in the history
Update gpt-4o model
Set max model context in env
  • Loading branch information
RyanXinOne committed Aug 12, 2024
1 parent a6a33af commit adc58c2
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 25 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
3 changes: 3 additions & 0 deletions service/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,6 @@ SERVER_PORT=3002

# boolean
DEBUG_MODE=false

# number
MAX_CONTEXT_TOKENS=127000
4 changes: 2 additions & 2 deletions service/package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand All @@ -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": {
Expand Down
22 changes: 10 additions & 12 deletions service/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 9 additions & 6 deletions service/src/chatgpt/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
}
Expand Down Expand Up @@ -62,21 +65,21 @@ 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}`)
global.console.log(`Messages: ${JSON.stringify(messages, null, 2)}`)
}
try {
const stream = await openai.chat.completions.create({
model,
model: model_name,
messages,
max_tokens: max_response_tokens,
stream: true,
Expand All @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion service/src/chatgpt/types.ts
Original file line number Diff line number Diff line change
@@ -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'

Expand All @@ -17,6 +17,7 @@ export interface RequestOptions {
}

export interface ModelContext {
model_name: ChatModel
max_context_tokens: number
max_response_tokens: number
}
6 changes: 3 additions & 3 deletions service/src/middleware/logger.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
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
}
}
}
}

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')}`
Expand Down

0 comments on commit adc58c2

Please sign in to comment.