yera.models.context.llm
Module containing the LLM context infrastructure.
Consists of * the context var that contains the current llm context. * the llm context class itself. * the llm_context function gets the active llm context.
Symbols
llm_context
llm_context() → LLMContextGet the current active LLM context.
Returns
the active context.
LLMContext
BaseModelContextManages the currently-active LLM instance and its execution context.
An LLM context encapsulates the interface to an LLM provider and provides access to the app workspace where message history and variables are stored. It acts as a context manager for proper initialization and cleanup of the LLM interface during execution.
Attributes: interface: The LLM provider interface for sending prompts and receiving responses. app_meta: Metadata about the app context this LLM is executing within.
Methods
LLMContext.add_sys_line
add_sys_line(
content: str,
) → NoneAdd a system message to the workspace and emit an event.
Parameters
The system message content.
LLMContext.add_user_line
add_user_line(
content: str,
) → NoneAdd a user message to the workspace and emit an event.
Parameters
The user message content.
LLMContext.add_assistant_line
add_assistant_line(
content: str,
) → NoneAdd an assistant message to the workspace.
Parameters
The assistant message content.
LLMContext.prompt_chat
prompt_chat(
prompt: str,
**kwargs,
) → strSend a prompt to the LLM and return the streamed chat response.
Adds the user prompt to the workspace, streams the response from the LLM interface, and records the assistant response in the workspace.
Parameters
The user prompt to send to the LLM.
Additional arguments to pass to the LLM interface.
Returns
The complete assistant response.
LLMContext.prompt_struct
prompt_struct(
prompt: str,
**kwargs,
) → TStructSend a prompt to the LLM and return a structured response.
Adds the user prompt to the workspace, requests a structured response from the LLM interface, parses the JSON response, and records it in the workspace.
Parameters
The user prompt to send to the LLM.
The Pydantic model class to parse the structured response into.
Additional arguments to pass to the LLM interface.
Returns
An instance of cls populated with the LLM response data.
LLMContext.__enter__
__enter__()Enter the context manager and initialize the LLM interface.
Captures the current app metadata, sets up the workspace, and initializes the LLM interface.
Returns
This context instance.
LLMContext.__exit__
__exit__(
exc_type: type[BaseException] | None,
exc_val: BaseException | None,
exc_tb: TracebackType | None,
)Exit the context manager and clean up the LLM interface.
Parameters
The exception type if an error occurred, else None.
The exception value if an error occurred, else None.
The exception traceback if an error occurred, else None.