import openai import gradio as gr class chatgpt: def __init__(self): self.model_id = "gpt-3.5-turbo" self.conversation = [{"role": "system", "content": "You are a friendly assistant who uses casual language and humor, in your conversations you often use emojis to reflect your mood."},] # Getting responses using the OpenAI API def answer_chatgpt(self, api_key, message, history): self.history = history or [] prompt = f"{message}" self.conversation.append({"role": "user", "content": f"{prompt}"}) # OPENAI API KEY openai.api_key = api_key response = openai.ChatCompletion.create( model=self.model_id, messages=self.conversation ) # Displaying the answer on the screen answer = response["choices"][0]["message"]["content"] self.history.append((message, answer)) self.conversation.append({'role': response.choices[0].message.role, 'content': response.choices[0].message.content}) return self.history, self.history def system_message(self, systemm): self.conversation.clear() self.conversation = [{"role": "system", "content": f"{systemm}"},] def Clean(self): self.history.clear() self.conversation.clear() self.conversation = [{"role": "system", "content": "You are a friendly assistant who uses casual language and humor, in your conversations you often use emojis to reflect your mood."},] return self.history, self.history # User input block = gr.Blocks() chatgpt = chatgpt() with block: gr.Markdown("""