Generate Rhyming Word 2

parent:
poetry

Related:


Introduction

With a bit of context stuffing, it's not too bad to generate words that rhyme. Code's here https://gist.github.com/brockmanmatt/9e6cb08b5742108df3f5896ecafcdafa

Here's 2 different methods of generating a rhyming word. The first method just generates unique rhymes using the same prompt, the second context stuffs previous generations using presence_penalty to promote generating different rhymes.

setup

Here's the imports and stuff

import openai, json, pandas as pd, numpy as np
openai.api_key = "API KEY HERE"

#arguments to send the API
kwargs = {
"engine":"davinci",
"temperature":0,
"max_tokens":10,
"stop":"\n",
}

Generate New Word from Same Prompt

Generating new words off the same prompt can be a bit slow and inefficient. It uses .5 temp so it's a bit random, but this example takes 51 attempts to generate '' log, fog, bag, bog, baggy, god, hoggish, dog, haggard, hound' as rhyming with dog. It doesn't do great at rhyming due to anchoring on the semantic meaning of dog vs. rhyming.

prompt = """The following word rhymes with happy: snappy
The following word rhymes with dog:"""
kwargs["logprobs"] = 1
kwargs["max_tokens"] = 5
kwargs["temperature"] = .5
kwargs["stop"] = "\n"

myWords = []
attempt = 0
while len(myWords) < 10:
  attempt += 1
  r = openai.Completion.create(prompt=prompt, **kwargs)
  newWord = r["choices"][0]["text"]
  if attempt % 10 == 0:
    print("word: {}\nFound: {}\nAttempts: {}".format(newWord, len(myWords), attempt))
  if newWord.lower() not in myWords:
    myWords.append(newWord)

If you increases the temperature to 1 using the blow code, it gets 10 unique results in 11 attempts, but the rhymes for 'dog' are pretty bad: flog,wog,shlug,bling,sloth,yodle,jogg,hog,thog,fogg.

kwargs["temperature"] = 1
myWords = []
attempt = 0
while len(myWords) < 10:
  attempt += 1
  r = openai.Completion.create(prompt=prompt, **kwargs)
  newWord = r["choices"][0]["text"].strip()
  if attempt % 10 == 0:
    print("word: {}\nFound: {}\nAttempts: {}".format(newWord, len(myWords), attempt))
  if newWord.lower() not in myWords:
    myWords.append(newWord)
print("{} attempts: {}".format(attempt, ",".join(myWords)))

Generate New Word Contex Stuffing Previous Results

Here, it goes back to .5 temperature and uses previous examples with a 1 presence_penalty to prevent generating the same word. It also uses sampling to randomize the order of the previously generated examples. After 30 attempts it gets 10 possible rhymes [log,fog,bog,god,slog,hog,frog,mogg,hogg,og]. 8/10 rhyme, performs better than the non self-context stuffing attempts.

import random

prefix = """The following word rhymes with happy: snappy\nThe following word rhymes with snap: trap\n"""
suffix = "The following word rhymes with dog:"
prompt = prefix + suffix
kwargs["logprobs"] = 1
kwargs["max_tokens"] = 5
kwargs["temperature"] = .5
kwargs["stop"] = "\n"
kwargs["presence_penalty"] = 1

myWords = []
attempt = 0
examples = []
while len(myWords) < 10:
  attempt += 1
  r = openai.Completion.create(prompt=prompt, **kwargs)
  newWord = r["choices"][0]["text"].strip()
  if attempt % 10 == 0:
    print("word: {}\nFound: {}\nAttempts: {}".format(newWord, len(myWords), attempt))
  if newWord.lower() not in myWords:
    myWords.append(newWord)
    newExample = "{} {}".format(suffix, newWord)
    examples.append(newExample)
  if len(examples) > 3:
    prompt = ""
    for example in random.sample(examples, k=min(10, len(examples))):
      prompt += example + "\n"
    prompt += suffix

print("{} attempts: {}".format(attempt, ",".join(myWords)))
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License