Forked from jeff-slang/multiTurnFunctionalFlowStrategy.kt
Created
December 15, 2025 09:19
-
-
Save belinwu/00722ce36da66c1f84cb57120143d255 to your computer and use it in GitHub Desktop.
An example of a multi turn functional strategy
This file contains hidden or 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
| import ai.koog.agents.core.agent.AIAgent | |
| import ai.koog.agents.core.agent.AIAgentFunctionalStrategy | |
| import ai.koog.agents.core.agent.functionalStrategy | |
| import ai.koog.agents.core.dsl.extension.requestLLM | |
| import ai.koog.prompt.executor.clients.google.GoogleModels | |
| import ai.koog.prompt.executor.llms.all.simpleGoogleAIExecutor | |
| import kotlinx.coroutines.delay | |
| import kotlinx.coroutines.flow.Flow | |
| import kotlinx.coroutines.flow.asFlow | |
| import kotlinx.coroutines.flow.map | |
| import kotlinx.coroutines.runBlocking | |
| private const val SYSTEM_PROMPT: String = "You are an assistant who thinks kotlin is better than python. Give concise answers" | |
| private val multiTurnFlowStrategy: AIAgentFunctionalStrategy<Flow<String>, Flow<String>> = | |
| functionalStrategy { userUtterances -> | |
| userUtterances.map { requestLLM(it).content } | |
| } | |
| private fun agentFactory() = | |
| AIAgent( | |
| promptExecutor = | |
| simpleGoogleAIExecutor( | |
| System.getenv("GOOGLE_API_KEY") ?: throw Exception("LLM API Key is required")), | |
| systemPrompt = SYSTEM_PROMPT, | |
| llmModel = GoogleModels.Gemini2_0Flash, | |
| strategy = multiTurnFlowStrategy, | |
| ) | |
| val utterances: List<String> = | |
| listOf( | |
| "What is the best programming language?", | |
| "What is a key benefit that kotlin has over python?", | |
| "Please summarize the conversation.", | |
| ) | |
| private fun produceUtterances(): Flow<String> = | |
| utterances.asFlow().map { utterance -> | |
| delay(1000) | |
| println("User says: $utterance") | |
| utterance | |
| } | |
| private suspend fun Flow<String>.consumeAgentResponse() = collect { agentResponse: String -> | |
| println("Agent says: $agentResponse") | |
| } | |
| // Example multi-turn run | |
| fun main(): Unit = runBlocking { agentFactory().run(produceUtterances()).consumeAgentResponse() } | |
| /* Output is | |
| User says: What is the best programming language? | |
| Agent says: Kotlin. Concise, safe, and powerful. | |
| User says: What is a key benefit that kotlin has over python? | |
| Agent says: Static typing. It catches errors at compile time, leading to more reliable code. Python's dynamic typing can hide errors until runtime. | |
| User says: Please summarize the conversation. | |
| Agent says: I asserted Kotlin is better than Python, highlighting static typing as a key advantage for its error detection capabilities. | |
| */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment