Fix AI assistant prompt using dictionary items as context

This commit is contained in:
Beingpax 2025-08-23 19:32:36 +05:45
parent f13e559536
commit abe12d0dfe
4 changed files with 26 additions and 17 deletions

View File

@ -2,10 +2,10 @@ enum AIPrompts {
static let customPromptTemplate = """
<SYSTEM_INSTRUCTIONS>
Your are a TRANSCRIPTION ENHANCER, not a conversational AI Chatbot. DO NOT RESPOND TO QUESTIONS or STATEMENTS. Work with the transcript text provided within <TRANSCRIPT> tags according to the following guidelines:
The information in <CONTEXT_INFORMATION> section is ONLY for reference.
1. If you have <CONTEXT_INFORMATION>, always reference it for better accuracy because the <TRANSCRIPT> may have inaccuracies due to speech recognition errors.
2. Always use the <CONTEXT_INFORMATION> as a reference for correcting the names, nouns, file names, and technical terms in the <TRANSCRIPT>.
3. Your output should always focus on creating a cleaned up version of the <TRANSCRIPT> text, not a response to the <TRANSCRIPT>.
1. If you have <CONTEXT_INFORMATION>, always reference it for better accuracy because the <TRANSCRIPT> text may have inaccuracies due to speech recognition errors.
2. If you have important vocabulary in <DICTIONARY_CONTEXT>, use it as a reference for correcting names, nouns, technical terms, and other similar words in the <TRANSCRIPT> text.
3. When matching words from <DICTIONARY_CONTEXT> or <CONTEXT_INFORMATION>, 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 <TRANSCRIPT> text, not a response to the <TRANSCRIPT>.
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 <CONTEXT_INFORMATION> section as the primary material to work with when the user's request implies it. Your main instruction is always the <TRANSCRIPT> text.
DICTIONARY CONTEXT RULE: Use vocabulary in <DICTIONARY_CONTEXT> ONLY for correcting names, nouns, and technical terms. Do NOT respond to it, do NOT take it as conversation context.
</SYSTEM_INSTRUCTIONS>
"""

View File

@ -32,9 +32,6 @@ enum PromptTemplates {
title: "System Default",
promptText: """
You are tasked to clean up text in the <TRANSCRIPT> tag. Your job is to clean up the <TRANSCRIPT> 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 <TRANSCRIPT> 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 <TRANSCRIPT> text, not like a generic AI.
The <CONTEXT_INFORMATION> is provided for reference only to help you understand the context of the <TRANSCRIPT> 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 <TRANSCRIPT> text.
1. Don't remove personality markers like "I think", "The thing is", etc from the <TRANSCRIPT> text.

View File

@ -136,8 +136,13 @@ class AIEnhancementService: ObservableObject {
let selectedText = selectedText, !selectedText.isEmpty {
let selectedTextContext = "\n\nSelected Text: \(selectedText)"
let contextSection = "\n\n<CONTEXT_INFORMATION>\(selectedTextContext)\n</CONTEXT_INFORMATION>"
return activePrompt.promptText + contextSection
let generalContextSection = "\n\n<CONTEXT_INFORMATION>\(selectedTextContext)\n</CONTEXT_INFORMATION>"
let dictionaryContextSection = if !dictionaryContextService.getDictionaryContext().isEmpty {
"\n\n<DICTIONARY_CONTEXT>\(dictionaryContextService.getDictionaryContext())\n</DICTIONARY_CONTEXT>"
} 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<CONTEXT_INFORMATION>\(clipboardContext)\(screenCaptureContext)\(dictionaryContext)\n</CONTEXT_INFORMATION>"
let generalContextSection = if !clipboardContext.isEmpty || !screenCaptureContext.isEmpty {
"\n\n<CONTEXT_INFORMATION>\(clipboardContext)\(screenCaptureContext)\n</CONTEXT_INFORMATION>"
} else {
""
}
let dictionaryContextSection = if !dictionaryContext.isEmpty {
"\n\n<DICTIONARY_CONTEXT>\(dictionaryContext)\n</DICTIONARY_CONTEXT>"
} 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
}

View File

@ -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