When 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:
Get blazing-fast virtual hosting in Paris with our KVM VPS sale! Choose RAM from 16-128GB and enjoy a 7-day free trial too. Windows/Linux options available. Check it now at https://t.co/Dqkq6aF8z6 #virtualhosting #KVMVPS #techdeals https://t.co/YFHnrRjpZt
— LowEndBox (@LowEndNetwork) April 9, 2023
🐰 Happy Easter! 🐰 EasyVM is having a clearance sale on virtual hosting! Enjoy discounted prices and short deployment times on packages in Dallas, Las Vegas, and Tampa. Click the links for coupon codes and more info. https://t.co/DiDiOFWC1u
— LowEndBox (@LowEndNetwork) April 9, 2023
Get powerful VPS in 5 locations starting at just $2/month with UNLIMITED bandwidth! Use code 26AHD2CWR2 to save -50% off your first order for 6 months. ☁️ #virtualprivateserver #cheapvps #inexpensivehosting #limitedtimeoffer https://t.co/AUxNGczmYe
— LowEndBox (@LowEndNetwork) April 7, 2023
What are you using ChatGPT for?
Related Posts:
- CYBER MONDAY: VerpexWeb has Cheap cPanel Hosting for Under $7/Year!DirectAdmin for Only $3.50/Year! - December 2, 2024
- CYBER MONDAY: A VPS for Only $8.88 a Year!Wow!Check Out DediRock’s Cyber Monday Sale - December 2, 2024
- CYBER MONDAY: HostDare has a VPS for Less Than $10/Year in Los Angeles, California! - December 2, 2024
LowEndBox is most definitely living in the future now!
Nice one, Andrew. The tweets are (often) hilarious now.