Skip to content

Commit 090c70d

Browse files
tiginamariainnateteniukmarinkovicmarko
authored
Koog Docs with Knit (#542)
Co-authored-by: Inna Teteniuk <[email protected]> Co-authored-by: Marko Marinkovic <[email protected]> Co-authored-by: Maria Tigina <[email protected]>
1 parent dde4ea8 commit 090c70d

38 files changed

+6871
-547
lines changed

.github/workflows/checks.yml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ jobs:
6262
run: |
6363
cd ./docs
6464
pip install mkdocs-material
65+
pip install mkdocs-redirects
6566
mkdocs build
6667
6768
tests:
@@ -151,3 +152,28 @@ jobs:
151152
env:
152153
QODANA_TOKEN: ${{ secrets.QODANA_TOKEN_1399872884 }}
153154
QODANA_ENDPOINT: 'https://qodana.cloud'
155+
knit:
156+
runs-on: ubuntu-latest
157+
steps:
158+
- uses: actions/checkout@v4
159+
- uses: actions/setup-java@v4
160+
with:
161+
distribution: 'temurin'
162+
java-version: 17
163+
- uses: gradle/actions/setup-gradle@v4
164+
- name: Generate and verify code examples
165+
run: |
166+
echo "Starting knit generation..."
167+
FILES_BEFORE_KNIT=$(find docs/src/ -name "*.kt" 2>/dev/null | wc -l || echo "0")
168+
echo "Files before knit: $FILES_BEFORE_KNIT"
169+
170+
./gradlew :docs:knit
171+
172+
FILES_AFTER_KNIT=$(find docs/src/ -name "*.kt" 2>/dev/null | wc -l || echo "0")
173+
echo "Files after knit: $FILES_AFTER_KNIT"
174+
175+
KNIT_GENERATED_FILES=$((FILES_AFTER_KNIT - FILES_BEFORE_KNIT))
176+
echo "Knit generated $KNIT_GENERATED_FILES files"
177+
178+
echo "Starting assemble..."
179+
./gradlew :docs:assemble

.github/workflows/push-to-main.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,5 @@ jobs:
2828
run: |
2929
cd ./docs
3030
pip install mkdocs-material
31-
mkdocs gh-deploy
31+
pip install mkdocs-redirects
32+
mkdocs gh-deploy

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@ env.properties
77
.qodana/code-coverage
88
local.properties
99
/kotlin-js-store/yarn.lock
10+
docs/src/main/kotlin/*.kt

docs/README.md

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
# Koog Documentation
2+
3+
This module contains documentation for the Koog framework, including user guides, API references, prompting guidelines, and other static files.
4+
5+
## Module Structure
6+
7+
The docs module is organized as follows:
8+
9+
- **docs/** - Contains markdown files with user documentation
10+
- **overrides/** - Custom overrides for the MkDocs theme
11+
- **prompt/** - Prompting guidelines with extensions for popular modules
12+
- **src/** - Knit generated source code from documentation code snippets, should not be commited
13+
14+
## Documentation System
15+
16+
### MkDocs
17+
18+
The documentation is built using [MkDocs](https://www.mkdocs.org/) with the Material theme. The configuration is defined in `mkdocs.yml`, which specifies:
19+
20+
- Navigation structure
21+
- Theme configuration
22+
- Markdown extensions
23+
- Repository links
24+
25+
The documentation is available at [https://docs.koog.ai/](https://docs.koog.ai/).
26+
27+
### Docs Code Snippets Verification
28+
29+
To ensure code snippets in documentation are compilable and up-to-date with the latest framework version, the [kotlinx-knit](https://github.com/Kotlin/kotlinx-knit) library is used.
30+
31+
Knit provides a Gradle plugin that extracts specially annotated Kotlin code snippets from markdown files and generates Kotlin source files.
32+
33+
#### How to fix docs?
34+
1. Run knit to extract code snippets to /src/main/kotlin:
35+
```
36+
./gradlew :docs:knit
37+
```
38+
2. Run assemble to get compilation arrows:
39+
```
40+
./gradlew :docs:assemble
41+
```
42+
3. Navigate to the file with the compilation error `example-[md-file-name]-[index].kt`
43+
4. Fix the error in this file
44+
5. Navigate to the code snippet in Markdown `md-file-name.md` by searing `<!--- KNIT example-[md-file-name]-[index].kt` -->`
45+
6. Update the code snippet to reflect the changes in kt file
46+
* Update dependencies (usually they are provided in `<!--- INCLUDE -->` section)
47+
* Edit code (don't forget about tabulation when you just copy paste from kt)
48+
49+
#### How to annotate docs?
50+
51+
To annotate new Kotlin code snippets in Markdown and make them compilable:
52+
1. Put an example annotation comment (`<!--- KNIT example-[md-file-name]-01.kt -->`) after every code block.
53+
It's not obligated to put right inexes, just set the `01` for each example, and they will be updated automatically after first knit run
54+
```
55+
```kotlin
56+
val agent = AIAgent(...)
57+
```
58+
<!--- KNIT example-[md-file-name]-01.kt -->
59+
```
60+
2. In case you need some imports, add include comment (`<!--- INCLIDE ... -->`)
61+
62+
```
63+
<!--- INCLUDE
64+
import ai.koog.agents.core.agent.AIAgent
65+
-->
66+
```kotlin
67+
val agent = AIAgent(...)
68+
```
69+
<!--- KNIT example-[md-file-name]-01.kt -->
70+
```
71+
3. In case you need to whap your code into `main` or other function imports,
72+
use include comment (`<!--- INCLIDE ... -->`) for prefix and suffix comment (`<!--- SUFFIX ... -->`) for suffix
73+
```
74+
<!--- INCLUDE
75+
import ai.koog.agents.core.agent.AIAgent
76+
fun main() {
77+
-->
78+
<!--- SUFFIX
79+
}
80+
-->
81+
```kotlin
82+
val agent = AIAgent(...)
83+
```
84+
<!--- KNIT example-[md-file-name]-01.kt -->
85+
```
86+
87+
For more information, follow the examples in the [kotlinx-knit](https://github.com/Kotlin/kotlinx-knit) repository
88+
or refer to already annotated code snippets in the documentation.
89+
90+
### API Documentation
91+
92+
API reference documentation is generated using [Dokka](https://github.com/Kotlin/dokka), a documentation engine for Kotlin. The API documentation is built with:
93+
94+
```
95+
./gradlew dokkaGenerate
96+
```
97+
98+
The generated API documentation is deployed to [https://api.koog.ai/](https://api.koog.ai/).
99+
100+
## Prompts
101+
102+
In the [prompt](./prompt) directory, prompting guidelines with extensions for popular modules are stored. These guidelines help users create effective prompts for different LLM models and use cases.

docs/build.gradle.kts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
group = rootProject.group
2+
version = rootProject.version
3+
4+
plugins {
5+
id("ai.kotlin.jvm")
6+
alias(libs.plugins.kotlin.serialization)
7+
alias(libs.plugins.knit)
8+
}
9+
10+
dependencies {
11+
implementation(project(":agents:agents-test"))
12+
implementation(project(":koog-agents"))
13+
implementation(libs.opentelemetry.exporter.otlp)
14+
implementation(libs.opentelemetry.exporter.logging)
15+
}
16+
17+
knit {
18+
rootDir = project.rootDir
19+
files = fileTree("docs/") {
20+
include("**/*.md")
21+
}
22+
moduleDocs = "docs/modules.md"
23+
siteRoot = "https://docs.koog.ai/"
24+
}

