Slide 1

Slide 1 text

Ktorで簡単AIアプリケーション 塚本啓太 @ スマートラウンド 2025年10月10日 Server-Side Kotlin LT大会 vol.16 1

Slide 2

Slide 2 text

自己紹介 名前・所属 塚本 啓太( @tsukakei1012 ) 株式会社スマートラウンド 執行役員VPoE サーバーサイドKotlin歴 ここ5年くらいずっとKotlinでやっています! 2019-04~2023-10 株式会社イエソド テックリード 2023-10~ 株式会社スマートラウンド 2024-01~2025-06 テックリード:サービス開発、イネーブルメント担当 2025-07~ 執行役員VPoE:開発部全体のマネジメント 2

Slide 3

Slide 3 text

会社および事業紹介 株式会社スマートラウンド 設立年2018年 ミッション:スタートアップが可能性を最大限に発揮できる世界をつくる プロダクト「smartround」 スタートアップ導入社数7,100社 株主総会・取締役会・SO管理・資本政策管理にお悩みの方向けサービス 投資家導入数4,300人 さらなる取り組み(子会社にて) 未上場株式のセカンダリー取引のプラットフォーム実現に向けて準備中! 3

Slide 4

Slide 4 text

技術スタック サーバーサイド Kotlin・Ktor・Exposed SSKM vol.1から運営企業をやらせてもらっています! フロントエンド TypeScript・Vue.js インフラ AWS 4

Slide 5

Slide 5 text

本日話す内容 KotlinでサクッとAIエージェントを作る方法とそれをサクッとWebアプリに載せる方法 をご紹介します 5

Slide 6

Slide 6 text

AIエージェントとは...? 6

Slide 7

Slide 7 text

AIエージェント エージェントとは、LLM が自律的に自らのプロセスやツールの使用方法を動的に制御 し、どのようにタスクを達成するかを自分で管理するシステムのことを指す。 Agents, on the other hand, are systems where LLMs dynamically direct their own processes and tool usage, maintaining control over how they accomplish tasks. from Building effective agents by Anthropic https://www.anthropic.com/engineering/building-effective-agents 7

Slide 8

Slide 8 text

AIエージェントフレームワーク PythonのLangChainやTypeScriptのMastraが有名だけどKotlinにはないの? 8

Slide 9

Slide 9 text

KotlinにもAIエージェントフレームワークあるよ 9

Slide 10

Slide 10 text

Koog JetBrains製のAIエージェントフレームワーク 今年のKotlinConfで急遽発表された 昨今のKMMの影響でJVMだけでなくJS、Wasm、Android、iOS対応している 他にKotlinのAIエージェントフレームワークとしてはEmbabelやArcというものもあ ります 10

Slide 11

Slide 11 text

Koogで簡単なエージェント まずはやってみよう! val agent = AIAgent( promptExecutor = simpleOpenAIExecutor(System.getenv("YOUR_API_KEY")), systemPrompt = "You are a helpful assistant. Answer user questions concisely.", llmModel = OpenAIModels.Chat.GPT4o ) fun main() = runBlocking { val result = agent.run("Hello! How can you help me?") } ね、お手軽でしょ? 11

Slide 12

Slide 12 text

Koogでできること まだまだ発展途上なものの基本的なことは大体揃ってきた マルチモーダル対応 構造化出力 ワークフロー トレーシング サーバーサイドFWとの統合 その他(ToolsやMemory、RAGなど) 12

Slide 13

Slide 13 text

マルチモーダル対応 画像・音声・映像・ファイルに対応 val prompt = prompt("multimodal_input") { system("You are a helpful assistant.") user { +"Describe these images" attachments { image("https://example.com/test.png") image(Path("/User/koog/image.png")) } } } 13

Slide 14

Slide 14 text

構造化出力 データクラスを定義するだけ(Webアプリと同様ですね) @Serializable @SerialName("WeatherForecast") @LLMDescription("Weather forecast for a given location") data class WeatherForecast( @property:LLMDescription("Temperature in Celsius") val temperature: Int, @property:LLMDescription("Weather conditions (e.g., sunny, cloudy, rainy)") val conditions: String, @property:LLMDescription("Chance of precipitation in percentage") val precipitation: Int ) 14

Slide 15

Slide 15 text

ワークフロー ワークフロー定義用のDSLが整備されている // Basic edge edge(sourceNode forwardTo targetNode) // Edge with condition edge(sourceNode forwardTo targetNode onCondition { output -> // Return true to follow this edge, false to skip it output.contains("specific text") }) // Edge with transformation edge(sourceNode forwardTo targetNode transformed { output -> // Transform the output before passing it to the target node "Modified: $output" }) // Combined condition and transformation edge(sourceNode forwardTo targetNode onCondition { it.isNotEmpty() } transformed { it.uppercase() }) 15

Slide 16

Slide 16 text

Tracing① これだけで各イベントごとにログを吐いてくれる val agent = AIAgent( promptExecutor = simpleOllamaAIExecutor(), llmModel = OllamaModels.Meta.LLAMA_3_2, ) { install(Tracing) { addMessageProcessor(TraceFeatureMessageLogWriter(logger)) } } 16

Slide 17

Slide 17 text

Tracing② OpenTelemetryやLangFuse対応も簡単 val agent = AIAgent( promptExecutor = simpleOpenAIExecutor(apiKey), llmModel = OpenAIModels.Chat.GPT4o, systemPrompt = "You are a helpful assistant.", installFeatures = { install(OpenTelemetry) { // Configuration options go here // LangFuseにも対応 // addLangfuseExporter() } } ) 17

Slide 18

Slide 18 text

Koogでできること(再掲) まだまだ発展途上なものの基本的なことは大体揃ってきた マルチモーダル対応 構造化出力 ワークフロー トレーシング サーバーサイドFWとの統合 ← 今日のポイント その他(ToolsやMemory、RAGなど) 18

Slide 19

Slide 19 text

Ktorとのインテグレーション① 8月末リリースのv0.4.0からKtorとの接続が圧倒的に簡単になった!! 設定ファイル fun Application.module() { install(Koog) { llm { openAI(apiKey = "your-openai-api-key") } } } 19

Slide 20

Slide 20 text

Ktorとのインテグレーション② ルーティングファイル routing { route("/ai") { post("/chat") { val userInput = call.receive() val output = aiAgent( strategy = reActStrategy(), model = OpenAIModels.Chat.GPT4_1, input = userInput ) call.respond(HttpStatusCode.OK, output) } } } 20

Slide 21

Slide 21 text

これ以上知りたい人向け 司会の福本さんがもう少し触った記事を書いてくれているので、ぜひそちらをお読み ください! https://zenn.dev/smartround_dev/articles/edfce676fd3aaf 21

Slide 22

Slide 22 text

ご清聴ありがとうございました! KotlinやAIやスタートアップが好きな人、ぜひお話ししましょう!! 採用ページも置いておきます https://jobs.smartround.com/ 22