Mirascope / mirascope

LLM abstractions that aren't obstructions
http://mirascope.com/
MIT License
757 stars 50 forks source link

decorator of decorator that turns a list of decorators into a single decorator #661

Closed CharlesFarhat closed 1 week ago

CharlesFarhat commented 2 weeks ago

Description

Would be super useful to solve situation where you have a stack of decorators :

    @retry() -> last catch the error and retry
    @handle_errors -> if call crashes, for now hacky but will save the usage and pass the error
    @save_usage(callback=lambda metrics: print(f"\nMetrics: {metrics}")) -> will save usage of each call
    @with_logfire() -> record the call
    @litellm.call(model="gpt-4o-mini", stream=True, call_params={"stream_options": {"include_usage": True}})
    @prompt_template(
willbakst commented 1 week ago

I think we should implement a helper method with proper typing that converts a list of decorators into a single decorator.

For example, maybe something like this:

from mirascope.core import merge_decorators

litellm_stream = merge_decorators(
    litellm.call("gpt-4o-mini", stream=True),
    with_logfire(),
    save_usage(callback=lambda metrics: print(f"\nMetrics: {metrics}")),
    handle_errors(),
    retry(),
)

@litellm_stream()
@prompt_template("Recommend a {genre} book")
def recommend_book(genre: str): ...

We already have something like this implemented for BasePrompt.run, but it will likely require some additional figuring out to make it work well

willbakst commented 1 week ago

This is released in v1.8!