Skip to content

input_model

Class info

Re-exported classes

Class Module Description
DefaultInputHandler llmling_models.models.input_model.input_handlers Default input handler using standard input.
InputHandler llmling_models.models.input_model.input_handlers Protocol for input handlers.
InputModel llmling_models.models.input_model.model Model that delegates responses to human input.

🛈 DocStrings

Input model.

DefaultInputHandler

Bases: InputHandler

Default input handler using standard input.

Source code in src/llmling_models/models/input_model/input_handlers.py
class DefaultInputHandler(InputHandler):
    """Default input handler using standard input."""

    def get_input(self, prompt: str) -> str:
        """Get input using basic console input."""
        return input(prompt).strip()

    async def stream_input(self, prompt: str) -> AsyncIterator[str]:
        """Simulate streaming input using standard input."""
        print(prompt, end="", flush=True)

        async def char_iterator() -> AsyncIterator[str]:
            while True:
                char = sys.stdin.read(1)
                if char == "\n":
                    break
                yield char

        return char_iterator()

    def format_messages(
        self,
        messages: list[ModelMessage],
        *,
        prompt_template: str,
        show_system: bool,
    ) -> str:
        """Format messages for display."""
        formatted: list[str] = []

        for message in messages:
            for part in message.parts:
                match part:
                    case SystemPromptPart(content=content) if show_system:
                        formatted.append(f"🔧 System: {content}")
                    case UserPromptPart(content=content):
                        formatted.append(prompt_template.format(prompt=content))
                    case TextPart(content=content):
                        formatted.append(f"Assistant: {content}")
                    case _:
                        continue

        return "\n\n".join(formatted)

format_messages

format_messages(messages: list[ModelMessage], *, prompt_template: str, show_system: bool) -> str

Format messages for display.

Source code in src/llmling_models/models/input_model/input_handlers.py
def format_messages(
    self,
    messages: list[ModelMessage],
    *,
    prompt_template: str,
    show_system: bool,
) -> str:
    """Format messages for display."""
    formatted: list[str] = []

    for message in messages:
        for part in message.parts:
            match part:
                case SystemPromptPart(content=content) if show_system:
                    formatted.append(f"🔧 System: {content}")
                case UserPromptPart(content=content):
                    formatted.append(prompt_template.format(prompt=content))
                case TextPart(content=content):
                    formatted.append(f"Assistant: {content}")
                case _:
                    continue

    return "\n\n".join(formatted)

get_input

get_input(prompt: str) -> str

Get input using basic console input.

Source code in src/llmling_models/models/input_model/input_handlers.py
def get_input(self, prompt: str) -> str:
    """Get input using basic console input."""
    return input(prompt).strip()

stream_input async

stream_input(prompt: str) -> AsyncIterator[str]

Simulate streaming input using standard input.

Source code in src/llmling_models/models/input_model/input_handlers.py
async def stream_input(self, prompt: str) -> AsyncIterator[str]:
    """Simulate streaming input using standard input."""
    print(prompt, end="", flush=True)

    async def char_iterator() -> AsyncIterator[str]:
        while True:
            char = sys.stdin.read(1)
            if char == "\n":
                break
            yield char

    return char_iterator()

InputHandler

Bases: Protocol

Protocol for input handlers.

Source code in src/llmling_models/models/input_model/input_handlers.py
@runtime_checkable
class InputHandler(Protocol):
    """Protocol for input handlers."""

    def get_input(self, prompt: str) -> str | Awaitable[str]:
        """Get single input response. Can be sync or async."""
        ...

    def stream_input(self, prompt: str) -> AsyncIterator[str] | Awaitable[AsyncIterator[str]]:
        """Stream input character by character. Can be sync or async."""
        ...

    def format_messages(
        self,
        messages: list[ModelMessage],
        *,
        prompt_template: str,
        show_system: bool,
    ) -> str:
        """Format messages for display."""
        ...

format_messages

format_messages(messages: list[ModelMessage], *, prompt_template: str, show_system: bool) -> str

Format messages for display.

Source code in src/llmling_models/models/input_model/input_handlers.py
def format_messages(
    self,
    messages: list[ModelMessage],
    *,
    prompt_template: str,
    show_system: bool,
) -> str:
    """Format messages for display."""
    ...

get_input

get_input(prompt: str) -> str | Awaitable[str]

Get single input response. Can be sync or async.

Source code in src/llmling_models/models/input_model/input_handlers.py
def get_input(self, prompt: str) -> str | Awaitable[str]:
    """Get single input response. Can be sync or async."""
    ...

stream_input

stream_input(prompt: str) -> AsyncIterator[str] | Awaitable[AsyncIterator[str]]

Stream input character by character. Can be sync or async.

Source code in src/llmling_models/models/input_model/input_handlers.py
def stream_input(self, prompt: str) -> AsyncIterator[str] | Awaitable[AsyncIterator[str]]:
    """Stream input character by character. Can be sync or async."""
    ...

InputModel dataclass

Bases: Model

Model that delegates responses to human input.

