-
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.
* First approach for GCP runtime * first rework of gcp instantiation [WIP] * revert changes out of scope of 0.0.3 release and issue * location and projectId now defaulted from env vars * small changes according to pr comments --------- Co-authored-by: Javi Pacheco <[email protected]>
- Loading branch information
1 parent
3105b29
commit 61a48cb
Showing
9 changed files
with
119 additions
and
43 deletions.
There are no files selected for viewing
28 changes: 5 additions & 23 deletions
28
examples/kotlin/src/main/kotlin/com/xebia/functional/xef/conversation/gpc/Chat.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
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
75 changes: 75 additions & 0 deletions
75
integrations/gcp/src/commonMain/kotlin/com/xebia/functional/xef/gcp/GCP.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,75 @@ | ||
package com.xebia.functional.xef.gcp | ||
|
||
import arrow.core.nonEmptyListOf | ||
import com.xebia.functional.xef.AIError | ||
import com.xebia.functional.xef.conversation.Conversation | ||
import com.xebia.functional.xef.conversation.PlatformConversation | ||
import com.xebia.functional.xef.env.getenv | ||
import com.xebia.functional.xef.store.LocalVectorStore | ||
import com.xebia.functional.xef.store.VectorStore | ||
import kotlin.jvm.JvmField | ||
import kotlin.jvm.JvmOverloads | ||
import kotlin.jvm.JvmStatic | ||
import kotlin.jvm.JvmSynthetic | ||
|
||
private const val GCP_TOKEN_ENV_VAR = "GCP_TOKEN" | ||
private const val GCP_PROJECT_ID_VAR = "GCP_PROJECT_ID" | ||
private const val GCP_LOCATION_VAR = "GCP_LOCATION" | ||
|
||
class GCP(projectId: String? = null, location: VertexAIRegion? = null, token: String? = null) { | ||
private val config = | ||
GcpConfig( | ||
token = token ?: tokenFromEnv(), | ||
projectId = projectId ?: projectIdFromEnv(), | ||
location = location ?: locationFromEnv(), | ||
) | ||
|
||
private fun tokenFromEnv(): String = fromEnv(GCP_TOKEN_ENV_VAR) | ||
|
||
private fun projectIdFromEnv(): String = fromEnv(GCP_PROJECT_ID_VAR) | ||
|
||
private fun locationFromEnv(): VertexAIRegion = | ||
fromEnv(GCP_LOCATION_VAR).let { envVar -> | ||
VertexAIRegion.entries.find { it.officialName == envVar } | ||
} | ||
?: throw AIError.Env.GCP( | ||
nonEmptyListOf( | ||
"invalid value for $GCP_LOCATION_VAR - valid values are ${VertexAIRegion.entries.map(VertexAIRegion::officialName)}" | ||
) | ||
) | ||
|
||
private fun fromEnv(name: String): String = | ||
getenv(name) ?: throw AIError.Env.GCP(nonEmptyListOf("missing $name env var")) | ||
|
||
val CODECHAT by lazy { GcpModel("codechat-bison@001", config) } | ||
val TEXT_EMBEDDING_GECKO by lazy { GcpModel("textembedding-gecko", config) } | ||
|
||
@JvmField val DEFAULT_CHAT = CODECHAT | ||
@JvmField val DEFAULT_EMBEDDING = TEXT_EMBEDDING_GECKO | ||
|
||
fun supportedModels(): List<GcpModel> = listOf(CODECHAT, TEXT_EMBEDDING_GECKO) | ||
|
||
companion object { | ||
|
||
@JvmField val FromEnvironment: GCP = GCP() | ||
|
||
@JvmSynthetic | ||
suspend inline fun <A> conversation( | ||
store: VectorStore, | ||
noinline block: suspend Conversation.() -> A | ||
): A = block(conversation(store)) | ||
|
||
@JvmSynthetic | ||
suspend fun <A> conversation(block: suspend Conversation.() -> A): A = | ||
block(conversation(LocalVectorStore(GcpEmbeddings(FromEnvironment.DEFAULT_EMBEDDING)))) | ||
|
||
@JvmStatic | ||
@JvmOverloads | ||
fun conversation( | ||
store: VectorStore = LocalVectorStore(GcpEmbeddings(FromEnvironment.DEFAULT_EMBEDDING)) | ||
): PlatformConversation = Conversation(store) | ||
} | ||
} | ||
|
||
suspend inline fun <A> GCP.conversation(noinline block: suspend Conversation.() -> A): A = | ||
block(Conversation(LocalVectorStore(GcpEmbeddings(DEFAULT_EMBEDDING)))) |
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
15 changes: 15 additions & 0 deletions
15
integrations/gcp/src/commonMain/kotlin/com/xebia/functional/xef/gcp/GcpScopeExtensions.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.gcp | ||
|
||
import com.xebia.functional.xef.conversation.AiDsl | ||
import com.xebia.functional.xef.conversation.Conversation | ||
import com.xebia.functional.xef.llm.Chat | ||
import com.xebia.functional.xef.prompt.Prompt | ||
import kotlinx.coroutines.flow.Flow | ||
|
||
@AiDsl | ||
suspend fun Conversation.promptMessage(prompt: Prompt, model: Chat = GCP().DEFAULT_CHAT): String = | ||
model.promptMessage(prompt, this) | ||
|
||
@AiDsl | ||
fun Conversation.promptStreaming(prompt: Prompt, model: Chat = GCP().DEFAULT_CHAT): Flow<String> = | ||
model.promptStreaming(prompt, this) |
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