Skip to content

Instantly share code, notes, and snippets.

@gdb

gdb/README.md Secret

Last active July 25, 2021 05:54
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gdb/34439745db7c29904c97961da861bd54 to your computer and use it in GitHub Desktop.
Save gdb/34439745db7c29904c97961da861bd54 to your computer and use it in GitHub Desktop.
Using OpenAI API as a creative tool for tweet-writing

An example creative tool to enhance your tweet-writing, using OpenAI API (request beta access here: https://beta.openai.com/). In practice, I find that a significant fraction of the generations are post-quality with no alterations, and many of the others are great inspiration. In any case, it's recommended to use this tool as a creative aid rather than a bot; if used as a bot without a human curator, make sure to identify it as such.

Usage

To run:

# Generate 100 example tweets
OPENAPI_API_KEY=... python generate.py -n 100

The script sorts the output by the API's estimate of probability. In general, the best results seem to happen somewhere in the top 25%, but not in the top few.

Example high-quality tweets

These were created by generating 1,000 tweets, scrolling down to tweet number 100 or so, and then selecting the best. These reflect probably about 1-in-3 to 1-in-5 of the generated tweet candidates:

  • Programming is hard. People are harder.

  • In programming, there is the correct way of doing things, and then there's the way everyone does it because the correct way is too much work.

  • Ever look at a coworker and think, wow, if I had a little more discipline and motivation, I could be like that! And then you realize that they're thinking exactly the same thing about you.

  • One of the things that constantly amazes me as a software developer: a tiny change in a bit can yield entirely different results

  • Sometimes I see code that I've written 10 years ago and I don't recognize it. It's like seeing a stranger in the mirror.

  • Software isn't like building a bridge, it's more like building a house of cards: all you need is a slight breeze and things come tumbling down.

  • "What is a social network?" - "Well, it's kind of like a phone network, except instead of just having phone numbers, you have friends"

  • Etymology is interesting. For example, English word "security" came from French "securité", which came from Latin "securitas". You can trace the history of the word across 2,000 years by looking at it's etymology! :)

  • Old programmers never die. They just control-Z a lot.

  • Programming success is mostly about solving people problems. If you're good at that, you're good at programming.

  • Software development is not a job, it's an identity

  • Debugging: a) Finding where the bug is b) Fixing the bug c) Writing unit tests to prevent it from happening again d) The bug is you

  • The first step to solving a problem is admitting you have one. I have problems.

  • I'm happy when my program works, and I'm even happier when it works correctly

  • Software isn't like building a bridge, it's more like building a house of cards: all you need is a slight breeze and things come tumbling down.

  • The biggest compliment is when someone asks me, "How did you do that?", rather than, "What did you do?"

  • Some of my best ideas come when the headphones are on, and I'm completely oblivious to my surroundings.

  • Programming is like herding cats. Except your cats are different, you have slightly different goals, and you're not actually herding.

  • What's the difference between the internet and a large fish? One is a massive collective of people voluntarily exchanging their knowledge, the other is a fish.

  • Two kinds of software developers: those that use logcat, and those that will.

  • On the one hand, there's Twitter, on the other hand there's work. There's only time for one hand

  • You are not the main character in your own story. Your future self is.

  • My life is basically a series of nested if statements.

  • In programming, finding the stupidest-possible thing that will work is a core skill

  • Programming is the art of meticulously making order out of pure entropy, and there's no better proof of this than the compiler error messages.

  • Every year that passes I realize more and more how true it is that the faster the computer you are working on, the slower you think

  • Best programming slogan I've heard: Better is the enemy of good

#!/usr/bin/env python
import argparse
import sys
import tqdm
import openai
prompt = """
My favorite programming tweets:
-------
I asked @ilyasut how to set neural network init. He accidentally replied with a poem:
You want to be on the edge of chaos
Too small, and the init will be too stable, with vanishing gradients
Too large, and you'll be unstable, due to exploding gradients
You want to be on the edge
-------
I've been programming for 10 years now. Still feels like magic out of a fantasy: say the words exactly right, and watch your intent get carried out; say the words slightly wrong, and things go haywire. Feeling of wonder and joy hasn't faded one bit.
-------
Web programming is the science of coming up with increasingly complicated ways of concatenating strings.
-------
If you ever feel alone in this world, read your firewall logs. Problem solved :)
-------
Always wanted to travel back in time to try fighting a younger version of yourself? Software development is the career for you!
-------
After 17 years as a professional developer, it seems that the answer to every programming question is "it depends"
-------
"""
stop = "\n-"
def main():
parser = argparse.ArgumentParser(description=None)
parser.add_argument(
"-v",
"--verbose",
action="count",
dest="verbosity",
default=0,
help="Set verbosity.",
)
parser.add_argument("-n", "--number", type=int, default=10)
parser.add_argument("-e", "--engine", default="davinci")
parser.add_argument("-b", "--best_of", type=int)
parser.add_argument("-m", "--max_batch", type=int, default=25)
args = parser.parse_args()
n = args.number
batches = []
while n > 0:
take = min(n, args.max_batch)
batches.append(take)
n -= take
choices = []
for b in tqdm.tqdm(batches):
completion = openai.Completion.create(
prompt=prompt,
n=b,
engine=args.engine,
max_tokens=128,
stop=stop,
temperature=0.9,
logprobs=0,
)
choices += completion.choices
def score(a):
return sum(a.logprobs.token_logprobs) / len(a.logprobs.token_logprobs)
choices = sorted(choices, key=lambda a: -score(a))
for i, choice in enumerate(choices):
print(f"======")
print(choice.text.strip())
return 0
if __name__ == "__main__":
sys.exit(main())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment