Custom Retry Handlers

Retry handlers validate the LLM response and automatically reprompts if fails.

from llumpy.providers import OllamaClient
from llumpy.retry import JSONRetryHandler

llama3_8b = OllamaClient('llama3', '8b')

print(llama3_8b.prompt_one("Hello!", handler=JSONRetryHandler(), retries=2))
terminal failure when exceed retries
from llumpy.providers import OllamaClient
from llumpy.retry import JSONRetryHandler

llama3_8b = OllamaClient('llama3', '8b')

print(llama3_8b.system("Only reply in JSON").user("Hello!").prompt(handler=JSONRetryHandler()))
terminal success with handler

Creating Custom Handler

Inherit the RetryHandler or AsyncRetryHandler class and methods. See JSONRetryHandler for an example implementation.

from typing import Any, Tuple, Type
from llumpy.retry import RetryHandler, AsyncRetryHandler


class MyRetryHandler(RetryHandler):
    def _format(self, response: str) -> Any:
        """Attempt to format the response to validate it and raise an exception"""
        pass

    @property
    def _retry_on(self) -> Tuple[Type[Exception], ...]:
        """(Optional) Return the tuple of exceptions thrown by _format"""
        pass


class MyAsyncRetryHandler(AsyncRetryHandler):
    def _format(self, response: str) -> Any:
        """Attempt to format the response to validate it and raise an exception"""
        pass

    @property
    def _retry_on(self) -> Tuple[Type[Exception], ...]:
        """(Optional) Return the tuple of exceptions thrown by _format"""
        pass