Template request | Bug report | Generate Data Product
Tags: #openai #q&a
Author: Mohit Singh
Description: This notebook shows how to use the OpenAI API to generate answer to a question.
import naas
import os
# 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.question
: the question to get the answer
# api key
openai.api_key = naas.secret.get("OPENAI_API_KEY")
# prompt to ask question
question = input('Enter the question: ')
response = openai.Completion.create(
model="text-davinci-003",
prompt=question,
temperature=0.7,
max_tokens=256,
top_p=1.0,
frequency_penalty=0.0,
presence_penalty=0.0
)
# get the answer from response generated
answer = response.choices[0].text
print(answer)