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

def llm_context — Get the current active LLM context.
class LLMContext — Manages the currently-active LLM instance and its execution context.

llm_context

llm_context() → LLMContext

Get the current active LLM context.

Returns

type: LLMContext

the active context.

LLMContext

Manages 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

add_sys_line — Add a system message to the workspace and emit an event.
add_user_line — Add a user message to the workspace and emit an event.
add_assistant_line — Add an assistant message to the workspace.
prompt_chat — Send a prompt to the LLM and return the streamed chat response.
prompt_struct — Send a prompt to the LLM and return a structured response.
__enter__ — Enter the context manager and initialize the LLM interface.
__exit__ — Exit the context manager and clean up the LLM interface.

LLMContext.add_sys_line

add_sys_line(
    content: str,
) → None

Add a system message to the workspace and emit an event.

Parameters

content
type: str

The system message content.

LLMContext.add_user_line

add_user_line(
    content: str,
) → None

Add a user message to the workspace and emit an event.

Parameters

content
type: str

The user message content.

LLMContext.add_assistant_line

add_assistant_line(
    content: str,
) → None

Add an assistant message to the workspace.

Parameters

content
type: str

The assistant message content.

LLMContext.prompt_chat

prompt_chat(
    prompt: str,
    **kwargs,
) → str

Send 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

prompt
type: str

The user prompt to send to the LLM.

**kwargs
type: str | int | float

Additional arguments to pass to the LLM interface.

Returns

type: str

The complete assistant response.

LLMContext.prompt_struct

prompt_struct(
    prompt: str,
    **kwargs,
) → TStruct

Send 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

prompt
type: str

The user prompt to send to the LLM.

cls
type: type[TStruct]

The Pydantic model class to parse the structured response into.

**kwargs
type: str | float | int

Additional arguments to pass to the LLM interface.

Returns

type: TStruct

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

exc_type
type: type[BaseException] | None

The exception type if an error occurred, else None.

exc_val
type: BaseException | None

The exception value if an error occurred, else None.

exc_tb
type: TracebackType | None

The exception traceback if an error occurred, else None.