Skip to content

utils

Class info

Classes

Name Children Inherits
ToolDisplay
llmling.cli.utils
Display representation of a LLMCallableTool.

    🛈 DocStrings

    ToolDisplay

    Bases: Schema

    Display representation of a LLMCallableTool.

    Source code in src/llmling/cli/utils.py
    class ToolDisplay(Schema):
        """Display representation of a LLMCallableTool."""
    
        name: str
        description: str
        function_schema: OpenAIFunctionTool
        system_prompt: str | None = None
        import_path: str | None = None
    

    format_output

    format_output(result: Any, output_format: OutputFormat = 'text') -> None
    

    Format and print data in the requested format.

    Parameters:

    Name Type Description Default
    result Any

    Object to format (BaseModel, dict, or sequence)

    required
    output_format OutputFormat

    One of: text, json, yaml

    'text'
    Source code in src/llmling/cli/utils.py
    def format_output(
        result: Any,
        output_format: OutputFormat = "text",
    ) -> None:
        """Format and print data in the requested format.
    
        Args:
            result: Object to format (BaseModel, dict, or sequence)
            output_format: One of: text, json, yaml
        """
        data = prepare_for_output(result)
        from rich.console import Console
    
        console = Console()
        match output_format:
            case "json":
                if isinstance(data, BaseModel):
                    print(data.model_dump_json(indent=2))
                else:
                    print(json.dumps(data, indent=2))
            case "yaml":
                import yamling
    
                if isinstance(data, BaseModel):
                    print(yamling.dump_yaml(data.model_dump()))
                else:
                    print(yamling.dump_yaml(data))
            case "text":
                console.print(data)
            case _:
                msg = f"Unknown format: {output_format}"
                raise ValueError(msg)
    

    get_command_help

    get_command_help(base_help: str) -> str
    

    Get command help text with active config information.

    Source code in src/llmling/cli/utils.py
    def get_command_help(base_help: str) -> str:
        """Get command help text with active config information."""
        if active := config_store.get_active():
            return f"{base_help}\n\n(Using config: {active})"
        return f"{base_help}\n\n(No active config set)"
    

    prepare_for_output

    prepare_for_output(obj: Any) -> BaseModel | dict[str, Any] | list[Any]
    

    Prepare object for output formatting.

    Converts LLMCallableTools to display models, keeps dicts as-is, and handles sequences.

    Source code in src/llmling/cli/utils.py
    def prepare_for_output(obj: Any) -> BaseModel | dict[str, Any] | list[Any]:
        """Prepare object for output formatting.
    
        Converts LLMCallableTools to display models, keeps dicts as-is,
        and handles sequences.
        """
        from llmling.tools.base import LLMCallableTool
    
        match obj:
            case LLMCallableTool():
                return ToolDisplay(
                    name=obj.name,
                    description=obj.description,
                    function_schema=obj.get_schema(),
                    # system_prompt=obj.system_prompt,
                    import_path=obj.import_path,
                )
            case list() | tuple():
                return [prepare_for_output(item) for item in obj]
            case BaseModel():
                return obj
            case dict():
                return obj
            case _:
                msg = f"Cannot format type {type(obj)}"
                raise TypeError(msg)