davidmigloz / langchain_dart

Build LLM-powered Dart/Flutter applications.
https://langchaindart.dev
MIT License
426 stars 75 forks source link

feat: Retry support for Runnables #540

Closed Ganeshsivakumar closed 2 months ago

Ganeshsivakumar commented 2 months ago

Resolves #514

RunnableRetry wraps a Runnable and retries it if it fails. It be created using runnable.withRetry().

By default, the runnable will be retried 3 times with exponential backoff strategy.

final chatModel = ChatOpenAI();
final chatModelWithRetry = chatModel.withRetry(maxRetries: 5);
final res = await chatModelWithRetry.invoke(...);

RunnableRetry can be used to retry any Runnable, including a chain of Runnables.

final promptTemplate = ChatPromptTemplate.fromTemplate('tell me a joke about {topic}');
final model = ChatOpenAI();
final chain = promptTemplate.pipe(model).withRetry();
final res = await chain.batch([
    {'topic': 'bears'},
    {'topic': 'cats'},
]);
davidmigloz commented 2 months ago

@Ganeshsivakumar Let me know if my changes make sense to you. If so, I'll merge it 🚀

Ganeshsivakumar commented 2 months ago

@davidmigloz Lets merge it 🚀