Is there example of use of `functionalStrategy` wh...
# koog-agentic-framework
j
Is there example of use of
functionalStrategy
where it's not just doing single run i.e. user can post follow up prompts etc?
actually, nvm, got Junie to do it 😃
update fstrategy to implement same as what's done in strategy
hmm, this is what it created....is fixed repeat expected?
Copy code
val fstrategy = functionalStrategy<String, String> { initialInput ->
    var inputMessage = initialInput
    var lastAssistantMessage = ""

    repeat(50) { // align with agentConfig.maxAgentIterations
        println("Calling LLM with Input = $inputMessage")
        var responses = requestLLMMultiple(inputMessage)

        // Resolve tools until none left, mirroring graph strategy
        while (responses.containsToolCalls()) {
            val pendingCalls = extractToolCalls(responses)
            println("Pending Calls")
            println(pendingCalls.map { "${it.tool} ${it.content}" })

            val results = executeMultipleTools(pendingCalls, parallelTools = true)

            // Finish condition: if ExitTool is called, return its result directly
            if (results.size == 1 && results.first().tool == ExitTool.name) {
                return@functionalStrategy results.first().result!!.toString()
            }

            // Send tool results back to LLM
            responses = sendMultipleToolResults(results)
        }

        // No more tool calls: deliver assistant message to UI and get possible user follow-up
        lastAssistantMessage = responses.first().asAssistantMessage().content
        val userReply = onAssistantMessage(lastAssistantMessage)

        // If user provides no reply, consider conversation finished and return assistant response
        if (userReply.isBlank()) {
            return@functionalStrategy lastAssistantMessage
        }

        // Prepare for next loop iteration with user's reply
        inputMessage = userReply
    }

    // Max iterations reached; return last assistant message
    lastAssistantMessage
}
d
IIRC, the choice of the type of strategy (graph or functional) is independent of the fact that AIAgent can be run only once. You need to use AIAgentService to be able to run it multiple times. Then you need to store/restore the context during each run to have a behavior like a chatbot. Or did I misunderstand your question?
which is using
AIAgent
but was going to update to use
functionalStrategy
instead
d
As for the
repeat
in your strategy, you should have access to the
maxAgentIterations
value through the agent config, which I believe you have access to from the
AgentFunctionalContext
.
v
Also please consider using subtask (see usages in https://github.com/JetBrains/koog/blob/develop/agents/agents-core/src/commonTest/kotlin/ai/koog/agents/core/agent/FunctionalAIAgentTest.kt) in order to not implement the loop with low-level primitives
I’d say the whole strategy might be something like: functionalStrategy(…) { input -> while(…) { val result = subtask(…) { input } println(result) input = readln() } }