以 Kotlin 打造 AI Agent:Koog Agent Framework 實戰入門

shengyou 0 views 39 slides Oct 01, 2025
Slide 1
Slide 1 of 39
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27
Slide 28
28
Slide 29
29
Slide 30
30
Slide 31
31
Slide 32
32
Slide 33
33
Slide 34
34
Slide 35
35
Slide 36
36
Slide 37
37
Slide 38
38
Slide 39
39

About This Presentation

隨著 AI 技術的迅速演進,開發者在打造 AI 應用時,越來越需要一套能提供完整元件、多模型整合能力,以及具高度彈性與可擴充性的架構。為回應這樣的需求,JetBrains 團隊延續一貫從實際開發需求出發的精神,推出由 Kotlin 原�...


Slide Content

范聖佑 (Shengyou Fan)
  JetBrains Developer Advocate
JCConf Taiwan 2024
2025/09/19 以 Kotlin 打造 AI Agent:
Koog Agent Framework 實戰入門

範例程式碼
https:github.com/shengyou/koog-demo

主題大綱

•綜覽當前 AI 趨勢
•實戰 Koog 入門
•Koog 進階應用
•回顧與展望
•學習資源 (+無情⼯商)

前言

•開發 AI Agent 是當前趨勢
•JetBrains 的解決方案 - Koog
•用 Kotlin 開發 Agent 框架的原因

2025 是 AI Agent 元年
經理人 - https://www.managertoday.com.tw/articles/view/70569
遠見 - https://www.gvm.com.tw/article/113965
IPO Forum - https://www.ipoforum.org.tw/news/50
! 思考 ➡ 行動 #

實作 AI Agent 的困難點
•援各家 LLM
•任務分解與規劃 (Planner)
•⼯具呼叫 (Tools / Function Calling)
•狀態管理 (State)
•記憶與知識 (Memory & RAG)
•追蹤與觀測 (Observability)
•…

Koog:JetBrains Kotlin 團隊的解決方案
•使用 Kotlin 編寫,充分運用原生語言特性
•援 KMP 多平台開發
•援主流 LLM 提供商
•強大的⼯具系統 (+ MCP 整合)
•內建記憶與知識系統
•提供企業級監控能力
(v0.4.2)

蛤?
你說用 Kotlin 寫 AI Agent?

為什麼用 Kotlin 開發 Agent 框架?
•公司產品及團隊的特性使然
•型別安全才有安全感
•對企業級框架的需求
•對多平台援的信念
•舒適的 JVM 開發生態
From Python to Kotlin: How JetBrains
Revolutionized AI Agent Development
https://bit.ly/koog-behind-the-scenes

簡易對話式 AI

•Koog 起手式
•Agent 基礎參數
•流式 (Stream) 輸出

準備開發環境
•IntelliJ IDEA
•JDK 17+
•各 LLM 提供商的 API Key

用 Kotlin Notebook 快速體驗
%useLatestDescriptors
%use koog

建立全新 Koog 專案
build.gradle.kts
plugins {
kotlin("jvm") version "2.2.20"
}
dependencies {
implementation(libs.koog.agents)
}
gradle/libs.versions.toml
[versions]
koog-agents = "0.4.2"
[libraries]
koog-agents = {
module = "ai.koog:koog-agents" , version.ref = "..." }

5 行程式碼實作一個 AI Agent
suspend fun main() {
val agent = AIAgent(
executor = simpleOpenAIExecutor (System.getenv("OPENAI_API_KEY")),
llmModel = OpenAIModels.CostOptimized.GPT4_1Mini
)
val response = agent.run("...")
println(response)
}

基礎參數
•executor:執行器
•llmModel:使用的模型
•systemPrompt:系統提示詞
•temperature:模型溫度
•maxIterations:Agent 執行的步驟數上限

executor 參數
•simpleOpenAIExecutor
•simpleGoogleAIExecutor
•simpleAnthropicExecutor
•simpleOpenRouterExecutor
•simpleOllamaAIExecutor
•simpleAzureOpenAIExecutor
•simpleBedrockExecutor

llmModel 參數
•OpenAI
•Google
•Anthropic
•OpenRouter
•Ollama
•DeepSeek
•Bedrock

temperature 參數
精確可控 平衡中庸高度創意
0.0 0.3 0.7 1.0 2.0

流式 (Stream) 輸出
•提供即時回饋
•降低回應延遲
•可即時資料處理管線
•讓互動體驗更好
•更有效率地處理大型回應

賦與 Agent 更多能力

•套用⼯具 (Tool)
•整合 MCP
•實作策略圖 (Strategy Graph)
•增加記憶 (Memory)
•與框架整合

套用⼯具 (Tool)
•目的:讓 AI Agent 具備實際的操作能力
•內建⼯具: SayToUser 、 AskUser 、 ExitTool
•自訂⼯具的方式:
-Class-based
-Annotation-based

