-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* GCP Pipeline jobs API - List * Finishes the Pipeline Endpoints implementation * Updates to conversational * spotless * add support for ktor's Closeable to AutoClose; extract common code of GCP HttpClient * extract common config code for accessing GCP web api (token, location, projectId) --------- Co-authored-by: ron <ron.spannagel@47deg.com>
- Loading branch information
1 parent
fd12b91
commit 5e84a22
Showing
10 changed files
with
232 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
examples/kotlin/src/main/kotlin/com/xebia/functional/xef/conversation/gpc/Pipelines.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package com.xebia.functional.xef.conversation.gpc | ||
|
||
import com.xebia.functional.gpt4all.conversation | ||
import com.xebia.functional.xef.env.getenv | ||
import com.xebia.functional.xef.gcp.GcpConfig | ||
import com.xebia.functional.xef.gcp.pipelines.GcpPipelinesClient | ||
|
||
suspend fun main() { | ||
conversation { | ||
val token = getenv("GCP_TOKEN") ?: error("missing gcp token") | ||
val pipelineClient = autoClose(GcpPipelinesClient(GcpConfig(token, "xefdemo", "us-central1"))) | ||
val answer = pipelineClient.list() | ||
println("\n🤖 $answer") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
integrations/gcp/src/commonMain/kotlin/com/xebia/functional/xef/gcp/GcpConfig.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package com.xebia.functional.xef.gcp | ||
|
||
data class GcpConfig( | ||
val token: String, | ||
val projectId: String, | ||
/** https://cloud.google.com/vertex-ai/docs/general/locations */ | ||
val location: String, // Supported us-central1 or europe-west4 | ||
) |
29 changes: 29 additions & 0 deletions
29
integrations/gcp/src/commonMain/kotlin/com/xebia/functional/xef/gcp/HttpClient.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package com.xebia.functional.xef.gcp | ||
|
||
import com.xebia.functional.xef.conversation.AutoClose | ||
import com.xebia.functional.xef.conversation.autoClose | ||
import io.ktor.client.* | ||
import io.ktor.client.plugins.* | ||
import io.ktor.client.plugins.contentnegotiation.* | ||
import io.ktor.serialization.kotlinx.json.* | ||
import kotlinx.serialization.json.Json | ||
|
||
/** default [HttpClient] to access GCP models using JSON */ | ||
internal fun AutoClose.jsonHttpClient(): HttpClient = | ||
HttpClient { | ||
install(HttpTimeout) { | ||
requestTimeoutMillis = 60_000 | ||
connectTimeoutMillis = 60_000 | ||
} | ||
install(HttpRequestRetry) | ||
install(ContentNegotiation) { | ||
json( | ||
Json { | ||
encodeDefaults = false | ||
isLenient = true | ||
ignoreUnknownKeys = true | ||
} | ||
) | ||
} | ||
} | ||
.let(::autoClose) |
148 changes: 148 additions & 0 deletions
148
...ns/gcp/src/commonMain/kotlin/com/xebia/functional/xef/gcp/pipelines/GcpPipelinesClient.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,148 @@ | ||
package com.xebia.functional.xef.gcp.pipelines | ||
|
||
import com.xebia.functional.xef.conversation.AutoClose | ||
import com.xebia.functional.xef.conversation.autoClose | ||
import com.xebia.functional.xef.gcp.GcpConfig | ||
import com.xebia.functional.xef.gcp.jsonHttpClient | ||
import io.ktor.client.* | ||
import io.ktor.client.call.* | ||
import io.ktor.client.request.* | ||
import io.ktor.client.statement.* | ||
import io.ktor.http.* | ||
import kotlinx.datetime.Instant | ||
import kotlinx.serialization.SerialName | ||
import kotlinx.serialization.Serializable | ||
|
||
class GcpPipelinesClient( | ||
private val config: GcpConfig, | ||
) : AutoClose by autoClose() { | ||
private val http: HttpClient = jsonHttpClient() | ||
|
||
// https://cloud.google.com/vertex-ai/docs/reference/rest/v1/projects.locations.pipelineJobs/list#google.cloud.aiplatform.v1.PipelineService.ListPipelineJobs | ||
@Serializable | ||
private data class ListPipelineJobs( | ||
val pipelineJobs: List<PipelineJob>? = null, | ||
val nextPageToken: String? = null | ||
) | ||
|
||
@Serializable | ||
enum class PipelineState { | ||
PIPELINE_STATE_UNSPECIFIED, | ||
PIPELINE_STATE_QUEUED, | ||
PIPELINE_STATE_PENDING, | ||
PIPELINE_STATE_RUNNING, | ||
PIPELINE_STATE_SUCCEEDED, | ||
PIPELINE_STATE_FAILED, | ||
PIPELINE_STATE_CANCELLING, | ||
PIPELINE_STATE_CANCELLED, | ||
PIPELINE_STATE_PAUSED | ||
} | ||
|
||
// https://cloud.google.com/vertex-ai/docs/reference/rest/v1/projects.locations.pipelineJobs#PipelineJob | ||
@Serializable | ||
data class PipelineJob( | ||
val name: String, | ||
val displayName: String, | ||
val createTime: Instant, | ||
val startTime: Instant?, | ||
val endTime: Instant?, | ||
val updateTime: Instant, | ||
val state: PipelineState | ||
) | ||
|
||
@Serializable | ||
data class CreatePipelineJob( | ||
val displayName: String, | ||
val runtimeConfig: RuntimeConfig, | ||
val templateUri: String | ||
) | ||
|
||
@Serializable | ||
data class RuntimeConfig(val gcsOutputDirectory: String, val parameterValues: ParameterValues) | ||
|
||
@Serializable | ||
data class ParameterValues( | ||
@SerialName("project") val project: String, | ||
@SerialName("model_display_name") val modelDisplayName: String, | ||
@SerialName("dataset_uri") val datasetUri: String, | ||
@SerialName("location") val location: String, | ||
@SerialName("large_model_reference") val largeModelReference: String, | ||
@SerialName("train_steps") val trainSteps: String, | ||
@SerialName("learning_rate_multiplier") val learningRateMultiplier: String | ||
) | ||
|
||
@Serializable | ||
data class Operation(val name: String, val done: Boolean, val error: OperationStatus?) | ||
|
||
@Serializable data class OperationStatus(val code: Int, val message: String) | ||
|
||
suspend fun list(): List<PipelineJob> { | ||
val response = | ||
http.get( | ||
"https://${config.location}-aiplatform.googleapis.com/v1/projects/${config.projectId}/locations/${config.location}/pipelineJobs" | ||
) { | ||
header("Authorization", "Bearer ${config.token}") | ||
contentType(ContentType.Application.Json) | ||
} | ||
|
||
return if (response.status.isSuccess()) response.body<ListPipelineJobs>().pipelineJobs.orEmpty() | ||
else throw GcpClientException(response.status, response.bodyAsText()) | ||
} | ||
|
||
suspend fun get(pipelineJobName: String): PipelineJob? { | ||
val response = | ||
http.get( | ||
"https://${config.location}-aiplatform.googleapis.com/v1/projects/${config.projectId}/locations/${config.location}/pipelineJobs/$pipelineJobName" | ||
) { | ||
header("Authorization", "Bearer ${config.token}") | ||
contentType(ContentType.Application.Json) | ||
} | ||
|
||
return if (response.status.isSuccess()) response.body<PipelineJob?>() | ||
else throw GcpClientException(response.status, response.bodyAsText()) | ||
} | ||
|
||
suspend fun create(pipelineJobId: String?, pipelineJob: CreatePipelineJob): PipelineJob? { | ||
val response = | ||
http.post( | ||
"https://${config.location}-aiplatform.googleapis.com/v1/projects/${config.projectId}/locations/${config.location}/pipelineJobs" | ||
) { | ||
header("Authorization", "Bearer ${config.token}") | ||
contentType(ContentType.Application.Json) | ||
parameter("pipelineJobId", pipelineJobId) | ||
setBody(pipelineJob) | ||
} | ||
|
||
return if (response.status.isSuccess()) response.body<PipelineJob?>() | ||
else throw GcpClientException(response.status, response.bodyAsText()) | ||
} | ||
|
||
suspend fun cancel(pipelineJobName: String): Unit { | ||
val response = | ||
http.post( | ||
"https://${config.location}-aiplatform.googleapis.com/v1/projects/${config.projectId}/locations/${config.location}/pipelineJobs/$pipelineJobName:cancel" | ||
) { | ||
header("Authorization", "Bearer ${config.token}") | ||
contentType(ContentType.Application.Json) | ||
} | ||
|
||
return if (response.status.isSuccess()) {} else | ||
throw GcpClientException(response.status, response.bodyAsText()) | ||
} | ||
|
||
suspend fun delete(pipelineJobName: String): Operation { | ||
val response = | ||
http.delete( | ||
"https://${config.location}-aiplatform.googleapis.com/v1/projects/${config.projectId}/locations/${config.location}/pipelineJobs/$pipelineJobName" | ||
) { | ||
header("Authorization", "Bearer ${config.token}") | ||
contentType(ContentType.Application.Json) | ||
} | ||
|
||
return if (response.status.isSuccess()) response.body<Operation>() | ||
else throw GcpClientException(response.status, response.bodyAsText()) | ||
} | ||
|
||
class GcpClientException(val httpStatusCode: HttpStatusCode, val error: String) : | ||
IllegalStateException("$httpStatusCode: $error") | ||
} |