Spaces:
Sleeping
Sleeping
File size: 1,816 Bytes
71fdb6d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
"""
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)
|