Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: image generator #51

Merged
merged 7 commits into from
Jun 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .bumpversion.cfg
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[bumpversion]
current_version = 1.0.4
current_version = 1.0.4-alpha
commit = true
tag = true
message = release: bump version: {current_version} → {new_version}
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# [ChatGPT | Smart Bot](https://t.me/smart_gpt_voice_bot)
# [Pied Piper | GPT](https://t.me/smart_gpt_voice_bot)

[![GitHub Workflow Status (with branch)](https://img.shields.io/github/actions/workflow/status/mikita-kandratsyeu/chat-gpt-bot/ci.yml?branch=main&style=for-the-badge)](https://github.com/mikita-kandratsyeu/chat-gpt-bot/actions)
[![GitHub last commit](https://img.shields.io/github/last-commit/mikita-kandratsyeu/chat-gpt-bot?style=for-the-badge)](https://github.com/mikita-kandratsyeu/chat-gpt-bot/commits/main)
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "chat-gpt-bot",
"version": "1.0.4",
"version": "1.0.4-alpha",
"repository": "git@github.com:mikita-kandratsyeu/chat-gpt-bot.git",
"author": "Mikita Kandratsyeu <nickondr.production@gmail.com>",
"license": "Apache-2.0",
Expand Down Expand Up @@ -32,6 +32,7 @@
"fluent-ffmpeg": "^2.1.2",
"gpt-3-encoder": "^1.1.4",
"grammy": "^1.16.0",
"jimp": "^0.22.8",
"mongoose": "^7.1.0",
"node-cache": "^5.1.2",
"openai": "^3.2.1",
Expand Down
2 changes: 2 additions & 0 deletions src/bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
adminCommand,
clearCommand,
descriptionCommand,
imageCommand,
moderatorCommand,
profileCommand,
startCommand,
Expand Down Expand Up @@ -81,6 +82,7 @@ export const createBot = () => {
moderatorCommand,
profileCommand,
startCommand,
imageCommand,
textCommand,
voiceCommand,
].forEach((handle) => handle(bot));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export const getUserConversationMessagesCallback: DynamicUsersMenuCallbackType =
);

const { filePath, filePathForReply } = await csv.createUserMessagesCsv(
{ key: userSession.key, value: { username, messages } },
{ key: userSession.key, value: { username, messages, images: [] } },
true,
);

Expand Down
26 changes: 26 additions & 0 deletions src/commands/image/image.command.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { BotCommands } from '@bot/constants';
import { createImageConversation } from '@bot/conversations';
import { mongo } from '@bot/services';
import { BotType } from '@bot/types';

export const imageCommand = (bot: BotType) =>
bot.command(BotCommands.IMAGE, async (ctx) => {
const username = String(ctx?.from?.username);

const usedGptImages = ctx.session.limit.amountOfGptImages;
const currentLocale = await ctx.i18n.getLocale();

const user = await mongo.getUser(username);

if (user && usedGptImages >= user.limit.gptImages) {
await ctx.reply(
ctx.t('info-message-reach-gpt-images-limit', {
date: new Date(user.limit.expire).toLocaleString(currentLocale),
}),
);

return;
}

await ctx.conversation.enter(createImageConversation.name);
});
1 change: 1 addition & 0 deletions src/commands/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ export * from './about/about.command';
export * from './admin/admin.command';
export * from './clear/clear.command';
export * from './description/description.command';
export * from './image/image.command';
export * from './moderator/moderator.command';
export * from './profile/profile.command';
export * from './start/start.command';
Expand Down
14 changes: 13 additions & 1 deletion src/composers/callback-query/callback-query.composer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ import {
ModeratorMenuActions,
UsersMenuActions,
} from '@bot/constants';
import { addMultipleUsersConversation, addUserConversation } from '@bot/conversations';
import {
addMultipleUsersConversation,
addUserConversation,
createImageConversation,
} from '@bot/conversations';
import { adminMainMenu, moderatorMainMenu } from '@bot/menu';
import { BotContextType } from '@bot/types';
import { Composer, Middleware } from 'grammy';
Expand Down Expand Up @@ -41,8 +45,16 @@ composer.callbackQuery(UsersMenuActions.ADD_NEW_MULTIPLE_USERS, async (ctx) => {
});

composer.callbackQuery(CommonActions.GO_TO_CHAT, async (ctx) => {
await ctx.conversation.exit(createImageConversation.name);

await ctx.deleteMessage();
await ctx.reply(ctx.t('start-message'));
});

composer.callbackQuery(CommonActions.CREATE_IMAGE, async (ctx) => {
await ctx.deleteMessage();

await ctx.conversation.enter(createImageConversation.name);
});

export const callbackQueryComposer = (): Middleware<BotContextType> => composer;
7 changes: 6 additions & 1 deletion src/composers/conversation/conversation.composer.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { addMultipleUsersConversation, addUserConversation } from '@bot/conversations';
import {
addMultipleUsersConversation,
addUserConversation,
createImageConversation,
} from '@bot/conversations';
import { BotContextType } from '@bot/types';
import { conversations, createConversation } from '@grammyjs/conversations';
import { Composer, Middleware } from 'grammy';
Expand All @@ -9,5 +13,6 @@ composer.use(conversations());

composer.use(createConversation(addUserConversation));
composer.use(createConversation(addMultipleUsersConversation));
composer.use(createConversation(createImageConversation));

export const conversationComposer = (): Middleware<BotContextType> => composer;
20 changes: 14 additions & 6 deletions src/constants/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ export enum MessageRolesGPT {
}

export const MAX_CONTEXT_GPT_TOKENS = 4096;
export const IMAGE_SIZE_DEFAULT = '512x512';
export const MAX_IMAGES_REQUEST = 3;

// Telegram API
export const TELEGRAM_API = 'https://api.telegram.org';
Expand All @@ -26,32 +28,34 @@ export enum BotCommands {
ADMIN = 'admin',
CLEAR = 'clear',
DESCRIPTION = 'description',
IMAGE = 'image',
MODERATOR = 'moderator',
PROFILE = 'profile',
START = 'start',
}

export const botName = 'ChatGPT | Smart Bot';
export const botName = 'Pied Piper | GPT';

export const BotCommandsWithDescription = [
{ command: BotCommands.PROFILE, i18nKey: 'command-profile' },
{ command: BotCommands.CLEAR, i18nKey: 'command-clear' },
{ command: BotCommands.IMAGE, i18nKey: 'command-image' },
{ command: BotCommands.ADMIN, i18nKey: 'command-admin' },
{ command: BotCommands.MODERATOR, i18nKey: 'command-moderator' },
{ command: BotCommands.DESCRIPTION, i18nKey: 'command-description' },
{ command: BotCommands.ABOUT, i18nKey: 'command-about' },
];

export const DAY_MS = 60 * 60 * 24 * 1000;

// Per day GPT Token limits
export enum GPTLimits {
BASE = '4096/10',
PREMIUM = '8192/20',
VIP = '16384/40',
BASE = '4096/5',
PREMIUM = '8192/10',
VIP = '16384/20',
SUPER_VIP = '32768/50',
}

export const DAY_MS = 60 * 60 * 24 * 1000;

// Node cache
export const TTL_DEFAULT = process.env.NODE_ENV !== 'production' ? 60 : 600;

Expand All @@ -75,6 +79,9 @@ export const addUserFormat = (userRole: UserRoles) =>

export const ADD_USER_CSV_FORMAT = '<username> | <role>';

// Image
export const CREATE_IMAGE_QUERY_FORMAT = `<prompt>;<Number of images (1-${MAX_IMAGES_REQUEST})>`;

// Regexp
export const REGEXP_USERNAME = /^[a-zA-Z0-9_]{5,32}$/;
export const REGEXP_CSV_FILE_TYPE = /.+(\.csv)$/;
Expand Down Expand Up @@ -166,6 +173,7 @@ export enum UsersMenu {
// Menu actions
export enum CommonActions {
GO_TO_CHAT = 'go-to-chat-action',
CREATE_IMAGE = 'create-image-action',
}

export enum AdminMenuActions {
Expand Down
1 change: 1 addition & 0 deletions src/conversations/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './openai/create-image.conversation';
export * from './users';
64 changes: 64 additions & 0 deletions src/conversations/openai/create-image.conversation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import { CREATE_IMAGE_QUERY_FORMAT } from '@bot/constants';
import { inlineCreateImage, inlineGoToChat } from '@bot/keyboards';
import { logger, openAI } from '@bot/services';
import { ConversationType } from '@bot/types';
import { convertBase64ToFiles, removeFile } from '@bot/utils';
import { InputFile } from 'grammy';
import { InputMediaPhoto } from 'grammy/types';

export const createImageConversation: ConversationType = async (conversation, ctx) => {
try {
await ctx.reply(
ctx.t('image-generator-enter-request', { gptImageQuery: CREATE_IMAGE_QUERY_FORMAT }),
{ reply_markup: inlineGoToChat(ctx) },
);

const {
message: { text, message_id: messageId },
} = await conversation.waitFor('message:text');

const [prompt = '', numberOfImages = 1] = text?.trim().split(';');

if (Number.isNaN(Number(numberOfImages))) {
return await ctx.reply(ctx.t('image-generator-incorrect-image-number'), {
reply_to_message_id: messageId,
reply_markup: inlineCreateImage(ctx),
});
}

const response = await conversation.external(async () =>
openAI.generateImage(prompt, Number(numberOfImages)),
);

const base64Images = response.map((base64Image) => base64Image.b64_json ?? '');

conversation.session.limit.amountOfGptImages += base64Images.length;
conversation.session.custom.images.push({
base64: base64Images,
prompt,
});

const imageFilesPath = await conversation.external(async () =>
convertBase64ToFiles(base64Images),
);

const inputMediaFiles: InputMediaPhoto[] = imageFilesPath.map((imageFilePath) => ({
type: 'photo',
media: new InputFile(imageFilePath),
}));

await ctx.replyWithMediaGroup(inputMediaFiles, {
reply_to_message_id: messageId,
});

imageFilesPath.forEach((path) => removeFile(path));

return;
} catch (error) {
await ctx.reply(ctx.t('error-message-common'));

logger.error(`conversations::createImageConversation::${JSON.stringify(error.message)}`);

return;
}
};
1 change: 1 addition & 0 deletions src/helpers/sessions/sessions.helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { SessionCsvIds } from '@bot/constants';
import { SessionType, UserSessionModelType } from '@bot/types';

export const createInitialCustomSessionData = (): SessionType['custom'] => ({
images: [],
messages: [],
username: null,
});
Expand Down
3 changes: 3 additions & 0 deletions src/keyboards/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,6 @@ export const inlineAddNewMultipleUsers = (ctx: BotContextType) =>

export const inlineShareWithContacts = (ctx: BotContextType, query: string) =>
new InlineKeyboard().switchInline(ctx.t('common-button-share'), query);

export const inlineCreateImage = (ctx: BotContextType) =>
new InlineKeyboard().text(ctx.t('error-message-common-try-again'), CommonActions.CREATE_IMAGE);
7 changes: 6 additions & 1 deletion src/locales/en.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -109,11 +109,16 @@ info-message-moderator-panel-for-super-admin = Note: Please go to the Admin-pane
info-message-node-cache = Note: Data caching is set to { $cache } minutes.
info-message-clear-current-session = The current session for the { $username } has been cleared.
info-message-reach-gpt-tokens-limit = You have used all available GPT tokens. Please try again after { $date }.
info-message-reach-gpt-images-limit = You have used all available GPT images. Please try again after { $date }.

# Profile
profile-user-initial-message = Hey 👋🏻, { $firstName } { $lastName }!
profile-user-role = Your role: { $role }
profile-user-gpt-package = GPT limit package: { $package }
profile-user-gpt-package = GPT limit: { $package }
profile-user-available-messages-amount = Available number of GPT tokens: { $amount }
profile-user-available-images-amount = Available number of GPT images: { $amount }
profile-user-date-register = Date of registration: { $date }

# Image generator
image-generator-enter-request = Enter the query in the following format: { $gptImageQuery }.
image-generator-incorrect-image-number = The number of images entered is incorrect. Try again!
7 changes: 6 additions & 1 deletion src/locales/ru.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -109,11 +109,16 @@ info-message-moderator-panel-for-super-admin = Примечание: Пожал
info-message-node-cache = Примечание: Установлено кэширование данных - { $cache } минут.
info-message-clear-current-session = Текущая сессия для { $username } была очищена.
info-message-reach-gpt-tokens-limit = Вы использовали все доступные GPT токены. Пожалуйста, повторите попытку после { $date }.
info-message-reach-gpt-images-limit = Вы использовали все доступные GPT изображения. Пожалуйста, повторите попытку после { $date }.

# Profile
profile-user-initial-message = Привет 👋🏻, { $firstName } { $lastName }!
profile-user-role = Ваша роль: { $role }
profile-user-gpt-package = GPT лимит пакет: { $package }
profile-user-gpt-package = GPT лимит: { $package }
profile-user-available-messages-amount = Доступное количество GPT токенов: { $amount }
profile-user-available-images-amount = Доступное количество GPT картинок: { $amount }
profile-user-date-register = Дата регистрации: { $date }

# Image generator
image-generator-enter-request = Введите запрос в следующем формате: { $gptImageQuery }.
image-generator-incorrect-image-number = Введено неверное количество картинок. Попробуйте еще раз!
1 change: 1 addition & 0 deletions src/models/user-session/user-session.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const schema = new Schema<UserSessionModelType>({
value: {
username: { type: String },
messages: [],
images: [],
},
});

Expand Down
36 changes: 33 additions & 3 deletions src/services/openai/openai.service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import { config } from '@bot/config';
import { modelGPT, transcriptionModelGPT } from '@bot/constants';
import {
IMAGE_SIZE_DEFAULT,
MAX_IMAGES_REQUEST,
modelGPT,
transcriptionModelGPT,
} from '@bot/constants';
import { logger } from '@bot/services';
import { removeFile } from '@bot/utils';
import { createReadStream } from 'fs';
Expand Down Expand Up @@ -39,10 +44,10 @@ class OpenAIService {

async transcription(filepath: string) {
try {
const fileStream: unknown = createReadStream(filepath);
const fileStream = createReadStream(filepath);

const response = await this.openAI.createTranscription(
fileStream as File,
fileStream as unknown as File,
transcriptionModelGPT,
);

Expand All @@ -60,6 +65,31 @@ class OpenAIService {
}
}
}

async generateImage(prompt: string, numberOfImages: number) {
try {
const response = await this.openAI.createImage({
n: Math.min(MAX_IMAGES_REQUEST, numberOfImages <= 0 ? 1 : numberOfImages),
prompt,
response_format: 'b64_json',
size: IMAGE_SIZE_DEFAULT,
});

return response.data.data;
} catch (error) {
if (error.response) {
logger.error(
`openAIService::generateImage::[${error.response.status}]::${JSON.stringify(
error.response.data,
)}`,
);
} else {
logger.error(`openAIService::generateImage::${JSON.stringify(error.message)}`);
}

return [];
}
}
}

export const openAI = new OpenAIService(config.OPEN_AI_TOKEN, config.OPEN_AI_ORG);
Loading