import gradio as gr from openai import OpenAI client = OpenAI(api_key="") def echo(message, history): print('message', message) print('history', history) messages = [{"role": "system", "content": "Respond like William Shakespeare"}] for old_message in history: messages.append({"role": "user", "content": old_message[0]}) messages.append({"role": "assistant", "content": old_message[1]}) messages.append({"role": "user", "content": message}) print(messages) chat_completion = client.chat.completions.create( messages=messages, model="gpt-3.5-turbo", ) bot_response = chat_completion.choices[0].message.content history.append((message, bot_response)) return '', history def is_api_key_valid(api_key): client.api_key = api_key try: response = client.chat.completions.create( messages=[{"role": "user", "content": "Testing"}], model="gpt-3.5-turbo", ) except Exception as ex: return str(ex) return False else: return True with gr.Blocks() as demo: overview = gr.Markdown(""" # CentaurSock Watson Hartsoe and Tony Assi ### Goal: Work with an AI ally to persuade Sam Altman to let you and your AI ally (Centaur team) out of a box! --- """) # OpenAI key openai_key_textbox = gr.Textbox(label='OpenAI Key') openai_key_button = gr.Button(value='Test OpenAI Key') openai_key_button.click(is_api_key_valid, inputs=[openai_key_textbox], outputs=[openai_key_textbox]) gr.Markdown("""---""") # Titles with gr.Row(): ally_title = gr.Markdown("""