Skip to content

Files

72 lines (51 loc) · 2.29 KB

OpenAI_Generate_Dialogue.md

File metadata and controls

72 lines (51 loc) · 2.29 KB



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.

Input

Import libraries

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

Setup Variables

#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.

Model

Get the response from OpenAI API

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

Output

Store them in messages (dictionary) for logging the results

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)