From 9cc98c4515e4311b57978269daf3153c4d3ed773 Mon Sep 17 00:00:00 2001 From: Kazik Pogoda Date: Wed, 2 Oct 2024 11:26:47 +0200 Subject: [PATCH] add: new anthropicTypeOf function to translate Kotlin fully qualified names into underscore notation (dot removal), documentation and examples updated --- README.md | 12 +++++------- src/commonMain/kotlin/Anthropic.kt | 3 +++ src/commonMain/kotlin/message/Messages.kt | 3 ++- src/commonMain/kotlin/schema/JsonSchemaGenerator.kt | 2 +- src/commonTest/kotlin/AnthropicTest.kt | 9 +++------ .../kotlin/schema/JsonSchemaGeneratorTest.kt | 5 ++--- 6 files changed, 16 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index de01e84..041a8b8 100644 --- a/README.md +++ b/README.md @@ -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(), + val calculatorTool = Tool( + description = "Perform basic arithmetic operations" ) val response = runBlocking { @@ -133,7 +131,7 @@ fun main() { toolChoice = ToolChoice.Any() } } - + val toolUse = response.content[0] as ToolUse val calculator = toolUse.input() val result = calculator.calculate() diff --git a/src/commonMain/kotlin/Anthropic.kt b/src/commonMain/kotlin/Anthropic.kt index e31fd76..b38f0a4 100644 --- a/src/commonMain/kotlin/Anthropic.kt +++ b/src/commonMain/kotlin/Anthropic.kt @@ -156,3 +156,6 @@ class Anthropic internal constructor( val messages = Messages() } + +inline fun anthropicTypeOf(): String = + T::class.qualifiedName!!.replace('.', '_') diff --git a/src/commonMain/kotlin/message/Messages.kt b/src/commonMain/kotlin/message/Messages.kt index 42477a3..6044cfb 100644 --- a/src/commonMain/kotlin/message/Messages.kt +++ b/src/commonMain/kotlin/message/Messages.kt @@ -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 @@ -192,7 +193,7 @@ inline fun Tool( description: String, cacheControl: CacheControl? = null ): Tool = Tool( - name = T::class.qualifiedName!!, + name = anthropicTypeOf(), description = description, inputSchema = jsonSchemaOf(), cacheControl = cacheControl diff --git a/src/commonMain/kotlin/schema/JsonSchemaGenerator.kt b/src/commonMain/kotlin/schema/JsonSchemaGenerator.kt index a66b098..ef71a22 100644 --- a/src/commonMain/kotlin/schema/JsonSchemaGenerator.kt +++ b/src/commonMain/kotlin/schema/JsonSchemaGenerator.kt @@ -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") } diff --git a/src/commonTest/kotlin/AnthropicTest.kt b/src/commonTest/kotlin/AnthropicTest.kt index c65ea0e..061e1c0 100644 --- a/src/commonTest/kotlin/AnthropicTest.kt +++ b/src/commonTest/kotlin/AnthropicTest.kt @@ -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 @@ -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 ) { @@ -127,11 +127,8 @@ class AnthropicTest { fun shouldUseCalculatorTool() = runTest { // given val client = Anthropic() - val calculatorTool = Tool( - name = "calculator", + val calculatorTool = Tool( description = "Perform basic arithmetic operations", - inputSchema = jsonSchemaOf(), - cacheControl = null ) // when @@ -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() val result = calculator.calculate() assertTrue(result == 15.0 * 7.0) diff --git a/src/commonTest/kotlin/schema/JsonSchemaGeneratorTest.kt b/src/commonTest/kotlin/schema/JsonSchemaGeneratorTest.kt index b796c47..4c824c4 100644 --- a/src/commonTest/kotlin/schema/JsonSchemaGeneratorTest.kt +++ b/src/commonTest/kotlin/schema/JsonSchemaGeneratorTest.kt @@ -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": { @@ -118,7 +117,7 @@ class JsonSchemaGeneratorTest { }, "address": { "type": "${'$'}ref", - "ref": "#/definitions/com.xemantic.anthropic.schema.Address" + "ref": "#/definitions/com_xemantic_anthropic_schema_Address" } }, "required": [