mirror of
https://github.com/google/adk-docs.git
synced 2026-09-14 16:16:59 +08:00
57869ca730
* Show maxLlmCalls in the RunConfig Kotlin tabs Two groups on the RunConfig page had a Kotlin tab that set only streamingMode while the Python, TypeScript and Java siblings also set max_llm_calls. The tab existed and every symbol in it was valid, so neither a symbol diff nor a missing-tab scan could see the gap - only comparing a tab's contents against its siblings. Also corrects the runtime-limits prose, which described the overflow error purely in Python terms. Kotlin rejects Int.MAX_VALUE the same way. The audio and speech group is deliberately left without a Kotlin tab: Kotlin's RunConfig carries only streamingMode, maxLlmCalls and customMetadata, so there is no speechConfig or responseModalities to show. * Update examples/kotlin/snippets/runtime/RunConfigExample.kt --------- Co-authored-by: Joe Fernandez <931947+joefernandez@users.noreply.github.com>
110 lines
3.3 KiB
Kotlin
110 lines
3.3 KiB
Kotlin
/*
|
|
* Copyright 2026 Google LLC
|
|
*
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
* you may not use this file except in compliance with the License.
|
|
* You may obtain a copy of the License at
|
|
*
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
*
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
* See the License for the specific language governing permissions and
|
|
* limitations under the License.
|
|
*/
|
|
|
|
package com.google.adk.kt.examples.runtime
|
|
|
|
import com.google.adk.kt.agents.LlmAgent
|
|
import com.google.adk.kt.agents.ResumabilityConfig
|
|
import com.google.adk.kt.agents.RunConfig
|
|
import com.google.adk.kt.agents.StreamingMode
|
|
import com.google.adk.kt.apps.App
|
|
import com.google.adk.kt.models.Gemini
|
|
import com.google.adk.kt.runners.InMemoryRunner
|
|
import com.google.adk.kt.sessions.InMemorySessionService
|
|
import com.google.adk.kt.types.Content
|
|
import com.google.adk.kt.types.Role
|
|
import kotlinx.coroutines.flow.collect
|
|
import kotlinx.coroutines.runBlocking
|
|
|
|
// --8<-- [start:basic_usage]
|
|
val config =
|
|
RunConfig(
|
|
streamingMode = StreamingMode.SSE,
|
|
// Cap the LLM calls a single run may make. Defaults to 500.
|
|
maxLlmCalls = 200,
|
|
)
|
|
|
|
// Pass it to runner.runAsync
|
|
// runner.runAsync(..., runConfig = config)
|
|
// --8<-- [end:basic_usage]
|
|
|
|
// --8<-- [start:full_example]
|
|
fun runWithConfig(runner: InMemoryRunner) =
|
|
runBlocking {
|
|
val config =
|
|
RunConfig(
|
|
streamingMode = StreamingMode.SSE,
|
|
customMetadata = mapOf("priority" to "high"),
|
|
)
|
|
|
|
runner
|
|
.runAsync(
|
|
userId = "user123",
|
|
sessionId = "session456",
|
|
newMessage = Content.fromText(Role.USER, "Hello!"),
|
|
runConfig = config,
|
|
).collect { event ->
|
|
// handle events
|
|
}
|
|
}
|
|
// --8<-- [end:full_example]
|
|
|
|
// --8<-- [start:custom_metadata]
|
|
val metadataConfig =
|
|
RunConfig(
|
|
customMetadata = mapOf("user_tier" to "premium"),
|
|
)
|
|
// --8<-- [end:custom_metadata]
|
|
|
|
// --8<-- [start:streaming_config]
|
|
// Note: Kotlin currently has no supportCfc equivalent
|
|
val streamingConfig =
|
|
RunConfig(
|
|
streamingMode = StreamingMode.SSE,
|
|
maxLlmCalls = 150,
|
|
)
|
|
// --8<-- [end:streaming_config]
|
|
|
|
private val rootAgent =
|
|
LlmAgent(name = "my_agent", model = Gemini(name = "gemini-flash-latest"))
|
|
|
|
// --8<-- [start:resumability_config]
|
|
val runner =
|
|
InMemoryRunner(
|
|
app =
|
|
App(
|
|
appName = "my_resumable_agent",
|
|
rootAgent = rootAgent,
|
|
resumabilityConfig = ResumabilityConfig(isResumable = true),
|
|
),
|
|
sessionService = InMemorySessionService(),
|
|
)
|
|
// --8<-- [end:resumability_config]
|
|
|
|
// --8<-- [start:resume_usage]
|
|
fun resumeAgent(runner: InMemoryRunner) =
|
|
runBlocking {
|
|
runner
|
|
.runAsync(
|
|
userId = "user123",
|
|
sessionId = "session456",
|
|
invocationId = "previous-invocation-id",
|
|
).collect { event ->
|
|
// resume execution from previous state
|
|
}
|
|
}
|
|
// --8<-- [end:resume_usage]
|