-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathExample.scala
45 lines (35 loc) · 1.24 KB
/
Example.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
package io.cequence.openaiscala.examples
import akka.actor.{ActorSystem, Scheduler}
import akka.stream.Materializer
import io.cequence.openaiscala.domain.response.ChatCompletionResponse
import io.cequence.openaiscala.service.{OpenAIService, OpenAIServiceFactory}
import io.cequence.wsclient.service.CloseableService
import scala.concurrent.{ExecutionContext, Future}
trait Example extends ExampleBase[OpenAIService] {
override protected val service = OpenAIServiceFactory()
}
trait ExampleBase[T <: CloseableService] {
implicit val system: ActorSystem = ActorSystem()
implicit val materializer: Materializer = Materializer(system)
implicit val scheduler: Scheduler = system.scheduler
implicit val ec: ExecutionContext = ExecutionContext.Implicits.global
protected val service: T
def main(args: Array[String]): Unit = {
run.recover { case e: Exception =>
println("Error received:")
e.printStackTrace()
closeAll()
System.exit(1)
}.onComplete { _ =>
closeAll()
System.exit(0)
}
}
private def closeAll() = {
service.close()
system.terminate()
}
protected def run: Future[_]
protected def printMessageContent(response: ChatCompletionResponse): Unit =
println(response.contentHead)
}