Skip to content

input_handlers

Class info

Classes

Name Children Inherits
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.

    🛈 DocStrings

    Input handling for interactive models.

    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."""
        ...