import os import openai import gradio as gr #if you have OpenAI API key as an environment variable, enable the below #openai.api_key = os.getenv("OPENAI_API_KEY") #if you have OpenAI API key as a string, enable the below openai.api_key = "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" start_sequence = "\nAI:" restart_sequence = "\nHuman: " prompt = "The following is a conversation with an AI assistant. The assistant is helpful, creative, clever, and very friendly.\n\nHuman: Hello, who are you?\nAI: I am an AI created by OpenAI. How can I help you today?\nHuman: " def openai_create(prompt): openai.api_key = os.getenv("OPENAI_API_KEY") response = openai.ChatCompletion.create( model="gpt-3.5-turbo-16k", messages=[ { "role": "system", "content": "You are a helpful assistant!" }, { "role": "user", "content": "" } ], temperature=0.4, max_tokens=12500, top_p=1, frequency_penalty=0, presence_penalty=0, stop=[" Human:", " AI:"] ) return response.choices[0].text def chatgpt_clone(input, history): history = history or [] s = list(sum(history, ())) s.append(input) inp = ' '.join(s) output = openai_create(inp) history.append((input, output)) return history, history block = gr.Blocks() with block: gr.Markdown("""