Skip to content

convert

Class info

Classes

Name Children Inherits
BaseContent
llmling_agent.models.content
Base class for special content types (non-text).
BaseImageContent
llmling_agent.models.content
Base for image content.
BasePDFContent
llmling_agent.models.content
Base for PDF document content.
ImageBase64Content
llmling_agent.models.content
Image from base64 data.

    🛈 DocStrings

    The main Agent. Can do all sort of crazy things.

    convert_prompts async

    convert_prompts(
        prompts: Sequence[AnyPromptType | Image | PathLike[str] | Content],
    ) -> list[str | Content]
    

    Convert prompts to our internal format.

    Handles: - PIL Images -> ImageBase64Content - UPath/PathLike -> Auto-detect and convert to appropriate Content - Regular prompts -> str via to_prompt - Content objects -> pass through

    Source code in src/llmling_agent/prompts/convert.py
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    async def convert_prompts(
        prompts: Sequence[AnyPromptType | PIL.Image.Image | os.PathLike[str] | Content],
    ) -> list[str | Content]:
        """Convert prompts to our internal format.
    
        Handles:
        - PIL Images -> ImageBase64Content
        - UPath/PathLike -> Auto-detect and convert to appropriate Content
        - Regular prompts -> str via to_prompt
        - Content objects -> pass through
        """
        from upath import UPath
    
        result: list[str | Content] = []
        for p in prompts:
            match p:
                case _ if is_pil_image(p):
                    # Only convert PIL images if PIL is available
                    result.append(ImageBase64Content.from_pil_image(p))  # type: ignore
    
                case os.PathLike():
                    from mimetypes import guess_type
    
                    path_obj = UPath(p)
                    mime_type, _ = guess_type(str(path_obj))
    
                    match mime_type:
                        case "application/pdf":
                            content: Content = await BasePDFContent.from_path(path_obj)
                            result.append(content)
                        case str() if mime_type.startswith("image/"):
                            content = await BaseImageContent.from_path(path_obj)
                            result.append(content)
                        case _:
                            # Non-media or unknown type
                            text = await read_path(path_obj)
                            result.append(text)
    
                case _ if not isinstance(p, BaseContent):
                    result.append(await to_prompt(p))
                case _:
                    result.append(p)  # type: ignore
        return result
    

    format_prompts async

    format_prompts(prompts: Sequence[str | Content]) -> str
    

    Format prompts for human readability using to_prompt.

    Source code in src/llmling_agent/prompts/convert.py
    81
    82
    83
    84
    85
    86
    async def format_prompts(prompts: Sequence[str | Content]) -> str:
        """Format prompts for human readability using to_prompt."""
        from toprompt import to_prompt
    
        parts = [await to_prompt(p) for p in prompts]
        return "\n\n".join(parts)
    

    is_pil_image

    is_pil_image(obj: Any) -> bool
    

    Check if object is a PIL.Image.Image instance without direct import.

    Source code in src/llmling_agent/prompts/convert.py
    27
    28
    29
    30
    31
    32
    33
    def is_pil_image(obj: Any) -> bool:
        """Check if object is a PIL.Image.Image instance without direct import."""
        if not find_spec("PIL"):
            return False
        import PIL.Image
    
        return isinstance(obj, PIL.Image.Image)