mirror of
https://github.com/google/adk-docs.git
synced 2026-09-14 16:16:59 +08:00
27e1303857
* 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>
36 lines
1.3 KiB
Kotlin
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]
|