整合 MCP (Model Context Protocol)
•目的:為 Agent 添加標準化的外部 ⼯具
•好處:
-標準化的 ⼯具整合方案
-降低開發成本
-提升系統可靠性

實作策略圖 (Strategy Graph)
•目的:定義 Agent 的⼯作流程
•實作方式:透過 DSL 定義決策流程
-策略 (Strategy)
-⼦圖 (Subgraphs)
-節點 (Nodes)
-邊緣 (Edges)

增加記憶 (Memory)
•目的:讓 Agent 記住重要資訊、學習使用者偏好
•資料結構
-Concept:定義要記住的資訊類型 (如:使用者偏好)
-Subject:定義資訊的主體 (歸屬)者 (如:使用者)
-Fact:具體的資訊內容 (如:關注的講師是 XXX)

Main.kt
fun Application.module() {
install(Koog) {
...
}
routing {
route("/ai") {
post("/chat") {
val userInput = call.receiveText()
val output = aiAgent(
strategy = reActStrategy(),
model = OpenAIModels.Chat.GPT4_1,
input = userInput
)
call.respond(HttpStatusCode.OK, output)
}
}
}
}
框架整合: Ktor
build.gradle.kts
dependencies {
implementation("ai.koog:koog-ktor:$ver")
}
application.yaml
koog:
openai:
apikey: ${OPENAI_API_KEY}
baseUrl: https:api.openai.com

框架整合: Spring Boot
build.gradle.kts
dependencies {
implementation("ai.koog:koog-spring-boot-
starter:${ver}")
}
application.properties
ai.koog.openai.api-key= ${OPENAI_API_KEY}
ai.koog.openai.base-url= ${OPENAI_API_URL}
DTO
data class ChatRequest(val message: String)
data class ChatResponse(val response: String)
Controller
@RestController
@RequestMapping("/api/chat")
class ChatController(
private val anthropicExecutor: SingleLLMPromptExecutor?
) {
@PostMapping
suspend fun chat(@RequestBody request: ChatRequest): ResponseEntity<ChatResponse>
{
return if (anthropicExecutor != null) {
try {
val prompt = prompt {
system("You are a helpful assistant" )
user( request.message)
}
val result = anthropicExecutor.execute(prompt)
ResponseEntity.ok(ChatResponse( result.text))
} catch (e: Exception) {
ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
.body(ChatResponse( "Error processing request" ))
}
} else {
ResponseEntity.status(HttpStatus.SERVICE_UNAVAILABLE)
.body(ChatResponse( "AI service not configured" ))
}
}
}

回顧

•主題回顧
•技術選型與比較
•待續遺珠

為什麼選擇 Koog?
•與 JVM 和 Kotlin 順暢整合
•廣泛整合 LLM 提供商
•開箱即用的完整解決方案
•援多平台與主流框架
•在 JetBrains 產品中的實戰驗證

待續遺珠
•歷史紀錄壓縮
•檢索增強生成 (RAG)
•追蹤與監控 (OpenTelemetry)

學習資源

•官方文件及範例
•演講活動錄影
•官方討論頻道
•社群趨動的 s?文件
•大神親撰的鐵人賽
•在地 Kotlin 社群

Koog 官方文件 (英文) Koog 官方範例集
Koog 官方文件及範例
docs.koog.ai

Koog 演講活動錄影
Building Smarter AI Agents
With Koog
https://www.youtube.com/live/
vDtnqQmiyck
Building AI Agents in Kotlin
with Koog
https://youtu.be/O8WQCrdza8E
How to Build Scalable AI Agents
with Kotlin, Ktor & Koog
https://youtu.be/AGHONAx8gjQ

官方討論 Slack 頻道
#koog-agentic-framework
•Kotlin 官方頻道
•填寫表單加入後,
搜尋指定頻道進入

提供 Kotlin 開發生態的多語系文件 Koog 官方文件中文化
社群趨動的 s?文件
openaidoc.org

Cash Wu 親撰的 Koog 鐵人賽
bit.ly/ironkoog
•知名大神手把手的
完整 Koog 教學
•共計 34 篇文章
•已成功完賽 $

在地 Kotlin 社群 - Taiwan Kotlin User Group
•由 JetBrains 持的
Kotlin 技術社群
•不定期舉辦 Meetup
活動、年會、並出沒
在各開源研討會

每日 Kotlin 新聞摘要
LINE 群 Telegram 群
digest.kotlin.tips

Kotlin 爐邊漫談 Podcast ✍ 歡迎投稿你的 Kotlin 實務案例
可在各大 Podcast 平台收聽 或到 YouTube 看影片版
podcast.kotlin.tips

歡迎交流 的大小事
[email protected]
fb.me/shengyoufan
shengyoufan
@shengyou