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 |
| String | Required | The ID of the model that will be used to perform the task. |
| String | Optional | The input text to edit. |
| String | Required | The instruction to edit the text. |
| 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."
.
// Importing OpenAIconst { Configuration, OpenAIApi } = require("openai");// Adding the secret key to configurationconst configuration = new Configuration({apiKey: "{{SECRET_KEY}}",});const openai = new OpenAIApi(configuration);// Function to call API via libraryasync 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 responseprintResponse(response);} catch (error) {// Printing error messageprintError(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 parametersmodel
,input
,instruction
,temperature
, andtop_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.
// Importing OpenAIconst { Configuration, OpenAIApi } = require("openai");// Adding the secret key to configurationconst configuration = new Configuration({apiKey: "{{SECRET_KEY}}",});const openai = new OpenAIApi(configuration);// Function to call API via libraryasync 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 responseprintResponse(response);} catch (error) {// Printing error messageprintError(error);}}editText();
Response fields
The response is a JSON object. Some essential attributes are given below:
Fields | Format | Description |
| 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 |
| String | The edited text. |
Get hands-on with 1200+ tech skills courses.