LowEndBox - Cheap VPS, Hosting and Dedicated Server Deals

How the LowEndTalk ChatGPT-Powered Auto-Tweeter Works

LowEndChatGPTWhen one of LowEndTalk’s community providers shares an offer on LET, we want to tell the world.  One of the ways we do this is by posting a link to the thread on Twitter.  We do this via a Python script which interacts with the Twitter API.

Once an hour, it looks at all the threads in the LET offers category (via the RSS feed, using Python’s feedparser module), compares it to a list of offers it’s already tweeted, and then selects the oldest un-posted offer and tweets about it.

BTW you can monitor offers yourself via Python: check out the tutorial we published.

Now we could just post the link, but we’d like to make it clear that it’s an offer.  Originally the code was literally this:

possible_tweet_bodies = [ 'Minds blown!" , "New deal posted!", 
   "Check out this deal!", etc. ]
tweet_body = random.choice (possible_tweet_bodies)

This works but it’s obviously repetitious and doesn’t really tell the reader anything about the offer.

Enter ChatGPT…and a Hurdle

To make things more useful, I decided to experiment with ChatGPT.  What the code does now is take the text of the post and feed it to ChatGPT.  Originally I fed ChatGPT a prompt like “write a tweet about this: (the content of the post)”.

It works, but the problem is that you could get content back that is way longer than 280 characters.   Unfortunately, ChatGPT does not “count characters” very well.  In the API call you can specify maximum tokens, but a token is a word – anything from “a” to “antidisestablishmentarianism”.

Also, we need to account for the link as well, which Twitter will shorten into a t.co link of 23 characters.  There’s no API to get the t.co link ahead of time, and even if it generates a link under 23 characters, Twitter still “budgets” 23 characters.  So if you’re using a link, your content can’t be longer than 257 characters.

Unfortunately, telling ChatGPT to “write a tweet of no more than 257 characters about…” doesn’t work.  It still comes back with > 280 characters (sometimes over 800 characters!)

We can’t just hard-trim at 257 – that might be in the middle of a sentence.  I tried combinations like “a short tweet”, “a very short tweeet”, etc. but with no luck.  I also tried saying “with no hashtags” but it didn’t help.

But hey this is generative and infinity is a tool we can use.  So the code looks like this:

response = ""
start = "write a short tweet about this sale on virtual hosting"
for counter in range ( 1, 31 ):
    chatgpt_query = "{0:s} {0:s}: {1:s}".format( start, offer.title , offer.summary )
    chatgpt_response = get_tweet_body ( chatgpt_query )
    chatgpt_content = chatgpt_response.content.replace('"','')
    if len(chatgpt_content) > 257:
        debug("instruction [{0:d}]: response length {1:d}, skipping".format(counter,len(chatgpt_content)))
    else:
        response = chatgpt_content
        break

if response == "":
    info("ERROR: no generated tweets under 257 chars")
    sys.exit(1)

In other words, we try 30 times.  If we don’t get text under 257 characters, we exit out – which in this case is OK because it’ll try again in an hour.  In a week-ish of operations, this hasn’t happened.

In case you’re curious, the get_tweet_body function is pretty simple:

def get_tweet_body (input):
   debug ("get_tweet_body() input: {0:s}".format(input))
   response = openai.ChatCompletion.create(
      model='gpt-3.5-turbo',
      messages=[ {
         'role': 'user', 
         'content': input
      } ],
      temperature=1.5
   )
   return response['choices'][0]['message']

I turned up temperature a bit for more variety.  We’re using the GPT 3.5 Turbo model based on some experimentation with OpenAI’s brood.

There have been a few goofy outliers.  For example: “Get top-performing, reliable network protection for all your enterprise critical websites till they take the roast beef scent!”  All in all, the tweets have been pretty on-point:

What are you using ChatGPT for?

 

 

 

 

 

 

raindog308

1 Comment

  1. LowEndBox is most definitely living in the future now!

    Nice one, Andrew. The tweets are (often) hilarious now.

    April 9, 2023 @ 4:23 pm | Reply

Leave a Reply

Some notes on commenting on LowEndBox:

  • Do not use LowEndBox for support issues. Go to your hosting provider and issue a ticket there. Coming here saying "my VPS is down, what do I do?!" will only have your comments removed.
  • Akismet is used for spam detection. Some comments may be held temporarily for manual approval.
  • Use <pre>...</pre> to quote the output from your terminal/console, or consider using a pastebin service.

Your email address will not be published. Required fields are marked *