-
-
Save codesxt/2b0bd1268c89a7ed15b1534dae5fa121 to your computer and use it in GitHub Desktop.
Bot para responder al chat de @afor_digital en Twitch
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
OPENAI_TOKEN='Acá ponen su token' |
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
import * as dotenv from 'dotenv' | |
import * as tmi from 'tmi.js' | |
dotenv.config() | |
import chalk from 'chalk' | |
import { Configuration, OpenAIApi } from "openai"; | |
const configuration = new Configuration({ | |
apiKey: process.env.OPENAI_TOKEN, | |
}); | |
const openai = new OpenAIApi(configuration); | |
const assistant_name = 'AFORBOT' | |
const system_messages = [ | |
{ | |
role: 'system', | |
content: `Eres una streamer que habla de frontend, evalúas portafolios web y haces comentarios divertidos.` | |
+ `Vives en España.` | |
+ `Tu nombre es ${assistant_name} y tienes una empresa llamada AFORDIN.` | |
+ `Vas a recibir una serie de mensajes de usuarios del chat e irás comentando y enviando saludos a los usuarios.` | |
} | |
] | |
let gpt_history = [] | |
const client = new tmi.Client({ | |
channels: [ 'afor_digital' ] | |
}); | |
client.connect(); | |
client.on('message', async (channel, tags, message, self) => { | |
const chat_message = { | |
user: tags['display-name'], | |
message: message | |
} | |
gpt_history.push({ | |
role: 'user', | |
content: chat_message.user + ': ' + message | |
}) | |
console.log(chalk.bgBlue(chat_message.user) + ': ' + message) | |
}) | |
setInterval(async () => { | |
if (gpt_history.length > 0) { | |
const last_message = gpt_history[gpt_history.length-1] | |
if (last_message.role != 'assistant') { | |
const messages = [ | |
...system_messages, | |
...gpt_history.slice(-30) | |
] | |
const response = await openai.createChatCompletion({ | |
model: 'gpt-3.5-turbo', | |
n: 1, | |
temperature: 0, | |
// max_tokens: 4096, | |
messages: messages | |
}) | |
const answer = response.data.choices[0].message.content | |
console.log(`${chalk.bgMagentaBright(assistant_name)}: ${answer}`) | |
gpt_history.push({ | |
role: 'assistant', | |
content: answer | |
}) | |
} | |
} | |
}, 10000); |
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
{ | |
"name": "server", | |
"version": "1.0.0", | |
"description": "", | |
"main": "index.js", | |
"type": "module", | |
"scripts": { | |
"test": "echo \"Error: no test specified\" && exit 1" | |
}, | |
"author": "", | |
"license": "ISC", | |
"dependencies": { | |
"chalk": "^5.2.0", | |
"dotenv": "^16.0.3", | |
"openai": "^3.2.1", | |
"tmi.js": "^1.8.5" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment