From cdce90bb273d487b892aa20d2e85de34308abd8f Mon Sep 17 00:00:00 2001 From: BBIT-Kai <2911862937@qq.com> Date: Thu, 21 Aug 2025 14:55:45 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B0=83=E6=95=B4=E6=97=A7=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ktor/build.gradle.kts | 5 +- ktor/gradle.properties | 3 +- .../ink/snowflake/server/Application.kt | 3 ++ .../kotlin/ink/snowflake/server/route/AI.kt | 50 +++++++++++++++++++ 4 files changed, 57 insertions(+), 4 deletions(-) create mode 100644 ktor/src/main/kotlin/ink/snowflake/server/route/AI.kt diff --git a/ktor/build.gradle.kts b/ktor/build.gradle.kts index c04b2ca..8fcda7d 100644 --- a/ktor/build.gradle.kts +++ b/ktor/build.gradle.kts @@ -5,12 +5,11 @@ val logback_version: String by project val kotlinx_html_version: String by project val ktor_version: String by project val exposed_version: String by project -val h2_version: String by project val postgres_version: String by project plugins { kotlin("jvm") version "2.1.0" - id("io.ktor.plugin") version "3.1.3" + id("io.ktor.plugin") version "3.2.3" id("org.jetbrains.kotlin.plugin.serialization") version "2.0.20" } @@ -103,4 +102,6 @@ dependencies { implementation("io.ktor:ktor-client-serialization:$ktor_version") // 数据库迁移 // implementation("org.flywaydb:flyway-core:10.13.0") + + implementation("ai.koog:koog-agents:0.3.0") } diff --git a/ktor/gradle.properties b/ktor/gradle.properties index bb9fc13..19706d2 100644 --- a/ktor/gradle.properties +++ b/ktor/gradle.properties @@ -1,8 +1,7 @@ kotlin.code.style=official -ktor_version=3.0.2 +ktor_version=3.2.3 kotlin_version=2.1.0 logback_version=1.4.14 kotlinx_html_version=0.10.1 exposed_version=1.0.0-beta-2 -h2_version=2.1.214 postgres_version=42.7.4 \ No newline at end of file diff --git a/ktor/src/main/kotlin/ink/snowflake/server/Application.kt b/ktor/src/main/kotlin/ink/snowflake/server/Application.kt index dee0e60..8182885 100644 --- a/ktor/src/main/kotlin/ink/snowflake/server/Application.kt +++ b/ktor/src/main/kotlin/ink/snowflake/server/Application.kt @@ -5,6 +5,7 @@ import ink.snowflake.server.plugins.* import ink.snowflake.server.route.User import ink.snowflake.server.route.chat import ink.snowflake.server.plugins.configureSockets +import ink.snowflake.server.route.AI import ink.snowflake.server.route.ImageAnalytics import ink.snowflake.server.route.RemoteDebug import ink.snowflake.server.route.VideoAnalytics @@ -65,4 +66,6 @@ fun Application.module() { VideoAnalyticsJetson() // 业务-图片分析 ImageAnalytics() + // 业务-AI + AI() } diff --git a/ktor/src/main/kotlin/ink/snowflake/server/route/AI.kt b/ktor/src/main/kotlin/ink/snowflake/server/route/AI.kt new file mode 100644 index 0000000..44df22f --- /dev/null +++ b/ktor/src/main/kotlin/ink/snowflake/server/route/AI.kt @@ -0,0 +1,50 @@ +package ink.snowflake.server.route + +import ai.koog.agents.core.agent.AIAgent +import ai.koog.agents.core.tools.ToolRegistry +import ai.koog.agents.ext.tool.SayToUser +import ai.koog.prompt.executor.clients.openai.OpenAIModels +import ai.koog.prompt.executor.llms.all.simpleOllamaAIExecutor +import ai.koog.prompt.executor.llms.all.simpleOpenAIExecutor +import ai.koog.prompt.executor.ollama.client.OllamaClient +import ai.koog.prompt.executor.ollama.client.OllamaModelCard +import ai.koog.prompt.llm.LLMCapability +import ai.koog.prompt.llm.LLMProvider +import ai.koog.prompt.llm.LLModel +import ink.snowflake.server.model.response.BaseResponse +import io.ktor.server.application.Application +import io.ktor.server.response.respond +import io.ktor.server.routing.get +import io.ktor.server.routing.route +import io.ktor.server.routing.routing +import kotlinx.coroutines.launch + +fun Application.AI() { + + val agent = AIAgent( + executor = simpleOllamaAIExecutor("http://171.212.101.199:13011/"), + systemPrompt = "You are a helpful assistant. Answer user questions concisely.", //系统提示词 + llmModel = LLModel( + provider = LLMProvider.Ollama, + id = "llama3.2:latest", + capabilities = listOf( + LLMCapability.Temperature, +// LLMCapability.Tools + ) + ), // 模型 + temperature = 0.7, // 温度 +// toolRegistry = ToolRegistry { +// tool(SayToUser) // 注册工具 +// }, + maxIterations = 30 // + ) + + routing { + route("/api") { + get("/getAiList") { + val result = agent.run("Hello! How can you help me?") + call.respond(BaseResponse(data = result)) + } + } + } +} \ No newline at end of file