Implement language detection in AI response generation and enhance message content extraction

This commit is contained in:
2025-03-01 14:48:57 +01:00
parent 1be345b07f
commit d326705b1d
2 changed files with 131 additions and 84 deletions

View File

@@ -43,12 +43,40 @@ func New(cfg config.AIConfig) *AI {
}
}
func (a *AI) detectLanguage(emailContent string) (string, error) {
logger.WithField("emailContentLength", len(emailContent)).Debug("Starting language detection")
systemMsg := "You are a language detection assistant. Analyze the provided text and respond ONLY with the language name (e.g., 'English', 'German', 'French', etc.). No other text or explanation."
userMsg := fmt.Sprintf("Detect the language of this text:\n\n%s", emailContent)
messages := []Message{
{Role: "system", Content: systemMsg},
{Role: "user", Content: userMsg},
}
response, err := a.makeAPIRequest(messages)
if err != nil {
return "", fmt.Errorf("language detection failed: %v", err)
}
// The response should be just the language code
langCode := response
logger.WithField("detectedLanguage", langCode).Debug("Language detection completed")
return langCode, nil
}
func (a *AI) GenerateReply(emailContent string, contextContent map[string]string) (string, error) {
logger.WithFields(logrus.Fields{
"emailContentLength": len(emailContent),
"contextUrls": len(contextContent),
"contextUrls": len(contextContent),
}).Debug("Starting AI reply generation")
// First, detect the language
lang, err := a.detectLanguage(emailContent)
if err != nil {
return "", err
}
// Prepare context from all URLs
var context string
for url, content := range contextContent {
@@ -59,19 +87,22 @@ func (a *AI) GenerateReply(emailContent string, contextContent map[string]string
}).Debug("Added context from URL")
}
// Prepare the system message and user message
// systemMsg := "You are a helpful assistant responding to emails. Analyze the language of the incoming email and translate the response to the same language. Format your response in HTML. Do not include any explanations or extra text - just write the email response directly in HTML format. Use appropriate HTML tags for formatting."
// systemMsg := "You are a helpful assistant who responds to emails. For each incoming email, first detect its language and then generate your response in the exact same language. Your reply must be written directly in HTML format using appropriate HTML tags for structure and styling. Do not include any explanations, commentary, or extra text—simply output the email response in HTML."
// systemMsg := "You are a helpful assistant who responds to emails. For each incoming email, first detect its language and then generate your response in the exact same language. Your reply must be written directly in HTML format using appropriate HTML tags for structure and styling. Do not include a subject line, explanations, commentary, or any extra text—simply output the email response content in HTML."
// systemMsg := "You are a helpful assistant who responds to emails. For every incoming email, carefully detect and confirm its language. Then, generate your email response entirely in that same language without deviation. Format your reply solely in HTML using appropriate HTML tags for structure and styling, and do not include a subject line, explanations, or any extra text. Ensure that every part of your response exactly matches the language of the incoming email."
systemMsg := "You are a helpful assistant who responds to emails. For every incoming email, strictly use only the email's content to detect its language and ignore any external or additional context. Then, generate your email response entirely in that same language without deviation. Format your reply solely in HTML using appropriate HTML tags for structure and styling, and do not include a subject line, explanations, commentary, or any extra text. Ensure that every part of your response exactly matches the language of the incoming email."
userMsg := fmt.Sprintf("Using the following context:\n%s\n\nPlease analyze the language of and generate a reply in the same language for this email:\n%s", context, emailContent)
// Prepare the system message with language-specific instruction
systemMsg := fmt.Sprintf("You are a helpful assistant who responds to emails.Regardless of the language used in the email content or context, your response must be entirely in %s. Format your reply solely in HTML using appropriate HTML tags for structure and styling. Do not include a subject line, explanations, commentary, or any extra text.", lang)
logger.WithFields(logrus.Fields{
"systemprompt": systemMsg,
}).Debug("Generating system prompt")
userMsg := fmt.Sprintf("### Additional Context:\n%s\n\n### Email Body:\n%s", context, emailContent)
messages := []Message{
{Role: "system", Content: systemMsg},
{Role: "user", Content: userMsg},
}
return a.makeAPIRequest(messages)
}
func (a *AI) makeAPIRequest(messages []Message) (string, error) {
const maxRetries = 3
var lastErr error