|
| 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