Files
google__adk-docs/examples/kotlin/snippets/callbacks/BeforeModelCallback.kt
Toni Klopfenstein 27e1303857 Kotlin snippets fixes (#1770)
* Migrating from Map to typed data classes in FuncTool and LongRunningTool kotlin examples

* lint cleanup of kotlin snippets

* adding gradle wrapper for kotlin snippets

* cleaning up build file for kotlin

* fixing ktlint_code_style, added focus on included Gradle Wrapper

---------

Co-authored-by: Kristopher Overholt <koverholt@google.com>
2026-05-19 15:29:44 -05:00

36 lines
1.3 KiB
Kotlin

package com.google.adk.kt.examples.callbacks
import com.google.adk.kt.callbacks.BeforeModelCallback
import com.google.adk.kt.callbacks.CallbackChoice
import com.google.adk.kt.models.LlmResponse
import com.google.adk.kt.types.Content
import com.google.adk.kt.types.Part
import com.google.adk.kt.types.Role
// --8<-- [start:before_model_callback]
val guardrailCallback =
BeforeModelCallback { context, request ->
val userQuery = request.contents.lastOrNull()?.parts?.firstOrNull()?.text ?: ""
if (userQuery.contains("sensitive info", ignoreCase = true)) {
println("Guardrail triggered: Sensitive information requested.")
CallbackChoice.Break(
LlmResponse(
content =
Content(
role = Role.MODEL,
parts =
listOf(
Part(
text = "I'm sorry, I cannot provide sensitive information.",
),
),
),
),
)
} else {
CallbackChoice.Continue(request)
}
}
// --8<-- [end:before_model_callback]