All Downloads are FREE. Search and download functionalities are using the official Maven repository.

commonMain.com.xebia.functional.xef.PromptMultipleClassifier.kt Maven / Gradle / Ivy

The newest version!
package com.xebia.functional.xef

import com.xebia.functional.xef.conversation.Description
import kotlin.reflect.KType
import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.KSerializer
import kotlinx.serialization.descriptors.SerialKind
import kotlinx.serialization.serializer

interface PromptMultipleClassifier {
  fun getItems(): List

  fun template(input: String): String {
    val items = getItems()

    return """
       |Based on the , identify whether the user is asking about one or more of the following items
       |
       |${
      items.joinToString("\n") { item -> "<${item.name}>${item.description}" }
    }
       | 
       |
       |${
      items.mapIndexed { index, item -> "\t${item.name}" }
        .joinToString("\n")
    }
       |
       |
       |$input
       |
    """
  }

  @OptIn(ExperimentalSerializationApi::class)
  fun KType.enumValuesName(serializer: KSerializer = serializer(this)): List {
    return if (serializer.descriptor.kind != SerialKind.ENUM) {
      emptyList()
    } else {
      (0 until serializer.descriptor.elementsCount).map { index ->
        val name =
          serializer.descriptor.getElementName(index).removePrefix(serializer.descriptor.serialName)
        val description =
          (serializer.descriptor.getElementAnnotations(index).first { it is Description }
              as Description)
            .value
        Classification(name, description)
      }
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy