-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathRetryAdapterExample.scala
72 lines (58 loc) · 2.49 KB
/
RetryAdapterExample.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package io.cequence.openaiscala.examples.adapters
import io.cequence.openaiscala.RetryHelpers.RetrySettings
import io.cequence.openaiscala.domain.settings.CreateChatCompletionSettings
import io.cequence.openaiscala.domain.{ModelId, SystemMessage, UserMessage}
import io.cequence.openaiscala.examples.ExampleBase
import io.cequence.openaiscala.service._
import io.cequence.openaiscala.service.adapter.OpenAIServiceAdapters
import io.cequence.openaiscala.{OpenAIScalaClientException, OpenAIScalaClientTimeoutException}
import scala.concurrent.Future
object RetryAdapterExample extends ExampleBase[OpenAIService] {
// adapters to use (round-robin, retry, etc.)
private val adapters = OpenAIServiceAdapters.forFullService
// implicit retry settings and scheduler
private implicit val retrySettings: RetrySettings = RetrySettings(maxRetries = 4)
// regular OpenAI service
private val regularService = OpenAIServiceFactory()
// to demonstrate the retry mechanism we introduce a service that always times out
private val failingService = adapters.preAction(
OpenAIServiceFactory(),
() => Future(throw new OpenAIScalaClientTimeoutException("Fake timeout"))
)
// we then map the failing service to a specific model - gpt_4o
// for all other models we use the regular service
private val mergedService = adapters.chatCompletionRouter(
serviceModels = Map(failingService -> Seq(ModelId.gpt_4o)),
regularService
)
// and finally we apply the retry mechanism to the merged service
override val service: OpenAIService = adapters.retry(
mergedService,
Some(println(_)) // simple logging
)
private val messages = Seq(
SystemMessage("You are a helpful assistant."),
UserMessage("What is the weather like in Norway?")
)
override protected def run: Future[_] =
for {
// this invokes the failing service, which triggers the retry mechanism
_ <- runChatCompletionAux(ModelId.gpt_4o).recover { case e: OpenAIScalaClientException =>
println(s"Too many retries, giving up on '${e.getMessage}'")
}
// should complete without retry
_ <- runChatCompletionAux(ModelId.o3_mini)
} yield ()
private def runChatCompletionAux(model: String) = {
println(s"Running chat completion with the model '$model'\n")
service
.createChatCompletion(
messages = messages,
settings = CreateChatCompletionSettings(model)
)
.map { response =>
printMessageContent(response)
println("--------")
}
}
}