-
Notifications
You must be signed in to change notification settings - Fork 24
/
VertexAICreateChatCompletionStreamedWithOpenAIAdapter.scala
41 lines (34 loc) · 1.53 KB
/
VertexAICreateChatCompletionStreamedWithOpenAIAdapter.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
package io.cequence.openaiscala.examples.nonopenai
import akka.stream.scaladsl.Sink
import io.cequence.openaiscala.domain.settings.CreateChatCompletionSettings
import io.cequence.openaiscala.domain.{NonOpenAIModelId, SystemMessage, UserMessage}
import io.cequence.openaiscala.examples.ExampleBase
import io.cequence.openaiscala.service.OpenAIChatCompletionService
import io.cequence.openaiscala.service.StreamedServiceTypes.OpenAIChatCompletionStreamedService
import scala.concurrent.Future
// requires `openai-scala-google-vertexai-client` as a dependency and `VERTEXAI_LOCATION` and `VERTEXAI_PROJECT_ID` environments variable to be set
object VertexAICreateChatCompletionStreamedWithOpenAIAdapter
extends ExampleBase[OpenAIChatCompletionService] {
override val service: OpenAIChatCompletionStreamedService = ChatCompletionProvider.vertexAI
// 2024-12-18: works only with us-central1
private val model = NonOpenAIModelId.gemini_2_0_flash_exp
private val messages = Seq(
SystemMessage("You are a helpful assistant who makes jokes about Google. Use markdown"),
UserMessage("What is the weather like in Norway?")
)
override protected def run: Future[_] =
service
.createChatCompletionStreamed(
messages = messages,
settings = CreateChatCompletionSettings(
model,
temperature = Some(0)
)
)
.runWith(
Sink.foreach { completion =>
val content = completion.choices.headOption.flatMap(_.delta.content)
print(content.getOrElse(""))
}
)
}