-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fixnfeat: ai-text-content and optimization
feat: New text-generation by AI, Ghola, as Gemini replacement fix: Reverting test commit before this commit
- Loading branch information
1 parent
64b7ca2
commit f1dc2dd
Showing
3 changed files
with
75 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters