com.github.frtu.kotlin.llm.os.tool.Function.kt Maven / Gradle / Ivy
package com.github.frtu.kotlin.llm.os.tool
import com.aallam.openai.api.chat.ChatCompletionFunction
import com.aallam.openai.api.chat.Parameters
import com.github.frtu.kotlin.serdes.json.schema.SchemaGen
import kotlin.reflect.KFunction2
/**
* Base class for callable function
*/
data class Function(
val name: String,
val description: String? = null,
val action: KFunction2,
val parameterJsonSchema: String,
val returnJsonSchema: String,
// val parameters: List,
) {
fun toChatCompletionFunction() = ChatCompletionFunction(
name, description,
Parameters.fromJsonString(parameterJsonSchema),
)
}
fun function(
name: String,
description: String? = null,
action: KFunction2,
parameterJsonSchema: String,
returnJsonSchema: String,
) = Function(name, description, action, parameterJsonSchema, returnJsonSchema)
fun function(
name: String,
description: String? = null,
action: KFunction2,
parameterClass: Class<*>,
returnClass: Class<*>,
) = Function(
name,
description,
action,
SchemaGen.generateJsonSchema(parameterClass),
SchemaGen.generateJsonSchema(returnClass),
)