Skip to content

Base

EmptyReasoningError

Bases: Exception

Raised when the extracted reasoning is empty.

Source code in src/kibad_llm/llms/base.py
38
39
40
41
class EmptyReasoningError(Exception):
    """Raised when the extracted reasoning is empty."""

    ...

EmptyResponseMessageError

Bases: Exception

Raised when the LLM response message is empty.

Source code in src/kibad_llm/llms/base.py
26
27
28
29
class EmptyResponseMessageError(Exception):
    """Raised when the LLM response message is empty."""

    ...

LLM

Bases: ABC

Source code in src/kibad_llm/llms/base.py
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
class LLM(ABC):

    @abstractmethod
    def call_llm_chat_with_guided_decoding(
        self,
        messages: list[SimpleChatMessage],
        *,
        json_schema: dict[str, Any] | None = None,
        **request_kwargs,
    ) -> ChatResponse:
        """Call a chat LLM with optional json schema for guided decoding."""
        ...

    def get_raw_message_from_chat_response(self, response: ChatResponse) -> Any:
        """Extract raw message from a chat response."""

        raw = response.raw
        if raw is None:
            raise MissingRawChatResponseError("ChatResponse is missing raw attribute.")

        try:
            msg = raw.choices[0].message
            return msg
        except (AttributeError, IndexError, TypeError):
            raise RawMessageExtractionError(
                "Could not extract message from chat response raw attribute."
            )

    def get_reasoning_from_chat_response(self, response: ChatResponse) -> str | None:
        """Extract reasoning from a chat response."""
        raise NotImplementedError(
            f"get_reasoning_from_chat_response() is not implemented for {type(self)}"
        )

    def get_response_content_from_chat_response(self, response: ChatResponse) -> str:
        """Extract content from chat response."""

        response_content = response.message.content
        if response_content is None:
            raise MissingResponseContentError("LLM response is missing content.")
        if not response_content.strip():
            raise EmptyResponseMessageError("LLM returned an empty message.")
        return response_content

call_llm_chat_with_guided_decoding(messages, *, json_schema=None, **request_kwargs) abstractmethod

Call a chat LLM with optional json schema for guided decoding.

Source code in src/kibad_llm/llms/base.py
52
53
54
55
56
57
58
59
60
61
@abstractmethod
def call_llm_chat_with_guided_decoding(
    self,
    messages: list[SimpleChatMessage],
    *,
    json_schema: dict[str, Any] | None = None,
    **request_kwargs,
) -> ChatResponse:
    """Call a chat LLM with optional json schema for guided decoding."""
    ...

get_raw_message_from_chat_response(response)

Extract raw message from a chat response.

Source code in src/kibad_llm/llms/base.py
63
64
65
66
67
68
69
70
71
72
73
74
75
76
def get_raw_message_from_chat_response(self, response: ChatResponse) -> Any:
    """Extract raw message from a chat response."""

    raw = response.raw
    if raw is None:
        raise MissingRawChatResponseError("ChatResponse is missing raw attribute.")

    try:
        msg = raw.choices[0].message
        return msg
    except (AttributeError, IndexError, TypeError):
        raise RawMessageExtractionError(
            "Could not extract message from chat response raw attribute."
        )

get_reasoning_from_chat_response(response)

Extract reasoning from a chat response.

Source code in src/kibad_llm/llms/base.py
78
79
80
81
82
def get_reasoning_from_chat_response(self, response: ChatResponse) -> str | None:
    """Extract reasoning from a chat response."""
    raise NotImplementedError(
        f"get_reasoning_from_chat_response() is not implemented for {type(self)}"
    )

get_response_content_from_chat_response(response)

Extract content from chat response.

Source code in src/kibad_llm/llms/base.py
84
85
86
87
88
89
90
91
92
def get_response_content_from_chat_response(self, response: ChatResponse) -> str:
    """Extract content from chat response."""

    response_content = response.message.content
    if response_content is None:
        raise MissingResponseContentError("LLM response is missing content.")
    if not response_content.strip():
        raise EmptyResponseMessageError("LLM returned an empty message.")
    return response_content

MissingRawChatResponseError

Bases: Exception

Raised when a ChatResponse is missing the raw attribute.

Source code in src/kibad_llm/llms/base.py
 8
 9
10
11
class MissingRawChatResponseError(Exception):
    """Raised when a ChatResponse is missing the raw attribute."""

    pass

MissingResponseContentError

Bases: Exception

Raised when the LLM response message has no content.

Source code in src/kibad_llm/llms/base.py
20
21
22
23
class MissingResponseContentError(Exception):
    """Raised when the LLM response message has no content."""

    ...

RawMessageExtractionError

Bases: Exception

Raised when a message cannot be extracted from a ChatResponse raw attribute.

Source code in src/kibad_llm/llms/base.py
14
15
16
17
class RawMessageExtractionError(Exception):
    """Raised when a message cannot be extracted from a ChatResponse raw attribute."""

    pass

ReasoningExtractionError

Bases: Exception

Raised when reasoning cannot be extracted from the LLM response message.

Source code in src/kibad_llm/llms/base.py
32
33
34
35
class ReasoningExtractionError(Exception):
    """Raised when reasoning cannot be extracted from the LLM response message."""

    ...