Source code in src/llmling_models/models/input_model/model.py
@dataclass
class InputModel(Model):
    """Model that delegates responses to human input."""

    prompt_template: str = field(default="👤 Please respond to: {prompt}")
    """Template for showing the prompt to the human."""

    show_system: bool = field(default=True)
    """Whether to show system messages to the human."""

    input_prompt: str = field(default="Your response: ")
    """Prompt to show when requesting input."""

    handler: InputHandler | type[InputHandler] = field(default=DefaultInputHandler)
    """Input handler class to use."""

    @property
    def model_name(self) -> str:
        """Return the model name."""
        return "input"

    @property
    def system(self) -> str:
        """Return the system/provider name."""
        return "input"

    async def request(
        self,
        messages: list[ModelMessage],
        model_settings: ModelSettings | None,
        model_request_parameters: ModelRequestParameters,
    ) -> ModelResponse:
        """Get response from human input."""
        # Format and display messages using handler
        handler = self.handler() if isinstance(self.handler, type) else self.handler
        display_text = handler.format_messages(
            messages,
            prompt_template=self.prompt_template,
            show_system=self.show_system,
        )
        print("\n" + "=" * 80, file=sys.stderr)
        print(display_text, file=sys.stderr)
        print("-" * 80, file=sys.stderr)

        # Get input using configured handler
        input_method = handler.get_input
        if inspect.iscoroutinefunction(input_method):
            response = await input_method(self.input_prompt)
        else:
            response_or_awaitable = input_method(self.input_prompt)
            if isinstance(response_or_awaitable, Awaitable):
                response = await response_or_awaitable
            else:
                response = response_or_awaitable

        return ModelResponse(parts=[TextPart(response)], timestamp=datetime.now(UTC))

    @asynccontextmanager
    async def request_stream(
        self,
        messages: list[ModelMessage],
        model_settings: ModelSettings | None,
        model_request_parameters: ModelRequestParameters,
        run_context: RunContext[Any] | None = None,
    ) -> AsyncGenerator[StreamedResponse]:
        """Stream responses character by character."""
        # Format and display messages using handler
        handler = self.handler() if isinstance(self.handler, type) else self.handler
        display_text = handler.format_messages(
            messages,
            prompt_template=self.prompt_template,
            show_system=self.show_system,
        )
        print("\n" + "=" * 80, file=sys.stderr)
        print(display_text, file=sys.stderr)
        print("-" * 80, file=sys.stderr)

        # Get streaming input using configured handler
        stream_method = handler.stream_input
        if inspect.iscoroutinefunction(stream_method):
            char_stream = await stream_method(self.input_prompt)
        else:
            stream_or_awaitable = stream_method(self.input_prompt)
            if isinstance(stream_or_awaitable, Awaitable):
                char_stream = await stream_or_awaitable
            else:
                char_stream = stream_or_awaitable
        reqs = ModelRequestParameters()
        yield InputStreamedResponse(model_request_parameters=reqs, stream=char_stream)

handler class-attribute instance-attribute

handler: InputHandler | type[InputHandler] = field(default=DefaultInputHandler)

Input handler class to use.

input_prompt class-attribute instance-attribute

input_prompt: str = field(default='Your response: ')

Prompt to show when requesting input.

model_name property

model_name: str

Return the model name.

prompt_template class-attribute instance-attribute

prompt_template: str = field(default='👤 Please respond to: {prompt}')

Template for showing the prompt to the human.

show_system class-attribute instance-attribute

show_system: bool = field(default=True)

Whether to show system messages to the human.

system property

system: str

Return the system/provider name.

request async

request(
    messages: list[ModelMessage],
    model_settings: ModelSettings | None,
    model_request_parameters: ModelRequestParameters,
) -> ModelResponse

Get response from human input.

Source code in src/llmling_models/models/input_model/model.py
async def request(
    self,
    messages: list[ModelMessage],
    model_settings: ModelSettings | None,
    model_request_parameters: ModelRequestParameters,
) -> ModelResponse:
    """Get response from human input."""
    # Format and display messages using handler
    handler = self.handler() if isinstance(self.handler, type) else self.handler
    display_text = handler.format_messages(
        messages,
        prompt_template=self.prompt_template,
        show_system=self.show_system,
    )
    print("\n" + "=" * 80, file=sys.stderr)
    print(display_text, file=sys.stderr)
    print("-" * 80, file=sys.stderr)

    # Get input using configured handler
    input_method = handler.get_input
    if inspect.iscoroutinefunction(input_method):
        response = await input_method(self.input_prompt)
    else:
        response_or_awaitable = input_method(self.input_prompt)
        if isinstance(response_or_awaitable, Awaitable):
            response = await response_or_awaitable
        else:
            response = response_or_awaitable

    return ModelResponse(parts=[TextPart(response)], timestamp=datetime.now(UTC))

request_stream async

request_stream(
    messages: list[ModelMessage],
    model_settings: ModelSettings | None,
    model_request_parameters: ModelRequestParameters,
    run_context: RunContext[Any] | None = None,
) -> AsyncGenerator[StreamedResponse]

Stream responses character by character.

Source code in src/llmling_models/models/input_model/model.py
@asynccontextmanager
async def request_stream(
    self,
    messages: list[ModelMessage],
    model_settings: ModelSettings | None,
    model_request_parameters: ModelRequestParameters,
    run_context: RunContext[Any] | None = None,
) -> AsyncGenerator[StreamedResponse]:
    """Stream responses character by character."""
    # Format and display messages using handler
    handler = self.handler() if isinstance(self.handler, type) else self.handler
    display_text = handler.format_messages(
        messages,
        prompt_template=self.prompt_template,
        show_system=self.show_system,
    )
    print("\n" + "=" * 80, file=sys.stderr)
    print(display_text, file=sys.stderr)
    print("-" * 80, file=sys.stderr)

    # Get streaming input using configured handler
    stream_method = handler.stream_input
    if inspect.iscoroutinefunction(stream_method):
        char_stream = await stream_method(self.input_prompt)
    else:
        stream_or_awaitable = stream_method(self.input_prompt)
        if isinstance(stream_or_awaitable, Awaitable):
            char_stream = await stream_or_awaitable
        else:
            char_stream = stream_or_awaitable
    reqs = ModelRequestParameters()
    yield InputStreamedResponse(model_request_parameters=reqs, stream=char_stream)