app ¶
Assembly: Loca LLama
Filename: app.py
Author: Terry D. Eppler
Created: 05-31-2024
Last Modified By: Terry D. Eppler
Last Modified On: 05-01-2025
Loca is python application for running local LLMs.
Copyright © 2023 Terry Eppler
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
You can contact me at: terryeppler@gmail.com or eppler.terry@epa.gov
is_docx_available ¶
is_llama_cpp_available ¶
is_pymupdf_available ¶
get_selected_model_name ¶
Purpose:¶
Return the currently selected local model name from Streamlit session state.
Parameters:¶
None
Returns:¶
str Selected model name.
Source code in app.py
get_selected_model_path ¶
Purpose:¶
Return the currently selected local GGUF model path from Streamlit session state.
Parameters:¶
None
Returns:¶
str Resolved local GGUF path for the selected model.
Source code in app.py
get_selected_model_spec ¶
Purpose:¶
Return the selected model specification from Streamlit session state.
Parameters:¶
None
Returns:¶
Dict[str, Any] Selected model metadata.
Source code in app.py
local_model_available ¶
Purpose:¶
Determine whether the selected or supplied local GGUF model file exists.
Parameters:¶
model_path : str | None Optional GGUF model path. When omitted, the selected model path is used.
Returns:¶
bool True when the configured model file exists; otherwise False.
Source code in app.py
get_default_model_name ¶
Purpose:¶
Return the configured default local model name from config.
Parameters:¶
None
Returns:¶
str Default model name.
Source code in app.py
get_model_names_for_state ¶
Purpose:¶
Return configured model names from the config registry while preserving fallback compatibility with cfg.MODEL_MAP.
Parameters:¶
None
Returns:¶
List[str] Configured model names.
Source code in app.py
get_default_mode_name ¶
Purpose:¶
Return the default UI mode for the selected model.
Parameters:¶
model_name : str Selected local model name.
Returns:¶
str Default UI mode name.
Source code in app.py
get_model_modes_for_state ¶
Purpose:¶
Return the supported modes for the selected model using the config model registry when available, while preserving fallback compatibility with cfg.MODES.
Parameters:¶
model_name : str Selected local model name.
Returns:¶
List[str] Supported mode names.
Source code in app.py
get_model_path_for_state ¶
Purpose:¶
Return the selected model path using the config model registry when available, while preserving fallback compatibility with cfg.MODEL_MAP and cfg.MODEL_PATH.
Parameters:¶
model_name : str Selected local model name.
Returns:¶
str Resolved GGUF model path.
Source code in app.py
get_model_spec_for_state ¶
Purpose:¶
Return the selected model registry specification when available.
Parameters:¶
model_name : str Selected local model name.
Returns:¶
Dict[str, Any] Model specification dictionary.
Source code in app.py
initialize_model_mode_state ¶
Purpose:¶
Initialize widget-owned and derived model/mode session-state keys before the sidebar widgets are created.
Parameters:¶
None
Returns:¶
None
Source code in app.py
synchronize_model_derived_state ¶
Purpose:¶
Synchronize derived model state without modifying widget-owned keys after their widgets have been instantiated.
Parameters:¶
None
Returns:¶
None
Source code in app.py
on_selected_model_change ¶
Purpose:¶
Streamlit callback used by the LLM selector to resynchronize derived model values after the selected model changes without directly modifying selected_mode.
Parameters:¶
None
Returns:¶
None
Source code in app.py
on_selected_mode_change ¶
Purpose:¶
Streamlit callback used by the AI Mode selector to keep the legacy mode key aligned with selected_mode.
Parameters:¶
None
Returns:¶
None
Source code in app.py
get_mode_constant ¶
Purpose:¶
Return a mode constant from config with a stable fallback. This allows app.py to accept expanded config.py mode definitions without crashing while config updates are being staged.
Parameters:¶
constant_name : str Name of the config.py constant.
str
Fallback mode name.
Returns:¶
str Resolved mode name.
Source code in app.py
get_mode_definition_text ¶
Purpose:¶
Return descriptive config.py text for expanded API modes when available.
Parameters:¶
mode_name : str UI mode name.
Returns:¶
str Mode description text.
Source code in app.py
get_selected_base_model ¶
Purpose:¶
Return the selected model's configured base model name.
Parameters:¶
None
Returns:¶
str Base model name.
Source code in app.py
get_selected_model_family ¶
Purpose:¶
Return the selected model's configured model family.
Parameters:¶
None
Returns:¶
str Model family name.
Source code in app.py
get_selected_chat_template ¶
Purpose:¶
Return the selected model's configured chat template.
Parameters:¶
None
Returns:¶
str Chat template name.
Source code in app.py
is_buddy_model ¶
Purpose:¶
Determine whether the selected model is Buddy or a Buddy base model.
Parameters:¶
None
Returns:¶
bool True when Buddy is selected; otherwise False.
Source code in app.py
is_gipity_model ¶
Purpose:¶
Determine whether the selected model is Gipity or a GPT-OSS base model.
Parameters:¶
None
Returns:¶
bool True when Gipity is selected; otherwise False.
Source code in app.py
is_gemma4_model ¶
Purpose:¶
Determine whether the selected model uses the Gemma 4 E4B base model.
Parameters:¶
None
Returns:¶
bool True when a Gemma 4 E4B model is selected; otherwise False.
Source code in app.py
is_jimi_or_nisty_model ¶
Purpose:¶
Determine whether the selected model is Jimi or Nisty.
Parameters:¶
None
Returns:¶
bool True when Jimi or Nisty is selected; otherwise False.
Source code in app.py
model_supports_mode ¶
Purpose:¶
Determine whether the selected model registry advertises a specific UI mode.
Parameters:¶
mode_name : str UI mode name.
Returns:¶
bool True when the mode is listed for the selected model; otherwise False.
Source code in app.py
get_runtime_multimodal_status ¶
Purpose:¶
Return the current runtime's multimodal adapter status. This detects whether app.py has an image/audio-capable local adapter configured separately from the model registry. The function fails closed so newly exposed modes cannot crash.
Parameters:¶
None
Returns:¶
Dict[str, Any] Runtime multimodal status flags and message.
Source code in app.py
get_active_model_capabilities ¶
Purpose:¶
Return selected model capability flags used by expanded Text, Image, Audio, Function Calling, Coding, Thinking, and Web Browsing workflows.
Parameters:¶
None
Returns:¶
Dict[str, Any] Capability contract for the selected model.
Source code in app.py
model_supports_capability ¶
Purpose:¶
Determine whether the selected model supports a named expanded capability.
Parameters:¶
capability : str Capability name.
Returns:¶
bool True when the selected model supports the capability; otherwise False.
Source code in app.py
get_capability_status_message ¶
Purpose:¶
Return a user-facing status message for unsupported or unavailable capabilities.
Parameters:¶
capability : str Capability name.
Returns:¶
str Status message.
Source code in app.py
get_default_function_schema_text ¶
Purpose:¶
Return a safe starter JSON schema for function-calling workflows.
Parameters:¶
None
Returns:¶
str Starter JSON function schema text.
Source code in app.py
initialize_capability_session_state ¶
Purpose:¶
Initialize expanded capability session-state keys before Image, Audio, Function Calling, Coding, Thinking, and Web Browsing controls are introduced.
Parameters:¶
None
Returns:¶
None
Source code in app.py
refresh_capability_session_state ¶
Purpose:¶
Refresh derived capability state after model or mode changes without clearing user-owned text, uploaded-file names, generated output, or existing chat state.
Parameters:¶
None
Returns:¶
None
Source code in app.py
get_model_retrieval_profile ¶
Purpose:¶
Return model-safe retrieval defaults for Document Q&A and Semantic Search. Smaller models receive narrower retrieval windows so grounded prompts stay concise and less likely to exceed practical local runtime limits.
Parameters:¶
model_name : str Selected local model name.
Returns:¶
Dict[str, Any] Retrieval profile values.
Source code in app.py
1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 | |
has_user_tuned_retrieval_controls ¶
Purpose:¶
Determine whether the current retrieval controls have already been changed by the user or by a previously applied model profile. This prevents model-safe defaults from overwriting user-tuned values on every Streamlit rerun.
Parameters:¶
None
Returns:¶
bool True when retrieval controls should be preserved; otherwise False.
Source code in app.py
mark_retrieval_controls_user_tuned ¶
Purpose:¶
Mark retrieval controls as user-tuned. Later controls can call this callback if needed to permanently preserve manual user settings across model changes.
Parameters:¶
None
Returns:¶
None
Source code in app.py
apply_retrieval_profile ¶
Purpose:¶
Apply a retrieval profile to Document Q&A and Semantic Search session-state keys. The profile is applied only when forced or when no user-tuned override exists.
Parameters:¶
profile : Dict[str, Any] Retrieval profile to apply.
bool
When True, apply the profile even if retrieval_controls_user_tuned is True.
Returns:¶
None
Source code in app.py
apply_model_safe_retrieval_defaults ¶
Purpose:¶
Apply model-safe retrieval defaults when the selected model changes. Buddy receives compact retrieval settings suitable for a 270M model; other models receive standard settings unless the user has already tuned retrieval controls.
Parameters:¶
model_name : str Optional selected model name. When omitted, the current selected model is used.
Returns:¶
None
Source code in app.py
reset_model_safe_retrieval_defaults ¶
Purpose:¶
Clear manual retrieval override state and reapply the selected model's recommended Document Q&A and Semantic Search retrieval profile.
Parameters:¶
None
Returns:¶
None
Source code in app.py
initialize_model_safe_retrieval_state ¶
Purpose:¶
Initialize retrieval-profile tracking keys and apply model-safe defaults once after capability session state is initialized.
Parameters:¶
None
Returns:¶
None
Source code in app.py
extract_json_object_from_text ¶
Purpose:¶
Extract the first valid JSON object from model-generated text. This supports function-calling outputs where the model may accidentally wrap the object in markdown fences or explanatory prose.
Parameters:¶
text : str Model-generated text.
Returns:¶
Dict[str, Any] Parsed JSON object.
Source code in app.py
1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 | |
normalize_tool_call ¶
Purpose:¶
Normalize a model-generated tool call into the app contract: {"name": "...", "arguments": {...}}.
Parameters:¶
tool_call : Dict[str, Any] Parsed tool-call JSON object.
Returns:¶
Dict[str, Any] Normalized tool-call object.
Source code in app.py
get_allowed_function_names ¶
Purpose:¶
Return the function names that app.py is allowed to execute. This prevents model output from invoking arbitrary functions.
Parameters:¶
None
Returns:¶
List[str] Allowlisted function names.
Source code in app.py
summarize_text_tool ¶
Purpose:¶
Summarize supplied text using a deterministic local sentence extraction fallback. This is intentionally non-agentic and does not execute arbitrary model code.
Parameters:¶
text : str Text to summarize.
int
Maximum number of bullets to return.
Returns:¶
str Bullet summary.
Source code in app.py
extract_keywords_tool ¶
Purpose:¶
Extract simple frequency-ranked keywords from supplied text without external dependencies.
Parameters:¶
text : str Text to analyze.
int
Maximum number of keywords to return.
Returns:¶
str Comma-separated keyword list.
Source code in app.py
is_private_or_local_hostname ¶
Purpose:¶
Determine whether a hostname resolves to a local, loopback, private, reserved, or link-local address. This blocks server-side requests to private network resources.
Parameters:¶
hostname : str URL hostname.
Returns:¶
bool True when the hostname is private or local; otherwise False.
Source code in app.py
1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 | |
validate_web_url ¶
Purpose:¶
Validate an outbound web-browsing URL. Only HTTP and HTTPS URLs are allowed, and private/local network targets are blocked.
Parameters:¶
url : str User-supplied URL.
str
Optional allowed domain suffix.
Returns:¶
str Validated URL.
Source code in app.py
html_to_readable_text ¶
Purpose:¶
Convert HTML to readable text using a dependency-free parser fallback.
Parameters:¶
html_text : str Raw HTML text.
Returns:¶
str Readable extracted text.
Source code in app.py
fetch_web_text ¶
fetch_web_text(
url: str,
allowed_domain: str = "",
timeout_seconds: int = 15,
max_chars: int = 12000,
) -> Dict[str, Any]
Purpose:¶
Fetch readable text from a public HTTP/HTTPS URL with timeout, size, and private network safeguards.
Parameters:¶
url : str User-supplied URL.
str
Optional allowed domain suffix.
int
Network timeout in seconds.
int
Maximum readable text characters returned.
Returns:¶
Dict[str, Any] Web fetch result.
Source code in app.py
1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 | |
web_browse_url_tool ¶
web_browse_url_tool(
url: str,
prompt: str = "",
allowed_domain: str = "",
max_chars: int = 12000,
) -> str
Purpose:¶
Fetch a public web page and return bounded text suitable for model grounding.
Parameters:¶
url : str User-supplied URL.
str
Optional user task for the fetched content.
str
Optional allowed domain suffix.
int
Maximum readable text characters returned.
Returns:¶
str Readable web context.
Source code in app.py
execute_allowlisted_function ¶
Purpose:¶
Execute a normalized, allowlisted app function. Arbitrary model-generated function names are rejected.
Parameters:¶
tool_call : Dict[str, Any] Normalized tool-call object.
Returns:¶
Dict[str, Any] Tool execution result.
Source code in app.py
build_tool_call_generation_prompt ¶
Purpose:¶
Build a focused prompt that asks the selected model to emit one strict JSON function-call object.
Parameters:¶
user_task : str User task to translate into a tool call.
Returns:¶
str Tool-call generation prompt.
Source code in app.py
generate_function_call_json ¶
Purpose:¶
Ask the selected local model to generate a strict JSON function-call object.
Parameters:¶
user_task : str User task to convert into a function call.
Returns:¶
str Generated model text.
Source code in app.py
execute_tool_call_text ¶
Purpose:¶
Parse and execute model-generated tool-call text through the app's allowlisted function layer.
Parameters:¶
tool_call_text : str Model-generated function-call JSON text.
Returns:¶
Dict[str, Any] Tool execution result.
Source code in app.py
build_tool_result_final_prompt ¶
Purpose:¶
Build a final-answer prompt from a validated tool execution result.
Parameters:¶
user_task : str Original user task.
Dict[str, Any]
Executed tool result.
Returns:¶
str Final answer prompt.
Source code in app.py
generate_tool_grounded_final_answer ¶
Purpose:¶
Generate a final answer grounded in an executed tool result.
Parameters:¶
user_task : str Original user task.
Dict[str, Any]
Executed tool result.
Returns:¶
str Model-generated final answer.
Source code in app.py
send_web_context_to_text_generation ¶
Purpose:¶
Send fetched web context into the shared Text Generation document context buffer.
Parameters:¶
context_text : str Web context text.
Returns:¶
None
Source code in app.py
get_model_logo_for_state ¶
Purpose:¶
Return the logo path associated with the selected model.
Parameters:¶
model_name : str Selected local model name.
Returns:¶
str Configured logo path.
Source code in app.py
resolve_resource_path ¶
Purpose:¶
Resolve a configured resource path relative to the application base directory when the supplied path is not already absolute.
Parameters:¶
path : str Configured resource path.
Returns:¶
Path Resolved resource path.
Source code in app.py
render_selected_model_logo ¶
Purpose:¶
Render the selected model logo using Streamlit's native logo API so the logo remains visible when the sidebar is collapsed.
Parameters:¶
model_name : str Selected local model name.
str
Streamlit logo size. Expected values are 'small', 'medium', or 'large'.
Returns:¶
None
Source code in app.py
normalize_text ¶
Purpose¶
Normalize text by: • Converting to lowercase • Removing punctuation except sentence delimiters (. ! ?) • Ensuring clean sentence boundary spacing • Collapsing whitespace
Parameters¶
text: str
Returns¶
str
Source code in app.py
chunk_text ¶
Purpose:¶
Split text into overlapping chunks using session-state defaults when explicit values are not provided.
Parameters:¶
text : str size : int | None overlap : int | None
Returns:¶
List[str]
Source code in app.py
convert_xml ¶
Purpose:
Convert XML-delimited prompt text into Markdown by treating XML-like tags as section delimiters, not as strict XML.
Parameters:¶
text (str) - Prompt text containing XML-like opening and closing tags.
Returns:¶
Markdown-formatted text using level-2 headings (##).
Source code in app.py
convert_markdown ¶
Purpose:¶
Convert between Markdown headings and simple XML-like heading tags.
Behavior:¶
Auto-detects direction: - If
...
/...
... exist, converts to Markdown (# / ## / ###). - Otherwise converts Markdown headings (# / ## / ###) toParameters:¶
text : Any Source text. Non-string values return "".
Returns:¶
str Converted text.
Source code in app.py
2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 | |
inject_response_css ¶
Purpose:
Set the the format via css.
Source code in app.py
style_subheaders ¶
Purpose:
Sets the style of subheaders in the main UI
Source code in app.py
fetch_prompt_names ¶
Purpose:¶
Retrieve template names from Prompts table.
Parameters:¶
db_path : str SQLite database path.
Returns:¶
list[str] Sorted prompt names.
Source code in app.py
fetch_prompt_text ¶
Purpose:¶
Retrieve template text by name.
Parameters:¶
db_path : str SQLite database path. name : str Template name.
Returns:¶
str | None Prompt text if found.
Source code in app.py
get_effective_system_instructions ¶
Purpose:¶
Return the authoritative system instructions text from session state.
Parameters:¶
None
Returns:¶
str
Source code in app.py
build_task_instruction_block ¶
Purpose:¶
Build a task-specific instruction block for Text Generation mode, including model-gated Thinking, Coding, and Function Calling directives for Gemma 4 and GPT-OSS-aligned local models.
Parameters:¶
None
Returns:¶
str Task instruction block.
Source code in app.py
2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 | |
build_effective_prompt_preview ¶
Purpose:¶
Build a readable preview of the effective prompt content used for generation.
Parameters:¶
user_input : str
Returns:¶
str
Source code in app.py
get_preset_system_instruction ¶
Purpose:¶
Return a starter system-instruction preset for the selected task type.
Parameters:¶
task_preset : str Selected task preset.
Returns:¶
str System-instruction preset text.
Source code in app.py
get_system_instruction_action_key ¶
Purpose:¶
Return the pending action key used by a specific System Instructions renderer.
Parameters:¶
prefix : str Renderer prefix.
Returns:¶
str Pending action session-state key.
Source code in app.py
get_system_instruction_template_key ¶
Purpose:¶
Return the template-select widget key used by a specific System Instructions renderer.
Parameters:¶
prefix : str Renderer prefix.
Returns:¶
str Template widget session-state key.
Source code in app.py
get_system_instruction_pending_template_key ¶
Purpose:¶
Return the pending template key used by a specific System Instructions renderer.
Parameters:¶
prefix : str Renderer prefix.
Returns:¶
str Pending template session-state key.
Source code in app.py
request_system_template_change ¶
Purpose:¶
Request a system-instruction template change without directly modifying the widget-owned system_instructions key.
Parameters:¶
prefix : str Renderer prefix.
Returns:¶
None
Source code in app.py
request_system_instruction_action ¶
Purpose:¶
Request a pending system-instruction action without directly modifying the widget-owned system_instructions key.
Parameters:¶
prefix : str Renderer prefix.
str
Requested action name.
Returns:¶
None
Source code in app.py
process_pending_system_instruction_requests ¶
Purpose:¶
Process pending System Instructions requests before the system_instructions text area is instantiated. This is the only safe place to modify the shared system_instructions widget-owned key.
Parameters:¶
prefix : str Renderer prefix.
Returns:¶
None
Source code in app.py
render_system_instructions ¶
render_system_instructions(
prefix: str,
include_apply_preset: bool = False,
include_preview: bool = False,
) -> None
Purpose:¶
Render a Streamlit-safe shared System Instructions control surface. All writes to the widget-owned system_instructions key are processed before the text-area widget is instantiated.
Parameters:¶
prefix : str Unique renderer prefix, such as 'text' or 'docqna'.
bool
When True, show the Apply Preset button.
bool
When True, show the Preview Prompt button and preview text area.
Returns:¶
None
Source code in app.py
3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 | |
get_runtime_llm ¶
Purpose:¶
Load the selected llama.cpp model using the currently selected model path and runtime settings.
Parameters:¶
None
Returns:¶
Any | None Loaded llama.cpp model instance when available; otherwise None.
Source code in app.py
build_prompt ¶
Purpose:¶
Build a llama.cpp-compatible prompt using unified system instructions, task-specific Text Generation settings, optional semantic/basic context, and chat history. Semantic context retrieval is guarded so Text Generation cannot crash when the embedder, database, embeddings table, or stored vectors are unavailable or inconsistent.
Parameters:¶
user_input : str User prompt text supplied by the Text Generation mode.
Returns:¶
str Prompt text formatted for the local llama.cpp chat template.
Source code in app.py
3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 | |
build_llama_call_args ¶
build_llama_call_args(
max_tokens: int,
temperature: float,
top_p: float,
repeat_penalty: float,
stream: bool,
) -> Dict[str, Any]
Purpose:¶
Build llama.cpp generation arguments from the current Streamlit runtime settings.
Parameters:¶
max_tokens : int Maximum number of generated tokens.
float
Sampling temperature.
float
Nucleus sampling value.
float
Repeat penalty value.
bool
Whether streaming output is requested.
Returns:¶
Dict[str, Any] Generation argument dictionary for llama.cpp.
Source code in app.py
get_missing_model_message ¶
Purpose:¶
Build a clear user-facing message when the selected local GGUF model or required llama.cpp dependency is not available.
Parameters:¶
None
Returns:¶
str Model availability message.
Source code in app.py
run_llm_turn ¶
run_llm_turn(
user_input: str,
temperature: float,
top_p: float,
repeat_penalty: float,
max_tokens: int,
stream: bool,
output: Any = None,
) -> str
Purpose:¶
Run a single local llama.cpp LLM turn using the selected model, current prompt contract, and runtime settings. Missing or unavailable GGUF models are handled safely with a user-facing diagnostic response instead of a callable None failure.
Parameters:¶
user_input : str User prompt or prepared application prompt.
float
Sampling temperature.
float
Nucleus sampling value.
float
Repeat penalty value.
int
Maximum number of generated tokens.
bool
Whether to stream output tokens into the supplied Streamlit placeholder.
Any | None
Optional Streamlit output placeholder.
Returns:¶
str Generated response text or diagnostic message.
Source code in app.py
3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 | |
get_prompt_categories ¶
Purpose:¶
Return supported prompt categories.
Parameters:¶
None
Returns:¶
List[str]
Source code in app.py
get_prompt_task_types ¶
infer_prompt_category ¶
Purpose:¶
Infer a prompt category from the prompt row content.
Parameters:¶
prompt_row : Dict[str, Any] | None
Returns:¶
str
Source code in app.py
build_starter_prompt_template ¶
build_starter_prompt_template(
category: str,
task_type: str,
response_format: str,
language: str,
) -> str
Purpose:¶
Build a starter prompt template from high-level prompt metadata.
Parameters:¶
category : str task_type : str response_format : str language : str
Returns:¶
str
Source code in app.py
generate_prompt_template_draft ¶
generate_prompt_template_draft(
goal: str,
constraints: str,
style: str,
category: str,
task_type: str,
response_format: str,
language: str,
) -> str
Purpose:¶
Generate a draft system prompt using the local model.
Parameters:¶
goal : str constraints : str style : str category : str task_type : str response_format : str language : str
Returns:¶
str
Source code in app.py
apply_prompt_to_text_generation ¶
apply_prompt_to_document_qna ¶
Purpose:¶
Apply a prompt to shared Document Q&A settings.
Parameters:¶
prompt_text : str
Returns:¶
None
Source code in app.py
apply_prompt_metadata_to_shared_state ¶
apply_prompt_metadata_to_shared_state(
category: str,
task_type: str,
response_format: str,
language: str,
) -> None
Purpose:¶
Apply prompt metadata to the shared app contract.
Parameters:¶
category : str task_type : str response_format : str language : str
Returns:¶
None
Source code in app.py
clone_prompt_record ¶
Purpose:¶
Clone a selected prompt into the edit surface as a new prompt draft.
Parameters:¶
source_prompt : Dict[str, Any] | None
Returns:¶
None
Source code in app.py
initialize_database ¶
Purpose:¶
Ensure required SQLite tables exist and that the Prompts table contains the columns required by the prompt utilities, Prompt Engineering mode, and AI-asset governance features.
Parameters:¶
None
Returns:¶
None
Source code in app.py
3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 | |
drop_table ¶
rename_table ¶
Purpose:¶
Rename an existing SQLite table. Attempts native ALTER TABLE rename first; if it fails, falls back to a schema-safe rebuild using the original CREATE TABLE statement and preserves indexes.
Parameters:¶
old_name : str Existing table name.
str
New table name.
Returns:¶
None
Source code in app.py
4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 | |
rename_column ¶
Purpose:¶
Rename a column within an existing SQLite table. Attempts native ALTER TABLE rename first; if it fails, falls back to a schema-safe rebuild preserving column order, data, and indexes.
Parameters:¶
table_name : str Table containing the column.
str
Existing column name.
str
New column name.
Returns:¶
None
Source code in app.py
4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 | |
create_index ¶
Purpose:¶
Create a safe SQLite index on a specified table column.
Handles
- Spaces in column names
- Special characters
- Reserved words
- Duplicate index names
- Validation against actual table schema
Parameters:¶
table : str Table name. column : str Column name to index.
Source code in app.py
get_sqlite_type ¶
Purpose:¶
Map a pandas dtype to an appropriate SQLite column type.
Parameters:¶
dtype : pandas dtype The dtype of a pandas Series.
Returns:¶
str SQLite column type.
Source code in app.py
create_custom_table ¶
Purpose:¶
Create a custom SQLite table from column definitions.
Parameters:¶
table_name : str Name of table.
list of dict
[ { "name": str, "type": str, "not_null": bool, "primary_key": bool, "auto_increment": bool } ]
Source code in app.py
is_safe_query ¶
Purpose:¶
Determine whether a SQL query is read-only and safe to execute.
Allows
SELECT WITH (CTE returning SELECT) EXPLAIN SELECT PRAGMA (read-only)
Blocks
INSERT, UPDATE, DELETE, DROP, ALTER, CREATE, ATTACH, DETACH, VACUUM, REPLACE, TRIGGER, and multiple statements.
Source code in app.py
create_identifier ¶
Purpose:¶
Sanitize a string into a safe SQLite identifier.
- Replaces invalid characters with underscores
- Ensures it starts with a letter or underscore
- Prevents empty names
Source code in app.py
get_ai_asset_tables ¶
get_table_row_count ¶
Purpose:¶
Return the row count for a SQLite table.
Parameters:¶
conn : sqlite3.Connection Open SQLite connection.
str
Table name.
Returns:¶
int Number of rows in the table.
Source code in app.py
get_ai_asset_counts ¶
Purpose:¶
Count rows in the AI asset governance tables used by Document Q&A, Semantic Search, and Data Management.
Parameters:¶
None
Returns:¶
Dict[str, int] Dictionary keyed by AI asset table name with row counts as values.
Source code in app.py
purge_orphaned_document_chunks ¶
Purpose:¶
Delete document chunk rows whose DocumentName no longer exists in the documents table.
Parameters:¶
conn : sqlite3.Connection Open SQLite connection.
Returns:¶
int Deleted row count.
Source code in app.py
purge_orphaned_document_embeddings ¶
Purpose:¶
Delete document embedding metadata rows whose DocumentName no longer exists in the documents table.
Parameters:¶
conn : sqlite3.Connection Open SQLite connection.
Returns:¶
int Deleted row count.
Source code in app.py
purge_orphaned_ai_assets ¶
Purpose:¶
Delete orphaned AI asset rows that depend on the governed documents table.
Parameters:¶
None
Returns:¶
Dict[str, int] Dictionary containing deleted chunk and embedding row counts.
Source code in app.py
get_timestamp_text ¶
register_session_documents ¶
Purpose:¶
Register active uploaded documents into the governed documents table.
Parameters:¶
None
Returns:¶
Dict[str, int]
Source code in app.py
4998 4999 5000 5001 5002 5003 5004 5005 5006 5007 5008 5009 5010 5011 5012 5013 5014 5015 5016 5017 5018 5019 5020 5021 5022 5023 5024 5025 5026 5027 5028 5029 5030 5031 5032 5033 5034 5035 5036 5037 5038 5039 5040 5041 5042 5043 5044 5045 5046 5047 5048 5049 5050 5051 5052 5053 5054 5055 5056 5057 5058 5059 5060 5061 5062 5063 5064 5065 5066 5067 5068 5069 5070 5071 5072 5073 5074 5075 5076 5077 5078 5079 5080 5081 5082 5083 5084 5085 5086 5087 5088 5089 5090 5091 5092 | |
register_session_chunks ¶
Purpose:¶
Register active document chunks into the governed document_chunks table.
Parameters:¶
None
Returns:¶
Dict[str, int]
Source code in app.py
register_session_embeddings ¶
Purpose:¶
Register active document embedding metadata into the governed document_embeddings table.
Parameters:¶
None
Returns:¶
Dict[str, int]
Source code in app.py
register_upload_images ¶
Purpose:¶
Register uploaded image metadata into the governed images table.
Parameters:¶
uploaded_files : List[Any]
Returns:¶
Dict[str, int]
Source code in app.py
5210 5211 5212 5213 5214 5215 5216 5217 5218 5219 5220 5221 5222 5223 5224 5225 5226 5227 5228 5229 5230 5231 5232 5233 5234 5235 5236 5237 5238 5239 5240 5241 5242 5243 5244 5245 5246 5247 5248 5249 5250 5251 5252 5253 5254 5255 5256 5257 5258 5259 5260 5261 5262 5263 5264 5265 5266 5267 5268 5269 5270 5271 5272 5273 5274 5275 5276 5277 5278 5279 5280 5281 5282 5283 5284 5285 5286 5287 5288 5289 5290 5291 5292 5293 5294 5295 5296 | |
load_llm ¶
Purpose:¶
Lazily load the selected local llama.cpp GGUF model using the supplied runtime settings. The model path is part of the cache key so switching models creates a distinct cached model resource.
Parameters:¶
model_path : str Local GGUF model path.
int
Context window size.
int
CPU thread count.
int
Random seed used by llama.cpp.
Returns:¶
Any | None Loaded llama.cpp model instance when available; otherwise None.
Source code in app.py
load_embedder ¶
Purpose:¶
Lazily load the sentence embedding model when the dependency is available.
Parameters:¶
None
Returns:¶
Any | None A sentence-transformer model instance when available; otherwise None.
Source code in app.py
create_docqna_instruction ¶
Purpose:¶
Return an instruction block for a selected document action.
Parameters:¶
action_name : str
Returns:¶
str
Source code in app.py
build_instruction_block ¶
Purpose:¶
Build a unified instruction block for document-grounded answering.
Parameters:¶
None
Returns:¶
str
Source code in app.py
extract_text_from_pdf_bytes ¶
Purpose:¶
Extract native text from PDF bytes using PyMuPDF when it is available.
Parameters:¶
file_bytes : bytes PDF file bytes.
bool
When True, include page markers before each extracted page.
Returns:¶
str Extracted PDF text.
Source code in app.py
extract_text_from_docx_bytes ¶
Purpose:¶
Extract text from DOCX bytes using python-docx when it is available.
Parameters:¶
file_bytes : bytes DOCX file bytes.
Returns:¶
str Extracted DOCX text.
Source code in app.py
compute_fingerprint ¶
Purpose:¶
Computes a stable fingerprint for the currently selected active documents and their byte contents.
Parameters:¶
active_docs: A List[ str ] of active document names. doc_bytes: A Dict[ str, bytes ] mapping document name to file bytes.
Returns:¶
A str fingerprint suitable for cache invalidation.
Source code in app.py
decode_text_bytes ¶
Purpose:¶
Decode text-like document bytes using common encodings and a permissive fallback.
Parameters:¶
file_bytes : bytes File bytes to decode.
Returns:¶
str Decoded text.
Source code in app.py
extract_text_from_bytes ¶
Purpose:¶
Extract text from supported document bytes using the file name extension and current parsing preferences.
Parameters:¶
file_bytes : bytes Source document bytes.
str
Source document name.
Returns:¶
str Extracted text.
Source code in app.py
5604 5605 5606 5607 5608 5609 5610 5611 5612 5613 5614 5615 5616 5617 5618 5619 5620 5621 5622 5623 5624 5625 5626 5627 5628 5629 5630 5631 5632 5633 5634 5635 5636 5637 5638 5639 5640 5641 5642 5643 5644 5645 5646 5647 5648 5649 5650 5651 5652 5653 5654 5655 5656 5657 5658 5659 5660 5661 5662 5663 5664 5665 5666 5667 5668 5669 5670 5671 | |
extract_text_bytes ¶
Purpose:¶
Backward-compatible wrapper for extracting text from document bytes.
Parameters:¶
file_bytes : bytes Source document bytes.
str
Source document name.
Returns:¶
str Extracted text.
Source code in app.py
extract_text ¶
Purpose:¶
Extract document text using the configured parsing behavior.
Parameters:¶
file_bytes : bytes Source document bytes.
str
Source document name.
Returns:¶
str Extracted text.
Source code in app.py
load_sqlite_vec ¶
Purpose:¶
Attempts to load sqlite-vec into the provided SQLite connection.
Parameters:¶
conn: The sqlite3.Connection.
Returns:¶
True if sqlite-vec loaded successfully; otherwise False.
Source code in app.py
ensure_schema ¶
Purpose:¶
Creates the sqlite-vec virtual table used for Document Q&A embeddings if possible.
Parameters:¶
dim: The embedding dimension (e.g., 384 for all-MiniLM-L6-v2).
Returns:¶
True if the schema exists and is usable; otherwise False.
Source code in app.py
build_docqna_inventory ¶
Purpose:¶
Build inventory rows for the currently active uploaded documents.
Parameters:¶
None
Returns:¶
List[Dict[str, Any]]
Source code in app.py
get_docqna_names ¶
Purpose:¶
Build a human-readable string of active document names.
Parameters:¶
None
Returns:¶
str
Source code in app.py
is_embedder_available ¶
Purpose:¶
Determine whether a sentence embedding model is available and usable.
Parameters:¶
candidate : Any | None Optional embedding model instance. When omitted, the global embedder is used.
Returns:¶
bool True when an embedder with an encode method is available; otherwise False.
Source code in app.py
get_embedder_unavailable_message ¶
Purpose:¶
Return a standard diagnostic message when sentence-transformer embeddings are unavailable.
Parameters:¶
None
Returns:¶
str Diagnostic message.
Source code in app.py
decode_embedding_vector ¶
Purpose:¶
Decode a stored embedding vector BLOB into a NumPy float32 array.
Parameters:¶
vector_blob : bytes | memoryview | None Stored vector BLOB.
Returns:¶
np.ndarray Decoded vector. Empty array when decoding fails.
Source code in app.py
rebuild_index ¶
Purpose:¶
Build or refresh the Document Q&A vector index when active documents or chunk settings change. The function fails closed when embeddings are unavailable instead of raising an AttributeError from embedder.encode(...).
Parameters:¶
embedder : Any | None Sentence embedding model instance.
Returns:¶
None
Source code in app.py
5896 5897 5898 5899 5900 5901 5902 5903 5904 5905 5906 5907 5908 5909 5910 5911 5912 5913 5914 5915 5916 5917 5918 5919 5920 5921 5922 5923 5924 5925 5926 5927 5928 5929 5930 5931 5932 5933 5934 5935 5936 5937 5938 5939 5940 5941 5942 5943 5944 5945 5946 5947 5948 5949 5950 5951 5952 5953 5954 5955 5956 5957 5958 5959 5960 5961 5962 5963 5964 5965 5966 5967 5968 5969 5970 5971 5972 5973 5974 5975 5976 5977 5978 5979 5980 5981 5982 5983 5984 5985 5986 5987 5988 5989 5990 5991 5992 5993 5994 5995 5996 5997 5998 5999 6000 6001 6002 6003 6004 6005 6006 6007 6008 6009 6010 6011 6012 6013 6014 6015 6016 6017 6018 6019 6020 6021 6022 6023 6024 | |
retrieve_chunks ¶
Purpose:¶
Retrieve top-k document chunks relevant to the query using sqlite-vec when available, with optional cosine-similarity fallback. Missing embeddings fail safely.
Parameters:¶
query : str User query.
int | None
Number of chunks to retrieve.
Returns:¶
List[Tuple[str, str, float]] Ranked retrieval results as document name, chunk text, and score or distance.
Source code in app.py
6026 6027 6028 6029 6030 6031 6032 6033 6034 6035 6036 6037 6038 6039 6040 6041 6042 6043 6044 6045 6046 6047 6048 6049 6050 6051 6052 6053 6054 6055 6056 6057 6058 6059 6060 6061 6062 6063 6064 6065 6066 6067 6068 6069 6070 6071 6072 6073 6074 6075 6076 6077 6078 6079 6080 6081 6082 6083 6084 6085 6086 6087 6088 6089 6090 6091 6092 6093 6094 6095 6096 6097 6098 6099 6100 6101 6102 6103 6104 6105 6106 6107 6108 6109 6110 6111 6112 6113 6114 6115 6116 6117 6118 6119 | |
build_docqna_input ¶
Purpose:¶
Build a document-grounded prompt using retrieved excerpts and the current document action. Missing retrieval returns a safe prompt rather than failing.
Parameters:¶
user_query : str User request.
int | None
Number of chunks to retrieve.
Returns:¶
str Document-grounded LLM prompt.
Source code in app.py
6121 6122 6123 6124 6125 6126 6127 6128 6129 6130 6131 6132 6133 6134 6135 6136 6137 6138 6139 6140 6141 6142 6143 6144 6145 6146 6147 6148 6149 6150 6151 6152 6153 6154 6155 6156 6157 6158 6159 6160 6161 6162 6163 6164 6165 6166 6167 6168 6169 6170 6171 6172 6173 6174 6175 6176 6177 6178 6179 6180 6181 6182 6183 6184 | |
decode_embedding_rows ¶
Purpose:¶
Read and decode rows from the semantic embeddings table. Database failures, missing tables, corrupt blobs, and empty vectors fail closed so Semantic Search and Text Generation context reuse cannot crash.
Parameters:¶
None
Returns:¶
List[Tuple[str, np.ndarray]] Decoded chunk/vector rows.
Source code in app.py
clear_semantic_index ¶
Purpose:¶
Clear the semantic embeddings table and reset Semantic Search diagnostics without raising hard database errors into the Streamlit UI execution path.
Parameters:¶
None
Returns:¶
None
Source code in app.py
build_semantic_index ¶
Purpose:¶
Build or append a semantic chunk index from uploaded files. The function preserves the existing embeddings table contract while guarding extraction, embedding, vector shape, and SQLite write failures.
Parameters:¶
uploaded_files : List[Any] Uploaded files from Streamlit.
Returns:¶
Dict[str, Any] Index build result.
Source code in app.py
6265 6266 6267 6268 6269 6270 6271 6272 6273 6274 6275 6276 6277 6278 6279 6280 6281 6282 6283 6284 6285 6286 6287 6288 6289 6290 6291 6292 6293 6294 6295 6296 6297 6298 6299 6300 6301 6302 6303 6304 6305 6306 6307 6308 6309 6310 6311 6312 6313 6314 6315 6316 6317 6318 6319 6320 6321 6322 6323 6324 6325 6326 6327 6328 6329 6330 6331 6332 6333 6334 6335 6336 6337 6338 6339 6340 6341 6342 6343 6344 6345 6346 6347 6348 6349 6350 6351 6352 6353 6354 6355 6356 6357 6358 6359 6360 6361 6362 6363 6364 6365 6366 6367 6368 6369 6370 6371 6372 6373 6374 6375 6376 6377 6378 6379 6380 6381 6382 6383 6384 6385 6386 6387 6388 6389 6390 6391 6392 6393 6394 6395 6396 6397 6398 6399 6400 6401 6402 6403 6404 6405 6406 6407 6408 6409 6410 6411 6412 6413 6414 6415 6416 6417 6418 6419 6420 6421 6422 6423 6424 6425 6426 6427 6428 6429 6430 6431 6432 6433 6434 6435 6436 6437 6438 6439 6440 6441 6442 6443 6444 6445 6446 | |
query_semantic_index ¶
Purpose:¶
Query the semantic index and return ranked chunk results. Missing embeddings, database failures, empty indexes, malformed vectors, and vector dimension mismatches fail closed instead of raising hard runtime errors.
Parameters:¶
query_text : str Query text.
Returns:¶
List[Dict[str, Any]] Ranked semantic result rows.
Source code in app.py
6448 6449 6450 6451 6452 6453 6454 6455 6456 6457 6458 6459 6460 6461 6462 6463 6464 6465 6466 6467 6468 6469 6470 6471 6472 6473 6474 6475 6476 6477 6478 6479 6480 6481 6482 6483 6484 6485 6486 6487 6488 6489 6490 6491 6492 6493 6494 6495 6496 6497 6498 6499 6500 6501 6502 6503 6504 6505 6506 6507 6508 6509 6510 6511 6512 6513 6514 6515 6516 6517 6518 6519 6520 6521 6522 6523 6524 6525 6526 6527 6528 6529 6530 6531 6532 6533 6534 6535 6536 6537 6538 6539 6540 6541 6542 6543 6544 6545 6546 6547 6548 6549 6550 6551 6552 6553 | |
create_semantic_context ¶
Purpose:¶
Build a semantic-context text block from selected search rows.
Parameters:¶
None
Returns:¶
str Semantic context text.
Source code in app.py
extract_selected_rows ¶
Purpose:¶
Extract selected semantic rows from a data_editor result payload.
Parameters:¶
edited_rows : Any Data editor result payload.
Returns:¶
List[Dict[str, Any]] Selected rows.
Source code in app.py
send_text_chunks ¶
Purpose:¶
Push selected semantic chunks into the shared basic document context buffer.
Parameters:¶
None
Returns:¶
None
Source code in app.py
send_docqna_chunks ¶
Purpose:¶
Push selected semantic chunks into the shared document context buffer used by Document Q&A prompts.
Parameters:¶
None
Returns:¶
None
Source code in app.py
request_text_generation_reset ¶
Purpose:¶
Request a Text Generation control reset without directly modifying any widget-owned keys after their widgets have been instantiated.
Parameters:¶
reset_name : str Name of the reset group to process on the next safe script pass.
Returns:¶
None
Source code in app.py
request_docqna_reset ¶
Purpose:¶
Request a Document Q&A control reset without directly modifying any widget-owned keys after their widgets have been instantiated.
Parameters:¶
reset_name : str Name of the reset group to process on the next safe script pass.
Returns:¶
None
Source code in app.py
request_document_unload ¶
render_pdf_preview ¶
Purpose:¶
Render a PDF preview using st.pdf when available, otherwise fall back to a base64 iframe and, if needed, extracted text.
Parameters:¶
file_bytes : bytes PDF file bytes.
str
Display name for the active PDF.
Returns:¶
None
Source code in app.py
run_image_mode_adapter ¶
Purpose:¶
Run an optional image-analysis adapter when one has been wired into app.py. The function fails closed when the selected model or runtime does not support image analysis.
Parameters:¶
image_bytes : bytes Uploaded image bytes.
str
Uploaded image filename.
str
User prompt for image analysis.
Returns:¶
str Image analysis response text.
Source code in app.py
build_image_context_text ¶
Purpose:¶
Build reusable image context text for Text Generation, Document Q&A, or Prompt Engineering workflows.
Parameters:¶
image_name : str Uploaded image filename.
str
User image-analysis prompt.
str
Image-analysis response or runtime status.
Returns:¶
str Reusable image context text.
Source code in app.py
run_audio_mode_adapter ¶
Purpose:¶
Run an optional audio-analysis adapter when one has been wired into app.py. The function fails closed when the selected model or runtime does not support audio transcription, translation, or audio analysis.
Parameters:¶
audio_bytes : bytes Uploaded audio bytes.
str
Uploaded audio filename.
str
User prompt for audio transcription or analysis.
Returns:¶
str Audio transcription, translation, analysis, or runtime status text.
Source code in app.py
build_audio_context_text ¶
Purpose:¶
Build reusable audio context text for Text Generation, Document Q&A, Semantic Search, or Prompt Engineering workflows.
Parameters:¶
audio_name : str Uploaded audio filename.
str
User audio-analysis prompt.
str
Audio transcription, translation, analysis, or runtime status.
Returns:¶
str Reusable audio context text.
Source code in app.py
get_audio_mime_type ¶
Purpose:¶
Return a browser-friendly MIME type for Streamlit audio preview based on the uploaded audio filename.
Parameters:¶
audio_name : str Uploaded audio filename.
Returns:¶
str Audio MIME type.