Skip to content

Commit

Permalink
add: new anthropicTypeOf function to translate Kotlin fully qualified…
Browse files Browse the repository at this point in the history
… names into underscore notation (dot removal), documentation and examples updated
  • Loading branch information
morisil committed Oct 2, 2024
1 parent cb37b25 commit 9cc98c4
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 18 deletions.
12 changes: 5 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,14 +114,12 @@ fun main() {

fun calculate() = operation.calculate(a, b)

}
}

val client = Anthropic()

val calculatorTool = Tool(
name = "calculator",
description = "Perform basic arithmetic operations",
inputSchema = jsonSchemaOf<Calculator>(),
val calculatorTool = Tool<Calculator>(
description = "Perform basic arithmetic operations"
)

val response = runBlocking {
Expand All @@ -133,7 +131,7 @@ fun main() {
toolChoice = ToolChoice.Any()
}
}

val toolUse = response.content[0] as ToolUse
val calculator = toolUse.input<Calculator>()
val result = calculator.calculate()
Expand Down
3 changes: 3 additions & 0 deletions src/commonMain/kotlin/Anthropic.kt
Original file line number Diff line number Diff line change
Expand Up @@ -156,3 +156,6 @@ class Anthropic internal constructor(
val messages = Messages()

}

inline fun <reified T> anthropicTypeOf(): String =
T::class.qualifiedName!!.replace('.', '_')
3 changes: 2 additions & 1 deletion src/commonMain/kotlin/message/Messages.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.xemantic.anthropic.message

import com.xemantic.anthropic.anthropicJson
import com.xemantic.anthropic.anthropicTypeOf
import com.xemantic.anthropic.schema.JsonSchema
import com.xemantic.anthropic.schema.jsonSchemaOf
import kotlinx.serialization.ExperimentalSerializationApi
Expand Down Expand Up @@ -192,7 +193,7 @@ inline fun <reified T> Tool(
description: String,
cacheControl: CacheControl? = null
): Tool = Tool(
name = T::class.qualifiedName!!,
name = anthropicTypeOf<T>(),
description = description,
inputSchema = jsonSchemaOf<T>(),
cacheControl = cacheControl
Expand Down
2 changes: 1 addition & 1 deletion src/commonMain/kotlin/schema/JsonSchemaGenerator.kt
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ private fun generateSchemaProperty(
)
StructureKind.MAP -> JsonSchemaProperty("object")
StructureKind.CLASS -> {
val refName = descriptor.serialName.trimEnd('?')
val refName = descriptor.serialName.replace('.', '_').trimEnd('?')
definitions[refName] = generateSchema(descriptor)
JsonSchemaProperty("\$ref", ref = "#/definitions/$refName")
}
Expand Down
9 changes: 3 additions & 6 deletions src/commonTest/kotlin/AnthropicTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import com.xemantic.anthropic.message.Text
import com.xemantic.anthropic.message.Tool
import com.xemantic.anthropic.message.ToolChoice
import com.xemantic.anthropic.message.ToolUse
import com.xemantic.anthropic.schema.jsonSchemaOf
import kotlinx.coroutines.flow.filterIsInstance
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.toList
Expand Down Expand Up @@ -110,6 +109,7 @@ class AnthropicTest {
val b: Double
) {

@Suppress("unused") // it is used, but by Anthropic, so we skip the warning
enum class Operation(
val calculate: (a: Double, b: Double) -> Double
) {
Expand All @@ -127,11 +127,8 @@ class AnthropicTest {
fun shouldUseCalculatorTool() = runTest {
// given
val client = Anthropic()
val calculatorTool = Tool(
name = "calculator",
val calculatorTool = Tool<Calculator>(
description = "Perform basic arithmetic operations",
inputSchema = jsonSchemaOf<Calculator>(),
cacheControl = null
)

// when
Expand All @@ -148,7 +145,7 @@ class AnthropicTest {
assertTrue(content.size == 1)
assertTrue(content[0] is ToolUse)
val toolUse = content[0] as ToolUse
assertTrue(toolUse.name == "calculator")
assertTrue(toolUse.name == "com_xemantic_anthropic_AnthropicTest_Calculator")
val calculator = toolUse.input<Calculator>()
val result = calculator.calculate()
assertTrue(result == 15.0 * 7.0)
Expand Down
5 changes: 2 additions & 3 deletions src/commonTest/kotlin/schema/JsonSchemaGeneratorTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,11 @@ class JsonSchemaGeneratorTest {
val schemaJson = json.encodeToString(schema)

// then
print(schemaJson)
schemaJson shouldEqualJson """
{
"type": "object",
"definitions": {
"com.xemantic.anthropic.schema.Address": {
"com_xemantic_anthropic_schema_Address": {
"type": "object",
"properties": {
"street": {
Expand Down Expand Up @@ -118,7 +117,7 @@ class JsonSchemaGeneratorTest {
},
"address": {
"type": "${'$'}ref",
"ref": "#/definitions/com.xemantic.anthropic.schema.Address"
"ref": "#/definitions/com_xemantic_anthropic_schema_Address"
}
},
"required": [
Expand Down

0 comments on commit 9cc98c4

Please sign in to comment.