Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion modules/ai_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from groq import Groq

from config.settings import Settings
import re


class AIDescriptionGenerator:
Expand All @@ -12,6 +13,18 @@ def __init__(self):
"""Initialize Groq client"""
self.client = Groq(api_key=Settings.get_groq_key())

@staticmethod
def _clean_summary(text: str) -> str:
"""Remove leading phrases like 'here the summary' from the output"""
patterns = [
r"^\s*here(?:'s| is)?\s+(?:the\s+)?summary[:,]?\s*",
r"^\s*summary[:,]?\s*",
]
cleaned = text
for pattern in patterns:
cleaned = re.sub(pattern, "", cleaned, flags=re.IGNORECASE)
return cleaned.strip()
Comment on lines +17 to +26

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The new _clean_summary method is a good addition for refining the AI-generated text!

To ensure its long-term reliability and handle various outputs from the AI, have you considered adding unit tests for this function? Testing with different inputs would be beneficial, for example:

  • Empty string: ""
  • String with no matching prefixes: "This is a normal sentence."
  • String with one of the target prefixes: "Here is the summary: Content" or "summary: More content"
  • Strings with different casing for the prefixes: "SUMMARY: Content"
  • Strings where a prefix might be followed by punctuation not currently handled (e.g., a period), to confirm current behavior: "Here is the summary. Content"
  • Strings that are similar but shouldn't be stripped because the keyword isn't at the very beginning (after accounting for whitespace): "A summary of events."

Dedicated unit tests would help catch regressions if the patterns are modified in the future or if new AI output behaviors emerge that need to be handled.


def generate_profile_summary(self, profile_data):
"""
Generate a professional profile summary
Expand Down Expand Up @@ -54,7 +67,8 @@ def generate_profile_summary(self, profile_data):
if not response.choices or response.choices[0].message.content == "":
raise Exception("No response from AI model")

return response.choices[0].message.content
raw_text = response.choices[0].message.content
return self._clean_summary(raw_text)

def generate_activity_summary(self, contributions):
"""
Expand Down
Loading