Template request | Bug report | Generate Data Product
Tags: #openai #gpt #api #prompt
Author: Sriniketh Jayasendil
Description: This template shows how to use the OpenAI API to generate responses to user input within a Naas notebook, allowing users to create interactive chatbots or dialogue systems.
import naas
try:
import openai
except ModuleNotFoundError:
!pip install --user openai
import openai
#inputs
openai.api_key = naas.secret.get(name="OPENAI_API_KEY")
choice = 1 # to ask every time if the user wants to continue the conversation(1) or not(0)
#outputs
messages = {} # Logs the prompts and response generated.
def gen_dialogue(prompt):
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,
stop=["\"\"\""]
)
print('\n🤖 OpenAI response: ' + response.choices[0].text)
messages[prompt] = response.choices[0].text
while(choice):
print()
prompt = input('👤 Enter the prompt: ')
gen_dialogue(prompt)
print()
choice = int(input('➡️ Enter the choice [0/1]: '))
print('\n\n\n')
print(messages)