Learn how the OpenAI API edits text with respect to an instruction.

We'll cover the following

OpenAI’s GPT-3 model can edit text, which has various applications like grammar and punctuation correction, rewriting text, or any other simple instructions that can be performed on a text. To do this, we provide the model with a text and an instruction for the edit. The model then returns an edited text.

In this chapter, we’ll use the OpenAI Node.js library to call the APIs. It has built-in implemented functions, and these functions can easily be called by passing the required parameters and API key. We can call the following function of the Node.js library to edit the text:

response = await openai.createEdit({
  model: "<model>",
  input: "<input text>",
  instruction: "<instructions to apply on the text>",
  ....
});

Request parameters

Let’s see some essential request parameters for this endpoint in the table below:

Fields

Format

Type

Description

model

String

Required

The ID of the model that will be used to perform the task.

input

String

Optional

The input text to edit.

instruction

String

Required

The instruction to edit the text.

n

Integer

Optional

The number of edit results the model will return.

In the example below, we’ve provided an incorrect verb "create" in the input text and an instruction "Fix the grammar.".

Press + to interact
// Importing OpenAI
const { Configuration, OpenAIApi } = require("openai");
// Adding the secret key to configuration
const configuration = new Configuration({
apiKey: "{{SECRET_KEY}}",
});
const openai = new OpenAIApi(configuration);
// Function to call API via library
async function editText() {
try {
let response = await openai.createEdit({
model: "text-davinci-edit-001",
input: "Educative is a leading online learning platform made by developers, create for developers.",
instruction: "Fix the grammar.",
temperature: 0,
top_p: 1
});
// Printing response
printResponse(response);
} catch (error) {
// Printing error message
printError(error);
}
}
editText();

Let’s see some code details:

  • Line 2: We import the openai library.

  • Line 5–7: We add the configuration settings.

  • Line 8: We fetch configured openai instance.

  • Line 11-26: We create the editText function to make the API call.

    • Line 13–19: We call the function createEdit with parameters model, input, instruction, temperature, and top_1.

    • Line 21: We call the custom function printResponse to print the response.

    • Line 24: We call the custom function printError to print the error.

  • Line 28: We call the editText function to make the API call.

In the example below, we convert a sentence from active voice to passive voice.

Press + to interact
// Importing OpenAI
const { Configuration, OpenAIApi } = require("openai");
// Adding the secret key to configuration
const configuration = new Configuration({
apiKey: "{{SECRET_KEY}}",
});
const openai = new OpenAIApi(configuration);
// Function to call API via library
async function editText() {
try {
let response = await openai.createEdit({
model: "text-davinci-edit-001",
input: "The kangaroo carries her baby in her pouch.",
instruction: "Convert sentence to passive voice",
temperature: 0,
top_p: 1
});
// Printing response
printResponse(response);
} catch (error) {
// Printing error message
printError(error);
}
}
editText();

Response fields

The response is a JSON object. Some essential attributes are given below:

Fields

Format

Description

choices

Object array

It is an array of objects. Every object contains valuable information about the response. The size of the array will be equal to the n parameter that we provided in the request parameter.

choices[i].text

String

The edited text.

Get hands-on with 1200+ tech skills courses.