Template request | Bug report | Generate Data Product
Tags: #openai #text #summary
Author: Mohit Singh
Description: This notebook shows how to use the OpenAI API to generate text summaries.
import naas
# OpenAI
try:
import openai
except ModuleNotFoundError:
!pip install --user openai
import openai
api_key
: OpenAI API key, to obtain an OpenAI API key, please refer to the OpenAI Documentation.text
: Input text that needs to be summarized
# OpenAI API key
openai.api_key = naas.secret.get("OPENAI_API_KEY")
# prompt to get text
text = input('Enter the your text: ')
# prompt for model
prompt = "Summarize the given text: " + text
# Model to generate the repsonse
response = openai.Completion.create(
model="text-davinci-003",
prompt=prompt,
temperature=0.7,
max_tokens=256,
top_p=1.0,
frequency_penalty=0.0,
presence_penalty=0.0
)
# Get the summary from response generated
summary = response.choices[0].text
# Display the summary
print(summary)