Add greeting detection and responses to LucidiaAI

Co-authored-by: blackboxprogramming <118287761+blackboxprogramming@users.noreply.github.com>
This commit is contained in:
copilot-swe-agent[bot]
2025-10-09 05:36:17 +00:00
parent 4cac5ff686
commit e232ed9cf8

View File

@@ -14,9 +14,13 @@ Example:
>>> from lucidia.core import LucidiaAI >>> from lucidia.core import LucidiaAI
>>> ai = LucidiaAI() >>> ai = LucidiaAI()
>>> ai.generate_response("I had a great day!") >>> ai.generate_response("Hi Lucidia!")
"Hello! I'm Lucidia. It's wonderful to meet you!"
>>> ai2 = LucidiaAI()
>>> ai2.generate_response("I had a great day!")
'That sounds wonderful! I am happy for you.' 'That sounds wonderful! I am happy for you.'
>>> ai.generate_response("I'm feeling sad today.") >>> ai3 = LucidiaAI()
>>> ai3.generate_response("I'm feeling sad today.")
"I'm sorry to hear that. I'm here if you want to talk about it." "I'm sorry to hear that. I'm here if you want to talk about it."
""" """
@@ -68,6 +72,14 @@ class LucidiaAI:
"depressed", "depressed",
"anxious", "anxious",
) )
GREETING_WORDS: Tuple[str, ...] = (
"hi",
"hello",
"hey",
"greetings",
"howdy",
"hiya",
)
def __init__(self, memory_file: Optional[str] = None) -> None: def __init__(self, memory_file: Optional[str] = None) -> None:
self.memory: List[Dict[str, str]] = [] self.memory: List[Dict[str, str]] = []
@@ -108,6 +120,33 @@ class LucidiaAI:
return -1 return -1
return 0 return 0
def is_greeting(self, text: str) -> bool:
"""Check if the input appears to be a greeting.
Returns True if the text contains common greeting words and is
relatively short (likely to be an initial greeting rather than
a longer message that happens to contain greeting words).
Parameters
----------
text : str
The user input to analyze.
Returns
-------
bool
True if the input appears to be a greeting, False otherwise.
"""
text_lower = text.lower()
# Check if any greeting word is present
has_greeting = any(word in text_lower for word in self.GREETING_WORDS)
# Also check for time-of-day greetings like "good morning"
time_greetings = ["morning", "afternoon", "evening", "night"]
has_time_greeting = any(f"good {time}" in text_lower for time in time_greetings)
# Greetings are typically short messages
is_short = len(text.split()) <= 5
return (has_greeting or has_time_greeting) and is_short
def generate_response(self, user_input: str) -> str: def generate_response(self, user_input: str) -> str:
"""Generate a context-aware and empathetic response. """Generate a context-aware and empathetic response.
@@ -127,6 +166,13 @@ class LucidiaAI:
str str
Lucidia's response. Lucidia's response.
""" """
# Check if this is a greeting
if self.is_greeting(user_input):
if self.memory:
response = "Hello again! It's nice to hear from you."
else:
response = "Hello! I'm Lucidia. It's wonderful to meet you!"
else:
sentiment = self.analyze_sentiment(user_input) sentiment = self.analyze_sentiment(user_input)
# Determine base response based on sentiment # Determine base response based on sentiment
if sentiment > 0: if sentiment > 0: