I'm having an issue with the strategy graph when a...
# koog-agentic-framework
j
I'm having an issue with the strategy graph when an LLM response includes two messages, one is an Assistant message and one is a Tool call request. This seems to cause
edge(nodeCallLLM forwardTo nodeFinish onAssistantMessage { true })
to handle it and
edge(nodeCallLLM forwardTo executeToolCall onToolCall { true })
doesn't. The Assistant message is a
<thinking>
block. Any ideas?
I was able to workaround this with:
Copy code
val nodeCallLLM by nodeLLMRequestMultiple()

val filterThinking by node<List<Message>, Message> { input ->
                    input.firstOrNull { it is Message.Tool } ?: input.first()
                }

edge(nodeStart forwardTo nodeCallLLM)
edge(nodeCallLLM forwardTo filterThinking)
e
I had the same problem with a Qwen thinking model. I changed the code to remove the <think> so I could continue testing. https://github.com/JetBrains/koog/issues/160#issuecomment-3241321797
v
Hi! There is
nodeLLMRequestMultiple
and
nodeExecuteMultipleTools
. Please feel free to check out how the default
singleRunStrategy
strategy is implemented here: https://github.com/JetBrains/koog/blob/eaabdfa8c9605172e98a495b67505cadd6d08199/ag[…]ain/kotlin/ai/koog/agents/core/agent/AIAgentSimpleStrategies.kt You can also implement parallel tool execution if you want as it’s done there, if your LLM outputs a mix of multiple ToolCalls + assistant messages
Actually, it’s usually even better to handle multiple tool calls at once where possible for LLM performance reasons 🙂
nodeExecuteMultipleTools
has
parallelTools = true/false
flag And there’s also
onMultipleToolCalls { ... }
condition that does exactly the filtering you need:
Copy code
public infix fun <IncomingOutput, OutgoingInput> AIAgentEdgeBuilderIntermediate<IncomingOutput, List<Message.Response>, OutgoingInput>.onMultipleToolCalls(
    block: suspend (List<Message.Tool.Call>) -> Boolean
): AIAgentEdgeBuilderIntermediate<IncomingOutput, List<Message.Tool.Call>, OutgoingInput> {
    return onIsInstance(List::class)
        .transformed { it to it.filterIsInstance<Message.Tool.Call>() }
        // skipping this edge in case we have list of only assistant messages
        .onCondition { (_, filtered) -> filtered.any() }
        .transformed { (_, filtered) -> filtered }
        .onCondition { toolCalls -> block(toolCalls) }
e
Thanks! I'll check it out! My issue is with the QWEN model in Ollama. It responds with a toolCall and an empty testMessage like "think/think". Since the message contains some text, Koog considers it to be a meaningful message and adds both to the result. The workflow prioritizes the text message and jumps to the next node instead of jumping to the toolCall node. Changing "ai.koog.prompt.executor.ollama.client.OllamaClient' to filter out these type of message solves the problem, but I think it's not a general solution.
Copy code
private val thinkPart = Regex( "think.*/think[ \n]*", setOf(RegexOption.MULTILINE, RegexOption.DOT_MATCHES_ALL))

private fun parseResponse(response: OllamaChatResponseDTO): ListMessage.Response {
    val messages = response.message ?: return emptyList()
    val content = messages.content.replace( thinkPart, "") // removes the think part
    val toolCalls = messages.toolCalls ?: emptyList()
It doesn't work. The problem arises when there are two responses, one empty like "<think>.*</think>" and a tool call. "AIAgentLLMSession" discards all except the first (the useless message) and never call the tool requested.
Copy code
protected suspend fun executeSingle(prompt: Prompt, tools: List<ToolDescriptor>): Message.Response =
        executeMultiple(prompt, tools).first()
v
Exactly, that’s why I suggest using multiple-tools node and multiple-responses node. Please let me know if that works for you
e
It works!
Copy code
val strategy = strategy<String, String>("general") {
        val nodeCallLLM by nodeLLMRequestMultiple()
        val nodeExecuteMultipleTools by nodeExecuteMultipleTools()
        val nodeSendMultipleToolResults by nodeLLMSendMultipleToolResults()

        edge(nodeStart forwardTo nodeCallLLM)
//        edge(nodeCallLLM forwardTo nodeExecuteTool onToolCall { true })
        edge(
            nodeCallLLM forwardTo nodeFinish
                onMultipleAssistantMessages {
                    !it.all { msg -> emptyResult.matches(msg.content) }
                }
                transformed { messages ->
                    messages.filter { msg -> !emptyResult.matches(msg.content) }
                        .joinToString(" ") { it.content.replace( thinkContent, "") }
                }
        )
        edge(nodeCallLLM forwardTo nodeExecuteMultipleTools onMultipleToolCalls { true })

        edge(nodeExecuteMultipleTools forwardTo nodeSendMultipleToolResults)
//        edge(nodeSendMultipleToolResults forwardTo nodeFinish onAssistantMessage { true })
        edge(nodeSendMultipleToolResults forwardTo nodeFinish
                onMultipleAssistantMessages { true }
                transformed { it.joinToString(" ") { msg -> msg.content.replace( thinkContent, "") } }
        )
        edge(nodeSendMultipleToolResults forwardTo nodeExecuteMultipleTools onMultipleToolCalls { true })
    }
With this model, although it is a single tool call, it requires
nodeLLMRequestMultiple
,
nodeExecuteMultipleTools
, and
nodeLLMSendMultipleToolResults
. Thanks!
kodee happy 1