Skip to content

Commit

Permalink
fixnfeat: ai-text-content and optimization
Browse files Browse the repository at this point in the history
feat: New text-generation by AI, Ghola, as Gemini replacement

fix: Reverting test commit before this commit
  • Loading branch information
gensart-ai committed Feb 28, 2024
1 parent 64b7ca2 commit f1dc2dd
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 2 deletions.
3 changes: 2 additions & 1 deletion src/command-hive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { imageToStickerText } from '@services/external/image-to-sticker-meme'
import { log } from '@services/internal/log'
import { indoSlangQuote } from '@services/internal/quote-indo-slang'
import { getPpCouple } from '@services/internal/pp-couple'
import { ghola } from '@services/external/ghola'

type Commands = {
[key: string]: (client: Client, message: Message) => any
Expand All @@ -30,7 +31,7 @@ const commands: Commands = {
'.ppcouple': getPpCouple,

// * AI-generated

'.tanya': ghola,

// * Translation
'.engtoindo': translateEnglishToIndo,
Expand Down
72 changes: 72 additions & 0 deletions src/services/external/ghola.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import 'dotenv/config'
import axios from 'axios'
import { Executor } from '@/command-hive'
import * as wweb from '@utils/wweb'
import config from '@/env'
import * as logger from '@utils/logger'

const GHOLA_TOKEN_ENDPOINT = 'https://www.ghola.ai/api/v1/chat/init';
const GHOLA_CHAT_ENDPOINT = 'https://www.ghola.ai/api/v1/chat';

const retrieveGholaToken = async () => {
const response = await axios.post(GHOLA_TOKEN_ENDPOINT, {
token: process.env.GHOLA_TOKEN,
email: process.env.GHOLA_EMAIL,
profileId: process.env.GHOLA_AI_PROFILE_ID
}, {
headers: {
'Content-Type': 'application/json'
}
});

return response.data.jwt ?? null;
}

const askGhola = async (token: string, message: string) => {
const response = await axios.post(GHOLA_CHAT_ENDPOINT, {
messages: [
{
role: "user",
content: message
}
]
}, {
headers: {
'Authorization': 'Bearer ' + token,
'Content-Type': 'application/json',
},
});

return response.data?.message ?? null;
}

const ghola: Executor = async (client, message) => {
const contact = await message.getContact();

// Extract question from message
const question: string = message.body.split(' ').slice(1).join(' ');

if (question.length === 0) {
wweb.replyMessage(message, `Berikan pertanyaan kamu kepada ${config.botShortName}, contoh : \`.tanya Hai, siapa kamu?\``);
return 0;
}

const token: string | null = await retrieveGholaToken();
if (token == null) {
wweb.replyMessage(message, `${config.botShortName} tidak dapat menjawab pertanyaanmu saat ini, maaf :(`);
logger.logError('tanyaGhola - Failed to retrieve Ghola AI token by ' + contact.pushname ?? 'unknown');
return 0;
}

const answer: string = await askGhola(token, question);
if (answer != null) {
wweb.replyMessage(message, answer);
} else {
wweb.replyMessage(message, `${config.botShortName} tidak dapat menjawab pertanyaanmu saat ini, maaf :(`);
logger.logError('tanyaGhola - Failed to retrieve Ghola question by ' + contact.pushname ?? 'unknown');
}
}

export {
ghola
}
2 changes: 1 addition & 1 deletion src/services/external/image-to-sticker-meme.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ const imageToStickerText: Executor = async (client, message) => {
} catch (error) {
const contact = await message.getContact();
const err = error as AxiosError;
logger.logError('imageToStickerText - ' + err.response?.data ?? err.message + ' by ' + contact?.pushname ?? 'unknown');
logger.logError('imageToStickerText - ' + err.message + ' by ' + contact?.pushname ?? 'unknown');

wweb.replyMessage(message, `${config.botShortName} gagal memproses gambar yang anda tujukan, mohon coba lagi dengan mengirim gambar baru.`)
}
Expand Down

0 comments on commit f1dc2dd

Please sign in to comment.