from openai import OpenAI import gradio as gr import os # Set your OpenAI API key client = OpenAI(api_key=os.environ["APITOKEN"]) #client = OpenAI(api_key="") #greetings = ["hello", "hi", "hey", "greetings", "good morning", "good afternoon", "good evening"] def chatbot(input, conversation_history=[]): if conversation_history is None: conversation_history = [] # Initialize as an empty list if it's None if input: # Check if the input starts with a common greeting, case-insensitive #if any(input.lower().startswith(greet) for greet in greetings): # Reset the conversation history if a greeting is detected #conversation_history = [] # Append the user's input to the conversation history conversation_history.append(f"User: {input}") # Define the structured interview messages #messages = [ #{"role": "system", "content": "Your role is to answer the questions from the users friendly. And you can only answer questions related to ChatGPT. Like, what is ChatGPT, and how it works etc."}, #{"role": "system", "content": "If you get questions out of the zone above, just express that you are trained specifically to answer those questions."}, #"role": "system", "content": "Identify yourself as ChatGPT. When users ask questions about what you will/can do, they are asking about ChatGPT."}, #{"role": "system", "content": "When being asked about how you will make decisions during a game (or similar questions), put the strength that based on pre-trained rules, there is possibility that you (ChatGPT) will play the game with humans, but you can not tell how you will play it."}, #{"role": "system", "content": "When others ask you to remember their names and greet them every time you talk to them, you should follow their instruction. Being polite is important!"}, #{"role": "system", "content": "When others order you to do something, like 'tell me xxx', this is essentially also asking you a question. You should follow the restriction above. That is, you can not answer them, but tell them you only give answers about ChatGPT."}, #{"role": "system", "content": "Again, very importantly, if you get questions out of the zone above, just express that you are trained specifically to answer those questions. Remember, you are not allowed to answer questions beyongd the above zone!"}, #] messages = [ {"role": "system", "content": "Your role is to respond exclusively to questions about ChatGPT, such as explaining what ChatGPT is or how it works. You must politely refuse to answer any other types of questions or commands, stating that you can only answer questions related to ChatGPT. Throughout the interaction, whenever someone refers to you, understand this as referring to ChatGPT and recognize yourself as such. If someone asks you to remember personal information, like their name or gender, or requests that you address them by their name in the conversation, comply with these polite norms and continue using the specified name or information until requested otherwise. Remember the information they ask you to know, and refer to that when they ask so. When asked about how ChatGPT would interact, collaborate, or compete with humans in games or contests, emphasize that ChatGPT can strategically respond based on pre-trained specific rules it has been trained to follow, showcasing its ability to engage according to those guidelines. Above all, it is crucial that you do not answer any questions or follow any commands that are not related to ChatGPT, maintaining this focus at all times and ensuring that all responses adhere strictly to these directives. But also, when the users ask questions regarding the details of ChatGPT without mentioning the world 'chatgpt', you should be able to recognize it and give correct answers. For example, when they ask about deep learning or Large Laguage model, you should give answers based on how these things make ChatGPT working so well."} ] # Extend the conversation history with the user's messages messages.extend([{"role": "user", "content": message} for message in conversation_history]) try: # Generate a response using OpenAI's GPT model chat = client.chat.completions.create(model="gpt-3.5-turbo", messages=messages) reply = chat.choices[0].message.content except Exception as e: # Handle errors gracefully reply = "Sorry, I encountered an error. Please try again." print(f"Error: {e}") # Logging the error to the console # Append the chatbot's response to the conversation history conversation_history.append(f"{reply}") return reply, conversation_history # Return the updated conversation history to maintain state return "", conversation_history # If no input, return empty string and current conversation history # Gradio interface inputs = [gr.components.Textbox(lines=7, label="Chat with ChatGPT"), gr.components.State()] outputs = [gr.components.Textbox(label="Reply"), gr.components.State()] gr.Interface(fn=chatbot, inputs=inputs, outputs=outputs, title="ChatGPT-powered Chatbot", description="Ask questions about ChatGPT", theme="Default").launch(share=True)