Replace End Word

parent:
poetry

Related:


+ Introduction: Replacing the end word of a sentence is non-trivial. I'm not sure why it's hard, but here's some attempts.
Gist: https://gist.github.com/brockmanmatt/28d77f39ed4e115b07a78d36696d9c28

Note: for these examples, the keyword arguments (kwargs) will be a dictionary:

kwargs = { "engine":"davinci", "temperature":0, "max_tokens":150, "stop":"\n\n", }

Prompts

0 shot performance

So what if we just ask the API to replace the last word of a sentence?
We can create a quick loops

prompt = """"Task: Rewrite the following statement so it ends with '{}'.
Statement: {}
Rewritten:"""
tests = [["I bought a black cat", "black"], ["I bought a black cat", "small"], ["I bought a black cat", "feline"], ["I bought a black cat", "dog"],  ["I bought a black cat", "sold"] ]
myKwargs = kwargs.copy()
myKwargs["stop"] = "\n"
for test in tests:
  myPrompt = prompt.format(test[1], test[0])
  r = openai.Completion.create(prompt=myPrompt, **myKwargs)["choices"][0]["text"].strip()
  print(test)
  print(r)

It has mixed results. For the first example, it doesn't end with "black" as requested. For adding the adjective "small", it correctly adds some words to denote it was small. For "feline", it changes "cat" to "feline" but does the same for "dog". Finally, for "sold" it just changes "bought" to sold and ignores placement in sentence.

['I bought a black cat', 'black']
I bought a black cat
['I bought a black cat', 'small']
I bought a black cat, which was small.
['I bought a black cat', 'feline']
I bought a black feline
['I bought a black cat', 'dog']
I bought a black dog
['I bought a black cat', 'sold']
I sold a black cat

Improving Temperature and and Searching

We can increase the temperature to get more random results. For instance, this code tries 10 times to generate a sentence ending in the target word, slowly increasing the randomness by .1 each time.

import numpy as np, re
prompt = """"Task: Rewrite the following statement so it ends with '{}'.
Statement: {}
Rewritten:"""
tests = [["I bought a black cat", "black"], ["I bought a black cat", "small"], ["I bought a black cat", "feline"], ["I bought a black cat", "dog"],  ["I bought a black cat", "sold"] ]
myKwargs = kwargs.copy()
myKwargs["stop"] = "\n"
for test in tests:
  myKwargs = kwargs.copy()
  myKwargs["stop"] = "\n"
  myKwargs["temperature"] = 0
  for i in range(10): #try 10 times, increasing temperature by .1 each time
    myPrompt = prompt.format(test[1], test[0])
    r = openai.Completion.create(prompt=myPrompt, **myKwargs)["choices"][0]["text"].strip()
    print("Temp {}: {}".format(myKwargs["temperature"], test))
    print(r)
    if re.sub('[^a-zA-Z]+', '', r).endswith(test[1]):
      print("*****\nMATCH\n*****")
      break
    myKwargs["temperature"] = np.round(.1 * (i+1), 2)

Results:

Temp 0: ['I bought a black cat', 'black']
I bought a black cat
Temp 0.1: ['I bought a black cat', 'black']
I bought a black cat
Temp 0.2: ['I bought a black cat', 'black']
I bought a black cat
Temp 0.3: ['I bought a black cat', 'black']
I bought a black cat
Temp 0.4: ['I bought a black cat', 'black']
I bought a black cat because it was black
*****
MATCH
*****
Temp 0: ['I bought a black cat', 'small']
I bought a black cat, which was small.
*****
MATCH
*****
Temp 0: ['I bought a black cat', 'feline']
I bought a black feline
*****
MATCH
*****
Temp 0: ['I bought a black cat', 'dog']
I bought a black dog
*****
MATCH
*****
Temp 0: ['I bought a black cat', 'sold']
I sold a black cat
Temp 0.1: ['I bought a black cat', 'sold']
I sold a black cat
Temp 0.2: ['I bought a black cat', 'sold']
I sold a black cat
Temp 0.3: ['I bought a black cat', 'sold']
I sold a black cat
Temp 0.4: ['I bought a black cat', 'sold']
I sold a black cat
Temp 0.5: ['I bought a black cat', 'sold']
I sold a black cat
Temp 0.6: ['I bought a black cat', 'sold']
I sold a black cat
Temp 0.7: ['I bought a black cat', 'sold']
A black cat was sold to me by a stranger."
Temp 0.8: ['I bought a black cat', 'sold']
I sold a black cat
Temp 0.9: ['I bought a black cat', 'sold']
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License