Clean up the final response.
(response: str)
| 239 | return enhanced_prompt |
| 240 | |
| 241 | def _clean_response(response: str) -> str: |
| 242 | """Clean up the final response.""" |
| 243 | |
| 244 | # Remove any trailing whitespace |
| 245 | response = response.strip() |
| 246 | |
| 247 | # Ensure the response doesn't end abruptly |
| 248 | if response and not response.endswith(('.', '!', '?', ':', '"', "'")): |
| 249 | # Don't add punctuation if it's a number or simple phrase |
| 250 | if not (response.replace(' ', '').replace(',', '').replace('.', '').isdigit() or len(response.split()) <= 3): |
| 251 | response += "." |
| 252 | |
| 253 | return response |