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.

🛈 DocStrings

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

convert_prompts async

convert_prompts(
    prompts: Sequence[PromptCompatible | BaseContent],
) -> list[str | BaseContent]

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
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
async def convert_prompts(
    prompts: Sequence[PromptCompatible | BaseContent],
) -> list[str | BaseContent]:
    """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
    """
    result: list[str | BaseContent] = []
    for p in prompts:
        match p:
            case os.PathLike() | upath.UPath():
                from mimetypes import guess_type

                path_obj = to_upath(p)
                mime_type, _ = guess_type(str(path_obj))

                match mime_type:
                    case "application/pdf":
                        content: BaseContent = 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 | BaseContent]) -> str

Format prompts for human readability using to_prompt.

Source code in src/llmling_agent/prompts/convert.py
60
61
62
63
64
65
async def format_prompts(prompts: Sequence[str | BaseContent]) -> 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)