Skip to content

Commit

Permalink
@description separated from the @AnthropicTool
Browse files Browse the repository at this point in the history
  • Loading branch information
morisil committed Oct 18, 2024
1 parent 3f7fbc7 commit 97d2902
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 38 deletions.
12 changes: 4 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,10 +125,8 @@ fun main() {
If you want to write AI agents, you need tools, and this is where this library shines:

```kotlin
@AnthropicTool(
name = "get_weather",
description = "Get the weather for a specific location"
)
@AnthropicTool("get_weather")
@Description("Get the weather for a specific location")
data class WeatherTool(val location: String): UsableTool {
override fun use(
toolUseId: String
Expand Down Expand Up @@ -187,10 +185,8 @@ services providing some facilities, like HTTP client to connect to the
internet or DB connection pool to access the database.

```kotlin
@AnthropicTool(
name = "query_database",
description = "Executes SQL on the database"
)
@AnthropicTool("query_database")
@Description("Executes SQL on the database")
data class DatabaseQueryTool(val sql: String): UsableTool {

@Transient
Expand Down
2 changes: 1 addition & 1 deletion src/commonMain/kotlin/schema/JsonSchema.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable

@OptIn(ExperimentalSerializationApi::class)
@Target(AnnotationTarget.PROPERTY)
@Target(AnnotationTarget.PROPERTY, AnnotationTarget.CLASS)
@MetaSerializable
annotation class Description(
val value: String
Expand Down
23 changes: 10 additions & 13 deletions src/commonMain/kotlin/tool/Tools.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.xemantic.anthropic.tool
import com.xemantic.anthropic.message.CacheControl
import com.xemantic.anthropic.message.Tool
import com.xemantic.anthropic.message.ToolResult
import com.xemantic.anthropic.schema.Description
import com.xemantic.anthropic.schema.jsonSchemaOf
import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.MetaSerializable
Expand All @@ -16,14 +17,12 @@ import kotlinx.serialization.serializer
* of the Anthropic API. It includes a name and description for the tool.
*
* @property name The name of the tool. This name is used during serialization and should be a unique identifier for the tool.
* @property description A comprehensive description of what the tool does and how it should be used.
*/
@OptIn(ExperimentalSerializationApi::class)
@MetaSerializable
@Target(AnnotationTarget.CLASS)
annotation class AnthropicTool(
val name: String,
val description: String = ""
val name: String
)

/**
Expand All @@ -45,15 +44,6 @@ interface UsableTool {

}

fun Tool.cacheControl(
cacheControl: CacheControl? = null
): Tool = if (cacheControl == null) this else Tool(
name,
description,
inputSchema,
cacheControl
)

@OptIn(ExperimentalSerializationApi::class)
inline fun <reified T : UsableTool> toolOf(
cacheControl: CacheControl? = null // TODO should it be here?
Expand All @@ -78,10 +68,17 @@ inline fun <reified T : UsableTool> toolOf(
"The class ${T::class.qualifiedName} must be annotated with @AnthropicTool"
)

val description = serializer
.descriptor
.annotations
.filterIsInstance<Description>()
.firstOrNull()
?.value

return Tool(
name = anthropicTool.name,
// annotation description cannot be null, so we allow empty and detect it here
description = if (anthropicTool.description.isNotBlank()) anthropicTool.description else null,
description = description,
inputSchema = jsonSchemaOf<T>(),
cacheControl = cacheControl
)
Expand Down
19 changes: 7 additions & 12 deletions src/commonTest/kotlin/test/AnthropicTestTools.kt
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
package com.xemantic.anthropic.test

import com.xemantic.anthropic.message.ToolResult
import com.xemantic.anthropic.schema.Description
import com.xemantic.anthropic.tool.AnthropicTool
import com.xemantic.anthropic.tool.UsableTool
import kotlinx.serialization.Transient

@AnthropicTool(
name = "FibonacciTool",
description = "Calculate Fibonacci number n"
)
@AnthropicTool("FibonacciTool")
@Description("Calculate Fibonacci number n")
data class FibonacciTool(val n: Int): UsableTool {

tailrec fun fibonacci(
Expand All @@ -23,10 +22,8 @@ data class FibonacciTool(val n: Int): UsableTool {

}

@AnthropicTool(
name = "Calculator",
description = "Calculates the arithmetic outcome of an operation when given the arguments a and b"
)
@AnthropicTool("Calculator")
@Description("Calculates the arithmetic outcome of an operation when given the arguments a and b")
data class Calculator(
val operation: Operation,
val a: Double,
Expand Down Expand Up @@ -64,10 +61,8 @@ class TestDatabase : Database {
}
}

@AnthropicTool(
name = "DatabaseQuery",
description = "Executes database query"
)
@AnthropicTool("DatabaseQuery")
@Description("Executes database query")
data class DatabaseQueryTool(
val query: String
) : UsableTool {
Expand Down
6 changes: 2 additions & 4 deletions src/commonTest/kotlin/tool/UsableToolTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,8 @@ import kotlin.test.Test

class UsableToolTest {

@AnthropicTool(
name = "TestTool",
description = "Test tool receiving a message and outputting it back"
)
@AnthropicTool("TestTool")
@Description("Test tool receiving a message and outputting it back")
class TestTool(
@Description("the message")
val message: String
Expand Down

0 comments on commit 97d2902

Please sign in to comment.