AbanteAI / spice

accelerant
Apache License 2.0
4 stars 2 forks source link

Make Retry Strategy Abstraction #91

Closed jakethekoenig closed 4 months ago

jakethekoenig commented 4 months ago

Validators and Converters are nice but we want to support more complex behavior. For example it would be nice to experiment with the following:

This introduces a RetryStrategy Abstract class which takes in the present state of the iteration and returns the call_args for the next step or return if the response is good. The current behavior is re-implemented in the DefaultStrategy and the ValidatorStrategy and ConverterStrategy are introduced to show how this can be used to show the model why it failed to help it out.

The following is a minimal example:

import asyncio

from spice import Spice, print_stream
from spice.retry_strategy.validator_strategy import ValidatorStrategy

# Toy example where the model always fails from incomplete information but when it sees why it fails always succeeds.

def validator(x: str):
    if int(x) < 0:
        return True, ""
    else:
        return False, "Must be a negative integer"

strategy = ValidatorStrategy(validator=validator, retries=3)

client = Spice()
asyncio.run(
    client.get_response(
        [{"role": "user", "content": "return a number"}],
        model="gpt-3.5-turbo",
        streaming_callback=print_stream,
        retry_strategy=strategy,
    )
)

Closes #87