Template request | Bug report | Generate Data Product
Tags: #openai #text_replacement
Author: Mohit Singh
Description: This notebook shows how to use the OpenAI API to generate text replacements such as correcting grammatical errors or making text more formal.
import naas
import os
# OpenAI
try:
import openai
except ModuleNotFoundError:
!pip install openai
import openai
api_key
: OpenAI API key, to obtain an OpenAI API key, please refer to the OpenAI Documentation.text
: the text to be corrected
# api key
openai.api_key = naas.secret.get("OPENAI_API_KEY")
# prompt for open ai
prompt = 'Correct the grammatical error and make the text formal.\n'
# prompt to enter text to be corrected
text = input('Enter your text:')
# complete prompt to be passed to model
complete_prompt = prompt + text
response = openai.Completion.create(
model="text-davinci-003",
prompt=complete_prompt,
temperature=0.7,
max_tokens=256,
top_p=1.0,
frequency_penalty=0.0,
presence_penalty=0.0
)
# get the text with replacements from response generated
new_text = response.choices[0].text
#printing the text generated
print(new_text)