-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathCreateCompletionStreamed.scala
35 lines (29 loc) · 1.29 KB
/
CreateCompletionStreamed.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
package io.cequence.openaiscala.examples
import akka.stream.scaladsl.Sink
import io.cequence.openaiscala.service.OpenAIServiceFactory
import io.cequence.openaiscala.service.OpenAIStreamedServiceImplicits._
import io.cequence.openaiscala.service.StreamedServiceTypes.OpenAIStreamedService
import scala.concurrent.Future
// requires `openai-scala-client-stream` as a dependency
object CreateCompletionStreamed extends ExampleBase[OpenAIStreamedService] {
// note the OpenAIStreamedServiceImplicits import allowing to create an OpenAI service with streaming capabilities
override val service: OpenAIStreamedService = OpenAIServiceFactory.withStreaming()
private val text =
"""Extract the name and mailing address from this email:
|Dear Kelly,
|It was great to talk to you at the seminar. I thought Jane's talk was quite good.
|Thank you for the book. Here's my address 2111 Ash Lane, Crestview CA 92002
|Best,
|Maya
""".stripMargin
override protected def run: Future[_] =
service
.createCompletionStreamed(text)
.runWith(
Sink.foreach { response =>
val content = response.choices.headOption.map(_.text).getOrElse("")
Thread.sleep(100) // to spot how the individual words are being added
print(content)
}
)
}