xebia-functional / xef

Building applications with LLMs through composability, in Kotlin, Scala, ...
https://xef.ai
Apache License 2.0
174 stars 15 forks source link

Enum description in tools #732

Closed Montagon closed 4 months ago

Montagon commented 4 months ago

This PR modifies the serialization info of a tool to allow descriptions in the enum values. So for the following structure:

@Description("Natural numbers")
enum class NaturalWithDescriptions {
  @Description("If the number is positive.") POSITIVE,
  @Description("If the number is negative.") NEGATIVE
}

@Serializable
data class SumInputWithDescription(
  @Description("Left operand") val left: Int,
  @Description("Right operand") val right: Int,
  val natural: NaturalWithDescriptions
)

class SumToolWithDescription : Tool<SumInputWithDescription, Int> {
  override suspend fun invoke(input: SumInputWithDescription): Int {
    return input.left + input.right
  }
}

The serialization will be:

{
  "$schema": "http://json-schema.org/draft-07/schema",
  "type": "object",
  "properties": {
    "left": {
      "type": "number",
      "description": "Left operand"
    },
    "right": {
      "type": "number",
      "description": "Right operand"
    },
    "natural": {
      "type": "string",
      "enum": [
        "POSITIVE",
        "NEGATIVE"
      ],
      "description": "Natural numbers\n - POSITIVE (If the number is positive.)\n - NEGATIVE (If the number is negative.)"
    }
  },
  "required": [
    "left",
    "right",
    "natural"
  ],
  "definitions": {}
}