From 6b534acbfaab60173f88c889467f5f2a0eb00a8e Mon Sep 17 00:00:00 2001 From: David Vega Lichacz <7826728+realdavidvega@users.noreply.github.com> Date: Thu, 26 Sep 2024 11:31:02 +0200 Subject: [PATCH 1/2] feat: expose tool input and output json config --- .../xef/llm/assistants/Assistant.kt | 12 +++---- .../functional/xef/llm/assistants/Tool.kt | 34 ++++++++++++------- 2 files changed, 28 insertions(+), 18 deletions(-) diff --git a/core/src/commonMain/kotlin/com/xebia/functional/xef/llm/assistants/Assistant.kt b/core/src/commonMain/kotlin/com/xebia/functional/xef/llm/assistants/Assistant.kt index 1c626147e..b3f16ca2c 100644 --- a/core/src/commonMain/kotlin/com/xebia/functional/xef/llm/assistants/Assistant.kt +++ b/core/src/commonMain/kotlin/com/xebia/functional/xef/llm/assistants/Assistant.kt @@ -63,17 +63,17 @@ class Assistant( suspend inline fun getToolRegistered(name: String, args: String): ToolOutput = try { val toolConfig = toolsConfig.firstOrNull { it.functionObject.name == name } + val (inputSerializer, outputSerializer) = + toolConfig?.serialization ?: error("Function $name not registered") - val toolSerializer = toolConfig?.serializers ?: error("Function $name not registered") - val input = toolConfig.json.decodeFromString(toolSerializer.inputSerializer, args) - + val input = inputSerializer.json.decodeFromString(inputSerializer.serializer, args) val tool: Tool = toolConfig.tool as Tool - val schema = buildJsonSchema(toolSerializer.outputSerializer.descriptor) + val schema = buildJsonSchema(outputSerializer.serializer.descriptor) val output: Any? = tool(input) val result = - toolConfig.json.encodeToJsonElement( - toolSerializer.outputSerializer as KSerializer, + outputSerializer.json.encodeToJsonElement( + outputSerializer.serializer as KSerializer, output ) ToolOutput(schema, result) diff --git a/core/src/commonMain/kotlin/com/xebia/functional/xef/llm/assistants/Tool.kt b/core/src/commonMain/kotlin/com/xebia/functional/xef/llm/assistants/Tool.kt index 7373026e7..673c9c618 100644 --- a/core/src/commonMain/kotlin/com/xebia/functional/xef/llm/assistants/Tool.kt +++ b/core/src/commonMain/kotlin/com/xebia/functional/xef/llm/assistants/Tool.kt @@ -14,26 +14,36 @@ fun interface Tool { data class ToolConfig( val functionObject: FunctionObject, - val serializers: ToolSerializer, - val tool: Tool, - val json: Json = Json.Default + val serialization: ToolSerialization, + val tool: Tool ) - data class ToolSerializer( - val inputSerializer: KSerializer<*>, - val outputSerializer: KSerializer<*> + data class ToolSerialization( + val inputSerializer: ToolSerializer, + val outputSerializer: ToolSerializer ) - inline fun toolOf(tool: Tool): ToolConfig { - val inputSerializer = serializer() - val outputSerializer = serializer() - val toolSerializer = ToolSerializer(inputSerializer, outputSerializer) - val fn = chatFunction(inputSerializer.descriptor) + data class ToolSerializer(val serializer: KSerializer<*>, val json: Json) + + data class ToolJson(val inputJson: Json, val outputJson: Json) { + companion object { + val Default = ToolJson(inputJson = Json.Default, outputJson = Json.Default) + } + } + + inline fun toolOf( + tool: Tool, + jsonConfig: ToolJson = ToolJson.Default + ): ToolConfig { + val inputSerializer = ToolSerializer(serializer(), jsonConfig.inputJson) + val outputSerializer = ToolSerializer(serializer(), jsonConfig.outputJson) + val toolSerializer = ToolSerialization(inputSerializer, outputSerializer) + val fn = chatFunction(inputSerializer.serializer.descriptor) val fnName = tool::class.simpleName ?: error("unnamed class") val fnDescription = defaultFunctionDescription(fnName) return ToolConfig( functionObject = fn.copy(name = fnName, description = fnDescription), - serializers = toolSerializer, + serialization = toolSerializer, tool = tool ) } From 5d0d9bc753f615e4281bc5d4477b56a2fc0b27d4 Mon Sep 17 00:00:00 2001 From: David Vega Lichacz <7826728+realdavidvega@users.noreply.github.com> Date: Thu, 26 Sep 2024 12:54:53 +0200 Subject: [PATCH 2/2] refactor: move json config out of companion --- .../xebia/functional/xef/llm/assistants/Tool.kt | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/core/src/commonMain/kotlin/com/xebia/functional/xef/llm/assistants/Tool.kt b/core/src/commonMain/kotlin/com/xebia/functional/xef/llm/assistants/Tool.kt index 673c9c618..5e0feb6ed 100644 --- a/core/src/commonMain/kotlin/com/xebia/functional/xef/llm/assistants/Tool.kt +++ b/core/src/commonMain/kotlin/com/xebia/functional/xef/llm/assistants/Tool.kt @@ -10,6 +10,12 @@ import kotlinx.serialization.serializer fun interface Tool { suspend operator fun invoke(input: Input): Output + data class JsonConfig(val inputJson: Json, val outputJson: Json) { + companion object { + val Default = JsonConfig(inputJson = Json.Default, outputJson = Json.Default) + } + } + companion object { data class ToolConfig( @@ -25,15 +31,9 @@ fun interface Tool { data class ToolSerializer(val serializer: KSerializer<*>, val json: Json) - data class ToolJson(val inputJson: Json, val outputJson: Json) { - companion object { - val Default = ToolJson(inputJson = Json.Default, outputJson = Json.Default) - } - } - inline fun toolOf( tool: Tool, - jsonConfig: ToolJson = ToolJson.Default + jsonConfig: JsonConfig = JsonConfig.Default ): ToolConfig { val inputSerializer = ToolSerializer(serializer(), jsonConfig.inputJson) val outputSerializer = ToolSerializer(serializer(), jsonConfig.outputJson)