openai-apigpt-3

Open AI GPT API: How to get completion in between (fill-in-the-middle)?


In this announcement: https://openai.com/blog/gpt-3-edit-insert, an insert mode that lets you have completion in the middle of a text is introduced. How can I have it through the Open AI GPT models' completion API?

For instance in the case of a code completion (which Github Copilot does currently):

def say_hello():
    print('hi', name)

should be completed with name argument inside the parentheses:

def say_hello(name):
    print('hi', name)

Solution

  • As insertion instruction guide explains, we can use suffix parameter to provide the text which is located after where you want to insert the new completion.

    prompt = "def say_hello("
    suffix = """):
      print('hi', name)"""
    
    response = client.completions.create(
        model="gpt-3.5-turbo-instruct",
        prompt=prompt,
        suffix=suffix,
        max_tokens=32
    )
    print(response.choices[0].text) # should be 'name'