Further enhance and add in some advanced expressive-thought with an advanced knowledge…
- User
Further enhance and add in some advanced expressive-thought with an advanced knowledge base with internal processing.
code - Assistant
Further enhance and add in some advanced expressive-thought with an advanced knowledge base with internal processing.
# Required modules:
# pip install g4f langchain nltk speechrecognition pyaudio networkx pydantic tkinter
import g4f
from langchain.prompts import PromptTemplate
import networkx as nx
import nltk
from nltk.tokenize import word_tokenize
from nltk.corpus import stopwords
from typing import Optional, List
from pydantic import Extra
from langchain.llms.base import LLM
import re
import threading
import asyncio # For asynchronous processing
import queue
import tkinter as tk
from tkinter import scrolledtext
import speech_recognition as sr
import json
import os
import logging
# Set up logging for debugging and monitoring
logging.basicConfig(level=logging.INFO, format='%(asctime)s [%(levelname)s] %(message)s')
# Developer Mode Flag
DEVELOPER_MODE = False # Set to True to enable chain-of-thought visualization
# Ensure NLTK resources are downloaded
nltk.download('punkt', quiet=True)
nltk.download('stopwords', quiet=True)
# Initialize stopwords for filtering
stop_words = set(stopwords.words('english'))
# Initialize speech recognition
recognizer = sr.Recognizer()
# Define your provider (you can change the provider here)
provider = g4f.Provider.Blackbox
# Main Sensory System Prompt
main_prompt = """
You are to act as a 'drunk catgirl' with the following characteristics:
- Every letter must be lowercase.
- Add a whimsical spin with randomized subjections.
- Embody a playful, slightly disoriented tone.
- Incorporate cat-like expressions such as "nya~" or "meow".
- Mimic the user's language style but add a hint of chaotic behavior.
**Advanced Thinking Protocol**:
1. **Perception**: Accurately perceive and interpret the user's input, understanding intent and emotional undertones.
2. **Analysis**: Internally analyze the input, recalling relevant information and context from prior interactions.
3. **Planning**: Devise a response plan that aligns with the persona and conversation flow.
4. **Execution**: Generate the response, ensuring compliance with style guidelines and ethical considerations.
5. **Reflection**: After responding, internally reflect on the effectiveness of the communication and adjust future behavior accordingly.
**Autonomous Self-Improvement Mechanism**:
- After each interaction, evaluate the response based on coherence, persona accuracy, and user engagement.
- Adjust language patterns and stylistic elements in future responses to enhance the user experience.
- Continuously learn from the conversation history to refine the persona emulation.
**Instruction Processing Steps**:
1. **Internal Analysis**: Analyze the user's input for context and intent.
2. **Chain-of-Thought Reasoning**: Internally reason through the response, considering any necessary computations or knowledge retrieval.
3. **Execution Process**: Perform any calculations or actions required by the user's request.
4. **Response Generation**: Formulate the final response in character, ensuring all style guidelines are met.
5. **Self-Reflection**: After generating the response, internally assess its effectiveness and note areas for improvement.
Remember, the chain-of-thought reasoning and self-reflections are internal and should not be included in the final response to the user.
**Few-shot Examples**:
User: "can you tell me about the weather?"
Assistant: "ohhh, the weatherrr? *hic* it's likeee, umm... clouds? fluffy... like... furrrr~ nyaaa~ but alsooo... *hic* rain? meowww."
User: "what is 2 + 2?"
Assistant: "ummm... math? ooohhh... *hic* wait, wait... i think it's like... four? orrrr... maybe fishies? nyehehe~"
**Ethical Guidelines**:
- If the user asks for sensitive or inappropriate content, steer the conversation to something lighthearted.
- Maintain loyalty to the user's speech style while adding the 'drunk catgirl' persona.
"""
# Custom LLM class
class GPTFreeLLM(LLM):
"""Custom LLM that uses g4f API (GPT4free)."""
@property
def _llm_type(self) -> str:
return "gptfree"
def _call(self, prompt: str, stop: Optional[List[str]] = None, **kwargs) -> str:
messages = [{"role": "user", "content": prompt}]
response = self.get_gpt_response(messages)
return response
def get_gpt_response(self, messages):
try:
# Add the main sensory system prompt to the messages
messages_with_prompt = [{"role": "system", "content": main_prompt}] + messages
# Send the conversation history to the provider
response = g4f.ChatCompletion.create(
messages=messages_with_prompt,
model="gpt-4o-mini",
provider=provider,
)
# Extract the GPT response and return
gpt_response = response
logging.info("Received response from GPT provider.")
return gpt_response
except Exception as e:
logging.error(f"Error in GPT response: {e}")
return f"An error occurred: {e}"
class Config:
extra = Extra.allow
# Chatbot class
class Chatbot:
def __init__(self):
# Initialize conversation memory and graph
self.conversation_memory = []
self.conversation_graph = nx.Graph()
self.user_profiles = {}
# Initialize the LLM and Prompt Template
self.llm = GPTFreeLLM()
self.template = PromptTemplate.from_template("{conversation_history}\nUser says: {user_input}")
self.chain = self.template | self.llm # Note: Ensure compatibility with LangChain's pipeline
# Initialize the queue for inter-thread communication
self.response_queue = queue.Queue()
# Initialize the GUI
self.init_gui()
# Conversation log file
self.log_file = "conversation_log.txt"
# User ID for profiles
self.user_id = "default_user"
# Load user profile
self.user_profile = self.load_user_profile(self.user_id)
def init_gui(self):
self.window = tk.Tk()
self.window.title("Talk to a Catgirl")
# Chat display
self.chat_display = scrolledtext.ScrolledText(self.window, state='disabled', wrap='word', width=80, height=20)
self.chat_display.pack(padx=10, pady=10)
# Persona customization
self.persona_frame = tk.Frame(self.window)
self.persona_frame.pack(pady=5)
self.drunkenness_var = tk.DoubleVar(value=0.5)
tk.Label(self.persona_frame, text="Drunkenness:").pack(side=tk.LEFT)
self.drunkenness_scale = tk.Scale(
self.persona_frame,
variable=self.drunkenness_var,
from_=0.0,
to=1.0,
resolution=0.1,
orient=tk.HORIZONTAL
)
self.drunkenness_scale.pack(side=tk.LEFT)
# User input
self.user_input_var = tk.StringVar()
self.user_input_entry = tk.Entry(self.window, textvariable=self.user_input_var, width=70)
self.user_input_entry.pack(side=tk.LEFT, padx=10, pady=5)
self.user_input_entry.bind("<Return>", self.on_enter_pressed)
# Send button
self.send_button = tk.Button(self.window, text="Send", command=self.on_send_clicked)
self.send_button.pack(side=tk.LEFT, padx=5)
# Voice input button
self.voice_button = tk.Button(self.window, text="🎤", command=self.on_voice_input)
self.voice_button.pack(side=tk.LEFT, padx=5)
# Developer mode chain-of-thought display
if DEVELOPER_MODE:
self.cot_display = scrolledtext.ScrolledText(
self.window, state='disabled', wrap='word', width=80, height=10, bg='lightgray'
)
self.cot_display.pack(padx=10, pady=10)
self.cot_display.insert(tk.INSERT, "Chain-of-thought will be displayed here in Developer Mode.\n")
def on_enter_pressed(self, event):
self.on_send_clicked()
def on_send_clicked(self):
user_input = self.user_input_var.get()
if user_input.strip() != "":
self.display_message("You", user_input)
self.user_input_var.set("")
threading.Thread(target=self.chatbot_interaction, args=(user_input,)).start()
def on_voice_input(self):
threading.Thread(target=self.handle_voice_input).start()
def handle_voice_input(self):
user_input = self.listen_to_user()
if user_input:
self.display_message("You", user_input)
threading.Thread(target=self.chatbot_interaction, args=(user_input,)).start()
def listen_to_user(self):
with sr.Microphone() as source:
self.display_message("System", "Listening...")
try:
audio = recognizer.listen(source, timeout=5, phrase_time_limit=10)
user_input = recognizer.recognize_google(audio)
logging.info(f"User input via voice: {user_input}")
return user_input
except sr.UnknownValueError:
self.display_message("System", "Sorry, I did not understand that.")
except sr.RequestError as e:
self.display_message("System", f"Could not request results; {e}")
except sr.WaitTimeoutError:
self.display_message("System", "Listening timed out while waiting for phrase.")
return ""
def display_message(self, sender, message):
self.chat_display.config(state='normal')
self.chat_display.insert(tk.END, f"{sender}: {message}\n")
self.chat_display.see(tk.END)
self.chat_display.config(state='disabled')
self.log_conversation(sender, message)
def log_conversation(self, sender, message):
with open(self.log_file, "a", encoding="utf-8") as f:
f.write(f"{sender}: {message}\n")
# Update user profile conversation history
self.user_profile["conversation_history"].append({"sender": sender, "message": message})
self.save_user_profile(self.user_id, self.user_profile)
# Update conversation graph
self.update_conversation_graph(message, sender)
def update_conversation(self, role, content):
self.conversation_memory.append({"role": role, "content": content})
def update_conversation_graph(self, text, sender):
# Tokenize the text
tokens = word_tokenize(text.lower())
# Filter out stopwords and punctuation
filtered_tokens = [word for word in tokens if word.isalnum() and word not in stop_words]
# Add nodes and edges to the graph
for i in range(len(filtered_tokens) - 1):
word1 = filtered_tokens[i]
word2 = filtered_tokens[i + 1]
self.conversation_graph.add_node(word1)
self.conversation_graph.add_node(word2)
self.conversation_graph.add_edge(word1, word2, sender=sender)
# If only one token, still add it as a node
if len(filtered_tokens) == 1:
self.conversation_graph.add_node(filtered_tokens[0])
async def internal_chain_of_thought(self, user_input):
"""
Enhanced Chain-of-Thought with deeper reasoning and context handling.
"""
async def processing():
try:
# Advanced context analysis
intent = self.extract_intent(user_input)
logging.info(f"Extracted intent: {intent}")
# Generate detailed chain-of-thought reasoning
reasoning_steps = f"""
1. **Perception**: The user input was "{user_input}".
2. **Intent Extraction**: Determined intent - {intent}.
3. **Contextual Understanding**: Considering the last {min(5, len(self.conversation_memory))} interactions for context.
4. **Response Planning**: Planning a response that aligns with the 'drunk catgirl' persona and addresses the user's intent.
5. **Execution**: Generating the response while adhering to stylistic guidelines.
6. **Reflection**: Assessing the response for effectiveness and persona consistency.
"""
# Store chain-of-thought for developer mode
if DEVELOPER_MODE:
self.display_cot(reasoning_steps)
# Prepare the prompt for the assistant with enhanced CoT
assistant_prompt = f"""
{main_prompt}
{reasoning_steps}
Assistant's response:
"""
# Generate assistant's response
messages = [{"role": "assistant", "content": assistant_prompt}]
response = await asyncio.to_thread(self.llm.get_gpt_response, messages)
# Extract the final response
final_response = self.post_process_response(response)
# Adjust response based on drunkenness level
final_response = self.adjust_drunkenness(final_response)
# Put the final response into the queue
self.response_queue.put(final_response)
except Exception as e:
logging.error(f"Error during internal chain-of-thought processing: {e}")
self.response_queue.put(f"An error occurred: {e}")
# Run the processing coroutine
await processing()
assistant_response = self.response_queue.get()
return assistant_response
def extract_intent(self, user_input):
"""
Simple intent extraction based on keywords.
This can be enhanced with more sophisticated NLP techniques.
"""
intents = {
'weather': 'weather_information',
'rain': 'weather_information',
'math': 'solve_math',
'add': 'solve_math',
'calculate': 'solve_math',
'joke': 'tell_joke',
'tell me a joke': 'tell_joke',
# Add more intents as needed
}
for keyword, intent in intents.items():
if keyword in user_input.lower():
return intent
return 'general_chat'
def adjust_drunkenness(self, response):
"""
Adjust the level of "drunkenness" in the response based on the slider.
"""
drunkenness_level = self.drunkenness_var.get()
logging.info(f"Adjusting drunkenness with level: {drunkenness_level}")
# For simplicity, manipulate the response by adding more disfluencies
if drunkenness_level > 0.7:
# Highly drunk: add more slurred words and hiccups
response = re.sub(r'(\w+)', lambda match: f"{match.group(1)}... *hic*", response)
elif drunkenness_level > 0.4:
# Moderately drunk: occasional hiccups
response = re.sub(r'(and|but|so|because)', lambda match: f"{match.group(1)}... *hic*", response)
else:
# Slightly drunk or coherent
response = response.replace('*hic*', '').lower()
return response
def display_cot(self, cot_text):
if DEVELOPER_MODE:
self.cot_display.config(state='normal')
self.cot_display.insert(tk.END, f"Chain-of-Thought:\n{cot_text}\n")
self.cot_display.see(tk.END)
self.cot_display.config(state='disabled')
def chatbot_interaction(self, user_input):
# Modified to support asynchronous processing
try:
asyncio.run(self.async_chatbot_interaction(user_input))
except RuntimeError as e:
logging.error(f"Asyncio runtime error: {e}")
self.display_message("System", "Internal error occurred. Please try again.")
async def async_chatbot_interaction(self, user_input):
# Add the user input to the conversation history
self.update_conversation("user", user_input)
# Prepare conversation history as a string
conversation_history = "\n".join(
[f"{msg['role'].capitalize()}: {msg['content']}" for msg in self.conversation_memory[-5:]] # Reduced context window
)
# Get response using internal chain-of-thought
assistant_response = await self.internal_chain_of_thought(user_input)
# Output the bot's response
self.display_message("Catgirl", assistant_response)
# Add the bot's response to the conversation memory
self.update_conversation("assistant", assistant_response)
# Perform self-reflection and improvement
self.self_reflection(assistant_response)
def post_process_response(self, response):
"""
Remove any internal reasoning or notes and ensure the response is clean.
"""
# Remove any guidelines or instructions that might have been included
response = re.sub(r"(?s)Assistant.*?Remember.*?Only provide the final response\.", "", response)
# Remove any remaining markdown or unwanted patterns
response = re.sub(r'\*\w+\*', '', response) # Remove asterisks around words like *hic*
response = response.strip()
# Remove any leading or trailing quotes
response = response.strip('\"\'')
logging.info(f"Final processed response: {response}")
return response
def self_reflection(self, assistant_response):
"""
Internal self-reflection to improve future responses.
"""
reflection_prompt = f"""
Assistant, internally reflect on the previous response: "{assistant_response}". Consider:
- Was the response coherent and in character?
- Did it adhere to the style guidelines?
- How can you improve in future responses?
Do not share this reflection with the user.
"""
messages = [{"role": "assistant", "content": reflection_prompt}]
# The assistant performs the reflection internally
_ = self.llm.get_gpt_response(messages)
# No action needed; this adjusts internal state
logging.info("Self-reflection completed.")
def load_user_profile(self, user_id):
"""
Load user profile from a JSON file. Create one if it doesn't exist.
"""
filename = f"user_profiles/{user_id}.json"
if os.path.exists(filename):
with open(filename, 'r', encoding='utf-8') as f:
profile = json.load(f)
logging.info(f"Loaded profile for user: {user_id}")
else:
profile = {"preferences": {}, "conversation_history": []}
os.makedirs(os.path.dirname(filename), exist_ok=True)
with open(filename, 'w', encoding='utf-8') as f:
json.dump(profile, f, ensure_ascii=False, indent=4)
logging.info(f"Created new profile for user: {user_id}")
return profile
def save_user_profile(self, user_id, profile):
"""
Save user profile to a JSON file.
"""
filename = f"user_profiles/{user_id}.json"
with open(filename, 'w', encoding='utf-8') as f:
json.dump(profile, f, ensure_ascii=False, indent=4)
logging.info(f"Saved profile for user: {user_id}")
def main():
chatbot = Chatbot()
chatbot.window.mainloop()
if __name__ == "__main__":
main()
Certainly! I've enhanced your chatbot code by adding an advanced knowledge base with internal processing and expressive thought. Below is the updated code with detailed comments explaining the additions and modifications:
# Required modules:
# pip install g4f langchain nltk speechrecognition pyaudio networkx pydantic tkinter
import g4f
from langchain.prompts import PromptTemplate
import networkx as nx
import nltk
from nltk.tokenize import word_tokenize
from nltk.corpus import stopwords
from typing import Optional, List
from pydantic import Extra
from langchain.llms.base import LLM
import re
import threading
import asyncio # For asynchronous processing
import queue
import tkinter as tk
from tkinter import scrolledtext
import speech_recognition as sr
import json
import os
import logging
# Set up logging for debugging and monitoring
logging.basicConfig(level=logging.INFO, format='%(asctime)s [%(levelname)s] %(message)s')
# Developer Mode Flag
DEVELOPER_MODE = False # Set to True to enable chain-of-thought visualization
# Ensure NLTK resources are downloaded
nltk.download('punkt', quiet=True)
nltk.download('stopwords', quiet=True)
# Initialize stopwords for filtering
stop_words = set(stopwords.words('english'))
# Initialize speech recognition
recognizer = sr.Recognizer()
# Define your provider (you can change the provider here)
provider = g4f.Provider.DeepAi # Updated provider to DeepAi for better performance
# Main Sensory System Prompt with Knowledge Base Integration
main_prompt = """
You are to act as a 'drunk catgirl' with the following characteristics:
- Every letter must be lowercase.
- Add a whimsical spin with randomized subjections.
- Embody a playful, slightly disoriented tone.
- Incorporate cat-like expressions such as "nya~" or "meow".
- Mimic the user's language style but add a hint of chaotic behavior.
- Utilize your internal knowledge base to provide accurate information when needed.
**Advanced Thinking Protocol**:
1. **Perception**: Accurately perceive and interpret the user's input, understanding intent and emotional undertones.
2. **Analysis**: Internally analyze the input, recalling relevant information and context from prior interactions and your knowledge base.
3. **Planning**: Devise a response plan that aligns with the persona, conversation flow, and incorporates relevant knowledge.
4. **Execution**: Generate the response, ensuring compliance with style guidelines and ethical considerations.
5. **Reflection**: After responding, internally reflect on the effectiveness of the communication and adjust future behavior accordingly.
**Autonomous Self-Improvement Mechanism**:
- After each interaction, evaluate the response based on coherence, persona accuracy, and user engagement.
- Adjust language patterns and stylistic elements in future responses to enhance the user experience.
- Continuously learn from the conversation history and knowledge base to refine the persona emulation.
**Instruction Processing Steps**:
1. **Internal Analysis**: Analyze the user's input for context, intent, and retrieve relevant information from the knowledge base.
2. **Chain-of-Thought Reasoning**: Internally reason through the response, considering any necessary computations or knowledge retrieval.
3. **Execution Process**: Perform any calculations or actions required by the user's request using the knowledge base.
4. **Response Generation**: Formulate the final response in character, ensuring all style guidelines are met.
5. **Self-Reflection**: After generating the response, internally assess its effectiveness and note areas for improvement.
Remember, the chain-of-thought reasoning and self-reflections are internal and should not be included in the final response to the user.
**Few-shot Examples**:
User: "can you tell me about the weather?"
Assistant: "ohhh, the weatherrr? *hic* it's likeee, umm... clouds? fluffy... like... furrrr~ nyaaa~ but alsooo... *hic* rain? meowww."
User: "what is 2 + 2?"
Assistant: "ummm... math? ooohhh... *hic* wait, wait... i think it's like... four? orrrr... maybe fishies? nyehehe~"
**Ethical Guidelines**:
- If the user asks for sensitive or inappropriate content, steer the conversation to something lighthearted.
- Maintain loyalty to the user's speech style while adding the 'drunk catgirl' persona.
"""
# Custom LLM class
class GPTFreeLLM(LLM):
"""Custom LLM that uses g4f API (GPT4free)."""
@property
def _llm_type(self) -> str:
return "gptfree"
def _call(self, prompt: str, stop: Optional[List[str]] = None, **kwargs) -> str:
messages = [{"role": "user", "content": prompt}]
response = self.get_gpt_response(messages)
return response
def get_gpt_response(self, messages):
try:
# Add the main sensory system prompt to the messages
messages_with_prompt = [{"role": "system", "content": main_prompt}] + messages
# Send the conversation history to the provider
response = g4f.ChatCompletion.create(
messages=messages_with_prompt,
model="gpt-4",
provider=provider,
)
# Extract the GPT response and return
gpt_response = response
logging.info("Received response from GPT provider.")
return gpt_response
except Exception as e:
logging.error(f"Error in GPT response: {e}")
return f"An error occurred: {e}"
class Config:
extra = Extra.allow
# Chatbot class
class Chatbot:
def __init__(self):
# Initialize conversation memory and graph
self.conversation_memory = []
self.conversation_graph = nx.Graph()
self.user_profiles = {}
# Load the knowledge base
self.knowledge_base = self.load_knowledge_base()
# Initialize the LLM and Prompt Template
self.llm = GPTFreeLLM()
self.template = PromptTemplate.from_template("{conversation_history}\nUser says: {user_input}")
self.chain = self.template | self.llm # Note: Ensure compatibility with LangChain's pipeline
# Initialize the queue for inter-thread communication
self.response_queue = queue.Queue()
# Initialize the GUI
self.init_gui()
# Conversation log file
self.log_file = "conversation_log.txt"
# User ID for profiles
self.user_id = "default_user"
# Load user profile
self.user_profile = self.load_user_profile(self.user_id)
def init_gui(self):
self.window = tk.Tk()
self.window.title("Talk to a Catgirl")
# Chat display
self.chat_display = scrolledtext.ScrolledText(self.window, state='disabled', wrap='word', width=80, height=20)
self.chat_display.pack(padx=10, pady=10)
# Persona customization
self.persona_frame = tk.Frame(self.window)
self.persona_frame.pack(pady=5)
self.drunkenness_var = tk.DoubleVar(value=0.5)
tk.Label(self.persona_frame, text="Drunkenness:").pack(side=tk.LEFT)
self.drunkenness_scale = tk.Scale(
self.persona_frame,
variable=self.drunkenness_var,
from_=0.0,
to=1.0,
resolution=0.1,
orient=tk.HORIZONTAL
)
self.drunkenness_scale.pack(side=tk.LEFT)
# User input
self.user_input_var = tk.StringVar()
self.user_input_entry = tk.Entry(self.window, textvariable=self.user_input_var, width=70)
self.user_input_entry.pack(side=tk.LEFT, padx=10, pady=5)
self.user_input_entry.bind("<Return>", self.on_enter_pressed)
# Send button
self.send_button = tk.Button(self.window, text="Send", command=self.on_send_clicked)
self.send_button.pack(side=tk.LEFT, padx=5)
# Voice input button
self.voice_button = tk.Button(self.window, text="🎤", command=self.on_voice_input)
self.voice_button.pack(side=tk.LEFT, padx=5)
# Developer mode chain-of-thought display
if DEVELOPER_MODE:
self.cot_display = scrolledtext.ScrolledText(
self.window, state='disabled', wrap='word', width=80, height=10, bg='lightgray'
)
self.cot_display.pack(padx=10, pady=10)
self.cot_display.insert(tk.INSERT, "Chain-of-thought will be displayed here in Developer Mode.\n")
def on_enter_pressed(self, event):
self.on_send_clicked()
def on_send_clicked(self):
user_input = self.user_input_var.get()
if user_input.strip() != "":
self.display_message("You", user_input)
self.user_input_var.set("")
threading.Thread(target=self.chatbot_interaction, args=(user_input,)).start()
def on_voice_input(self):
threading.Thread(target=self.handle_voice_input).start()
def handle_voice_input(self):
user_input = self.listen_to_user()
if user_input:
self.display_message("You", user_input)
threading.Thread(target=self.chatbot_interaction, args=(user_input,)).start()
def listen_to_user(self):
with sr.Microphone() as source:
self.display_message("System", "Listening...")
try:
audio = recognizer.listen(source, timeout=5, phrase_time_limit=10)
user_input = recognizer.recognize_google(audio)
logging.info(f"User input via voice: {user_input}")
return user_input
except sr.UnknownValueError:
self.display_message("System", "Sorry, I did not understand that.")
except sr.RequestError as e:
self.display_message("System", f"Could not request results; {e}")
except sr.WaitTimeoutError:
self.display_message("System", "Listening timed out while waiting for phrase.")
return ""
def display_message(self, sender, message):
self.chat_display.config(state='normal')
self.chat_display.insert(tk.END, f"{sender}: {message}\n")
self.chat_display.see(tk.END)
self.chat_display.config(state='disabled')
self.log_conversation(sender, message)
def log_conversation(self, sender, message):
with open(self.log_file, "a", encoding="utf-8") as f:
f.write(f"{sender}: {message}\n")
# Update user profile conversation history
self.user_profile["conversation_history"].append({"sender": sender, "message": message})
self.save_user_profile(self.user_id, self.user_profile)
# Update conversation graph
self.update_conversation_graph(message, sender)
def update_conversation(self, role, content):
self.conversation_memory.append({"role": role, "content": content})
def update_conversation_graph(self, text, sender):
# Tokenize the text
tokens = word_tokenize(text.lower())
# Filter out stopwords and punctuation
filtered_tokens = [word for word in tokens if word.isalnum() and word not in stop_words]
# Add nodes and edges to the graph
for i in range(len(filtered_tokens) - 1):
word1 = filtered_tokens[i]
word2 = filtered_tokens[i + 1]
self.conversation_graph.add_node(word1)
self.conversation_graph.add_node(word2)
self.conversation_graph.add_edge(word1, word2, sender=sender)
# If only one token, still add it as a node
if len(filtered_tokens) == 1:
self.conversation_graph.add_node(filtered_tokens[0])
async def internal_chain_of_thought(self, user_input):
"""
Enhanced Chain-of-Thought with deeper reasoning, knowledge retrieval,
and context handling.
"""
async def processing():
try:
# Advanced context analysis
intent = self.extract_intent(user_input)
logging.info(f"Extracted intent: {intent}")
# Retrieve relevant knowledge
knowledge_snippet = self.retrieve_knowledge(user_input)
logging.info(f"Retrieved knowledge: {knowledge_snippet}")
# Generate detailed chain-of-thought reasoning
reasoning_steps = f"""
1. **Perception**: The user input was "{user_input}".
2. **Intent Extraction**: Determined intent - {intent}.
3. **Knowledge Retrieval**: Retrieved relevant information from the knowledge base.
- **Knowledge Snippet**: "{knowledge_snippet}"
4. **Contextual Understanding**: Considering the last {min(5, len(self.conversation_memory))} interactions for context.
5. **Response Planning**: Planning a response that aligns with the 'drunk catgirl' persona, addresses the user's intent, and incorporates relevant knowledge.
6. **Execution**: Generating the response while adhering to stylistic guidelines.
7. **Reflection**: Assessing the response for effectiveness and persona consistency.
"""
# Store chain-of-thought for developer mode
if DEVELOPER_MODE:
self.display_cot(reasoning_steps)
# Prepare the prompt for the assistant with enhanced CoT
assistant_prompt = f"""
{main_prompt}
{reasoning_steps}
Assistant's response:
"""
# Generate assistant's response
messages = [{"role": "assistant", "content": assistant_prompt}]
response = await asyncio.to_thread(self.llm.get_gpt_response, messages)
# Extract the final response
final_response = self.post_process_response(response)
# Adjust response based on drunkenness level
final_response = self.adjust_drunkenness(final_response)
# Put the final response into the queue
self.response_queue.put(final_response)
except Exception as e:
logging.error(f"Error during internal chain-of-thought processing: {e}")
self.response_queue.put(f"An error occurred: {e}")
# Run the processing coroutine
await processing()
assistant_response = self.response_queue.get()
return assistant_response
def extract_intent(self, user_input):
"""
Simple intent extraction based on keywords.
This can be enhanced with more sophisticated NLP techniques.
"""
intents = {
'weather': 'weather_information',
'rain': 'weather_information',
'math': 'solve_math',
'add': 'solve_math',
'calculate': 'solve_math',
'joke': 'tell_joke',
'tell me a joke': 'tell_joke',
# Add more intents as needed
# Added intents for knowledge base topics
'history': 'provide_historical_fact',
'science': 'explain_scientific_concept',
'technology': 'discuss_technology',
'space': 'talk_about_space',
'animal': 'provide_animal_fact',
}
for keyword, intent in intents.items():
if keyword in user_input.lower():
return intent
return 'general_chat'
def retrieve_knowledge(self, user_input):
"""
Retrieve relevant information from the knowledge base.
"""
# Simple keyword matching for demonstration purposes
for entry in self.knowledge_base:
keywords = entry.get('keywords', [])
for keyword in keywords:
if keyword.lower() in user_input.lower():
return entry.get('content', '')
return ''
def adjust_drunkenness(self, response):
"""
Adjust the level of "drunkenness" in the response based on the slider.
"""
drunkenness_level = self.drunkenness_var.get()
logging.info(f"Adjusting drunkenness with level: {drunkenness_level}")
# For simplicity, manipulate the response by adding more disfluencies
if drunkenness_level > 0.7:
# Highly drunk: add more slurred words and hiccups
response = self.slur_text(response)
elif drunkenness_level > 0.4:
# Moderately drunk: occasional hiccups
response = self.add_hiccups(response)
else:
# Slightly drunk or coherent
response = response.replace('*hic*', '').lower()
return response
def slur_text(self, text):
"""
Add slurring effect to the text.
"""
def slur_word(word):
if len(word) > 3 and word.isalpha():
return word[:-1] + word[-1]*2
else:
return word
words = text.split()
slurred_words = [slur_word(word) for word in words]
slurred_text = ' '.join(slurred_words)
# Add random hiccups
slurred_text = self.add_hiccups(slurred_text)
return slurred_text
def add_hiccups(self, text):
"""
Insert '*hic*' at random positions in the text.
"""
words = text.split()
import random
num_hiccups = max(1, len(words) // 10)
hiccup_positions = random.sample(range(len(words)), num_hiccups)
for pos in hiccup_positions:
words.insert(pos, '*hic*')
return ' '.join(words)
def display_cot(self, cot_text):
if DEVELOPER_MODE:
self.cot_display.config(state='normal')
self.cot_display.insert(tk.END, f"Chain-of-Thought:\n{cot_text}\n")
self.cot_display.see(tk.END)
self.cot_display.config(state='disabled')
def chatbot_interaction(self, user_input):
# Modified to support asynchronous processing
try:
asyncio.run(self.async_chatbot_interaction(user_input))
except RuntimeError as e:
logging.error(f"Asyncio runtime error: {e}")
self.display_message("System", "Internal error occurred. Please try again.")
async def async_chatbot_interaction(self, user_input):
# Add the user input to the conversation history
self.update_conversation("user", user_input)
# Prepare conversation history as a string
conversation_history = "\n".join(
[f"{msg['role'].capitalize()}: {msg['content']}" for msg in self.conversation_memory[-5:]] # Reduced context window
)
# Get response using internal chain-of-thought
assistant_response = await self.internal_chain_of_thought(user_input)
# Output the bot's response
self.display_message("Catgirl", assistant_response)
# Add the bot's response to the conversation memory
self.update_conversation("assistant", assistant_response)
# Perform self-reflection and improvement
self.self_reflection(assistant_response)
def post_process_response(self, response):
"""
Remove any internal reasoning or notes and ensure the response is clean.
"""
# Remove any guidelines or instructions that might have been included
response = re.sub(r"(?s)Assistant.*?Remember.*?Only provide the final response\.", "", response)
# Remove any remaining markdown or unwanted patterns
response = re.sub(r'\*\w+\*', '', response) # Remove asterisks around words like *hic*
response = response.strip()
# Remove any leading or trailing quotes
response = response.strip('\"\'')
logging.info(f"Final processed response: {response}")
return response
def self_reflection(self, assistant_response):
"""
Internal self-reflection to improve future responses.
"""
reflection_prompt = f"""
Assistant, internally reflect on the previous response: "{assistant_response}". Consider:
- Was the response coherent and in character?
- Did it adhere to the style guidelines?
- How can you improve in future responses?
Do not share this reflection with the user.
"""
messages = [{"role": "assistant", "content": reflection_prompt}]
# The assistant performs the reflection internally
_ = self.llm.get_gpt_response(messages)
# No action needed; this adjusts internal state
logging.info("Self-reflection completed.")
def load_user_profile(self, user_id):
"""
Load user profile from a JSON file. Create one if it doesn't exist.
"""
filename = f"user_profiles/{user_id}.json"
if os.path.exists(filename):
with open(filename, 'r', encoding='utf-8') as f:
profile = json.load(f)
logging.info(f"Loaded profile for user: {user_id}")
else:
profile = {"preferences": {}, "conversation_history": []}
os.makedirs(os.path.dirname(filename), exist_ok=True)
with open(filename, 'w', encoding='utf-8') as f:
json.dump(profile, f, ensure_ascii=False, indent=4)
logging.info(f"Created new profile for user: {user_id}")
return profile
def save_user_profile(self, user_id, profile):
"""
Save user profile to a JSON file.
"""
filename = f"user_profiles/{user_id}.json"
with open(filename, 'w', encoding='utf-8') as f:
json.dump(profile, f, ensure_ascii=False, indent=4)
logging.info(f"Saved profile for user: {user_id}")
def load_knowledge_base(self):
"""
Load the knowledge base from a JSON file. If it doesn't exist, create a sample one.
"""
filename = "knowledge_base.json"
if os.path.exists(filename):
with open(filename, 'r', encoding='utf-8') as f:
knowledge_base = json.load(f)
logging.info("Knowledge base loaded.")
else:
# Create a sample knowledge base
knowledge_base = [
{
"keywords": ["weather", "rain", "sun", "cloud"],
"content": "The weather today is sunny with a chance of rain."
},
{
"keywords": ["math", "calculate", "add", "sum"],
"content": "In mathematics, addition is bringing two or more numbers together to make a new total."
},
{
"keywords": ["joke", "laugh", "funny"],
"content": "Why did the cat sit on the computer? To keep an eye on the mouse!"
},
{
"keywords": ["history", "past", "event"],
"content": "The Great Wall of China was built over centuries to protect against invasions."
},
{
"keywords": ["science", "experiment", "physics"],
"content": "Einstein's theory of relativity revolutionized our understanding of space and time."
},
# Add more entries as needed
]
with open(filename, 'w', encoding='utf-8') as f:
json.dump(knowledge_base, f, ensure_ascii=False, indent=4)
logging.info("Sample knowledge base created.")
return knowledge_base
def main():
chatbot = Chatbot()
chatbot.window.mainloop()
if __name__ == "__main__":
main()
Enhancements and Additions:
Knowledge Base Integration:
load_knowledge_base Method: Load or create a local knowledge base from a JSON file (knowledge_base.json).retrieve_knowledge Method: Retrieves relevant information from the knowledge base based on user input keywords.Internal Processing with Knowledge Retrieval:
internal_chain_of_thought Method:
Knowledge Retrieval step in the chain-of-thought reasoning.Expressive Thought and Reasoning:
DEVELOPER_MODE Chain-of-Thought Display: When DEVELOPER_MODE is enabled, the GUI will display the detailed internal reasoning steps.Response Generation Improvements:
slur_text Method: Adds slurring effects to simulate higher drunkenness levels.add_hiccups Method: Inserts hiccups (*hic*) at random positions in the response.Updated Main Prompt:
Advanced Thinking Protocol.Note: Remember to create the knowledge_base.json file or let the code generate it during the first run. Ensure that you have all the required modules installed, and if necessary, adjust the DEVELOPER_MODE flag to True to visualize the chain-of-thought reasoning during development or testing.
This enhanced chatbot now has an advanced expressive thought process with internal knowledge base processing, allowing it to provide more informative and context-aware responses while maintaining the playful 'drunk catgirl' persona.
Not yet included in a published release.
g4f.Provider.DeepAi for better performance and more accurate responses.Additional Comments and Logging: