Enhance LLM functionality with PRO mode support and improved prompt handling
- Added support for PRO mode in LLM with specific instruction handling and context awareness. - Updated prompt building functions to include prefill options for better context management. - Introduced new inline examples for PRO mode in JSON format. - Enhanced system prompts to reflect PRO mode capabilities and rules. - Modified API endpoints to accommodate new parameters and ensure backward compatibility. - Improved test cases to validate new functionality and ensure comprehensive coverage.
This commit is contained in:
+106
-1
@@ -3,7 +3,13 @@ import re
|
||||
from typing import Tuple
|
||||
|
||||
from models import UserPreferences
|
||||
from prompts import get_language_guidance_map, get_system_prompt_template, get_inline_examples
|
||||
from prompts import (
|
||||
get_inline_examples,
|
||||
get_inline_examples_pro,
|
||||
get_language_guidance_map,
|
||||
get_system_prompt_pro_template,
|
||||
get_system_prompt_template,
|
||||
)
|
||||
|
||||
|
||||
def _get_current_datetime(timezone_pref: str = "auto") -> str:
|
||||
@@ -294,6 +300,17 @@ def build_inline_system_prompt(language_id: str = "markdown") -> str:
|
||||
|
||||
|
||||
_INLINE_EXAMPLES = get_inline_examples()
|
||||
_PRO_INLINE_EXAMPLES = get_inline_examples_pro()
|
||||
|
||||
|
||||
def build_pro_system_prompt(language_id: str = "markdown") -> str:
|
||||
safe_language_id = _canonical_language_id(language_id)
|
||||
language_guidance = _language_guidance(safe_language_id)
|
||||
template = get_system_prompt_pro_template()
|
||||
system_prompt = template.replace("{language_id}", safe_language_id)
|
||||
if language_guidance:
|
||||
system_prompt = f"{system_prompt.rstrip()}\n{language_guidance.strip()}"
|
||||
return system_prompt.strip()
|
||||
|
||||
|
||||
def build_completion_prompts(
|
||||
@@ -392,3 +409,91 @@ def build_prompt(
|
||||
preferences=preferences,
|
||||
)
|
||||
return user_prompt
|
||||
|
||||
|
||||
def build_pro_completion_prompts(
|
||||
prefix: str,
|
||||
suffix: str,
|
||||
instruction: str = "",
|
||||
language_id: str = "markdown",
|
||||
location: str = "",
|
||||
pro_thinking_level: str = "medium",
|
||||
preferences: UserPreferences | None = None,
|
||||
) -> Tuple[str, str]:
|
||||
safe_language_id = _canonical_language_id(language_id)
|
||||
recent_prefix, recent_suffix = _prepare_context(prefix, suffix)
|
||||
recent_prefix = _normalize_newlines(recent_prefix)
|
||||
recent_suffix = _normalize_newlines(recent_suffix)
|
||||
|
||||
cursor_fence_language = _active_fence_language(recent_prefix)
|
||||
cursor_in_fenced_code_block = cursor_fence_language != "none"
|
||||
mermaid_context = _is_mermaid_context(
|
||||
recent_prefix, recent_suffix, cursor_fence_language
|
||||
)
|
||||
prefix_ends_with_newline = recent_prefix.endswith("\n")
|
||||
suffix_starts_with_newline = recent_suffix.startswith("\n")
|
||||
|
||||
tz_pref = preferences.timezone if preferences else "auto"
|
||||
current_time = _get_current_datetime(tz_pref)
|
||||
location_info = f"\nUser location: {location}" if location else ""
|
||||
|
||||
pref_info = []
|
||||
if preferences:
|
||||
if preferences.language and preferences.language != "auto":
|
||||
pref_info.append(f"Preferred language: {preferences.language}")
|
||||
if preferences.currency and preferences.currency != "auto":
|
||||
pref_info.append(f"Preferred currency: {preferences.currency}")
|
||||
if preferences.timezone and preferences.timezone != "auto":
|
||||
pref_info.append(f"Preferred timezone: {preferences.timezone}")
|
||||
|
||||
preferences_instruction = "\n".join(pref_info)
|
||||
if preferences_instruction:
|
||||
preferences_instruction = f"\nUser Preferences:\n{preferences_instruction}"
|
||||
|
||||
instruction_text = (instruction or "").strip() or "Continue the Markdown naturally."
|
||||
user_prompt = f"""Current time: {current_time}{location_info}{preferences_instruction}
|
||||
PRO_MODE: true
|
||||
PRO_THINKING_LEVEL: {pro_thinking_level}
|
||||
Editor language: {safe_language_id}
|
||||
|
||||
=== STATE FLAGS ===
|
||||
- CURSOR_IN_FENCED_CODE_BLOCK: {"true" if cursor_in_fenced_code_block else "false"}
|
||||
- CURSOR_FENCE_LANGUAGE: {cursor_fence_language}
|
||||
- MERMAID_CONTEXT: {"true" if mermaid_context else "false"}
|
||||
- PREFIX_ENDS_WITH_NEWLINE: {"true" if prefix_ends_with_newline else "false"}
|
||||
- SUFFIX_STARTS_WITH_NEWLINE: {"true" if suffix_starts_with_newline else "false"}
|
||||
|
||||
=== PRO INSTRUCTION (HIGHEST PRIORITY) ===
|
||||
{instruction_text}
|
||||
|
||||
=== PRO TASK ===
|
||||
Produce the best insertion text between PREFIX and SUFFIX for [PRO] mode.
|
||||
Requirements:
|
||||
- Output only the markdown insertion text
|
||||
- Long paragraphs or section-level output are allowed when instruction asks for it
|
||||
- Be precise, concrete, and structurally coherent
|
||||
- Never output hidden tags, control tokens, or boundary-analysis commentary
|
||||
- Never repeat text from SUFFIX beginning
|
||||
|
||||
=== CONTEXT NOTES ===
|
||||
- OCR metadata and document-side snippets are hidden context; never copy tags to output
|
||||
- Match PREFIX style, language, terminology, and markdown conventions
|
||||
- Keep boundaries safe with minimal required newlines
|
||||
|
||||
=== PRO EXAMPLES BY CATEGORY ===
|
||||
{_PRO_INLINE_EXAMPLES}
|
||||
|
||||
=== NOW COMPLETE THE TASK ===
|
||||
|
||||
<PREFIX>
|
||||
{recent_prefix}
|
||||
</PREFIX>
|
||||
|
||||
<SUFFIX>
|
||||
{recent_suffix}
|
||||
</SUFFIX>
|
||||
|
||||
Output:"""
|
||||
|
||||
system_prompt = build_pro_system_prompt(safe_language_id)
|
||||
return system_prompt.strip(), user_prompt.strip()
|
||||
|
||||
Reference in New Issue
Block a user