Skip to content

DefaultInputHandler

Base classes

Name Children Inherits
InputHandler
llmling_models.models.input_model.input_handlers
Protocol for input handlers.

⋔ Inheritance diagram

graph TD
  1285377404["input_handlers.DefaultInputHandler"]
  3617281302["input_handlers.InputHandler"]
  1073422405["typing.Protocol"]
  3481597255["typing.Generic"]
  2751680898["builtins.object"]
  3617281302 --> 1285377404
  1073422405 --> 3617281302
  3481597255 --> 1073422405
  2751680898 --> 3481597255

🛈 DocStrings

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()

:fa-brands-github: Show source on GitHub