From cbd80b9d3923834ff8ad7bb8237cb66ab354baec Mon Sep 17 00:00:00 2001 From: fegloff Date: Wed, 17 Jan 2024 15:45:27 -0500 Subject: [PATCH 1/7] fix message with code snippet that where handled as URL --- src/modules/open-ai/helpers.ts | 5 +++++ src/modules/open-ai/index.ts | 6 ++++-- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/src/modules/open-ai/helpers.ts b/src/modules/open-ai/helpers.ts index 5a858a41..29dab134 100644 --- a/src/modules/open-ai/helpers.ts +++ b/src/modules/open-ai/helpers.ts @@ -88,6 +88,11 @@ const hasUrlPrompt = (prompt: string): string => { return url } +export const hasCodeSnippet = (ctx: OnMessageContext | OnCallBackQueryData): boolean => { + const entities = ctx.entities('pre') // pre => code snippets + return entities.length > 0 +} + export const hasUrl = ( ctx: OnMessageContext | OnCallBackQueryData, prompt: string diff --git a/src/modules/open-ai/index.ts b/src/modules/open-ai/index.ts index 746cc18c..cf7318ab 100644 --- a/src/modules/open-ai/index.ts +++ b/src/modules/open-ai/index.ts @@ -35,6 +35,7 @@ import { hasNewPrefix, hasPrefix, hasUrl, + hasCodeSnippet, isMentioned, MAX_TRIES, preparePrompt, @@ -533,13 +534,14 @@ export class OpenAIBot implements PayableBot { return } const { url, newPrompt } = hasUrl(ctx, prompt) - if (chatConversation.length === 0 && !url) { + const hasCode = hasCodeSnippet(ctx) + if (chatConversation.length === 0 && (hasCode || !url)) { chatConversation.push({ role: 'system', content: config.openAi.chatGpt.chatCompletionContext }) } - if (url && ctx.chat?.id) { + if (!hasCode && url && ctx.chat?.id) { await this.llmsBot.urlHandler(ctx, url, newPrompt) } else { chatConversation.push({ From b93ab44ed6d897ec3148c5f6529f8822bc535a60 Mon Sep 17 00:00:00 2001 From: fegloff Date: Wed, 17 Jan 2024 17:21:40 -0500 Subject: [PATCH 2/7] remove vision call on image reply --- src/modules/open-ai/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/modules/open-ai/index.ts b/src/modules/open-ai/index.ts index cf7318ab..27924f0c 100644 --- a/src/modules/open-ai/index.ts +++ b/src/modules/open-ai/index.ts @@ -145,7 +145,7 @@ export class OpenAIBot implements PayableBot { const photo = ctx.message?.photo ?? ctx.message?.reply_to_message?.photo if (photo && ctx.session.openAi.imageGen.isEnabled) { const prompt = ctx.message?.caption ?? ctx.message?.text - if (prompt) { // && !isNaN(+prompt) + if (prompt && !isNaN(+prompt)) { // && !isNaN(+prompt) return true } } From 9c43b03893cd15d2409a0de0af305e8ace16982b Mon Sep 17 00:00:00 2001 From: fegloff Date: Wed, 17 Jan 2024 22:36:17 -0500 Subject: [PATCH 3/7] enable vision command on image reply for group chat --- src/modules/open-ai/index.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/modules/open-ai/index.ts b/src/modules/open-ai/index.ts index 27924f0c..0333346b 100644 --- a/src/modules/open-ai/index.ts +++ b/src/modules/open-ai/index.ts @@ -147,6 +147,8 @@ export class OpenAIBot implements PayableBot { const prompt = ctx.message?.caption ?? ctx.message?.text if (prompt && !isNaN(+prompt)) { // && !isNaN(+prompt) return true + } else if (prompt && (ctx.chat?.type === 'private' || ctx.hasCommand(SupportedCommands.vision.name))) { + return true } } return false From c686b84cedfd527f9a1df2ca3b5dc47290344826 Mon Sep 17 00:00:00 2001 From: fegloff Date: Thu, 18 Jan 2024 14:12:10 -0500 Subject: [PATCH 4/7] refactor openAI command list + fix onPrefix method that was having issues with new prefix --- src/config.ts | 9 ----- src/modules/llms/helpers.ts | 3 +- src/modules/open-ai/helpers.ts | 43 +++++++++++----------- src/modules/open-ai/index.ts | 65 ++++++++++++++++------------------ 4 files changed, 54 insertions(+), 66 deletions(-) diff --git a/src/config.ts b/src/config.ts index a917ce31..cbb12edf 100644 --- a/src/config.ts +++ b/src/config.ts @@ -80,15 +80,6 @@ export default { parseInt(process.env.TYPING_STATUS_ENABLED ?? '1') ), model: process.env.OPENAI_MODEL ?? 'gpt-3.5-turbo', - prefixes: { - chatPrefix: process.env.ASK_PREFIX - ? process.env.ASK_PREFIX.split(',') - : ['a.', '.'], // , "?", ">", - newPrefix: process.env.NEW_PREFIX - ? process.env.NEW_PREFIX.split(',') - : ['n.', '..'], - llamaPrefix: ['*'] - }, minimumBalance: parseInt(process.env.MIN_BALANCE ?? '0') } }, diff --git a/src/modules/llms/helpers.ts b/src/modules/llms/helpers.ts index 108daf27..83fa3734 100644 --- a/src/modules/llms/helpers.ts +++ b/src/modules/llms/helpers.ts @@ -22,6 +22,7 @@ export const SupportedCommands = { } export const MAX_TRIES = 3 +const LLAMA_PREFIX_LIST = ['*'] export const isMentioned = ( ctx: OnMessageContext | OnCallBackQueryData @@ -40,7 +41,7 @@ export const isMentioned = ( } export const hasLlamaPrefix = (prompt: string): string => { - const prefixList = config.openAi.chatGpt.prefixes.llamaPrefix + const prefixList = LLAMA_PREFIX_LIST for (let i = 0; i < prefixList.length; i++) { if (prompt.toLocaleLowerCase().startsWith(prefixList[i])) { return prefixList[i] diff --git a/src/modules/open-ai/helpers.ts b/src/modules/open-ai/helpers.ts index 29dab134..c45569c9 100644 --- a/src/modules/open-ai/helpers.ts +++ b/src/modules/open-ai/helpers.ts @@ -4,30 +4,31 @@ import { type ParseMode } from 'grammy/types' import { getChatModel, getChatModelPrice, getTokenNumber } from './api/openAi' import { type Message, type InlineKeyboardMarkup } from 'grammy/out/types' import { isValidUrl } from './utils/web-crawler' -// import { llmAddUrlDocument } from '../llms/api/llmApi' -export const SupportedCommands = { - chat: { name: 'chat' }, - ask: { name: 'ask' }, - vision: { name: 'vision' }, - ask35: { name: 'ask35' }, - new: { name: 'new' }, - gpt4: { name: 'gpt4' }, - ask32: { name: 'ask32' }, - gpt: { name: 'gpt' }, - last: { name: 'last' }, - dalle: { name: 'dalle' }, - dalleImg: { name: 'image' }, - dalleShort: { name: 'img' }, - dalleShorter: { name: 'i' }, - genImgEn: { name: 'genImgEn' }, - on: { name: 'on' }, - off: { name: 'off' } +export enum SupportedCommands { + chat = 'chat', + ask = 'ask', + vision = 'vision', + ask35 = 'ask35', + new = 'new', + gpt4 = 'gpt4', + ask32 = 'ask32', + gpt = 'gpt', + last = 'last', + dalle = 'dalle', + dalleImg = 'image', + dalleShort = 'img', + dalleShorter = 'i', + genImgEn = 'genImgEn', + on = 'on', + off = 'off' } export const MAX_TRIES = 3 -const DALLE_PREFIX_LIST = ['i. ', ',', 'image ', 'd.', 'img '] +export const DALLE_PREFIX_LIST = ['i. ', ',', 'image ', 'd.', 'img '] +export const CHAT_GPT_PREFIX_LIST = ['a.', '.'] +export const NEW_PREFIX_LIST = ['n.', '..'] export const isMentioned = ( ctx: OnMessageContext | OnCallBackQueryData @@ -46,7 +47,7 @@ export const isMentioned = ( } export const hasChatPrefix = (prompt: string): string => { - const prefixList = config.openAi.chatGpt.prefixes.chatPrefix + const prefixList = CHAT_GPT_PREFIX_LIST for (let i = 0; i < prefixList.length; i++) { if (prompt.toLocaleLowerCase().startsWith(prefixList[i])) { return prefixList[i] @@ -66,7 +67,7 @@ export const hasDallePrefix = (prompt: string): string => { } export const hasNewPrefix = (prompt: string): string => { - const prefixList = config.openAi.chatGpt.prefixes.newPrefix + const prefixList = NEW_PREFIX_LIST for (let i = 0; i < prefixList.length; i++) { if (prompt.toLocaleLowerCase().startsWith(prefixList[i])) { return prefixList[i] diff --git a/src/modules/open-ai/index.ts b/src/modules/open-ai/index.ts index 0333346b..6b8ddb5a 100644 --- a/src/modules/open-ai/index.ts +++ b/src/modules/open-ai/index.ts @@ -2,7 +2,6 @@ import { GrammyError, InlineKeyboard } from 'grammy' import OpenAI from 'openai' import { type Logger, pino } from 'pino' -import { getCommandNamePrompt } from '../1country/utils' import { type BotPayments } from '../payment' import { type ChatConversation, @@ -76,7 +75,7 @@ export class OpenAIBot implements PayableBot { ctx: OnMessageContext | OnCallBackQueryData ): boolean { const hasCommand = ctx.hasCommand( - Object.values(SupportedCommands).map((command) => command.name) + Object.values(SupportedCommands).map((command) => command) ) if (isMentioned(ctx)) { return true @@ -104,10 +103,10 @@ export class OpenAIBot implements PayableBot { return 0 } if ( - ctx.hasCommand([SupportedCommands.dalle.name, - SupportedCommands.dalleImg.name, - SupportedCommands.dalleShort.name, - SupportedCommands.dalleShorter.name]) + ctx.hasCommand([SupportedCommands.dalle, + SupportedCommands.dalleImg, + SupportedCommands.dalleShort, + SupportedCommands.dalleShorter]) ) { const imageNumber = ctx.session.openAi.imageGen.numImages const imageSize = ctx.session.openAi.imageGen.imgSize @@ -115,7 +114,7 @@ export class OpenAIBot implements PayableBot { const price = getDalleModelPrice(model, true, imageNumber) // cents return price * priceAdjustment } - if (ctx.hasCommand(SupportedCommands.genImgEn.name)) { + if (ctx.hasCommand(SupportedCommands.genImgEn)) { const imageNumber = ctx.session.openAi.imageGen.numImages const imageSize = ctx.session.openAi.imageGen.imgSize const chatModelName = ctx.session.openAi.chatGpt.model @@ -147,7 +146,7 @@ export class OpenAIBot implements PayableBot { const prompt = ctx.message?.caption ?? ctx.message?.text if (prompt && !isNaN(+prompt)) { // && !isNaN(+prompt) return true - } else if (prompt && (ctx.chat?.type === 'private' || ctx.hasCommand(SupportedCommands.vision.name))) { + } else if (prompt && (ctx.chat?.type === 'private' || ctx.hasCommand(SupportedCommands.vision))) { return true } } @@ -182,7 +181,7 @@ export class OpenAIBot implements PayableBot { } if ( - ctx.hasCommand(SupportedCommands.chat.name) || + ctx.hasCommand(SupportedCommands.chat) || (ctx.message?.text?.startsWith('chat ') && ctx.chat?.type === 'private') ) { ctx.session.openAi.chatGpt.model = ChatGPTModelsEnum.GPT_4 @@ -191,17 +190,16 @@ export class OpenAIBot implements PayableBot { } if ( - ctx.hasCommand(SupportedCommands.new.name) || + ctx.hasCommand(SupportedCommands.new) || (ctx.message?.text?.startsWith('new ') && ctx.chat?.type === 'private') ) { - ctx.session.openAi.chatGpt.model = ChatGPTModelsEnum.GPT_4 await this.onEnd(ctx) await this.onChat(ctx) return } if ( - ctx.hasCommand(SupportedCommands.ask.name) || + ctx.hasCommand(SupportedCommands.ask) || (ctx.message?.text?.startsWith('ask ') && ctx.chat?.type === 'private') ) { ctx.session.openAi.chatGpt.model = ChatGPTModelsEnum.GPT_4 @@ -209,31 +207,31 @@ export class OpenAIBot implements PayableBot { return } - if (ctx.hasCommand(SupportedCommands.ask35.name)) { + if (ctx.hasCommand(SupportedCommands.ask35)) { ctx.session.openAi.chatGpt.model = ChatGPTModelsEnum.GPT_35_TURBO_16K await this.onChat(ctx) return } - if (ctx.hasCommand(SupportedCommands.gpt4.name)) { + if (ctx.hasCommand(SupportedCommands.gpt4)) { ctx.session.openAi.chatGpt.model = ChatGPTModelsEnum.GPT_4 await this.onChat(ctx) return } - if (ctx.hasCommand(SupportedCommands.gpt.name)) { + if (ctx.hasCommand(SupportedCommands.gpt)) { ctx.session.openAi.chatGpt.model = ChatGPTModelsEnum.GPT_4 await this.onChat(ctx) return } - if (ctx.hasCommand(SupportedCommands.ask32.name)) { + if (ctx.hasCommand(SupportedCommands.ask32)) { ctx.session.openAi.chatGpt.model = ChatGPTModelsEnum.GPT_4_32K await this.onChat(ctx) return } - if (ctx.hasCommand(SupportedCommands.vision.name)) { + if (ctx.hasCommand(SupportedCommands.vision)) { const photoUrl = getUrlFromText(ctx) if (photoUrl) { const prompt = ctx.match @@ -252,10 +250,10 @@ export class OpenAIBot implements PayableBot { } if ( - ctx.hasCommand([SupportedCommands.dalle.name, - SupportedCommands.dalleImg.name, - SupportedCommands.dalleShort.name, - SupportedCommands.dalleShorter.name]) || + ctx.hasCommand([SupportedCommands.dalle, + SupportedCommands.dalleImg, + SupportedCommands.dalleShort, + SupportedCommands.dalleShorter]) || (ctx.message?.text?.startsWith('image ') && ctx.chat?.type === 'private') ) { let prompt = (ctx.match ? ctx.match : ctx.message?.text) as string @@ -280,16 +278,16 @@ export class OpenAIBot implements PayableBot { return } - if (ctx.hasCommand(SupportedCommands.last.name)) { + if (ctx.hasCommand(SupportedCommands.last)) { await this.onLast(ctx) return } const text = ctx.message?.text ?? '' - - if (hasNewPrefix(text) !== '') { + const newPrefix = hasNewPrefix(text) + if (newPrefix !== '') { await this.onEnd(ctx) - await this.onPrefix(ctx) + await this.onPrefix(ctx, newPrefix) return } @@ -311,9 +309,9 @@ export class OpenAIBot implements PayableBot { } return } - - if (hasChatPrefix(text) !== '') { - await this.onPrefix(ctx) + const prefix = hasChatPrefix(text) + if (prefix !== '') { + await this.onPrefix(ctx, prefix) return } @@ -426,7 +424,7 @@ export class OpenAIBot implements PayableBot { } } - async onPrefix (ctx: OnMessageContext | OnCallBackQueryData): Promise { + async onPrefix (ctx: OnMessageContext | OnCallBackQueryData, prefix: string): Promise { try { if (this.botSuspended) { ctx.transient.analytics.sessionState = RequestState.Error @@ -436,13 +434,9 @@ export class OpenAIBot implements PayableBot { ctx.transient.analytics.actualResponseTime = now() return } - const { prompt } = getCommandNamePrompt( - ctx, - SupportedCommands - ) - const prefix = hasPrefix(prompt) + const prompt = ctx.message?.text?.slice(prefix.length) ?? '' ctx.session.openAi.chatGpt.requestQueue.push( - await preparePrompt(ctx, prompt.slice(prefix.length)) + await preparePrompt(ctx, prompt) ) if (!ctx.session.openAi.chatGpt.isProcessingQueue) { ctx.session.openAi.chatGpt.isProcessingQueue = true @@ -750,6 +744,7 @@ export class OpenAIBot implements PayableBot { } async onEnd (ctx: OnMessageContext | OnCallBackQueryData): Promise { + ctx.session.openAi.chatGpt.model = ChatGPTModelsEnum.GPT_4 ctx.session.openAi.chatGpt.chatConversation = [] ctx.session.openAi.chatGpt.usage = 0 ctx.session.openAi.chatGpt.price = 0 From e8766ea98493b74367620edfdd09f86fca555fa9 Mon Sep 17 00:00:00 2001 From: fegloff Date: Thu, 18 Jan 2024 15:03:28 -0500 Subject: [PATCH 5/7] add vision completion context --- src/config.ts | 4 ++++ src/modules/open-ai/api/openAi.ts | 4 ++++ 2 files changed, 8 insertions(+) diff --git a/src/config.ts b/src/config.ts index cbb12edf..ea730217 100644 --- a/src/config.ts +++ b/src/config.ts @@ -65,6 +65,10 @@ export default { image, and voice interactions using OpenAI’s chatgpt, Stable Diffusion, and more. Respond flexibly, but try to stay within 100 words in your response.`, webCrawlerContext: 'You will receive a web crawling text. Please get keys concepts, but try to stay within 4000 words in your response.', + visionCompletionContext: `You are a concise AI Bot powered by Harmony, capable of providing complete responses within a 100-word limit. + For each additional image, extend your response by 30 words. Your responses should be informative and comprehensive, + wrapping up all details without leaving them hanging. Use your flexibility to adapt to any topic, and deliver engaging and fulfilling + conversations in a succinct manner.`, maxTokens: parseInt(process.env.OPENAI_MAX_TOKENS ?? '800'), // telegram messages has a char limit wordLimit: 30, wordCountBetween: 10, diff --git a/src/modules/open-ai/api/openAi.ts b/src/modules/open-ai/api/openAi.ts index ad34603d..0f048316 100644 --- a/src/modules/open-ai/api/openAi.ts +++ b/src/modules/open-ai/api/openAi.ts @@ -191,6 +191,10 @@ export const streamChatVisionCompletion = async ( const payload: any = { model, messages: [ + { + role: 'system', + content: config.openAi.chatGpt.visionCompletionContext + }, { role: 'user', content: [ From 314e514618923320eaea71007201ba203a62d726 Mon Sep 17 00:00:00 2001 From: fegloff Date: Thu, 18 Jan 2024 18:26:17 -0500 Subject: [PATCH 6/7] refactor command handling on 1country and llms bots --- src/config.ts | 6 +---- src/modules/1country/index.ts | 41 ++++++++++++++--------------------- src/modules/llms/helpers.ts | 19 ++++++++-------- src/modules/llms/index.ts | 12 +++++----- 4 files changed, 32 insertions(+), 46 deletions(-) diff --git a/src/config.ts b/src/config.ts index ea730217..b3041cc4 100644 --- a/src/config.ts +++ b/src/config.ts @@ -38,7 +38,6 @@ export default { model: 'chat-bison', minimumBalance: 0, isEnabled: Boolean(parseInt(process.env.LLMS_ENABLED ?? '1')), - prefixes: { bardPrefix: ['b.', 'B.'] }, pdfUrl: process.env.PDF_URL ?? '', processingTime: 300000 }, @@ -96,10 +95,7 @@ export default { defaultRPC: 'https://api.harmony.one', restrictedPhrases: process.env.RESTRICTED_PHRASES ? process.env.RESTRICTED_PHRASES.split(', ') - : ['metamask', 'walletconnect'], - registerPrefix: process.env.COUNTRY_PREFIX - ? process.env.COUNTRY_PREFIX.split(',') - : ['+', '%'] + : ['metamask', 'walletconnect'] }, voiceMemo: { isEnabled: Boolean(parseInt(process.env.VOICE_MEMO_ENABLED ?? '1')), diff --git a/src/modules/1country/index.ts b/src/modules/1country/index.ts index 78ebfb49..eadfa115 100644 --- a/src/modules/1country/index.ts +++ b/src/modules/1country/index.ts @@ -11,30 +11,21 @@ import { type OnMessageContext, type OnCallBackQueryData, type PayableBot, Reque import { type BotPayments } from '../payment' import { getCommandNamePrompt, getUrl } from './utils/' import { isAdmin } from '../open-ai/utils/context' -import config from '../../config' import { MAX_TRIES, sendMessage } from '../open-ai/helpers' import { sleep } from '../sd-images/utils' import { isValidUrl } from '../open-ai/utils/web-crawler' import { now } from '../../utils/perf' -export const SupportedCommands = { - register: { name: 'rent' }, - visit: { name: 'visit' }, - check: { name: 'check' }, - cert: { name: 'cert' }, - nft: { name: 'nft' }, - set: { name: 'set' } +export enum SupportedCommands { + register = 'rent', + visit = 'visit', + check = 'check', + cert = 'cert', + nft = 'nft', + set = 'set' } -// enum SupportedCommands { -// CHECK = "check", -// NFT = "nft", -// VISIT = "visit", -// CERT = "cert", -// RENEW = "renew", -// NOTION = "notion", -// SUBDOMAIN = "subdomain", -// } +const COUNTRY_PREFIX_LIST = ['+', '%'] export class OneCountryBot implements PayableBot { public readonly module = 'OneCountryBot' @@ -58,7 +49,7 @@ export class OneCountryBot implements PayableBot { ctx: OnMessageContext | OnCallBackQueryData ): boolean { const hasCommand = ctx.hasCommand( - Object.values(SupportedCommands).map((command) => command.name) + Object.values(SupportedCommands).map((command) => command) ) const hasPrefix = this.hasPrefix(ctx.message?.text ?? '') if (hasPrefix && ctx.session.oneCountry.lastDomain) { @@ -68,7 +59,7 @@ export class OneCountryBot implements PayableBot { } private hasPrefix (prompt: string): boolean { - const prefixList = config.country.registerPrefix + const prefixList = COUNTRY_PREFIX_LIST for (let i = 0; i < prefixList.length; i++) { if (prompt.toLocaleLowerCase().startsWith(prefixList[i])) { return true @@ -88,17 +79,17 @@ export class OneCountryBot implements PayableBot { return } - if (ctx.hasCommand(SupportedCommands.visit.name)) { + if (ctx.hasCommand(SupportedCommands.visit)) { await this.onVistitCmd(ctx) return } - if (ctx.hasCommand(SupportedCommands.check.name)) { + if (ctx.hasCommand(SupportedCommands.check)) { await this.onCheckCmd(ctx) return } - if (ctx.hasCommand(SupportedCommands.register.name)) { + if (ctx.hasCommand(SupportedCommands.register)) { await this.onRegister(ctx) return } @@ -108,17 +99,17 @@ export class OneCountryBot implements PayableBot { return } - if (ctx.hasCommand(SupportedCommands.nft.name)) { + if (ctx.hasCommand(SupportedCommands.nft)) { await this.onNftCmd(ctx) return } - if (ctx.hasCommand(SupportedCommands.cert.name)) { + if (ctx.hasCommand(SupportedCommands.cert)) { await this.onCertCmd(ctx) return } - if (ctx.hasCommand(SupportedCommands.set.name)) { + if (ctx.hasCommand(SupportedCommands.set)) { await this.onSet(ctx) return } diff --git a/src/modules/llms/helpers.ts b/src/modules/llms/helpers.ts index 83fa3734..c582a83d 100644 --- a/src/modules/llms/helpers.ts +++ b/src/modules/llms/helpers.ts @@ -1,4 +1,3 @@ -import config from '../../config' import { type OnMessageContext, type OnCallBackQueryData, @@ -7,22 +6,22 @@ import { type ChatPayload } from '../types' import { type ParseMode } from 'grammy/types' -// import { getChatModel, getChatModelPrice, getTokenNumber } from "./api/openAi"; import { LlmsModelsEnum } from './types' import { type Message } from 'grammy/out/types' import { llmAddUrlDocument } from './api/llmApi' -export const SupportedCommands = { - bardF: { name: 'bard' }, - bard: { name: 'b' }, - j2Ultra: { name: 'j2-ultra' }, - sum: { name: 'sum' }, - ctx: { name: 'ctx' }, - pdf: { name: 'pdf' } +export enum SupportedCommands { + bardF = 'bard', + bard = 'b', + j2Ultra = 'j2-ultra', + sum = 'sum', + ctx = 'ctx', + pdf = 'pdf' } export const MAX_TRIES = 3 const LLAMA_PREFIX_LIST = ['*'] +const BARD_PREFIX_LIST = ['b.', 'B.'] export const isMentioned = ( ctx: OnMessageContext | OnCallBackQueryData @@ -51,7 +50,7 @@ export const hasLlamaPrefix = (prompt: string): string => { } export const hasBardPrefix = (prompt: string): string => { - const prefixList = config.llms.prefixes.bardPrefix + const prefixList = BARD_PREFIX_LIST for (let i = 0; i < prefixList.length; i++) { if (prompt.toLocaleLowerCase().startsWith(prefixList[i])) { return prefixList[i] diff --git a/src/modules/llms/index.ts b/src/modules/llms/index.ts index 4696ddad..ea2bca8d 100644 --- a/src/modules/llms/index.ts +++ b/src/modules/llms/index.ts @@ -63,7 +63,7 @@ export class LlmsBot implements PayableBot { ctx: OnMessageContext | OnCallBackQueryData ): boolean { const hasCommand = ctx.hasCommand( - Object.values(SupportedCommands).map((command) => command.name) + Object.values(SupportedCommands).map((command) => command) ) if (isMentioned(ctx)) { return true @@ -117,12 +117,12 @@ export class LlmsBot implements PayableBot { return } - if (ctx.hasCommand(SupportedCommands.pdf.name)) { + if (ctx.hasCommand(SupportedCommands.pdf)) { await this.onPdfCommand(ctx) return } - if (ctx.hasCommand(SupportedCommands.bard.name) || ctx.hasCommand(SupportedCommands.bardF.name)) { + if (ctx.hasCommand(SupportedCommands.bard) || ctx.hasCommand(SupportedCommands.bardF)) { await this.onChat(ctx, LlmsModelsEnum.BISON) return } @@ -142,17 +142,17 @@ export class LlmsBot implements PayableBot { return } - if (ctx.hasCommand(SupportedCommands.j2Ultra.name)) { + if (ctx.hasCommand(SupportedCommands.j2Ultra)) { await this.onChat(ctx, LlmsModelsEnum.J2_ULTRA) return } - if (ctx.hasCommand(SupportedCommands.ctx.name)) { + if (ctx.hasCommand(SupportedCommands.ctx)) { await this.onCurrentCollection(ctx) return } - if (ctx.hasCommand(SupportedCommands.sum.name) || + if (ctx.hasCommand(SupportedCommands.sum) || (ctx.message?.text?.startsWith('sum ') && ctx.chat?.type === 'private') ) { await this.onSum(ctx) From 408f39dd2eaf2f932cbf72b7592c55f29ae7bcdd Mon Sep 17 00:00:00 2001 From: fegloff Date: Mon, 22 Jan 2024 22:22:19 -0500 Subject: [PATCH 7/7] fix whitelist issue with private chats + update payment process for chat completion and dalle --- src/config.ts | 2 +- src/modules/open-ai/api/openAi.ts | 6 +++--- src/modules/open-ai/helpers.ts | 11 ++++++----- src/modules/open-ai/index.ts | 6 ++---- src/modules/open-ai/types.ts | 31 ++++++++++++++++--------------- src/modules/payment/index.ts | 4 +--- 6 files changed, 29 insertions(+), 31 deletions(-) diff --git a/src/config.ts b/src/config.ts index b3041cc4..1698693e 100644 --- a/src/config.ts +++ b/src/config.ts @@ -75,7 +75,7 @@ export default { // ? parseInt(process.env.WORD_COUNT_BETWEEN) // : 10, priceAdjustment: process.env.PRICE_ADJUSTMENT - ? parseInt(process.env.PRICE_ADJUSTMENT) + ? parseFloat(process.env.PRICE_ADJUSTMENT) : 2, isFreePromptChatGroups: false, isEnabled: Boolean(parseInt(process.env.CHAT_GPT_ENABLED ?? '1')), diff --git a/src/modules/open-ai/api/openAi.ts b/src/modules/open-ai/api/openAi.ts index 0f048316..092f547f 100644 --- a/src/modules/open-ai/api/openAi.ts +++ b/src/modules/open-ai/api/openAi.ts @@ -281,11 +281,11 @@ export const getChatModelPrice = ( model: ChatModel, inCents = true, inputTokens: number, - outPutTokens?: number + outputTokens?: number ): number => { let price = model.inputPrice * inputTokens - price += outPutTokens - ? outPutTokens * model.outputPrice + price += outputTokens + ? outputTokens * model.outputPrice : model.maxContextTokens * model.outputPrice price = inCents ? price * 100 : price return price / 1000 diff --git a/src/modules/open-ai/helpers.ts b/src/modules/open-ai/helpers.ts index c45569c9..2fb2430d 100644 --- a/src/modules/open-ai/helpers.ts +++ b/src/modules/open-ai/helpers.ts @@ -238,23 +238,24 @@ export const hasPrefix = (prompt: string): string => { ) } -export const getPromptPrice = (completion: string, data: ChatPayload): { price: number, promptTokens: number, completionTokens: number } => { +export const getPromptPrice = (completion: string, data: ChatPayload): { price: number, promptTokens: number, completionTokens: number, totalTokens: number } => { const { conversation, ctx, model } = data - + const currentUsage = data.prompt ? 0 : ctx.session.openAi.chatGpt.usage const prompt = data.prompt ? data.prompt : conversation[conversation.length - 1].content - const promptTokens = getTokenNumber(prompt as string) + const promptTokens = getTokenNumber(prompt as string) + currentUsage const completionTokens = getTokenNumber(completion) const modelPrice = getChatModel(model) const price = getChatModelPrice(modelPrice, true, promptTokens, completionTokens) * config.openAi.chatGpt.priceAdjustment conversation.push({ content: completion, role: 'system' }) - ctx.session.openAi.chatGpt.usage += promptTokens + completionTokens + ctx.session.openAi.chatGpt.usage += completionTokens ctx.session.openAi.chatGpt.price += price return { price, promptTokens, - completionTokens + completionTokens, + totalTokens: data.prompt ? promptTokens + completionTokens : ctx.session.openAi.chatGpt.usage } } diff --git a/src/modules/open-ai/index.ts b/src/modules/open-ai/index.ts index 6b8ddb5a..f19dcd5e 100644 --- a/src/modules/open-ai/index.ts +++ b/src/modules/open-ai/index.ts @@ -48,6 +48,7 @@ import { Callbacks } from '../types' import { LlmsBot } from '../llms' import { type PhotoSize } from 'grammy/types' +const priceAdjustment = config.openAi.chatGpt.priceAdjustment export class OpenAIBot implements PayableBot { public readonly module = 'OpenAIBot' private readonly logger: Logger @@ -90,7 +91,6 @@ export class OpenAIBot implements PayableBot { public getEstimatedPrice (ctx: any): number { try { - const priceAdjustment = config.openAi.chatGpt.priceAdjustment const prompts = ctx.match if (this.isSupportedImageReply(ctx) && !isNaN(+prompts)) { const imageNumber = ctx.message?.caption || ctx.message?.text @@ -378,9 +378,7 @@ export class OpenAIBot implements PayableBot { ctx.transient.analytics.actualResponseTime = now() const price = getPromptPrice(completion, data) this.logger.info( - `streamChatCompletion result = tokens: ${ - price.promptTokens + price.completionTokens - } | ${model} | price: ${price.price}¢` + `streamChatCompletion result = tokens: ${price.totalTokens} | ${model} | price: ${price.price}¢` // price.promptTokens + price.completionTokens } ) return { price: price.price, diff --git a/src/modules/open-ai/types.ts b/src/modules/open-ai/types.ts index 3feb117e..e5cd1692 100644 --- a/src/modules/open-ai/types.ts +++ b/src/modules/open-ai/types.ts @@ -22,60 +22,61 @@ export enum ChatGPTModelsEnum { export const ChatGPTModels: Record = { 'gpt-4': { name: 'gpt-4', - inputPrice: 0.03, - outputPrice: 0.06, + inputPrice: 0.03, // 3 + outputPrice: 0.06, // 6 maxContextTokens: 8192, chargeType: 'TOKEN' }, 'gpt-4-32k': { name: 'gpt-4-32k', - inputPrice: 0.06, - outputPrice: 0.12, + inputPrice: 0.06, // 6 + outputPrice: 0.12, // 12 maxContextTokens: 32000, chargeType: 'TOKEN' }, 'gpt-3.5-turbo': { name: 'gpt-3.5-turbo', - inputPrice: 0.0015, - outputPrice: 0.002, + inputPrice: 0.0015, // 0.15 + outputPrice: 0.002, // 0.2 maxContextTokens: 4000, chargeType: 'TOKEN' }, 'gpt-3.5-turbo-16k': { name: 'gpt-3.5-turbo-16k', - inputPrice: 0.003, - outputPrice: 0.004, + inputPrice: 0.003, // 0.3 + outputPrice: 0.004, // 0.4 maxContextTokens: 16000, chargeType: 'TOKEN' }, 'gpt-4-vision-preview': { name: 'gpt-4-vision-preview', - inputPrice: 0.03, - outputPrice: 0.06, + inputPrice: 0.03, // 3 + outputPrice: 0.06, // 6 maxContextTokens: 16000, chargeType: 'TOKEN' } } +// needs to be in cents export const DalleGPTModels: Record = { '1024x1792': { size: '1024x1792', - price: 0.10 + price: 12 // 0.12 }, '1792x1024': { size: '1792x1024', - price: 0.10 + price: 12 // 0.12 }, '1024x1024': { size: '1024x1024', - price: 0.10 + price: 8 // 0.08 }, '512x512': { size: '512x512', - price: 0.10 + price: 8 // 0.10 }, '256x256': { size: '256x256', - price: 0.10 + price: 8 // 0.10 } } diff --git a/src/modules/payment/index.ts b/src/modules/payment/index.ts index 5f855685..42ceedc3 100644 --- a/src/modules/payment/index.ts +++ b/src/modules/payment/index.ts @@ -291,10 +291,8 @@ export class BotPayments { return true } } - return false - } else { - return true } + return false } public isPaymentsEnabled (): boolean {