-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathRoundRobinAdapterExample.scala
54 lines (42 loc) · 1.59 KB
/
RoundRobinAdapterExample.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
package io.cequence.openaiscala.examples.adapters
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 scala.concurrent.Future
object RoundRobinAdapterExample extends ExampleBase[OpenAIService] {
private val adapters = OpenAIServiceAdapters.forFullService
// OpenAI
private val openAIService1 = adapters.log(
OpenAIServiceFactory(),
"openAIService1",
println(_) // simple logging
)
private val openAIService2 = adapters.log(
OpenAIServiceFactory(), // normally we would pass here e.g. a different access token
"openAIService2",
println(_) // simple logging
)
override val service: OpenAIService = adapters.roundRobin(openAIService1, openAIService2)
private val messages = Seq(
SystemMessage("You are a helpful assistant."),
UserMessage("What is the weather like in Norway?")
)
override protected def run: Future[_] =
for {
// runs on the first service
_ <- runChatCompletionAux(ModelId.gpt_4o)
// runs on the second service
_ <- runChatCompletionAux(ModelId.gpt_4o)
} yield ()
private def runChatCompletionAux(model: String) = {
println(s"Running chat completion with the model '$model'\n")
service
.createChatCompletion(
messages = messages,
settings = CreateChatCompletionSettings(model)
)
.map(printMessageContent)
}
}