Skip to content

base

Class info

Classes

Name Children Inherits
ResourceProvider
llmling_agent.resource_providers.base
Base class for resource providers.

🛈 DocStrings

Base resource provider interface.

ResourceProvider

Base class for resource providers.

Provides tools, prompts, and other resources to agents. Default implementations return empty lists - override as needed.

Source code in src/llmling_agent/resource_providers/base.py
16
17
18
19
20
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
class ResourceProvider:
    """Base class for resource providers.

    Provides tools, prompts, and other resources to agents.
    Default implementations return empty lists - override as needed.
    """

    def __init__(self, name: str, owner: str | None = None) -> None:
        """Initialize the resource provider."""
        self.name = name
        self.owner = owner

    async def __aenter__(self) -> Self:
        """Async context entry if required."""
        return self

    async def __aexit__(self, *exc: object) -> None:
        """Async context cleanup if required."""

    def __repr__(self) -> str:
        return f"{self.__class__.__name__}(name={self.name!r})"

    @property
    def requires_async(self) -> bool:
        return False

    async def get_tools(self) -> list[ToolInfo]:
        """Get available tools. Override to provide tools."""
        return []

    async def get_prompts(self) -> list[BasePrompt]:
        """Get available prompts. Override to provide prompts."""
        return []

    async def get_resources(self) -> list[ResourceInfo]:
        """Get available resources. Override to provide resources."""
        return []

    async def get_formatted_prompt(
        self, name: str, arguments: dict[str, str] | None = None
    ) -> ChatMessage[str]:
        """Get a prompt formatted with arguments.

        Args:
            name: Name of the prompt to format
            arguments: Optional arguments for prompt formatting

        Returns:
            Single chat message with merged content

        Raises:
            KeyError: If prompt not found
            ValueError: If formatting fails
        """
        from llmling_agent.messaging.messages import ChatMessage

        prompts = await self.get_prompts()
        prompt = next((p for p in prompts if p.name == name), None)
        if not prompt:
            msg = f"Prompt {name!r} not found"
            raise KeyError(msg)

        messages = await prompt.format(arguments or {})
        if not messages:
            msg = f"Prompt {name!r} produced no messages"
            raise ValueError(msg)

        # Use role from first message (usually system)
        role = messages[0].role
        # Merge all message contents
        content = "\n\n".join(msg.get_text_content() for msg in messages)

        return ChatMessage(
            content=content,
            role=role,  # type: ignore
            name=self.name,
            metadata={
                "prompt_name": name,
                "arguments": arguments or {},  # type: ignore
            },
        )

__aenter__ async

__aenter__() -> Self

Async context entry if required.

Source code in src/llmling_agent/resource_providers/base.py
28
29
30
async def __aenter__(self) -> Self:
    """Async context entry if required."""
    return self

__aexit__ async

__aexit__(*exc: object) -> None

Async context cleanup if required.

Source code in src/llmling_agent/resource_providers/base.py
32
33
async def __aexit__(self, *exc: object) -> None:
    """Async context cleanup if required."""

__init__

__init__(name: str, owner: str | None = None) -> None

Initialize the resource provider.

Source code in src/llmling_agent/resource_providers/base.py
23
24
25
26
def __init__(self, name: str, owner: str | None = None) -> None:
    """Initialize the resource provider."""
    self.name = name
    self.owner = owner

get_formatted_prompt async

get_formatted_prompt(
    name: str, arguments: dict[str, str] | None = None
) -> ChatMessage[str]

Get a prompt formatted with arguments.

Parameters:

Name Type Description Default
name str

Name of the prompt to format

required
arguments dict[str, str] | None

Optional arguments for prompt formatting

None

Returns:

Type Description
ChatMessage[str]

Single chat message with merged content

Raises:

Type Description
KeyError

If prompt not found

ValueError

If formatting fails

Source code in src/llmling_agent/resource_providers/base.py
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
async def get_formatted_prompt(
    self, name: str, arguments: dict[str, str] | None = None
) -> ChatMessage[str]:
    """Get a prompt formatted with arguments.

    Args:
        name: Name of the prompt to format
        arguments: Optional arguments for prompt formatting

    Returns:
        Single chat message with merged content

    Raises:
        KeyError: If prompt not found
        ValueError: If formatting fails
    """
    from llmling_agent.messaging.messages import ChatMessage

    prompts = await self.get_prompts()
    prompt = next((p for p in prompts if p.name == name), None)
    if not prompt:
        msg = f"Prompt {name!r} not found"
        raise KeyError(msg)

    messages = await prompt.format(arguments or {})
    if not messages:
        msg = f"Prompt {name!r} produced no messages"
        raise ValueError(msg)

    # Use role from first message (usually system)
    role = messages[0].role
    # Merge all message contents
    content = "\n\n".join(msg.get_text_content() for msg in messages)

    return ChatMessage(
        content=content,
        role=role,  # type: ignore
        name=self.name,
        metadata={
            "prompt_name": name,
            "arguments": arguments or {},  # type: ignore
        },
    )

get_prompts async

get_prompts() -> list[BasePrompt]

Get available prompts. Override to provide prompts.

Source code in src/llmling_agent/resource_providers/base.py
46
47
48
async def get_prompts(self) -> list[BasePrompt]:
    """Get available prompts. Override to provide prompts."""
    return []

get_resources async

get_resources() -> list[ResourceInfo]

Get available resources. Override to provide resources.

Source code in src/llmling_agent/resource_providers/base.py
50
51
52
async def get_resources(self) -> list[ResourceInfo]:
    """Get available resources. Override to provide resources."""
    return []

get_tools async

get_tools() -> list[ToolInfo]

Get available tools. Override to provide tools.

Source code in src/llmling_agent/resource_providers/base.py
42
43
44
async def get_tools(self) -> list[ToolInfo]:
    """Get available tools. Override to provide tools."""
    return []