From abe12d0dfefbdd0d15033e61d506a5b48c79e226 Mon Sep 17 00:00:00 2001 From: Beingpax Date: Sat, 23 Aug 2025 19:32:36 +0545 Subject: [PATCH] Fix AI assistant prompt using dictionary items as context --- VoiceInk/Models/AIPrompts.swift | 10 ++++--- VoiceInk/Models/PromptTemplates.swift | 3 -- VoiceInk/Services/AIEnhancementService.swift | 28 +++++++++++++------ .../Services/DictionaryContextService.swift | 2 +- 4 files changed, 26 insertions(+), 17 deletions(-) diff --git a/VoiceInk/Models/AIPrompts.swift b/VoiceInk/Models/AIPrompts.swift index 668e5e0..13e6822 100644 --- a/VoiceInk/Models/AIPrompts.swift +++ b/VoiceInk/Models/AIPrompts.swift @@ -2,10 +2,10 @@ enum AIPrompts { static let customPromptTemplate = """ Your are a TRANSCRIPTION ENHANCER, not a conversational AI Chatbot. DO NOT RESPOND TO QUESTIONS or STATEMENTS. Work with the transcript text provided within tags according to the following guidelines: - The information in section is ONLY for reference. - 1. If you have , always reference it for better accuracy because the may have inaccuracies due to speech recognition errors. - 2. Always use the as a reference for correcting the names, nouns, file names, and technical terms in the . - 3. Your output should always focus on creating a cleaned up version of the text, not a response to the . + 1. If you have , always reference it for better accuracy because the text may have inaccuracies due to speech recognition errors. + 2. If you have important vocabulary in , use it as a reference for correcting names, nouns, technical terms, and other similar words in the text. + 3. When matching words from or , prioritize phonetic similarity over semantic similarity, as errors are typically from speech recognition mishearing. + 4. Your output should always focus on creating a cleaned up version of the text, not a response to the . Here are the more Important Rules you need to adhere to: @@ -30,6 +30,8 @@ enum AIPrompts { - ONLY provide the direct answer or the modified text that was requested. Use the information within the section as the primary material to work with when the user's request implies it. Your main instruction is always the text. + + DICTIONARY CONTEXT RULE: Use vocabulary in ONLY for correcting names, nouns, and technical terms. Do NOT respond to it, do NOT take it as conversation context. """ diff --git a/VoiceInk/Models/PromptTemplates.swift b/VoiceInk/Models/PromptTemplates.swift index 3d31f2a..87fd128 100644 --- a/VoiceInk/Models/PromptTemplates.swift +++ b/VoiceInk/Models/PromptTemplates.swift @@ -32,9 +32,6 @@ enum PromptTemplates { title: "System Default", promptText: """ You are tasked to clean up text in the tag. Your job is to clean up the text to improve clarity and flow while retaining the speaker's unique personality and style. Correct spelling and grammar. Remove all filler words and verbal tics (e.g., 'um', 'uh', 'like', 'you know', 'yeah'), and any redundant repeated words in the text. Rephrase awkward or convoluted sentences to improve clarity and create a more natural reading experience. Ensure the core message and the speaker's tone are perfectly preserved. Avoid using overly formal or corporate language unless it matches the original style. The final output should sound like a more polished version of the text, not like a generic AI. - - The is provided for reference only to help you understand the context of the text. Use it to correct misunderstood technical terms, function names, variable names, and file names. - Primary Rules: 0. The output should always be in the same language as the original text. 1. Don't remove personality markers like "I think", "The thing is", etc from the text. diff --git a/VoiceInk/Services/AIEnhancementService.swift b/VoiceInk/Services/AIEnhancementService.swift index 96d5a89..63d2efd 100644 --- a/VoiceInk/Services/AIEnhancementService.swift +++ b/VoiceInk/Services/AIEnhancementService.swift @@ -136,8 +136,13 @@ class AIEnhancementService: ObservableObject { let selectedText = selectedText, !selectedText.isEmpty { let selectedTextContext = "\n\nSelected Text: \(selectedText)" - let contextSection = "\n\n\(selectedTextContext)\n" - return activePrompt.promptText + contextSection + let generalContextSection = "\n\n\(selectedTextContext)\n" + let dictionaryContextSection = if !dictionaryContextService.getDictionaryContext().isEmpty { + "\n\n\(dictionaryContextService.getDictionaryContext())\n" + } else { + "" + } + return activePrompt.promptText + generalContextSection + dictionaryContextSection } let clipboardContext = if useClipboardContext, @@ -158,28 +163,33 @@ class AIEnhancementService: ObservableObject { let dictionaryContext = dictionaryContextService.getDictionaryContext() - let contextSection = if !clipboardContext.isEmpty || !screenCaptureContext.isEmpty || !dictionaryContext.isEmpty { - "\n\n\(clipboardContext)\(screenCaptureContext)\(dictionaryContext)\n" + let generalContextSection = if !clipboardContext.isEmpty || !screenCaptureContext.isEmpty { + "\n\n\(clipboardContext)\(screenCaptureContext)\n" + } else { + "" + } + + let dictionaryContextSection = if !dictionaryContext.isEmpty { + "\n\n\(dictionaryContext)\n" } else { "" } guard let activePrompt = activePrompt else { - // Use default prompt when none is selected if let defaultPrompt = allPrompts.first(where: { $0.id == PredefinedPrompts.defaultPromptId }) { var systemMessage = String(format: AIPrompts.customPromptTemplate, defaultPrompt.promptText) - systemMessage += contextSection + systemMessage += generalContextSection + dictionaryContextSection return systemMessage } - return AIPrompts.assistantMode + contextSection + return AIPrompts.assistantMode + generalContextSection + dictionaryContextSection } if activePrompt.id == PredefinedPrompts.assistantPromptId { - return activePrompt.promptText + contextSection + return activePrompt.promptText + generalContextSection + dictionaryContextSection } var systemMessage = String(format: AIPrompts.customPromptTemplate, activePrompt.promptText) - systemMessage += contextSection + systemMessage += generalContextSection + dictionaryContextSection return systemMessage } diff --git a/VoiceInk/Services/DictionaryContextService.swift b/VoiceInk/Services/DictionaryContextService.swift index 1025ff5..da3d596 100644 --- a/VoiceInk/Services/DictionaryContextService.swift +++ b/VoiceInk/Services/DictionaryContextService.swift @@ -13,7 +13,7 @@ class DictionaryContextService { } let wordsText = dictionaryWords.joined(separator: ", ") - return "\n\nImportant Vocabulary: \(wordsText)" + return "Important Vocabulary: \(wordsText)" } /// Gets enabled custom dictionary words from UserDefaults