Skip to content

Commit

Permalink
upload chat to openai
Browse files Browse the repository at this point in the history
  • Loading branch information
lluisd committed Nov 12, 2024
1 parent 8312767 commit 080d2ff
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 6 deletions.
22 changes: 21 additions & 1 deletion handlers/openAI.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,32 @@
const OpenAIService = require('../services/openAI')

const LoggerService = require('../services/logger')
const moment = require("moment/moment");
class OpenAI {
async askOpenAI (target, text, username, bot) {
const response = await OpenAIService.askAssistant(text)
if (response) {
bot.say(target, `@${username} ${response}`)
}
}

async createAndUploadToChat (target, bot, isToday = false) {
const today = moment().tz('Europe/Madrid')
const date = isToday ? today : today.subtract(1, 'days')
const startOfDay = date.startOf('day').toDate();
const endOfDay = date.endOf('day').toDate();

const response = await LoggerService.getLogChatMessagesBetweenDays(config.twitch.roomId,startOfDay, endOfDay)
const json = JSON.stringify(response)

const formattedDate = date.format('YYYY-MM-DD');
const result = await OpenAIService.uploadFileToVectorStore(json, formattedDate)
if (result.success) {
bot.say(target, `IA actualizada con el chat de ${formattedDate}`)
console.log('Chat uploaded to openai ' + result.filename)
} else {
console.log('Error uploading chat to openai of date ' + formattedDate)
}
}
}

module.exports = OpenAI
10 changes: 9 additions & 1 deletion helpers/dbmanager.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,13 @@ async function addChatLogLine (roomId, nick, text, date) {
return conn.model('chatLog').insertMany({roomId: roomId, nick: nick, text: text, date: date})
}

async function getChatLogLines (roomId, startOfDay, endOfDay) {
const conn = await logConn.getConnection()
return conn.model('chatLog')
.find({roomId: roomId, date: { $gte: startOfDay, $lt: endOfDay }})
.select('nick text date -_id').lean()
}

async function addTitleLogLine (roomId, title, date) {
const conn = await logConn.getConnection()
return conn.model('titleLog').insertMany({roomId: roomId, title: title, date: date})
Expand Down Expand Up @@ -139,5 +146,6 @@ module.exports = {
addBans,
getPermanentBans,
getTimeouts,
removeBan
removeBan,
getChatLogLines
}
4 changes: 4 additions & 0 deletions lib/inputParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,10 @@ class InputParser {
isAskingOpenAI (text) {
return text.toLowerCase().startsWith('!chat')
}

isAskingToUpdateChatToOpenAI (text) {
return text.toLowerCase().startsWith('!updatechat')
}
}

module.exports = InputParser
7 changes: 5 additions & 2 deletions lib/messenger.js
Original file line number Diff line number Diff line change
Expand Up @@ -182,15 +182,18 @@ class Messenger {
return handlers.events.sendTarracoMangaEvent()

// OPEN AI
if (textSplit.length > 1 && inputParser.isAskingOpenAI(textSplit[0]) && this._isNotCooldown('openai',15)) {
if (textSplit.length > 1 && inputParser.isAskingOpenAI(textSplit[0]) && this._isNotCooldown('openai',10)) {
return handlers.openAI.askOpenAI(target, textSplit.slice(1).join(' '), context['display-name'], this.bot)
}

if (textSplit.length > 1 && inputParser.isAskingBotOpenAI(text) && this._isNotCooldown('openai',15)) {
if (textSplit.length > 1 && inputParser.isAskingBotOpenAI(text) && this._isNotCooldown('openai',10)) {
const regex = new RegExp(`@?${config.twitch.username}`, 'gi')
const textWithoutMention = text.replace(regex, '').trim()
return handlers.openAI.askOpenAI(target, textWithoutMention, context['display-name'], this.bot)
}

if (textSplit.length > 0 && inputParser.isAskingToUpdateChatToOpenAI(textSplit[0]) && isAdmin(context))
return await handlers.openAI.createAndUploadToChat(target, this.bot, true)
}

handleConnect (addr, port) {
Expand Down
4 changes: 4 additions & 0 deletions lib/notifier.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ class Notifier {
await handlers.ban.updateBansList()
})

cron.schedule('15 4 * * *', async () => {
await handlers.openAI.createAndUploadToChat(this.twitchBot, this.target)
})

return Promise.resolve()
}

Expand Down
9 changes: 8 additions & 1 deletion services/logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,15 @@ async function logStreamTitle(roomId, title) {
return result
}

async function getLogChatMessagesBetweenDays(roomId, startOfDay, endOfDay) {
let result = null
result = await dbManager.getChatLogLines(roomId, startOfDay, endOfDay)
return result
}


module.exports = {
logChatMessage,
logStreamTitle
logStreamTitle,
getLogChatMessagesBetweenDays
}
31 changes: 30 additions & 1 deletion services/openAI.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,32 @@ async function askOpenAI(message) {

let assistantThread = null


async function uploadFileToVectorStore(json, formattedDate) {
try {
const filename = `chat_${formattedDate}.json`
const buffer = Buffer.from(json, 'utf-8');
const newFile = new File([buffer], filename, {
type: 'application/json',
});

const files = [];
for await (const file of assistantsClient.files.list()) {
files.push({id: file.id, name: file.filename });
}
const matchedFiles = files.filter((file) => file.name === filename);
for (const file of matchedFiles) {
await assistantsClient.files.del(file.id);
}

await assistantsClient.beta.vectorStores.files.uploadAndPoll(config.openAI.vectorStoreId, newFile);
return { success: true, filename: filename }

} catch (error) {
return { success: false }
}
}

async function askAssistant(message) {
let result
try {
Expand Down Expand Up @@ -106,6 +132,8 @@ async function askAssistant(message) {
return result
}



function cleanAssistantText(text) {
return text.replaceAll(/【.*?】/g, "")
}
Expand All @@ -123,5 +151,6 @@ async function _getHeaders () {

module.exports = {
askOpenAI,
askAssistant
askAssistant,
uploadFileToVectorStore
}

0 comments on commit 080d2ff

Please sign in to comment.