Skip to content

Commit 0aa171b

Browse files
committed
update README with new retry usage
1 parent 7174cb5 commit 0aa171b

File tree

1 file changed

+37
-5
lines changed

1 file changed

+37
-5
lines changed

README.md

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,7 @@ Examples:
194194
}
195195
```
196196

197+
197198
**✔️ Important Note**: After you are done using the service, you should close it by calling (🔥 new) `service.close`. Otherwise, the underlying resources/threads won't be released.
198199

199200
**III. Using multiple services (🔥 new)**
@@ -228,17 +229,48 @@ Examples:
228229
}
229230
```
230231

232+
- Create completion and retry on transient errors (e.g. rate limit error)
233+
```scala
234+
import akka.actor.{ActorSystem, Scheduler}
235+
import io.cequence.openaiscala.RetryHelpers
236+
import io.cequence.openaiscala.RetryHelpers.RetrySettings
237+
import io.cequence.openaiscala.domain.{ChatRole, MessageSpec}
238+
import io.cequence.openaiscala.service.{OpenAIService, OpenAIServiceFactory}
239+
240+
import javax.inject.Inject
241+
import scala.concurrent.duration.DurationInt
242+
import scala.concurrent.{ExecutionContext, Future}
243+
244+
class MyCompletionService @Inject() (
245+
val actorSystem: ActorSystem,
246+
implicit val ec: ExecutionContext,
247+
implicit val scheduler: Scheduler
248+
)(val apiKey: String)
249+
extends RetryHelpers {
250+
val service: OpenAIService = OpenAIServiceFactory(apiKey)
251+
implicit val retrySettings: RetrySettings =
252+
RetrySettings(interval = 10.seconds)
253+
254+
def ask(prompt: String): Future[String] =
255+
for {
256+
completion <- service
257+
.createChatCompletion(
258+
List(MessageSpec(ChatRole.User, prompt))
259+
)
260+
.retryOnFailure
261+
} yield completion.choices.head.message.content
262+
}
263+
```
264+
231265
- Retries with `OpenAIRetryServiceAdapter`
232266

233267
```scala
234268
val serviceAux = ... // your service
235269

270+
implicit val retrySettings: RetrySettings =
271+
RetrySettings(maxAttempts = 10).constantInterval(10.seconds)
236272
// wrap it with the retry adapter
237-
val service = OpenAIRetryServiceAdapter(
238-
serviceAux,
239-
maxAttempts = 10,
240-
sleepOnFailureMs = Some(1000) // 1 second
241-
)
273+
val service = OpenAIRetryServiceAdapter(serviceAux)
242274

243275
service.listModels.map { models =>
244276
models.foreach(println)

0 commit comments

Comments
 (0)