Skip to content

Latest commit

 

History

History
74 lines (52 loc) · 2.22 KB

OpenAI_Generate_text_replacements.md

File metadata and controls

74 lines (52 loc) · 2.22 KB



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.

Input

import naas
import os
# OpenAI
try:
    import openai
except ModuleNotFoundError:
    !pip install openai
    import openai

Setup Variables

  • 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

Model

Establish connection with OpenAI and get the response

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

Output

#printing the text generated
print(new_text)