Skip to content

Files

Latest commit

 

History

History
73 lines (51 loc) · 2.07 KB

OpenAI_Generate_text_summaries.md

File metadata and controls

73 lines (51 loc) · 2.07 KB



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.

Input

Install package

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

Setup Variables

  • 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

Establish connection with OpenAI and get the response

# 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

Output

# Display the summary
print(summary)