Compile a short document using a multi-step LLM pipeline with caching. Step 1: Build base context A (schema + doc content), generate summary. Steps 2-4: Delegated to ``_compile_concepts``.
(
doc_name: str,
source_path: Path,
kb_dir: Path,
model: str,
max_concurrency: int = DEFAULT_COMPILE_CONCURRENCY,
)
| 2174 | |
| 2175 | |
| 2176 | async def compile_short_doc( |
| 2177 | doc_name: str, |
| 2178 | source_path: Path, |
| 2179 | kb_dir: Path, |
| 2180 | model: str, |
| 2181 | max_concurrency: int = DEFAULT_COMPILE_CONCURRENCY, |
| 2182 | ) -> None: |
| 2183 | """Compile a short document using a multi-step LLM pipeline with caching. |
| 2184 | |
| 2185 | Step 1: Build base context A (schema + doc content), generate summary. |
| 2186 | Steps 2-4: Delegated to ``_compile_concepts``. |
| 2187 | """ |
| 2188 | from openkb.config import load_config |
| 2189 | |
| 2190 | openkb_dir = kb_dir / ".openkb" |
| 2191 | config = load_config(openkb_dir / "config.yaml") |
| 2192 | language: str = config.get("language", "en") |
| 2193 | entity_types = resolve_entity_types(config) |
| 2194 | |
| 2195 | wiki_dir = kb_dir / "wiki" |
| 2196 | schema_md = get_agents_md(wiki_dir) |
| 2197 | content = source_path.read_text(encoding="utf-8") |
| 2198 | |
| 2199 | # Base context A: system + document. cache_control marker on the doc |
| 2200 | # message creates a cache breakpoint that covers (system + doc) for |
| 2201 | # every downstream call (summary, concepts-plan, every concept page). |
| 2202 | system_msg = { |
| 2203 | "role": "system", |
| 2204 | "content": _SYSTEM_TEMPLATE.format( |
| 2205 | schema_md=schema_md, |
| 2206 | language=language, |
| 2207 | ), |
| 2208 | } |
| 2209 | doc_msg = { |
| 2210 | "role": "user", |
| 2211 | "content": _cached_text( |
| 2212 | _SUMMARY_USER.format( |
| 2213 | doc_name=doc_name, |
| 2214 | content=content, |
| 2215 | ) |
| 2216 | ), |
| 2217 | } |
| 2218 | |
| 2219 | # --- Step 1: Generate summary (v1, held in memory) --- |
| 2220 | # The summary is NOT written to disk yet — it's used as cache context |
| 2221 | # for the plan + concept-generation calls, then rewritten into a final |
| 2222 | # v2 (with a whitelist of known wikilink targets) inside |
| 2223 | # _compile_concepts before being written to disk. |
| 2224 | summary_raw = _llm_call( |
| 2225 | model, [system_msg, doc_msg], "summary", response_format=_JSON_RESPONSE_FORMAT |
| 2226 | ) |
| 2227 | try: |
| 2228 | summary_parsed = _parse_json(summary_raw) |
| 2229 | doc_brief = summary_parsed.get("description", "") |
| 2230 | summary = summary_parsed.get("content", summary_raw) |
| 2231 | except (json.JSONDecodeError, ValueError): |
| 2232 | doc_brief = "" |
| 2233 | summary = summary_raw |