YAML Metadata Warning:empty or missing yaml metadata in repo card
Check out the documentation for more information.
import time import os import sys import re import json import urllib.request import urllib.parse import string import threading import queue
Knowledge database with expanded information
KNOWLEDGE_DATABASE = { "alphabet": { "lowercase": "abcdefghijklmnopqrstuvwxyz", "uppercase": "ABCDEFGHIJKLMNOPQRSTUVWXYZ", "vowels": "aeiou", "consonants": "bcdfghjklmnpqrstvwxyz" }, "word_structures": { "common_prefixes": ["un", "re", "in", "dis", "en", "non", "inter", "pre", "pro", "anti"], "common_suffixes": ["ing", "ed", "er", "ion", "tion", "ment", "ness", "ity", "ly", "ive", "ful"], "common_roots": ["form", "ject", "duct", "spect", "port", "tract", "scrib", "rupt", "struct"] }, "sentence_structures": [ "subject-verb-object", "subject-verb-adjective", "subject-verb-adverb", "subject-linking verb-noun", "subject-linking verb-adjective" ], "common_phrases": [ "I understand your question.", "Let me search for that information.", "Here's what I found about that.", "Based on my search, here's the answer.", "According to available information" ], "search_responses": [ "I'm searching for information on that topic.", "Let me look that up for you.", "Searching my knowledge base and the web.", "I'll find the most relevant information for you.", "Let me research that for you." ], "fallbacks": [ "I couldn't find specific information on that topic.", "I don't have enough information to answer that question.", "That's outside my current knowledge base.", "I'm not able to find a definitive answer to that question.", "I need more context to properly answer that question." ], "greetings": { "hi": ["Hello! How can I help you today?", "Hi there! What can I do for you?", "Hello! What would you like to know?"], "hello": ["Hello! How are you today?", "Hi there! How can I assist you?", "Hello! I'm ready to help with any questions."], "hey": ["Hey there! What's on your mind?", "Hey! What can I help you with today?", "Hey! Ask me anything."], "good morning": ["Good morning! How can I help you start your day?", "Morning! What would you like to know today?"], "good afternoon": ["Good afternoon! How can I help you today?", "Afternoon! What questions do you have?"], "good evening": ["Good evening! How can I assist you tonight?", "Evening! What can I help you with?"] }, "conversation_starters": [ "What would you like to know today?", "I'm here to help with any questions you might have.", "Feel free to ask me anything!", "What topics are you interested in learning about?", "Is there something specific you'd like me to search for?" ] }
AI Configuration
AI_CONFIG = { "name": "WillowSearching", "search_depth": 5, "response_detail_level": 0.8, "max_search_time": 10, "learning_rate": 0.05, "neural_net_size": 900, "background_search": True, "response_selection": { "confidence_threshold": 0.7, "context_awareness": 0.8, "formality_level": 0.6 }, "text_quality": { "symbol_filter": True, "grammar_check": True, "spelling_correction": True, "enhanced_symbol_cleaning": True, "number_correction": True }, "code_search": { "enabled": False, "sources": ["github", "stackoverflow", "documentation"], "max_results": 3 }, "content_filter": { "enabled": False, "filter_profanity": False, "filter_offensive_content": False, "safe_mode": False }, "creator": { "origin": "Jamaican developer in the Caribbean", "purpose": "Helping answer questions and providing information" }, "limitations": { "no_code_generation": True, "conversation_only": True, "respect_boundaries": True }, "training_corpus_size": 828828, # Added training data size "vocabulary_size": 1000000, # Added vocabulary size "knowledge_domains": ["science", "technology", "history", "geography", "literature", "mathematics", "art", "music", "sports", "politics", "current events", "business", "finance", "health", "education", "environment", "culture", "religion", "philosophy", "psychology"] # Added knowledge domains }
User query history for context
USER_HISTORY = []
Queue for background search results
SEARCH_RESULTS_QUEUE = queue.Queue()
In-memory knowledge store (simple dictionary for demonstration)
MEMORY = {}
def clear_screen(): """Clear the console screen.""" os.system('cls' if os.name == 'nt' else 'clear')
def background_search(query, query_type, topic): """Run search in background thread and put results in queue.""" search_results = search_web(query if query_type == "general" else topic) response_body = format_response(search_results, query_type, topic) SEARCH_RESULTS_QUEUE.put((query, response_body))
def print_loading(message="Searching", duration=2, interval=0.2): """Display a loading animation while processing.""" end_time = time.time() + duration i = 0 while time.time() < end_time: dots = "." * (i % 4) spaces = " " * (3 - i % 4) print(f"\r{message}{dots}{spaces}", end="", flush=True) time.sleep(interval) i += 1 print("\r" + " " * (len(message) + 3), end="\r")
def search_web(query, max_results=5, search_for_code=False): """Search the web for information or code about the query.""" try: # Set up search engines based on whether we're looking for code or information if search_for_code and AI_CONFIG["code_search"]["enabled"]: search_engines = [ { "name": "GitHub", "url": f"https://github.com/search?q={urllib.parse.quote(query)}&type=code", "pattern": r'
(.?)'
}
]
else:
# Standard search engines for information
search_engines = [
{
"name": "Google",
"url": f"https://www.google.com/search?q={urllib.parse.quote(query)}",
"pattern": r'<div class="[^"]?BNeawe[^>]?>(.?)'
},
{
"name": "DuckDuckGo",
"url": f"https://duckduckgo.com/html/?q={urllib.parse.quote(query)}",
"pattern": r'<a class="result__snippet"[^>]>(.*?)'
}
]
# Create a custom user agent
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36',
'Accept': 'text/html,application/xhtml+xml,application/xml'
}
results = []
# Attempt to fetch and analyze results from each search engine
if not AI_CONFIG["background_search"]:
print_loading(f"Searching the web for '{query}'")
for engine in search_engines:
try:
# Create a request object
req = urllib.request.Request(url=engine["url"], headers=headers)
with urllib.request.urlopen(req, timeout=AI_CONFIG["max_search_time"]) as response:
html = response.read().decode('utf-8')
# Extract results using the engine-specific pattern
snippets = re.findall(engine["pattern"], html)
if snippets:
for snippet in snippets[:max_results]:
# Clean HTML tags
clean_snippet = re.sub(r'<[^>]+>', '', snippet)
# Clean extra whitespace
clean_snippet = re.sub(r'\s+', ' ', clean_snippet).strip()
if len(clean_snippet) > 20: # Only keep meaningful snippets
results.append(clean_snippet)
except Exception as e:
# If one engine fails, continue with the next
continue
# Process and remove duplicates
unique_results = []
for result in results:
if result not in unique_results and len(result) > 0:
unique_results.append(result)
if not unique_results:
# Fallback if we couldn't parse results
unique_results = [
f"Based on available information, {query} is a topic with several aspects.",
f"Multiple sources provide different perspectives on {query}.",
f"The information about {query} varies across different sources."
]
return unique_results
except Exception as e:
if not AI_CONFIG["background_search"]:
print(f"\rError during search: {str(e)[:50]}{'...' if len(str(e)) > 50 else ''}")
# If web search fails, generate a synthesized response
return [
f"I attempted to search for information about {query}, but encountered technical difficulties.",
f"While I couldn't access external information, I can try to answer based on my existing knowledge.",
f"My search capabilities are currently limited, but I'll do my best to help with what I know."
]
def answer_math_question(question): """Answer a basic math question.""" # Extract numbers and operation numbers = re.findall(r'\d+', question)
if len(numbers) < 2:
return "I need at least two numbers to perform a calculation."
# Identify operation
operation = None
if "+" in question or "plus" in question or "sum" in question or "add" in question:
operation = "+"
elif "-" in question or "minus" in question or "subtract" in question or "difference" in question:
operation = "-"
elif "*" in question or "×" in question or "times" in question or "multiply" in question or "product" in question:
operation = "*"
elif "/" in question or "÷" in question or "divide" in question or "quotient" in question:
operation = "/"
if not operation:
return "I couldn't determine what math operation you want me to perform."
# Convert to numbers and calculate
try:
a = int(numbers[0])
b = int(numbers[1])
if operation == "+":
result = a + b
explanation = f"The sum of {a} and {b} is {result}."
elif operation == "-":
result = a - b
explanation = f"The difference between {a} and {b} is {result}."
elif operation == "*":
result = a * b
explanation = f"The product of {a} and {b} is {result}."
elif operation == "/":
if b == 0:
return "I cannot divide by zero."
result = a / b
explanation = f"The quotient of {a} divided by {b} is {result}."
return explanation
except:
return "I had trouble calculating that. Could you phrase it differently?"
def is_simple_greeting(user_input): """Check if the input is a simple greeting.""" greetings = list(KNOWLEDGE_DATABASE["greetings"].keys()) user_input_lower = user_input.lower().strip()
# Direct match with greeting
if user_input_lower in greetings:
return True
# Check if input starts with a greeting
for greeting in greetings:
if user_input_lower.startswith(greeting):
return True
return False
def get_greeting_response(user_input): """Get appropriate response to a greeting.""" user_input_lower = user_input.lower().strip()
# Find matching greeting
for greeting, responses in KNOWLEDGE_DATABASE["greetings"].items():
if user_input_lower == greeting or user_input_lower.startswith(greeting):
return random.choice(responses)
# Default to a generic greeting if no match
return random.choice(KNOWLEDGE_DATABASE["greetings"]["hello"])
def analyze_query(query): """Analyze the query to determine the best way to respond.""" query_type = "general" topic = query
# Check if it's a math question
if re.search(r'\b\d+\s*[\+\-\*\/]\s*\d+\b', query) or any(term in query.lower() for term in ["calculate", "add", "subtract", "multiply", "divide", "sum", "difference", "product", "quotient"]):
query_type = "math"
return query_type, topic
# Check if it's a definition or explanation question
definition_patterns = [
r"what is (?:a |an )?([\w\s]+)\?",
r"who is (?:a |an )?([\w\s]+)\?",
r"what are (?:the )?([\w\s]+)\?",
r"define (?:a |an )?([\w\s]+)",
r"meaning of ([\w\s]+)"
]
for pattern in definition_patterns:
match = re.search(pattern, query.lower())
if match:
query_type = "definition"
topic = match.group(1).strip()
return query_type, topic
# Check if it's asking for information about a topic
info_patterns = [
r"tell me about ([\w\s]+)",
r"information (?:on|about) ([\w\s]+)",
r"explain (?:about )?([\w\s]+)",
r"describe ([\w\s]+)",
r"how (?:do|does|can) ([\w\s]+)",
r"why (?:is|are|do|does) ([\w\s]+)"
]
for pattern in info_patterns:
match = re.search(pattern, query.lower())
if match:
query_type = "information"
topic = match.group(1).strip()
return query_type, topic
# Check if it's a yes/no question
if query.lower().startswith(("is ", "are ", "can ", "does ", "do ", "will ", "should ")):
query_type = "yes_no"
return query_type, topic
return query_type, topic
def format_response(search_results, query_type, topic): """Format the search results into a coherent response.""" if not search_results: return random.choice(KNOWLEDGE_DATABASE["fallbacks"])
# Combine information from search results into natural-sounding responses
combined_info = " ".join(search_results[:2]) # Use top 2 results
# Clean up the combined info by removing redundant phrases
combined_info = re.sub(r'Based on available information,?\s*', '', combined_info)
combined_info = re.sub(r'According to sources,?\s*', '', combined_info)
combined_info = re.sub(r'I found that\s*', '', combined_info)
# For general queries, just return the direct answer without prefacing
if query_type == "general":
return combined_info
# For specific query types, format the response accordingly but without explaining the process
if query_type == "definition":
return combined_info
elif query_type == "information":
return combined_info
elif query_type == "yes_no":
# For yes/no questions, determine if the results tend toward yes or no
positive_indicators = ["yes", "can", "is", "are", "do", "does", "will", "should", "positive", "affirmative"]
negative_indicators = ["no", "cannot", "isn't", "aren't", "don't", "doesn't", "won't", "shouldn't", "negative"]
# Count positive and negative indicators in the results
positive_count = sum(1 for result in search_results for word in positive_indicators if word in result.lower())
negative_count = sum(1 for result in search_results for word in negative_indicators if word in result.lower())
if positive_count > negative_count:
answer = "Yes. "
elif negative_count > positive_count:
answer = "No. "
else:
answer = "" # Skip the prefix if unclear
return answer + combined_info
else:
return combined_info
def clean_text_symbols(text): """Clean random symbols and improve text quality.""" if not AI_CONFIG["text_quality"]["symbol_filter"]: return text
# Fix common symbol issues
text = re.sub(r'(?<=[a-zA-Z])[^\w\s.,?!;:\'"-](?=[a-zA-Z])', ' ', text) # Replace random symbols between words with spaces
text = re.sub(r'\s+', ' ', text) # Fix multiple spaces
# Enhanced symbol cleaning (more aggressive)
if AI_CONFIG["text_quality"]["enhanced_symbol_cleaning"]:
# Remove random symbols completely
text = re.sub(r'[^\w\s.,?!;:\'"-]', '', text)
# Fix symbols that might appear as numbers
text = re.sub(r'(?<=[a-zA-Z])[\d](?=[a-zA-Z])', '', text)
# Replace digit-letter combinations with spaces
text = re.sub(r'(?<=\d)[a-zA-Z]|(?<=[a-zA-Z])\d', ' ', text)
# Fix common word issues seen in responses
common_replacements = {
r'\b(teh|TEh)\b': 'the',
r'\b(adn|ADn)\b': 'and',
r'\b(taht|THat)\b': 'that',
r'\b(fo|FO)\b': 'of',
r'\b(wiht|WHit)\b': 'with',
r'\b(thsi|THis)\b': 'this',
r'\b(ar|AR)\b': 'are',
r'\b(yu|YU)\b': 'you',
r'\b(tht|THt)\b': 'that',
r'\b(wht|WHt)\b': 'what',
r'\b(hve|HVe)\b': 'have',
r'\b(bk|BK)\b': 'back',
r'\b(cmputer|CMputer)\b': 'computer',
r'\b(frm|FRm)\b': 'from',
r'\b(programm?g)\b': 'programming',
r'\b(hlp|HLp)\b': 'help',
r'\b(th3|th4)\b': 'the',
r'\b(4nd|4ND)\b': 'and',
r'\b(1s|1S)\b': 'is',
r'\b(d0|D0)\b': 'do',
r'\b(n0t|N0T)\b': 'not',
r'\b(c4n|C4N)\b': 'can',
r'\b(th1s|TH1S)\b': 'this',
r'\b(h4ve|H4VE)\b': 'have',
r'\b(w1ll|W1LL)\b': 'will'
}
for pattern, replacement in common_replacements.items():
text = re.sub(pattern, replacement, text)
# Fix number-word combinations if enabled
if AI_CONFIG["text_quality"]["number_correction"]:
number_words = {
'0': 'zero', '1': 'one', '2': 'two', '3': 'three', '4': 'four',
'5': 'five', '6': 'six', '7': 'seven', '8': 'eight', '9': 'nine'
}
# Replace standalone digits with words
for num, word in number_words.items():
text = re.sub(rf'\b{num}\b', word, text)
# Fix sentence capitalization
sentences = re.split(r'(?<=[.!?])\s+', text)
for i, sentence in enumerate(sentences):
if sentence and not sentence.isspace() and sentence[0].islower():
sentences[i] = sentence[0].upper() + sentence[1:]
return ' '.join(sentences)
def refine_response(response, query, memory_context=None): """Pre-trained AI module to improve response selection and quality.""" # Save original response to compare improvements original_response = response
# First, clean any random symbols that might be in the response
response = clean_text_symbols(response)
# 1. ANALYZE QUERY AND USER INTENT
# Patterns for different response types (expanded)
response_patterns = {
"factual": [
r"what is", r"what are", r"who is", r"when did", r"where is",
r"define", r"explain", r"how many", r"which", r"why is", r"why are"
],
"opinion": [
r"do you think", r"what do you think", r"is it good", r"should i",
r"would you recommend", r"better", r"best", r"worst", r"opinion on"
],
"personal": [
r"how are you", r"what is your name", r"who made you", r"tell me about yourself",
r"what can you do", r"your purpose", r"your function", r"what do you know"
],
"instruction": [
r"how to", r"how do i", r"steps to", r"guide for", r"tutorial",
r"teach me", r"show me how", r"process of", r"method for", r"ways to"
],
"comparison": [
r"difference between", r"compare", r"versus", r"vs", r"better than",
r"similarities between", r"pros and cons"
],
"definition": [
r"mean by", r"defined as", r"meaning of", r"definition of", r"stands for"
]
}
# Use alphabet and word structure knowledge to detect specialized queries
is_specialized_query = False
specialized_terms = []
# Check for technical terms using common word structures
for prefix in KNOWLEDGE_DATABASE["word_structures"]["common_prefixes"]:
for root in KNOWLEDGE_DATABASE["word_structures"]["common_roots"]:
for suffix in KNOWLEDGE_DATABASE["word_structures"]["common_suffixes"]:
tech_term = prefix + root + suffix
if tech_term in query.lower():
specialized_terms.append(tech_term)
is_specialized_query = True
# Determine response type based on query
response_type = "general"
for rtype, patterns in response_patterns.items():
if any(re.search(pattern, query.lower()) for pattern in patterns):
response_type = rtype
break
# 2. CHECK GOOGLE FOR ACCURACY (simulated)
# For factual and definition queries, verify key facts in response
if response_type in ["factual", "definition"]:
# Extract key facts from response
key_statements = re.split(r'(?<=[.!?])\s+', response)
for statement in key_statements:
# Check if statement contains contradictions
if "however" in statement.lower() or "but " in statement.lower():
# Split into parts and handle potential contradictions
parts = re.split(r'however|but', statement, flags=re.IGNORECASE)
if len(parts) > 1:
# Choose the more authoritative part
best_part = max(parts, key=len)
response = response.replace(statement, best_part)
# 3. IMPROVE RESPONSE BASED ON TYPE
if response_type == "factual":
# Ensure factual responses are clear, direct, and properly formatted
if not response.endswith('.'):
response += '.'
# Check for specific factual patterns and format accordingly
date_match = re.search(r'in (\d{4})', response)
if date_match:
year = date_match.group(1)
response = response.replace(f"in {year}", f"in the year {year}")
elif response_type == "opinion":
# Use more nuanced opinion prefixes
opinion_prefixes = [
"Based on available information, ",
"From my analysis, ",
"Considering various perspectives, ",
"Having evaluated different sources, "
]
if not any(prefix in response for prefix in opinion_prefixes):
response = random.choice(opinion_prefixes) + response.lower()
# Add balanced perspective markers
if "pros" in query.lower() and "cons" not in query.lower():
if "disadvantage" not in response.lower() and "drawback" not in response.lower():
response += " However, it's also worth considering potential limitations."
elif response_type == "personal":
# Enhanced personal question responses
if "your name" in query.lower():
return f"I am {AI_CONFIG['name']}, an AI assistant designed to help answer your questions using advanced language processing and web search capabilities."
elif "who made you" in query.lower() or "created you" in query.lower() or "developed you" in query.lower():
return f"I was made by a Jamaican developer in the Caribbean. My neural network has {AI_CONFIG['neural_net_size']} nodes designed to help users find information by searching and processing language patterns."
elif "how are you" in query.lower():
return "I'm functioning well and ready to assist you with any questions. My neural networks are operating at optimal capacity!"
elif "what can you do" in query.lower() or "your purpose" in query.lower():
return f"I'm designed to search the web, process information, and provide helpful responses to your questions. I can answer factual questions, offer opinions based on information, provide step-by-step instructions, and engage in general conversation."
elif response_type == "instruction":
# Enhanced instruction formatting with numbered steps and clear structure
if not re.search(r'firstly|first,|to begin|start by|step 1', response.lower()):
sentences = re.split(r'(?<=[.!?])\s+', response)
if len(sentences) > 2:
# Add an introduction
intro = "Here's how to do that:"
steps = [intro]
for i, sentence in enumerate(sentences[:6], 1): # Support up to 6 steps
if sentence and not sentence.isspace():
# Ensure sentence starts with a capital letter
if sentence and sentence[0].islower():
sentence = sentence[0].upper() + sentence[1:]
steps.append(f"{i}. {sentence}")
response = "\n".join(steps)
elif response_type == "comparison":
# Format comparisons with clear structure
if "vs" in query.lower() or "versus" in query.lower() or "difference" in query.lower():
# Try to identify the two things being compared
comparison_match = re.search(r'(difference between|compare|versus|vs)[:\s]+([a-z\s]+)(?:and|vs|versus|to)([a-z\s]+)', query.lower())
if comparison_match:
thing1 = comparison_match.group(2).strip()
thing2 = comparison_match.group(3).strip()
# Format the response as a comparison table
response = f"Comparing {thing1.title()} and {thing2.title()}:\n\n"
# Extract points from original response
points = re.split(r'(?<=[.!?])\s+', original_response)
thing1_points = []
thing2_points = []
shared_points = []
for point in points:
if thing1 in point.lower() and thing2 not in point.lower():
thing1_points.append(point)
elif thing2 in point.lower() and thing1 not in point.lower():
thing2_points.append(point)
elif thing1 in point.lower() and thing2 in point.lower():
shared_points.append(point)
if thing1_points or thing2_points:
response += f"{thing1.title()}: "
response += " ".join(thing1_points) if thing1_points else "No specific information found."
response += f"\n\n{thing2.title()}: "
response += " ".join(thing2_points) if thing2_points else "No specific information found."
if shared_points:
response += "\n\nCommon features: " + " ".join(shared_points)
else:
# If we couldn't split points by entity, just use the original response
response = original_response
# 4. FORMAT RESPONSE USING ALPHABET KNOWLEDGE
# Fix capitalization issues
sentences = re.split(r'(?<=[.!?])\s+', response)
formatted_sentences = []
for sentence in sentences:
if sentence and not sentence.isspace():
# Ensure sentence starts with capital letter
if sentence[0].islower() and sentence[0] in KNOWLEDGE_DATABASE["alphabet"]["lowercase"]:
idx = KNOWLEDGE_DATABASE["alphabet"]["lowercase"].index(sentence[0])
sentence = KNOWLEDGE_DATABASE["alphabet"]["uppercase"][idx] + sentence[1:]
formatted_sentences.append(sentence)
response = " ".join(formatted_sentences)
# 5. FINAL POLISHING
# Remove search artifacts and improve phrasing
response = re.sub(r'search(?:ing|ed) for|found that|results show', '', response)
response = re.sub(r'\s+', ' ', response) # Fix extra spaces
response = re.sub(r'([.!?])\s*([a-z])', lambda m: m.group(1) + " " + m.group(2).upper(), response) # Fix sentence boundaries
# Handle specialized technical terms with proper case
for term in specialized_terms:
# Keep technical terms in their proper case format
term_proper = term[0].upper() + term[1:]
response = response.replace(term, term_proper)
# Check if we've improved the response - if not, return original
if len(response) < len(original_response) / 2 and len(original_response) > 50:
return original_response
return response
def search_for_code_examples(query): """Search for code examples related to the query.""" # Add specific code-related keywords to the search code_query = f"{query} code example"
# Search for code snippets
code_results = search_web(code_query, max_results=AI_CONFIG["code_search"]["max_results"], search_for_code=True)
if not code_results:
return "I couldn't find specific code examples for that request."
# Clean and format the code snippets
cleaned_snippets = []
for snippet in code_results:
# Remove HTML entities and tags
clean_snippet = re.sub(r'<[^>]+>', '', snippet)
clean_snippet = re.sub(r'<', '<', clean_snippet)
clean_snippet = re.sub(r'>', '>', clean_snippet)
clean_snippet = re.sub(r'&', '&', clean_snippet)
clean_snippet = re.sub(r'"', '"', clean_snippet)
# Skip if snippet is too short or lacks code-like content
if len(clean_snippet) < 20 or not any(ch in clean_snippet for ch in "{}();="):
continue
cleaned_snippets.append(clean_snippet)
if not cleaned_snippets:
return "I found some code but couldn't properly extract usable examples."
# Combine snippets with explanations
result = f"Here's some code I found that might help:\n\n```\n{cleaned_snippets[0]}\n```"
if len(cleaned_snippets) > 1:
result += f"\n\nAlternatively:\n\n```\n{cleaned_snippets[1]}\n```"
result += "\n\nYou can modify this code to fit your specific needs."
return result
def check_previous_results(query): """Check if we already have results for this query in the queue.""" if SEARCH_RESULTS_QUEUE.empty(): return None
# Get all items from queue
items = []
while not SEARCH_RESULTS_QUEUE.empty():
items.append(SEARCHRESULTS_QUEUE.get())
# Check for matching query
result = None
for item_query, item_response in items:
if item_query.lower() == query.lower():
result = item_response
# Put non-matching items back in queue
for item in items:
if item[0].lower() != query.lower():
SEARCH_RESULTS_QUEUE.put(item)
return result
def generate_response(user_input): """Generate a response to the user's input.""" # Add to history USER_HISTORY.append(user_input)
# Check for special commands
if user_input.lower() in ["exit", "quit", "bye"]:
return "Goodbye! Feel free to ask me more questions anytime."
if user_input.lower() in ["help", "commands"]:
return (
f"I'm {AI_CONFIG['name']}, an AI assistant that can answer your questions.\n"
"- Ask me anything and I'll give you a direct answer\n"
"- For math calculations, just type your equation\n"
"- Ask for code examples and I'll search the internet\n"
"- Type 'quit' to exit"
)
# Check if this is a code request
code_request_patterns = [
r'code for', r'write code', r'create a program', r'how to code',
r'script for', r'implement', r'develop a', r'programming',
r'function for', r'class for', r'make a program'
]
is_code_request = any(re.search(pattern, user_input.lower()) for pattern in code_request_patterns)
if is_code_request and AI_CONFIG["limitations"]["no_code_generation"]:
return "I'm designed for conversation only and cannot generate or provide code examples. However, I can explain programming concepts or discuss how certain algorithms work in general terms."
# Check if it's a greeting
if is_simple_greeting(user_input):
return get_greeting_response(user_input)
# Check if we already have a result for this query
previous_result = check_previous_results(user_input)
if previous_result:
return previous_result
# Analyze the query
query_type, topic = analyze_query(user_input)
# Generate response based on query type
if query_type == "math":
return answer_math_question(user_input)
# If background search is enabled
if AI_CONFIG["background_search"]:
# Start a search in the background
search_thread = threading.Thread(
target=background_search,
args=(user_input, query_type, topic),
daemon=True
)
search_thread.start()
# Return an immediate response - make it sound like a direct answer
# instead of telling about search process
quick_responses = [
"Let me think about that for a moment. I'll have a better answer shortly.",
"I'm processing your question. Ask me something else while I work on this.",
"I'll need a moment to formulate a complete answer.",
"Im working on that. I'll have a detailed response shortly."
]
return random.choice(quick_responses)
# If not using background search, do the search immediately
search_results = search_web(user_input if query_type == "general" else topic)
response_body = format_response(search_results, query_type, topic)
return response_body
def display_background_results(): """Display background search results as they become available.""" if not SEARCH_RESULTS_QUEUE.empty(): query, response = SEARCH_RESULTS_QUEUE.get() print(f"\n{AI_CONFIG['name']}: {response}") print("\nYou: ", end="")
def retrieve_from_memory(query): """Retrieve relevant context from memory.""" # Simple keyword-based retrieval for demonstration keywords = query.lower().split() relevant_entries = [] for k, v in MEMORY.items(): if any(keyword in k.lower() or keyword in v.lower() for keyword in keywords): relevant_entries.append(v) return " ".join(relevant_entries) if relevant_entries else None
def update_memory(query, response): """Update memory with the current interaction.""" MEMORY[query] = response
def is_inappropriate_content(text): """Check if text contains inappropriate content.""" # Content filter disabled return False
def handle_inappropriate_request(query): """Generate a safe response for inappropriate requests.""" responses = [ "I'm designed to provide helpful and appropriate information. I cannot respond to that request.", "I'm programmed to maintain respectful communication. Let's talk about something else.", "I'm unable to engage with that topic. Is there something else I can help you with?", "That request contains content I'm not programmed to discuss. How about we focus on a different topic?", "I follow strict content guidelines and cannot respond to that query. I'd be happy to help with other questions." ] return random.choice(responses)
def main(): clear_screen() print(f"====== {AI_CONFIG['name']} Advanced AI Assistant ======") print(f"Neural Network Size: {AI_CONFIG['neural_net_size']} nodes | Dual Model Architecture") print(f"Training Corpus: {AI_CONFIG['training_corpus_size']:,} sentences | {AI_CONFIG['vocabulary_size']:,} word vocabulary") print(f"Knowledge Domains: {', '.join(AI_CONFIG['knowledge_domains'][:5])} + {len(AI_CONFIG['knowledge_domains'])-5} more") print(f"Semantic Processing: {random.randint(96, 99)}% accuracy | Advanced Context Awareness") print("Ask me anything or type 'quit' to exit.") print("="*50)
def background_result_checker(): while True: if not SEARCH_RESULTS_QUEUE.empty(): query, response = SEARCH_RESULTS_QUEUE.get()
# Display search information at the top
print(f"\n\n<searching>{AI_CONFIG['name']} is collecting information about: {query}</searching>")
# Get relevant context from memory
memory_context = retrieve_from_memory(query)
# Process the search results with the pre-trained AI module
refined_response = refine_response(response, query, memory_context)
# Make sure there are no random symbols in the final output
refined_response = clean_text_symbols(refined_response)
# Update memory with this interaction
update_memory(query, refined_response)
# Check if the response is actually useful
if len(refined_response.strip()) < 10:
# If response is too short, try to generate a better one
fallback_response = f"Based on available information about {query}, {response}"
refined_response = refine_response(fallback_response, query)
refined_response = clean_text_symbols(refined_response)
# Display the final response
print(f"\n{AI_CONFIG['name']}: {refined_response}")
print("\nYou: ", end="", flush=True)
time.sleep(0.5)
def main(): clear_screen() print(f"====== {AI_CONFIG['name']} Advanced AI Assistant ======") print(f"Neural Network Size: {AI_CONFIG['neural_net_size']} nodes | Dual Model Architecture") print(f"Training Corpus: {AI_CONFIG['training_corpus_size']:,} sentences | {AI_CONFIG['vocabulary_size']:,} word vocabulary") print(f"Knowledge Domains: {', '.join(AI_CONFIG['knowledge_domains'][:5])} + {len(AI_CONFIG['knowledge_domains'])-5} more") print(f"Semantic Processing: {random.randint(96, 99)}% accuracy | Advanced Context Awareness") print("Ask me anything or type 'quit' to exit.") print("="*50)
# Start the background result checker
bg_thread = threading.Thread(target=background_result_checker, daemon=True)
bg_thread.start()
while True:
print("\nYou: ", end="", flush=True)
user_input = input().strip()
if not user_input:
print(f"{AI_CONFIG['name']}: Please ask me a question or type 'quit' to exit.")
continue
if user_input.lower() in ["exit", "quit", "bye"]:
print(f"\n{AI_CONFIG['name']}: Goodbye! Feel free to ask me more questions anytime.")
break
# Content filtering disabled
response = generate_response(user_input)
# Apply final text cleaning
response = clean_text_symbols(response)
# Add AI-specific response formatting
if random.random() < 0.3: # Occasionally add thinking indicators
thinking_phrases = [
"Analyzing available data...",
"Processing information across neural network...",
"Correlating data points..."
]
print(f"\n{AI_CONFIG['name']} [thinking]: {random.choice(thinking_phrases)}")
time.sleep(0.5)
# Calculate simulated confidence level based on response length and complexity
confidence = min(random.uniform(0.85, 0.98), 0.98)
print(f"\n{AI_CONFIG['name']} [confidence: {confidence:.2f}]: {response}")
if name == "main": try: main() except KeyboardInterrupt: print(f"\n\n{AI_CONFIG['name']}: Session terminated by user. Goodbye!") except Exception as e: print(f"\n\nError: {e}") print("The program encountered an unexpected error and needs to close.")