-
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 client for Google AI platform (#276)
- Loading branch information
Showing
3 changed files
with
192 additions
and
31 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,51 +1,89 @@ | ||
plugins { | ||
id(libs.plugins.kotlin.multiplatform.get().pluginId) | ||
id(libs.plugins.kotlinx.serialization.get().pluginId) | ||
alias(libs.plugins.spotless) | ||
alias(libs.plugins.arrow.gradle.publish) | ||
alias(libs.plugins.semver.gradle) | ||
id(libs.plugins.kotlin.multiplatform.get().pluginId) | ||
id(libs.plugins.kotlinx.serialization.get().pluginId) | ||
alias(libs.plugins.spotless) | ||
alias(libs.plugins.arrow.gradle.publish) | ||
alias(libs.plugins.semver.gradle) | ||
} | ||
|
||
repositories { | ||
mavenCentral() | ||
mavenCentral() | ||
} | ||
|
||
java { | ||
sourceCompatibility = JavaVersion.VERSION_11 | ||
targetCompatibility = JavaVersion.VERSION_11 | ||
toolchain { | ||
languageVersion = JavaLanguageVersion.of(11) | ||
} | ||
sourceCompatibility = JavaVersion.VERSION_11 | ||
targetCompatibility = JavaVersion.VERSION_11 | ||
toolchain { | ||
languageVersion = JavaLanguageVersion.of(11) | ||
} | ||
} | ||
|
||
kotlin { | ||
jvm() | ||
js(IR) { | ||
browser() | ||
nodejs() | ||
jvm() | ||
js(IR) { | ||
browser() | ||
nodejs() | ||
} | ||
|
||
linuxX64() | ||
macosX64() | ||
macosArm64() | ||
mingwX64() | ||
|
||
sourceSets { | ||
val commonMain by getting { | ||
dependencies { | ||
api(projects.xefCore) | ||
implementation(libs.bundles.ktor.client) | ||
} | ||
} | ||
|
||
val jvmMain by getting { | ||
dependencies { | ||
implementation(libs.logback) | ||
api(libs.ktor.client.cio) | ||
} | ||
} | ||
|
||
val jsMain by getting { | ||
dependencies { | ||
api(libs.ktor.client.js) | ||
} | ||
} | ||
|
||
linuxX64() | ||
macosX64() | ||
macosArm64() | ||
mingwX64() | ||
|
||
sourceSets { | ||
val commonMain by getting { | ||
dependencies { | ||
api(projects.xefCore) | ||
} | ||
} | ||
val linuxX64Main by getting { | ||
dependencies { | ||
api(libs.ktor.client.cio) | ||
} | ||
} | ||
|
||
val macosX64Main by getting { | ||
dependencies { | ||
api(libs.ktor.client.cio) | ||
} | ||
} | ||
|
||
val macosArm64Main by getting { | ||
dependencies { | ||
api(libs.ktor.client.cio) | ||
} | ||
} | ||
|
||
val mingwX64Main by getting { | ||
dependencies { | ||
api(libs.ktor.client.winhttp) | ||
} | ||
} | ||
} | ||
} | ||
|
||
spotless { | ||
kotlin { | ||
target("**/*.kt") | ||
ktfmt().googleStyle() | ||
} | ||
kotlin { | ||
target("**/*.kt") | ||
ktfmt().googleStyle() | ||
} | ||
} | ||
|
||
tasks.withType<AbstractPublishToMaven> { | ||
dependsOn(tasks.withType<Sign>()) | ||
dependsOn(tasks.withType<Sign>()) | ||
} |
116 changes: 116 additions & 0 deletions
116
integrations/gcp/src/commonMain/kotlin/com/xebia/functional/xef/gcp/GcpClient.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,116 @@ | ||
package com.xebia.functional.xef.gcp | ||
|
||
import com.xebia.functional.xef.AIError | ||
import io.ktor.client.HttpClient | ||
import io.ktor.client.call.body | ||
import io.ktor.client.plugins.HttpRequestRetry | ||
import io.ktor.client.plugins.HttpTimeout | ||
import io.ktor.client.plugins.contentnegotiation.ContentNegotiation | ||
import io.ktor.client.request.header | ||
import io.ktor.client.request.post | ||
import io.ktor.client.request.setBody | ||
import io.ktor.client.statement.bodyAsText | ||
import io.ktor.http.ContentType | ||
import io.ktor.http.HttpStatusCode | ||
import io.ktor.http.contentType | ||
import io.ktor.http.isSuccess | ||
import io.ktor.serialization.kotlinx.json.json | ||
import kotlinx.serialization.Serializable | ||
import kotlinx.serialization.json.Json | ||
|
||
@OptIn(ExperimentalStdlibApi::class) | ||
class GcpClient( | ||
private val apiEndpoint: String, | ||
private val projectId: String, | ||
private val modelId: String, | ||
private val token: String | ||
) : AutoCloseable { | ||
private val http: HttpClient = HttpClient { | ||
install(HttpTimeout) | ||
install(HttpRequestRetry) | ||
install(ContentNegotiation) { | ||
json( | ||
Json { | ||
encodeDefaults = false | ||
isLenient = true | ||
} | ||
) | ||
} | ||
} | ||
|
||
@Serializable | ||
private data class Prompt(val instances: List<Instance>, val parameters: Parameters? = null) | ||
|
||
@Serializable | ||
private data class Instance( | ||
val context: String? = null, | ||
val examples: List<Example>? = null, | ||
val messages: List<Message>, | ||
) | ||
|
||
@Serializable data class Example(val input: String, val output: String) | ||
|
||
@Serializable private data class Message(val author: String, val content: String) | ||
|
||
@Serializable | ||
private class Parameters( | ||
val temperature: Double? = null, | ||
val maxOutputTokens: Int? = null, | ||
val topK: Int? = null, | ||
val topP: Double? = null | ||
) | ||
|
||
@Serializable data class Response(val predictions: List<Predictions>) | ||
|
||
@Serializable | ||
data class SafetyAttributes( | ||
val blocked: Boolean, | ||
val scores: List<String>, | ||
val categories: List<String> | ||
) | ||
|
||
@Serializable data class CitationMetadata(val citations: List<String>) | ||
|
||
@Serializable data class Candidates(val author: String?, val content: String?) | ||
|
||
@Serializable | ||
data class Predictions( | ||
val safetyAttributes: List<SafetyAttributes>, | ||
val citationMetadata: List<CitationMetadata>, | ||
val candidates: List<Candidates> | ||
) | ||
|
||
suspend fun promptMessage( | ||
prompt: String, | ||
temperature: Double? = null, | ||
maxOutputTokens: Int? = null, | ||
topK: Int? = null, | ||
topP: Double? = null | ||
): String { | ||
val body = | ||
Prompt( | ||
listOf(Instance(messages = listOf(Message(author = "user", content = prompt)))), | ||
Parameters(temperature, maxOutputTokens, topK, topP) | ||
) | ||
val response = | ||
http.post( | ||
"https://$apiEndpoint/v1/projects/$projectId/locations/us-central1/publishers/google/models/$modelId:predict" | ||
) { | ||
header("Authorization", "Bearer $token") | ||
contentType(ContentType.Application.Json) | ||
setBody(body) | ||
} | ||
|
||
return if (response.status.isSuccess()) | ||
response.body<Response>().predictions.firstOrNull()?.candidates?.firstOrNull()?.content | ||
?: throw AIError.NoResponse() | ||
else throw GcpClientException(response.status, response.bodyAsText()) | ||
} | ||
|
||
class GcpClientException(val httpStatusCode: HttpStatusCode, val error: String) : | ||
IllegalStateException("$httpStatusCode: $error") | ||
|
||
override fun close() { | ||
http.close() | ||
} | ||
} |