API: Inference Layer (LLM-backed)¶
Semantic front-end: Novel → Contract via Reader Response inference.
Inference interfaces for generating narrative contracts automatically.
The inference layer converts parsed Modules into ModuleContracts that include inferred ReaderState and module intent (expected_changes).
ContractInferencer ¶
Bases: ABC
Strategy interface for contract inference.
Implementations may use: - LLM inference (OpenAI API) - deterministic NLP features (NLTK/spaCy + rules) - hybrid approaches
infer
abstractmethod
¶
infer(modules, *, novel_title)
Infer a full contract for a set of modules.
:param modules: Parsed modules from a Novel. :param novel_title: Novel title for context. :return: Fully populated list of ModuleContract objects.
OpenAI LLM client adapter for contract inference.
This module isolates API integration so the rest of the codebase does not depend on OpenAI SDK details.
LLMClientConfig
dataclass
¶
Configuration for the LLM client.
:param model: Model name used for inference. :param timeout_s: Request timeout (seconds).
OpenAILLMClient ¶
OpenAI-backed LLM client using the official Python SDK.
Requires OPENAI_API_KEY to be set in the environment.
infer_json ¶
infer_json(*, user_prompt)
Call the model and return parsed JSON.
:param user_prompt: Prompt content for the module. :return: Parsed JSON dict. :raises ValueError: if output is not valid JSON.
LLM-based ContractInferencer implementation.
OpenAIContractInferencer ¶
Bases: ContractInferencer
Contract inference using OpenAI LLM calls.
This inferencer treats the novel as a sequence: - pre_state is the prior module's post_state - post_state is inferred from the current module text
High-level helpers to infer a narrative contract from annotated Markdown.
infer_contract_from_markdown ¶
infer_contract_from_markdown(markdown_text, *, title, inferencer)
Infer a complete narrative contract from annotated Markdown.
Pipeline
Annotated Markdown → Parse → Infer
These modules allow the system to read a novel and produce a contract without author annotation.
They do not replace authorship.
They expose it.