Skip to content

Commit fa2fb04

Browse files
authored
Merge pull request #206 from joreilly/koog_updates
start using Koog functionalStrategy + run tools in parallel
2 parents dab1855 + 59627da commit fa2fb04

File tree

2 files changed

+25
-30
lines changed

2 files changed

+25
-30
lines changed

composeApp/src/commonMain/kotlin/dev/johnoreilly/climatetrace/agent/ClimateTraceAgent.kt

Lines changed: 25 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,45 @@
11
package dev.johnoreilly.climatetrace.agent
22

33
import ai.koog.agents.core.agent.AIAgent
4+
import ai.koog.agents.core.agent.asAssistantMessage
5+
import ai.koog.agents.core.agent.containsToolCalls
6+
import ai.koog.agents.core.agent.executeMultipleTools
7+
import ai.koog.agents.core.agent.extractToolCalls
8+
import ai.koog.agents.core.agent.functionalStrategy
9+
import ai.koog.agents.core.agent.requestLLM
10+
import ai.koog.agents.core.agent.requestLLMMultiple
11+
import ai.koog.agents.core.agent.sendMultipleToolResults
412
import ai.koog.agents.core.tools.ToolRegistry
5-
import ai.koog.agents.features.eventHandler.feature.handleEvents
613
import ai.koog.prompt.executor.model.PromptExecutor
714
import ai.koog.prompt.llm.LLModel
815
import dev.johnoreilly.climatetrace.data.ClimateTraceRepository
916

1017

11-
1218
expect fun getLLModel(): LLModel
1319
expect fun getPromptExecutor(apiKey: String = ""): PromptExecutor
1420

1521
class ClimateTraceAgent(private val climateTraceRepository: ClimateTraceRepository) {
1622
private val apiKeyGoogle = ""
1723

18-
suspend fun createAgent() = AIAgent(
24+
suspend fun createAgent() = AIAgent<String, String>(
1925
promptExecutor = getPromptExecutor(apiKeyGoogle),
2026
llmModel = getLLModel(),
2127
toolRegistry = createToolSetRegistry(climateTraceRepository),
28+
strategy = functionalStrategy { input ->
29+
println("Calling LLM with Input = $input")
30+
var responses = requestLLMMultiple(input)
31+
32+
while (responses.containsToolCalls()) {
33+
val pendingCalls = extractToolCalls(responses)
34+
println("Pending Calls")
35+
println(pendingCalls.map { "${it.tool} ${it.content}" })
36+
val results = executeMultipleTools(pendingCalls, parallelTools = true)
37+
responses = sendMultipleToolResults(results)
38+
}
39+
40+
val draft = responses.single().asAssistantMessage().content
41+
requestLLM("Improve and clarify: $draft").asAssistantMessage().content
42+
},
2243
systemPrompt =
2344
"""
2445
You an AI assistant specialising in providing information about global climate emissions.
@@ -32,33 +53,7 @@ class ClimateTraceAgent(private val climateTraceRepository: ClimateTraceReposito
3253
Pass the list of country codes and the year to the GetEmissionsTool tool to get climate emission information.
3354
Use units of millions for the emissions data.
3455
""",
35-
) {
36-
handleEvents {
37-
onLLMCallStarting { ctx ->
38-
println("Request to LLM")
39-
}
40-
41-
onLLMCallCompleted { ctx ->
42-
println("Response from LLM")
43-
ctx.responses.forEach { println(" $it") }
44-
}
45-
46-
onToolCallStarting { eventContext ->
47-
println("Tool called: ${eventContext.tool} with args ${eventContext.toolArgs}")
48-
}
49-
onAgentExecutionFailed { eventContext ->
50-
println("An error occurred: ${eventContext.throwable.message}\n${eventContext.throwable.stackTraceToString()}")
51-
}
52-
53-
onNodeExecutionStarting {
54-
println(it.node.name)
55-
}
56-
57-
onAgentCompleted {
58-
println("onAgentCompleted")
59-
}
60-
}
61-
}
56+
)
6257

6358

6459

0 commit comments

Comments
 (0)