Skip to content

Commit fe468b1

Browse files
committed
Vertex AI - chat completion example with multiple regions
1 parent 1ad4529 commit fe468b1

File tree

4 files changed

+100
-3
lines changed

4 files changed

+100
-3
lines changed

anthropic-client/src/main/scala/io/cequence/openaiscala/anthropic/service/impl/Anthropic.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ trait Anthropic
1919
with HandleAnthropicErrorCodes
2020
with JsonFormats {
2121

22-
protected val logger = Logger(LoggerFactory.getLogger(this.getClass))
22+
protected val logger: Logger = Logger(LoggerFactory.getLogger(this.getClass))
2323

2424
protected def createBodyParamsForMessageCreation(
2525
messages: Seq[Message],

openai-core/src/main/scala/io/cequence/openaiscala/domain/ChatCompletionInterceptData.scala

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,6 @@ case class ChatCompletionInterceptData(
99
response: ChatCompletionResponse,
1010
timeRequestReceived: java.util.Date,
1111
timeResponseReceived: java.util.Date
12-
)
12+
) {
13+
def execTimeMs: Long = timeResponseReceived.getTime - timeRequestReceived.getTime
14+
}

openai-examples/src/main/scala/io/cequence/openaiscala/examples/nonopenai/VertexAICreateChatCompletionStreamedWithOpenAIAdapter.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ object VertexAICreateChatCompletionStreamedWithOpenAIAdapter
1616
override val service: OpenAIChatCompletionStreamedService = ChatCompletionProvider.vertexAI
1717

1818
// 2024-12-18: works only with us-central1
19-
private val model = NonOpenAIModelId.gemini_2_0_flash_exp
19+
private val model = NonOpenAIModelId.gemini_2_0_flash_thinking_exp_1219
2020

2121
private val messages = Seq(
2222
SystemMessage("You are a helpful assistant who makes jokes about Google. Use markdown"),
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
package io.cequence.openaiscala.examples.nonopenai
2+
3+
import com.typesafe.scalalogging.Logger
4+
import io.cequence.openaiscala.domain.settings.CreateChatCompletionSettings
5+
import io.cequence.openaiscala.domain.{NonOpenAIModelId, SystemMessage, UserMessage}
6+
import io.cequence.openaiscala.examples.ExampleBase
7+
import io.cequence.openaiscala.service.OpenAIChatCompletionService
8+
import io.cequence.openaiscala.service.adapter.OpenAIServiceAdapters
9+
import io.cequence.openaiscala.vertexai.service.VertexAIServiceFactory
10+
import org.slf4j.LoggerFactory
11+
12+
import scala.concurrent.Future
13+
14+
// requires `openai-scala-google-vertexai-client` as a dependency and `VERTEXAI_LOCATION` and `VERTEXAI_PROJECT_ID` environments variable to be set
15+
object VertexAIRegionsCreateChatCompletionWithOpenAIAdapter
16+
extends ExampleBase[OpenAIChatCompletionService] {
17+
18+
protected val logger: Logger = Logger(LoggerFactory.getLogger(this.getClass))
19+
20+
private val model = NonOpenAIModelId.gemini_2_0_flash_exp
21+
22+
private val messages = Seq(
23+
SystemMessage("You are a helpful assistant who makes jokes about Google."),
24+
UserMessage("What is the weather like in Norway?")
25+
)
26+
27+
private val vertexAILocations = Seq(
28+
"us-central1",
29+
"asia-east1",
30+
"asia-east2",
31+
"asia-northeast1", // model only supports up to 32767 tokens
32+
"asia-northeast3",
33+
"asia-south1",
34+
"asia-southeast1",
35+
"australia-southeast1", // model only supports up to 32767 tokens
36+
"europe-central2",
37+
"europe-north1",
38+
"europe-southwest1",
39+
"europe-west1",
40+
"europe-west2",
41+
"europe-west3",
42+
"europe-west4",
43+
"europe-west6",
44+
"europe-west8",
45+
"europe-west9",
46+
"me-central1",
47+
"me-central2",
48+
"me-west1",
49+
"northamerica-northeast1", // model only supports up to 32767 tokens
50+
"southamerica-east1",
51+
"us-east1",
52+
"us-east2",
53+
"us-east3",
54+
"us-east4", // seems slows but revisit
55+
"us-east5", // seems slows but revisit
56+
"us-south1",
57+
"us-west1",
58+
"us-west4"
59+
)
60+
61+
private val adapters = OpenAIServiceAdapters.forChatCompletionService
62+
63+
override val service: OpenAIChatCompletionService =
64+
adapters.roundRobin(
65+
vertexAILocations.map { location =>
66+
adapters.chatCompletionIntercept(data =>
67+
Future(
68+
logger.info(
69+
"Execution for the location {} succeeded! (took {} ms)",
70+
location,
71+
data.execTimeMs
72+
)
73+
)
74+
)(
75+
VertexAIServiceFactory.asOpenAI(location = location)
76+
)
77+
}: _*
78+
)
79+
80+
override protected def run: Future[_] =
81+
Future.sequence(vertexAILocations.map(_ => runForRegion)).map(_ => ())
82+
83+
private def runForRegion: Future[_] = {
84+
service.createChatCompletion(
85+
messages = messages,
86+
settings = CreateChatCompletionSettings(
87+
model,
88+
temperature = Some(0)
89+
)
90+
)
91+
}.recover { case e: Exception =>
92+
logger.error(s"Location FAILED due to ${e.getMessage}.")
93+
Future(())
94+
}
95+
}

0 commit comments

Comments
 (0)