-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathCreateChatCompletionStreamedJson.scala
44 lines (38 loc) · 1.41 KB
/
CreateChatCompletionStreamedJson.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
package io.cequence.openaiscala.examples
import akka.stream.scaladsl.Sink
import io.cequence.openaiscala.domain._
import io.cequence.openaiscala.domain.settings.{
ChatCompletionResponseFormatType,
CreateChatCompletionSettings
}
import io.cequence.openaiscala.service.OpenAIStreamedServiceImplicits._
import io.cequence.openaiscala.service.StreamedServiceTypes.OpenAIStreamedService
import io.cequence.openaiscala.service.{OpenAIServiceConsts, OpenAIServiceFactory}
import scala.concurrent.Future
// requires `openai-scala-client-stream` as a dependency
object CreateChatCompletionStreamedJson
extends ExampleBase[OpenAIStreamedService]
with TestFixtures
with OpenAIServiceConsts {
override val service: OpenAIStreamedService = OpenAIServiceFactory.withStreaming()
private val messages = Seq(
SystemMessage(capitalsPrompt),
UserMessage("List all asian countries and their capitals.")
)
override protected def run: Future[_] =
service
.createChatCompletionStreamed(
messages = messages,
settings = CreateChatCompletionSettings(
model = ModelId.gpt_4_5_preview,
max_tokens = Some(1000),
response_format_type = Some(ChatCompletionResponseFormatType.json_schema),
jsonSchema = Some(capitalsSchemaDef1)
)
)
.runWith(
Sink.foreach { completion =>
print(completion.contentHead.getOrElse(""))
}
)
}