Skip to content

Latest commit

 

History

History
66 lines (46 loc) · 1.92 KB

OpenAI_Generate_Q&A.md

File metadata and controls

66 lines (46 loc) · 1.92 KB



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.

Input

Install package

import naas
import os
# 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.
  • 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: ')

Model

Establish connection with OpenAI and get the response

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

Output

print(answer)