Skip to content

Commit 408f39d

Browse files
committed
fix whitelist issue with private chats + update payment process for chat completion and dalle
1 parent 314e514 commit 408f39d

File tree

6 files changed

+29
-31
lines changed

6 files changed

+29
-31
lines changed

src/config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ export default {
7575
// ? parseInt(process.env.WORD_COUNT_BETWEEN)
7676
// : 10,
7777
priceAdjustment: process.env.PRICE_ADJUSTMENT
78-
? parseInt(process.env.PRICE_ADJUSTMENT)
78+
? parseFloat(process.env.PRICE_ADJUSTMENT)
7979
: 2,
8080
isFreePromptChatGroups: false,
8181
isEnabled: Boolean(parseInt(process.env.CHAT_GPT_ENABLED ?? '1')),

src/modules/open-ai/api/openAi.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -281,11 +281,11 @@ export const getChatModelPrice = (
281281
model: ChatModel,
282282
inCents = true,
283283
inputTokens: number,
284-
outPutTokens?: number
284+
outputTokens?: number
285285
): number => {
286286
let price = model.inputPrice * inputTokens
287-
price += outPutTokens
288-
? outPutTokens * model.outputPrice
287+
price += outputTokens
288+
? outputTokens * model.outputPrice
289289
: model.maxContextTokens * model.outputPrice
290290
price = inCents ? price * 100 : price
291291
return price / 1000

src/modules/open-ai/helpers.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -238,23 +238,24 @@ export const hasPrefix = (prompt: string): string => {
238238
)
239239
}
240240

241-
export const getPromptPrice = (completion: string, data: ChatPayload): { price: number, promptTokens: number, completionTokens: number } => {
241+
export const getPromptPrice = (completion: string, data: ChatPayload): { price: number, promptTokens: number, completionTokens: number, totalTokens: number } => {
242242
const { conversation, ctx, model } = data
243-
243+
const currentUsage = data.prompt ? 0 : ctx.session.openAi.chatGpt.usage
244244
const prompt = data.prompt ? data.prompt : conversation[conversation.length - 1].content
245-
const promptTokens = getTokenNumber(prompt as string)
245+
const promptTokens = getTokenNumber(prompt as string) + currentUsage
246246
const completionTokens = getTokenNumber(completion)
247247
const modelPrice = getChatModel(model)
248248
const price =
249249
getChatModelPrice(modelPrice, true, promptTokens, completionTokens) *
250250
config.openAi.chatGpt.priceAdjustment
251251
conversation.push({ content: completion, role: 'system' })
252-
ctx.session.openAi.chatGpt.usage += promptTokens + completionTokens
252+
ctx.session.openAi.chatGpt.usage += completionTokens
253253
ctx.session.openAi.chatGpt.price += price
254254
return {
255255
price,
256256
promptTokens,
257-
completionTokens
257+
completionTokens,
258+
totalTokens: data.prompt ? promptTokens + completionTokens : ctx.session.openAi.chatGpt.usage
258259
}
259260
}
260261

src/modules/open-ai/index.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ import { Callbacks } from '../types'
4848
import { LlmsBot } from '../llms'
4949
import { type PhotoSize } from 'grammy/types'
5050

51+
const priceAdjustment = config.openAi.chatGpt.priceAdjustment
5152
export class OpenAIBot implements PayableBot {
5253
public readonly module = 'OpenAIBot'
5354
private readonly logger: Logger
@@ -90,7 +91,6 @@ export class OpenAIBot implements PayableBot {
9091

9192
public getEstimatedPrice (ctx: any): number {
9293
try {
93-
const priceAdjustment = config.openAi.chatGpt.priceAdjustment
9494
const prompts = ctx.match
9595
if (this.isSupportedImageReply(ctx) && !isNaN(+prompts)) {
9696
const imageNumber = ctx.message?.caption || ctx.message?.text
@@ -378,9 +378,7 @@ export class OpenAIBot implements PayableBot {
378378
ctx.transient.analytics.actualResponseTime = now()
379379
const price = getPromptPrice(completion, data)
380380
this.logger.info(
381-
`streamChatCompletion result = tokens: ${
382-
price.promptTokens + price.completionTokens
383-
} | ${model} | price: ${price.price}¢`
381+
`streamChatCompletion result = tokens: ${price.totalTokens} | ${model} | price: ${price.price}¢` // price.promptTokens + price.completionTokens }
384382
)
385383
return {
386384
price: price.price,

src/modules/open-ai/types.ts

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -22,60 +22,61 @@ export enum ChatGPTModelsEnum {
2222
export const ChatGPTModels: Record<string, ChatModel> = {
2323
'gpt-4': {
2424
name: 'gpt-4',
25-
inputPrice: 0.03,
26-
outputPrice: 0.06,
25+
inputPrice: 0.03, // 3
26+
outputPrice: 0.06, // 6
2727
maxContextTokens: 8192,
2828
chargeType: 'TOKEN'
2929
},
3030
'gpt-4-32k': {
3131
name: 'gpt-4-32k',
32-
inputPrice: 0.06,
33-
outputPrice: 0.12,
32+
inputPrice: 0.06, // 6
33+
outputPrice: 0.12, // 12
3434
maxContextTokens: 32000,
3535
chargeType: 'TOKEN'
3636
},
3737
'gpt-3.5-turbo': {
3838
name: 'gpt-3.5-turbo',
39-
inputPrice: 0.0015,
40-
outputPrice: 0.002,
39+
inputPrice: 0.0015, // 0.15
40+
outputPrice: 0.002, // 0.2
4141
maxContextTokens: 4000,
4242
chargeType: 'TOKEN'
4343
},
4444
'gpt-3.5-turbo-16k': {
4545
name: 'gpt-3.5-turbo-16k',
46-
inputPrice: 0.003,
47-
outputPrice: 0.004,
46+
inputPrice: 0.003, // 0.3
47+
outputPrice: 0.004, // 0.4
4848
maxContextTokens: 16000,
4949
chargeType: 'TOKEN'
5050
},
5151
'gpt-4-vision-preview': {
5252
name: 'gpt-4-vision-preview',
53-
inputPrice: 0.03,
54-
outputPrice: 0.06,
53+
inputPrice: 0.03, // 3
54+
outputPrice: 0.06, // 6
5555
maxContextTokens: 16000,
5656
chargeType: 'TOKEN'
5757
}
5858
}
5959

60+
// needs to be in cents
6061
export const DalleGPTModels: Record<string, DalleGPTModel> = {
6162
'1024x1792': {
6263
size: '1024x1792',
63-
price: 0.10
64+
price: 12 // 0.12
6465
},
6566
'1792x1024': {
6667
size: '1792x1024',
67-
price: 0.10
68+
price: 12 // 0.12
6869
},
6970
'1024x1024': {
7071
size: '1024x1024',
71-
price: 0.10
72+
price: 8 // 0.08
7273
},
7374
'512x512': {
7475
size: '512x512',
75-
price: 0.10
76+
price: 8 // 0.10
7677
},
7778
'256x256': {
7879
size: '256x256',
79-
price: 0.10
80+
price: 8 // 0.10
8081
}
8182
}

src/modules/payment/index.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -291,10 +291,8 @@ export class BotPayments {
291291
return true
292292
}
293293
}
294-
return false
295-
} else {
296-
return true
297294
}
295+
return false
298296
}
299297

300298
public isPaymentsEnabled (): boolean {

0 commit comments

Comments
 (0)