Skip to content

Commit

Permalink
Set default description for assistant tools (#777)
Browse files Browse the repository at this point in the history
* feat: set tool description if available for assistant tool

* style: spotless
  • Loading branch information
realdavidvega authored Aug 20, 2024
1 parent 7295e6c commit f741c95
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.xebia.functional.xef.llm.assistants

import com.xebia.functional.openai.generated.model.FunctionObject
import com.xebia.functional.xef.llm.chatFunction
import com.xebia.functional.xef.llm.defaultFunctionDescription
import kotlinx.serialization.KSerializer
import kotlinx.serialization.serializer

Expand All @@ -22,14 +23,16 @@ fun interface Tool<Input, out Output> {
)

inline fun <reified I, reified O> toolOf(tool: Tool<I, O>): ToolConfig<I, O> {
val serializer = serializer<I>()
val inputSerializer = serializer<I>()
val outputSerializer = serializer<O>()
val toolSerializer = ToolSerializer(serializer, outputSerializer)
val fn = chatFunction(serializer.descriptor)
val toolSerializer = ToolSerializer(inputSerializer, outputSerializer)
val fn = chatFunction(inputSerializer.descriptor)
val fnName = tool::class.simpleName ?: error("unnamed class")
val fnDescription = defaultFunctionDescription(fnName)
return ToolConfig(
fn.copy(name = tool::class.simpleName ?: error("unnamed class")),
toolSerializer,
tool
functionObject = fn.copy(name = fnName, description = fnDescription),
serializers = toolSerializer,
tool = tool
)
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.xebia.functional.xef.assistants

import com.xebia.functional.xef.llm.assistants.Tool
import com.xebia.functional.xef.llm.defaultFunctionDescription
import io.kotest.core.spec.style.StringSpec
import io.kotest.matchers.shouldBe
import kotlinx.serialization.Serializable

class ToolDescriptionTests :
StringSpec({
"Tool has tool name and default description" {
val tool = TestTool()
val toolConfig = Tool.toolOf(tool)
val function = toolConfig.functionObject
val fnName = tool::class.simpleName ?: error("unnamed class")
function.name shouldBe fnName
function.description shouldBe defaultFunctionDescription(fnName)
}
}) {

@Serializable data class Request(val input: String)

@Serializable data class Response(val output: String = "Test response")

class TestTool : Tool<Request, Response> {
override suspend fun invoke(input: Request): Response = Response()
}
}

0 comments on commit f741c95

Please sign in to comment.