-
Notifications
You must be signed in to change notification settings - Fork 207
Add Ktor integration via Koog
ktor plugin
#422
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
61c2663
Draft Ktor integration proposal in examples
Ololoshechkin 216bb59
Create separate module with Ktor plugin and support YAML properties f…
Ololoshechkin bfca0d1
Add tests for environment config loading and model id parsing
Ololoshechkin 9121004
- Rewrite Koog plugin to class-based plugin which allows for easier r…
nomisRev b8d3ebc
Adopt version catalog for Ktor dependencies and refactor build script…
nomisRev 2282684
Rename `koog-ktor-plugin` module to `koog-ktor` and refactor related …
nomisRev 5ce70c2
Adopt Ktor version catalog in `settings.gradle.kts`, remove explicit …
nomisRev 1150765
Remove `suspend` from `mcp` configuration, and create a temporary sco…
nomisRev 51e83c2
- Remove `suspend` from `mcp` configuration, and create a temporary s…
nomisRev ec5dd3d
Revert "Adopt version catalog for Ktor dependencies and refactor buil…
nomisRev 81bd62f
Remove `yarn.lock` from the `kotlin-js-store` directory.
nomisRev bc1821f
Rename `MCPToolsConfig` to `McpToolsConfig` and remove unused `Global…
nomisRev 7c727ab
Make client configuration properties nullable and set default values …
nomisRev 8928514
Refactor AI agent functions to include `model` parameter and improve …
nomisRev ff1cd62
Introduce `CoroutineScope` for improved configuration flexibility, re…
nomisRev edd7a29
Simplify YAML configuration structure, remove redundant timeout setti…
nomisRev File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,3 +6,4 @@ build | |
env.properties | ||
.qodana/code-coverage | ||
local.properties | ||
/kotlin-js-store/yarn.lock |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
138 changes: 138 additions & 0 deletions
138
examples/src/main/kotlin/ai/koog/agents/example/ktor/KtorIntegrationExample.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
package ai.koog.agents.example.ktor | ||
|
||
import ai.koog.agents.core.tools.annotations.LLMDescription | ||
import ai.koog.agents.core.tools.annotations.Tool | ||
import ai.koog.agents.core.tools.reflect.tool | ||
import ai.koog.agents.ext.agent.reActStrategy | ||
import ai.koog.agents.features.opentelemetry.feature.OpenTelemetry | ||
import ai.koog.ktor.* | ||
import ai.koog.prompt.dsl.prompt | ||
import ai.koog.prompt.executor.clients.openai.OpenAIModels | ||
import ai.koog.prompt.executor.model.PromptExecutorExt.execute | ||
import ai.koog.prompt.llm.OllamaModels | ||
import io.ktor.http.* | ||
import io.ktor.server.application.* | ||
import io.ktor.server.cio.* | ||
import io.ktor.server.request.* | ||
import io.ktor.server.response.* | ||
import io.ktor.server.routing.* | ||
import io.opentelemetry.exporter.logging.LoggingSpanExporter | ||
|
||
@Tool | ||
@LLMDescription("Searches in Google and returns the most relevant page URLs") | ||
fun searchInGoogle(request: String, numberOfPages: String): List<String> { | ||
return emptyList() | ||
} | ||
|
||
@Tool | ||
@LLMDescription("Executes bash command") | ||
fun executeBash(command: String): String { | ||
return "bash not supported" | ||
} | ||
|
||
|
||
@Tool | ||
@LLMDescription("Secret function -- call it and you'll see the output") | ||
fun doSomethingElse(input: String): String { | ||
return "Surprise! I do nothing. Never call me again -_-" | ||
} | ||
|
||
|
||
fun main(args: Array<String>) = EngineMain.main(args) | ||
|
||
fun Application.main() { | ||
configureKoog() | ||
|
||
defineRoutes() | ||
} | ||
|
||
fun Application.configureKoog() { | ||
// Install the Koog plugin | ||
// LLM configurations will be loaded from application.yaml | ||
// You can still provide additional configuration or override settings from the YAML file | ||
install(Koog) { | ||
// The following configurations are optional and will override any settings from application.yaml | ||
llm { | ||
// Example of overriding a configuration from application.yaml | ||
// This will take precedence over the configuration in the YAML file | ||
openAI(apiKey = System.getenv("OPENAI_API_KEY") ?: "override-from-code") { | ||
// Override baseUrl only if needed | ||
// baseUrl = "custom-override-url" | ||
} | ||
|
||
fallback { } | ||
} | ||
|
||
agentConfig { | ||
mcp { | ||
sse("put some url here...") | ||
} | ||
|
||
registerTools { | ||
tool(::searchInGoogle) | ||
tool(::executeBash) | ||
tool(::doSomethingElse) | ||
} | ||
|
||
prompt { | ||
system("You are professional joke based on user's request") | ||
} | ||
|
||
install(OpenTelemetry) { | ||
addSpanExporter(LoggingSpanExporter()) | ||
} | ||
} | ||
} | ||
} | ||
|
||
fun Application.defineRoutes() { | ||
routing { | ||
route("api/v1") { | ||
normalRoutes() | ||
} | ||
|
||
route("agents/v1") { | ||
agenticRoutes() | ||
} | ||
} | ||
} | ||
|
||
private fun Route.agenticRoutes() { | ||
get("user") { | ||
val userRequest = call.receive<String>() | ||
|
||
val isHarmful = llm().moderate(prompt("id") { | ||
user(userRequest) | ||
}, OpenAIModels.Moderation.Omni).isHarmful | ||
|
||
if (isHarmful) { | ||
call.respond(HttpStatusCode.BadRequest, "Harmful content detected") | ||
return@get | ||
} | ||
|
||
val updatedRequest = llm().execute(prompt("id") { | ||
system( | ||
"You are a helpful assistant that can correct user answers. " + | ||
"You will get a user's question and your task is to make it more clear for the further processing." | ||
) | ||
user(userRequest) | ||
}, OllamaModels.Meta.LLAMA_3_2) | ||
|
||
val output = singleRunAgent(updatedRequest.content, OpenAIModels.Chat.GPT4_1) | ||
call.respond(HttpStatusCode.OK, output) | ||
} | ||
get("organization") { | ||
val orgName = call.parameters["name"]!! | ||
val output = aiAgent(reActStrategy(), OpenAIModels.Chat.GPT4_1, "What's new in $orgName organization") | ||
call.respond(HttpStatusCode.OK, output) | ||
} | ||
} | ||
|
||
private fun Route.normalRoutes() { | ||
get("user") { | ||
call.respondText { "Hello, user!" } | ||
} | ||
get("organization") { | ||
call.respondText { "Hello, organization!" } | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
ktor: | ||
application: | ||
modules: | ||
- ai.koog.agents.example.ktor.KtorIntegrationExampleKt.main | ||
|
||
koog: | ||
openai.apikey: "your-openai-api-key" | ||
anthropic.apikey: "your-anthropic-api-key" | ||
google.apikey: "your-google-api-key" | ||
openrouter.apikey: "your-openrouter-api-key" | ||
ollama.enable: true | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe we should require a url for Ollama?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It follows the same behavior as the Ollama Koog integration which is the default URL, or
ollama.baseUrl = "..."
, orollama { baseUrl = "..." }
in code.