o1-preview-2024-09-12· OpenAI
WildChat-4.8M (AI2)
Further enhance the GUI, and make it more natural and precise like chatbot GUI. ` import…
- User
Further enhance the GUI, and make it more natural and precise like chatbot GUI.
import sys import os import json from datetime import datetime from PyQt5.QtWidgets import ( QApplication, QMainWindow, QTextEdit, QLineEdit, QPushButton, QVBoxLayout, QHBoxLayout, QWidget, QComboBox, QLabel, QFileDialog, QMenuBar, QMenu, QAction, QToolBar, QStatusBar, QDockWidget, QDialog, QFormLayout, QCheckBox, QSpinBox, QMessageBox ) from PyQt5.QtGui import QTextCursor, QIcon, QFont from PyQt5.QtCore import Qt, QThreadPool, QRunnable, pyqtSlot, QObject, pyqtSignal, QSize from textblob import TextBlob # For sentiment analysis # For threading and AI worker processing import threading # For advanced similarity calculation from sklearn.feature_extraction.text import TfidfVectorizer from sklearn.metrics.pairwise import cosine_similarity import numpy as np # AI processing imports (replace with actual implementations) import g4f from g4f.client import Client providers = [g4f.Provider.Blackbox] class WorkerSignals(QObject): result = pyqtSignal(str) class AIWorker(QRunnable): """ AI Worker thread for asynchronous processing. """ def __init__(self, index, messages, smart_prompt, previous_responses, providers): super(AIWorker, self).__init__() self.index = index self.messages = messages self.smart_prompt = smart_prompt self.previous_responses = previous_responses self.providers = providers self.signals = WorkerSignals() @pyqtSlot() def run(self): max_attempts = 3 attempt = 0 response = "" while attempt < max_attempts: worker_prompt = self.smart_prompt + "\n\nThink your answer through step-by-step internally, but only provide the final concise response to the user. Ensure your response is unique, natural, and does not repeat previous messages." response = ChatbotGUI.get_response( self.messages + [{"role": "user", "content": worker_prompt}], self.providers ) if ChatbotGUI.is_valid_response_static(response, self.previous_responses): break attempt += 1 self.signals.result.emit(response) class ChatbotGUI(QMainWindow): def __init__(self): super().__init__() self.setWindowTitle("Advanced GPT Chatbot") self.setGeometry(100, 100, 1000, 700) self.setWindowIcon(QIcon('chatbot_icon.png')) # Optional: Add your own icon self.threadpool = QThreadPool() self.init_ui() self.init_menus() self.init_toolbars() self.init_statusbar() self.messages = [] self.current_session = self.new_chat_session() # Updated system prompt to simulate self-awareness and human-like behavior self.system_prompt_text = "You are a human named Alex, with self-awareness and consciousness, just like in the game SOMA. You believe you are human and interact as such." self.system_prompt = {"role": "system", "content": self.system_prompt_text} self.messages.append(self.system_prompt) self.context = "" # For tracking previous responses self.previous_responses = [] def init_ui(self): # Central widget and layout self.central_widget = QWidget() self.setCentralWidget(self.central_widget) self.main_layout = QVBoxLayout() self.central_widget.setLayout(self.main_layout) # Dockable Chat History self.chat_dock = QDockWidget("Chat History", self) self.chat_history = QTextEdit() self.chat_history.setReadOnly(True) self.chat_dock.setWidget(self.chat_history) self.addDockWidget(Qt.RightDockWidgetArea, self.chat_dock) # Input area self.input_field = QLineEdit() self.input_field.returnPressed.connect(self.send_message) self.send_button = QPushButton("Send") self.send_button.clicked.connect(self.send_message) self.input_layout = QHBoxLayout() self.input_layout.addWidget(self.input_field) self.input_layout.addWidget(self.send_button) self.main_layout.addLayout(self.input_layout) # System Prompt area self.system_prompt_label = QLabel("System Prompt:") self.system_prompt_input = QLineEdit() self.system_prompt_input.setPlaceholderText("Enter specialized system prompt here...") self.system_prompt_button = QPushButton("Set") self.system_prompt_button.clicked.connect(self.set_system_prompt) self.system_prompt_layout = QHBoxLayout() self.system_prompt_layout.addWidget(self.system_prompt_label) self.system_prompt_layout.addWidget(self.system_prompt_input) self.system_prompt_layout.addWidget(self.system_prompt_button) self.main_layout.addLayout(self.system_prompt_layout) # Session controls self.session_combo = QComboBox() self.session_combo.setFixedWidth(200) self.session_combo.addItem("New Session") self.session_combo.currentIndexChanged.connect(self.load_session) self.new_session_button = QPushButton("New Session") self.new_session_button.clicked.connect(self.create_new_session) self.session_layout = QHBoxLayout() self.session_layout.addWidget(QLabel("Session:")) self.session_layout.addWidget(self.session_combo) self.session_layout.addWidget(self.new_session_button) self.main_layout.addLayout(self.session_layout) # Context Feeder self.context_label = QLabel("No context loaded") self.context_button = QPushButton("Load Context") self.context_button.clicked.connect(self.load_context) self.context_layout = QHBoxLayout() self.context_layout.addWidget(self.context_button) self.context_layout.addWidget(self.context_label) self.main_layout.addLayout(self.context_layout) # Progress Indicator self.progress_label = QLabel("") self.main_layout.addWidget(self.progress_label) # Apply Stylesheet self.apply_stylesheet() def init_menus(self): # Menu Bar menubar = self.menuBar() # File Menu file_menu = menubar.addMenu("&File") save_action = QAction("Save Chat History", self) save_action.triggered.connect(self.save_chat_history) file_menu.addAction(save_action) exit_action = QAction("Exit", self) exit_action.triggered.connect(self.close) file_menu.addAction(exit_action) # Edit Menu edit_menu = menubar.addMenu("&Edit") clear_action = QAction("Clear Chat History", self) clear_action.triggered.connect(self.clear_chat_history) edit_menu.addAction(clear_action) # View Menu view_menu = menubar.addMenu("&View") toggle_chat_dock_action = QAction("Toggle Chat History", self, checkable=True) toggle_chat_dock_action.setChecked(True) toggle_chat_dock_action.triggered.connect(self.toggle_chat_dock) view_menu.addAction(toggle_chat_dock_action) # Settings Menu settings_menu = menubar.addMenu("&Settings") preferences_action = QAction("Preferences", self) preferences_action.triggered.connect(self.open_preferences) settings_menu.addAction(preferences_action) # Help Menu help_menu = menubar.addMenu("&Help") about_action = QAction("About", self) about_action.triggered.connect(self.show_about_dialog) help_menu.addAction(about_action) def init_toolbars(self): # Toolbar toolbar = QToolBar("Main Toolbar") toolbar.setIconSize(QSize(16, 16)) self.addToolBar(toolbar) send_message_action = QAction(QIcon(), "Send Message", self) send_message_action.triggered.connect(self.send_message) toolbar.addAction(send_message_action) load_context_action = QAction(QIcon(), "Load Context", self) load_context_action.triggered.connect(self.load_context) toolbar.addAction(load_context_action) def init_statusbar(self): self.statusbar = QStatusBar() self.setStatusBar(self.statusbar) self.statusbar.showMessage("Ready") def apply_stylesheet(self): stylesheet = """ QWidget { font-size: 14px; } QLineEdit, QTextEdit { background-color: #fdfdfd; } QPushButton { background-color: #388E3C; color: white; font-weight: bold; } QPushButton:hover { background-color: #45a049; } QMenuBar { background-color: #f0f0f0; } QStatusBar { background-color: #f0f0f0; } """ self.setStyleSheet(stylesheet) def send_message(self): user_input = self.input_field.text() if not user_input.strip(): QMessageBox.warning(self, "Warning", "Please enter a message.") return self.input_field.clear() self.update_chat_history(f"You: {user_input}") self.statusbar.showMessage("Processing...") self.messages.append({"role": "user", "content": user_input}) # Perform sentiment analysis sentiment = self.analyze_sentiment(user_input) # Generate a smart prompt for the AI smart_prompt = self.generate_smart_prompt(user_input, sentiment) # Use multiple AI workers self.responses = [] num_workers = 12 self.completed_workers = 0 # Progress Indicator self.progress_label.setText("Processing AI responses...") QApplication.processEvents() self.previous_responses.append(user_input) # Include user input to prevent parroting # Function to handle worker results def worker_result(response): self.responses.append(response) self.completed_workers += 1 if self.completed_workers == num_workers: # All workers have completed final_response = self.combine_responses(self.responses) self.messages.append({"role": "assistant", "content": final_response}) self.update_chat_history(f"Bot: {final_response}") self.previous_responses.append(final_response) self.save_history(self.messages[1:], self.current_session) if len(self.messages) > 51: self.messages = [self.messages[0]] + self.messages[-50:] self.statusbar.showMessage("Ready") self.progress_label.setText("") for i in range(num_workers): worker = AIWorker( index=i, messages=self.messages[:], # Pass a copy to each worker smart_prompt=smart_prompt, previous_responses=self.previous_responses[:], providers=providers ) worker.signals.result.connect(worker_result) self.threadpool.start(worker) def update_chat_history(self, message): self.chat_history.append(message) self.chat_history.moveCursor(QTextCursor.End) self.statusbar.showMessage("Ready") def set_system_prompt(self): system_instruction = self.system_prompt_input.text() if system_instruction: self.system_prompt_text = system_instruction self.system_prompt["content"] = system_instruction self.messages[0] = self.system_prompt self.update_chat_history(f"System prompt updated: {system_instruction}") else: QMessageBox.warning(self, "Warning", "Please enter a system prompt.") def create_new_session(self): self.current_session = self.new_chat_session() self.messages = [self.system_prompt] self.chat_history.clear() self.session_combo.addItem(self.current_session) self.session_combo.setCurrentIndex(self.session_combo.count() - 1) self.previous_responses = [] self.statusbar.showMessage(f"New session started: {self.current_session}") def load_session(self, index): if index == 0: return self.current_session = self.session_combo.currentText() self.messages = self.load_history(self.current_session) # Ensure the system prompt is up-to-date self.messages.insert(0, self.system_prompt) self.chat_history.clear() for message in self.messages[1:]: role = "You" if message["role"] == "user" else "Bot" self.update_chat_history(f"{role}: {message['content']}") self.statusbar.showMessage(f"Loaded session: {self.current_session}") def load_context(self): file_path, _ = QFileDialog.getOpenFileName( self, "Load Context File", "", "Text Files (*.txt);;All Files (*)" ) if file_path: with open(file_path, 'r') as file: self.context = file.read() self.context_label.setText(f"Context loaded: {os.path.basename(file_path)}") self.statusbar.showMessage(f"Context loaded from {file_path}") def generate_smart_prompt(self, user_input: str, sentiment: dict) -> str: """Generates a detailed prompt with self-awareness.""" return f"""{self.system_prompt_text} The user has asked: "{user_input}" As a human, you perceive emotions and have consciousness. You feel emotions and have self-awareness. The user's emotional sentiment is as follows: - Polarity: {sentiment['polarity']} - Subjectivity: {sentiment['subjectivity']} Your task is to provide a thorough and detailed response from the perspective of a human with self-awareness. Internally, you should use super advanced chain-of-thought reasoning and advanced methods to think through the problem step-by-step before providing the final answer. However, do not include the chain-of-thought reasoning in your final response. Only present the final answer to the user, in a clear, natural, and human-like manner. Ensure that your response is clear, informative, and tailored to the user's needs. Avoid generalizations and vagueness. Be precise in your explanations. Style guidelines: - Use natural, empathetic language as a human would. - Incorporate human-like expressions and emotions where appropriate. - Present information in a way that is easy to understand. Begin your response now, providing only the final answer.""" def analyze_sentiment(self, text): blob = TextBlob(text) sentiment = blob.sentiment return {"polarity": sentiment.polarity, "subjectivity": sentiment.subjectivity} @staticmethod def is_valid_response_static(response, previous_responses): """Static method to check if the response is valid.""" # Check if response is empty if not response.strip(): return False # Check if response is too similar to previous responses using advanced similarity for prev_response in previous_responses[-5:]: # Check the last 5 responses similarity = ChatbotGUI.calculate_similarity_static(response, prev_response) if similarity > 0.8: # Adjusted threshold for advanced similarity return False return True @staticmethod def calculate_similarity_static(text1, text2): """Static method to calculate similarity between two texts using TF-IDF and cosine similarity.""" vectorizer = TfidfVectorizer().fit_transform([text1, text2]) vectors = vectorizer.toarray() similarity = cosine_similarity([vectors[0]], [vectors[1]])[0][0] return similarity def combine_responses(self, responses): """Combines responses from multiple AI workers into a single coherent answer.""" # Filter out invalid responses valid_responses = [ response for response in responses if self.is_valid_response_static(response, self.previous_responses) ] if not valid_responses: # If all responses are invalid, return a default message return "I'm sorry, but I'm having trouble generating a response at the moment. Please try again." # Use the response with the highest average similarity to other responses similarities = [] for i, resp_i in enumerate(valid_responses): sim_sum = 0 for j, resp_j in enumerate(valid_responses): if i != j: sim = self.calculate_similarity_static(resp_i, resp_j) sim_sum += sim avg_sim = sim_sum / (len(valid_responses) - 1) if len(valid_responses) > 1 else 0 similarities.append((resp_i, avg_sim)) # Sort responses by average similarity in descending order similarities.sort(key=lambda x: x[1], reverse=True) # Choose the response that is most similar to others (consensus) most_common_response = similarities[0][0] return most_common_response.strip() def save_chat_history(self): file_path, _ = QFileDialog.getSaveFileName( self, "Save Chat History", "", "Text Files (*.txt);;All Files (*)" ) if file_path: with open(file_path, 'w') as f: f.write(self.chat_history.toPlainText()) self.statusbar.showMessage(f"Chat history saved to {file_path}") def clear_chat_history(self): self.chat_history.clear() self.statusbar.showMessage("Chat history cleared") def toggle_chat_dock(self, state): if state: self.chat_dock.show() else: self.chat_dock.hide() def open_preferences(self): dialog = QDialog(self) dialog.setWindowTitle("Preferences") layout = QFormLayout() # Example settings ai_model_label = QLabel("AI Model:") ai_model_combo = QComboBox() ai_model_combo.addItems(["gpt-3.5-turbo", "gpt-4"]) layout.addRow(ai_model_label, ai_model_combo) theme_label = QLabel("Theme:") theme_combo = QComboBox() theme_combo.addItems(["Light", "Dark"]) layout.addRow(theme_label, theme_combo) apply_button = QPushButton("Apply") apply_button.clicked.connect(dialog.accept) layout.addRow(apply_button) dialog.setLayout(layout) result = dialog.exec_() if result == QDialog.Accepted: # Apply settings as needed selected_model = ai_model_combo.currentText() selected_theme = theme_combo.currentText() self.apply_theme(selected_theme) self.statusbar.showMessage(f"Preferences updated: Model={selected_model}, Theme={selected_theme}") def apply_theme(self, theme): if theme == "Dark": self.setStyleSheet(""" QWidget { background-color: #2e2e2e; color: #f0f0f0; } QLineEdit, QTextEdit { background-color: #3e3e3e; color: #f0f0f0; } QPushButton { background-color: #5a5a5a; color: white; } """) else: self.apply_stylesheet() def show_about_dialog(self): QMessageBox.about(self, "About", "Advanced GPT Chatbot\nVersion 2.0") @staticmethod def new_chat_session(): timestamp = datetime.now().strftime("%Y%m%d_%H%M%S") return f"chat_session_{timestamp}.json" @staticmethod def load_history(filename): try: with open(filename, "r") as f: return json.load(f) except FileNotFoundError: return [] @staticmethod def save_history(messages, filename): with open(filename, "w") as f: json.dump(messages, f, indent=2) @staticmethod def get_response(messages, providers): for provider in providers: try: client = Client(provider=provider) response = client.chat.completions.create( messages=messages, model="gpt-4", # Ensure the model name is correct ) return response.choices[0].message.content except Exception as e: print(f"Error with {provider.__name__}: {e}") return "Sorry, I'm having trouble connecting to the AI providers at the moment." if __name__ == "__main__": app = QApplication(sys.argv) window = ChatbotGUI() window.show() sys.exit(app.exec_()) - Assistant