summary_generator
zocrates.rag.summary_generator ¶
Document summary generator utility with LiteLLM OpenRouter integration and deterministic extractive lead text heuristics.
DEFAULT_SUMMARY_MODEL
module-attribute
¶
SUMMARY_SYSTEM_PROMPT
module-attribute
¶
SUMMARY_SYSTEM_PROMPT = 'You are an expert academic research assistant. Provide a concise, dense, factual summary (2-3 sentences, 100-150 words) of the following document. Focus on the primary research problem, core methodology/system architecture, and key findings or results. Do not include conversational filler, meta-commentary, or any thinking process/scratchpad. Output ONLY the final summary text directly.'
_BOILERPLATE_LINE_RE
module-attribute
¶
_BOILERPLATE_LINE_RE = re.compile('(@[a-zA-Z0-9._-]+\\.[a-zA-Z]+|\\barxiv:\\s*[\\d.]+|\\bdoi:\\s*10\\.\\d+|\\bdepartment of\\b|\\buniversity\\b|\\bfaculty of\\b|\\bschool of\\b|\\binstitute of\\b|\\bcopyright\\s*(?:©|\\(c\\)|20\\d\\d|19\\d\\d)|\\ball rights reserved\\b|\\bcreative commons\\b|\\bcc-by\\b)', re.IGNORECASE)
_CODE_FENCE_RE
module-attribute
¶
_FIGURE_BLOCK_RE
module-attribute
¶
_HTML_COMMENT_RE
module-attribute
¶
_IN_BODY_ABSTRACT_RE
module-attribute
¶
_IN_BODY_ABSTRACT_RE = re.compile('(?:^|\\n)(?:#{1,6}\\s+|(?:\\*\\*|__)\\s*)Abstract(?:\\s*(?:\\*\\*|__)|:|\\s*\\n)([\\s\\S]*?)(?=(?:\\n#{1,6}\\s+|\\n<figure|\\Z))', re.IGNORECASE)
_clean_markdown_prose ¶
Strip markdown links, formatting, and excess whitespace from a text snippet.
Source code in zocrates/rag/summary_generator.py
_truncate_to_sentence_boundary ¶
Truncate text at the last complete sentence boundary within char and word budgets.
Source code in zocrates/rag/summary_generator.py
extract_lead_text ¶
Extract substantive lead text or in-body abstract deterministically without an LLM.
Implements the 3-step heuristic
- In-body 'Abstract' section detection (fast path).
- Prose paragraph accumulation & boilerplate filtering (general path).
- Safety fallback.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
Converted Markdown document text (with or without frontmatter). |
required |
Returns:
| Type | Description |
|---|---|
str
|
Clean, informative extractive lead text string. |
Source code in zocrates/rag/summary_generator.py
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 | |
generate_summary ¶
generate_summary(text, title=None, zotero_data_dir=None, model=None, timeout=DEFAULT_SUMMARY_TIMEOUT_SECONDS)
Generate a concise document summary using LiteLLM with extractive fallback.
Hierarchy
- Model: Explicit override, configured
summary_model,llm_model, oropenai/gpt-oss-20bvia OpenRouter. - Offline / Error Fallback: Deterministic extractive lead text.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
Converted Markdown text. |
required |
title
|
str | None
|
Optional document title. |
None
|
zotero_data_dir
|
Path | str | None
|
Optional Zotero data directory for loading config/keys. |
None
|
model
|
str | None
|
Optional explicit model override. |
None
|
timeout
|
float
|
Request timeout in seconds (default: 10s). |
DEFAULT_SUMMARY_TIMEOUT_SECONDS
|
Returns:
| Type | Description |
|---|---|
tuple[str, str]
|
A 2-tuple of |
Source code in zocrates/rag/summary_generator.py
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 | |
get_secure_api_key ¶
get_secure_api_key(key_name='openrouter_api_key', env_vars=('OPENROUTER_API_KEY', 'OPEN_ROUTER_API_KEY'))
Load a named API key from in-memory session environment variables.
Zotero Preferences / Login Manager serves as the secure persistent vault. Backend keys are in-memory session variables (os.environ) synced from Zotero.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key_name
|
str
|
Entry identifier (e.g., 'openrouter_api_key', 'openai_api_key'). |
'openrouter_api_key'
|
env_vars
|
list[str] | tuple[str, ...] | None
|
Environment variable names to check. |
('OPENROUTER_API_KEY', 'OPEN_ROUTER_API_KEY')
|
Returns:
| Type | Description |
|---|---|
str | None
|
The API key string or None if not found. |
Source code in zocrates/config.py
load_zocrates_config ¶
Load the Zocrates config from the data directory.
If zotero_data_dir is None, attempts to obtain it from the active
LibrarySyncer service or the ZOTERO_DATA_DIR environment variable.
Returns config merged with standard defaults.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
zotero_data_dir
|
Path | str | None
|
Root Zotero data directory. |
None
|
Returns:
| Type | Description |
|---|---|
dict
|
Parsed JSON config dict merged with defaults. |
Source code in zocrates/config.py
760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 | |
strip_existing_frontmatter ¶
Strip leading YAML frontmatter block if present.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
markdown
|
str
|
Input Markdown string. |
required |
Returns:
| Type | Description |
|---|---|
str
|
Markdown content with leading YAML frontmatter removed. |