Skip to content

Provider ABC

pycodeloop.abc.provider.Provider

Bases: ABC

Import

You can import the Provider class with:

from pycodeloop.abc.provider import Provider

Every LLM backend CodeLoop can drive implements this interface. Swap providers by passing a different instance to Agent(provider=...) or Config(provider=...).

Source code in pycodeloop/abc/provider.py
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
class Provider(ABC):
    """
    Import:
        You can import the **Provider** class with:

            from pycodeloop.abc.provider import Provider

    Every LLM backend CodeLoop can drive implements this interface. Swap
    providers by passing a different instance to `Agent(provider=...)`
    or `Config(provider=...)`.
    """

    name: str = "base"

    def __init__(self, model: str, api_key: str | None = None, **kwargs) -> None:
        self.model = model
        self.api_key = api_key
        self.extra = kwargs

    @abstractmethod
    def complete(
        self,
        system_prompt: str,
        messages: list,
        tools: list[dict],
        on_delta: Callable[[str], None] | None = None,
    ) -> ProviderResponse:
        """Send conversation + tool schema, return the model's response.

        When `on_delta` is given, call it with each text chunk as it
        arrives instead of only returning the assembled text at the end.
        """

api_key = api_key instance-attribute

extra = kwargs instance-attribute

model = model instance-attribute

name = 'base' class-attribute instance-attribute

__init__(model, api_key=None, **kwargs)

Source code in pycodeloop/abc/provider.py
53
54
55
56
def __init__(self, model: str, api_key: str | None = None, **kwargs) -> None:
    self.model = model
    self.api_key = api_key
    self.extra = kwargs

complete(system_prompt, messages, tools, on_delta=None) abstractmethod

Send conversation + tool schema, return the model's response.

When on_delta is given, call it with each text chunk as it arrives instead of only returning the assembled text at the end.

Source code in pycodeloop/abc/provider.py
58
59
60
61
62
63
64
65
66
67
68
69
70
@abstractmethod
def complete(
    self,
    system_prompt: str,
    messages: list,
    tools: list[dict],
    on_delta: Callable[[str], None] | None = None,
) -> ProviderResponse:
    """Send conversation + tool schema, return the model's response.

    When `on_delta` is given, call it with each text chunk as it
    arrives instead of only returning the assembled text at the end.
    """

pycodeloop.abc.provider.ProviderResponse dataclass

Source code in pycodeloop/abc/provider.py
30
31
32
33
34
35
36
@dataclass
class ProviderResponse:
    text: str = ""
    tool_calls: list[ToolCall] = field(default_factory=list)
    stop_reason: str = "end_turn"
    usage: Usage = field(default_factory=Usage)
    raw: Any = None

raw = None class-attribute instance-attribute

stop_reason = 'end_turn' class-attribute instance-attribute

text = '' class-attribute instance-attribute

tool_calls = field(default_factory=list) class-attribute instance-attribute

usage = field(default_factory=Usage) class-attribute instance-attribute

__init__(text='', tool_calls=list(), stop_reason='end_turn', usage=Usage(), raw=None)

pycodeloop.abc.provider.ToolCall dataclass

Source code in pycodeloop/abc/provider.py
11
12
13
14
15
@dataclass
class ToolCall:
    id: str
    name: str
    arguments: dict[str, Any]

arguments instance-attribute

id instance-attribute

name instance-attribute

__init__(id, name, arguments)

pycodeloop.abc.provider.Usage dataclass

Source code in pycodeloop/abc/provider.py
18
19
20
21
22
23
24
25
26
27
@dataclass
class Usage:
    input_tokens: int = 0
    output_tokens: int = 0

    def __add__(self, other: Usage) -> Usage:
        return Usage(
            input_tokens=self.input_tokens + other.input_tokens,
            output_tokens=self.output_tokens + other.output_tokens,
        )

input_tokens = 0 class-attribute instance-attribute

output_tokens = 0 class-attribute instance-attribute

__add__(other)

Source code in pycodeloop/abc/provider.py
23
24
25
26
27
def __add__(self, other: Usage) -> Usage:
    return Usage(
        input_tokens=self.input_tokens + other.input_tokens,
        output_tokens=self.output_tokens + other.output_tokens,
    )

__init__(input_tokens=0, output_tokens=0)