docs/docs/agent-events.md

Lines changed: 38 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -38,43 +38,65 @@ ai/agents/agents-features/agents-features-event-handler/ai.koog.agents.local.fea
3838

3939
To install the feature and configure event handlers for the agent, do the following:
4040

41-
```kotlin
42-
{
43-
install(EventHandler){
44-
// Define event handlers here
45-
onToolCall = { stage, tool, toolArgs ->
46-
// Handle tool call event
47-
}
41+
<!--- INCLUDE
42+
import ai.koog.agents.core.agent.AIAgent
43+
import ai.koog.agents.features.eventHandler.feature.handleEvents
44+
import ai.koog.prompt.executor.llms.all.simpleOllamaAIExecutor
45+
import ai.koog.prompt.llm.OllamaModels
4846
49-
onAgentFinished = { strategyName, result ->
50-
// Handle event triggered when the agent completes its execution
51-
}
47+
val agent = AIAgent(
48+
executor = simpleOllamaAIExecutor(),
49+
llmModel = OllamaModels.Meta.LLAMA_3_2,
50+
) {
51+
-->
52+
<!--- SUFFIX
53+
}
54+
-->
5255

53-
// Define other event handlers
56+
```kotlin
57+
handleEvents {
58+
// Handle tool calls
59+
onToolCall { eventContext ->
60+
println("Tool called: ${eventContext.tool} with args ${eventContext.toolArgs}")
61+
}
62+
// Handle event triggered when the agent completes its execution
63+
onAgentFinished { eventContext ->
64+
println("Agent finished with result: ${eventContext.result}")
5465
}
66+
67+
// Other event handlers
5568
}
5669
```
70+
<!--- KNIT example-events-01.kt -->
5771

5872
For more details about event handler configuration, see [API reference](https://api.koog.ai/agents/agents-features/agents-features-event-handler/ai.koog.agents.local.features.eventHandler.feature/-event-handler-config/index.html).
5973

6074
You can also set up event handlers using the `handleEvents` extension function when creating an agent.
6175
This function also installs the event handler feature and configures event handlers for the agent. Here is an example:
6276

77+
<!--- INCLUDE
78+
import ai.koog.agents.core.agent.AIAgent
79+
import ai.koog.agents.features.eventHandler.feature.handleEvents
80+
import ai.koog.prompt.executor.llms.all.simpleOllamaAIExecutor
81+
import ai.koog.prompt.llm.OllamaModels
82+
-->
6383
```kotlin
6484
val agent = AIAgent(
65-
// Initialization options
85+
executor = simpleOllamaAIExecutor(),
86+
llmModel = OllamaModels.Meta.LLAMA_3_2,
6687
){
6788
handleEvents {
6889
// Handle tool calls
69-
onToolCall = { stage, tool, toolArgs ->
70-
println("Tool called: ${tool.name} with args $toolArgs")
90+
onToolCall { eventContext ->
91+
println("Tool called: ${eventContext.tool} with args ${eventContext.toolArgs}")
7192
}
7293
// Handle event triggered when the agent completes its execution
73-
onAgentFinished = { strategyName, result ->
74-
println("Agent finished with result: $result")
94+
onAgentFinished { eventContext ->
95+
println("Agent finished with result: ${eventContext.result}")
7596
}
7697

7798
// Other event handlers
7899
}
79100
}
80101
```
102+
<!--- KNIT example-events-02.kt -->

0 commit comments

Comments
 (0)