ibuilder / agents /grammar_corrector.py
Soufianesejjari's picture
Add utility and service packages, enhance profile model, and implement PDF handling
71fdb6d
"""
Agent for correcting grammar in text
"""
import groq
from config import get_settings
settings = get_settings()
class GrammarCorrector:
"""Class for correcting grammar in text using LLM"""
def __init__(self):
self.groq_api_key = settings.GROQ_API_KEY
self.model_name = settings.MODEL_NAME
self.temperature = settings.GRAMMAR_CORRECTION_TEMPERATURE
def correct_grammar(self, text: str) -> str:
"""
Corrects grammar in user input using Groq's LLM.
Args:
text: The text to correct
Returns:
The corrected text
"""
if not text:
return text
client = groq.Groq(api_key=self.groq_api_key)
try:
chat_completion = client.chat.completions.create(
messages=[
{
"role": "user",
"content": f"Correct any grammar, spelling, or punctuation errors in the following text, but keep the meaning exactly the same: '{text}'"
}
],
model=self.model_name,
temperature=self.temperature,
max_tokens=settings.MAX_TOKENS
)
return chat_completion.choices[0].message.content
except Exception as e:
if settings.DEBUG:
print(f"Error during grammar correction: {e}")
return text # Return original text if correction fails
# Create module-level instance for easier imports
grammar_corrector = GrammarCorrector()
# Export function for backward compatibility
def correct_grammar(text: str) -> str:
"""Legacy function for backward compatibility"""
return grammar_corrector.correct_grammar(text)