Skip to content

Commit

Permalink
Rollback AI filtering
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeto143 committed May 9, 2024
1 parent dffb7c6 commit ea7b39b
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 49 deletions.
63 changes: 23 additions & 40 deletions 5m5v-bot.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,14 @@ if ([
'TWITTER_PASSWORD',
'TWEETS_API_ENDPOINT',
'TWEETS_API_KEY',
'OPENAI_API_KEY',
].some(key => !process.env[key])) {
console.error('One or more required environment variables are missing. Exiting.');
process.exit(1);
}

const { Rettiwt } = require('rettiwt-api');
const { OpenAI } = require('openai');
const axios = require('axios');

const getAiPrompt = require('./data/prompt');
const util = require('./lib/util');
const TweetFilter = require('./lib/filter');

const pollingIntervalMs = 21 * 60 * 1000;
const retryLoginDelayMs = 5 * 60 * 1000;
Expand All @@ -32,15 +28,15 @@ const languageKeys = {
german: 'de',
};

const tweetFilter = new TweetFilter([], Object.keys(languageKeys));

const streamFilter = {
includeWords: [
'"vegan" OR "végétalien" OR "végétalienne" OR "végane" OR "vegano" OR "vegana"',
'"I want to" OR "I would like" OR "thinking of" OR "I should try" OR "planning on" OR "I wish" OR "Je souhaite" OR "Je veux" OR "J\'ai envie de" OR "J\'espère" OR "Quiero" OR "Deseo" OR "Tengo ganas de" OR "Estoy pensando en" OR "He decidido" OR "Planeo" OR "Me estoy planteando" OR "Voy a" OR "Mi intención es" OR "Ich will" OR "Ich möchte" OR "Ich beabsichtige" OR "Ich habe vor"',
],
};

const openai = new OpenAI({ apiKey: process.env.OPENAI_API_KEY });

let twitterApiKey = null;

(async () => {
Expand All @@ -54,29 +50,27 @@ let twitterApiKey = null;

try {
for await (const tweet of rettiwt.tweet.stream(streamFilter, pollingIntervalMs)) {
const lang = await findMatchingLanguageKey(tweet.fullText);

if (lang === null) {
continue;
}

console.log('Found tweet', tweet.id, tweet.fullText, lang);

try {
if (!isDryRun) {
await axios.post(process.env.TWEETS_API_ENDPOINT, {
lang,
tweets: [buildTweetPayload(tweet)],
}, {
headers: {
'X-API-KEY': process.env.TWEETS_API_KEY,
},
});
const matchingLanguages = tweetFilter.matches(tweet) || [];

console.log('Found tweet', tweet.id, tweet.fullText, matchingLanguages);

for (const language of matchingLanguages) {
try {
if (!isDryRun) {
await axios.post(process.env.TWEETS_API_ENDPOINT, {
lang: languageKeys[language],
tweets: [buildTweetPayload(tweet)],
}, {
headers: {
'X-API-KEY': process.env.TWEETS_API_KEY,
},
});
}

console.log(`Sent tweet ${tweet.id} in ${language}:\n${tweet.fullText}`);
} catch (error) {
console.error(`Unable to send tweet ${tweet.id} in ${language}: ${error.message}`);
}

console.log(`Sent tweet ${tweet.id} in ${lang}:\n${tweet.fullText}`);
} catch (error) {
console.error(`Unable to send tweet ${tweet.id} in ${lang}: ${error.message}`);
}
}
} catch (error) {
Expand Down Expand Up @@ -115,17 +109,6 @@ async function loginToTwitter() {
}
}

async function findMatchingLanguageKey(tweetText) {
const completion = await openai.chat.completions.create({
messages: [{ role: 'user', content: getAiPrompt(tweetText) }],
model: 'gpt-3.5-turbo',
});

const response = completion.choices[0].message.content.trim();

return ['en', 'fr', 'es', 'de'].includes(response) ? response : null;
}

function buildTweetPayload(tweet) {
return {
id: tweet.id,
Expand Down
9 changes: 0 additions & 9 deletions data/prompt.js

This file was deleted.

0 comments on commit ea7b39b

Please sign in to comment.