Skip to content

Commit

Permalink
feat: expose and add default Json serialization to ToolConfig (#778)
Browse files Browse the repository at this point in the history
Co-authored-by: Francisco Diaz <[email protected]>
  • Loading branch information
realdavidvega and franciscodr authored Aug 22, 2024
1 parent 30280d3 commit 46a4d4f
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,21 @@
package com.xebia.functional.xef.llm.assistants

import com.xebia.functional.openai.generated.api.Assistants
import com.xebia.functional.openai.generated.model.*
import com.xebia.functional.openai.generated.model.AssistantObject
import com.xebia.functional.openai.generated.model.AssistantObjectToolsInner
import com.xebia.functional.openai.generated.model.AssistantToolsCode
import com.xebia.functional.openai.generated.model.AssistantToolsFileSearch
import com.xebia.functional.openai.generated.model.AssistantToolsFunction
import com.xebia.functional.openai.generated.model.CreateAssistantRequest
import com.xebia.functional.openai.generated.model.CreateAssistantRequestModel
import com.xebia.functional.openai.generated.model.CreateAssistantRequestToolResources
import com.xebia.functional.openai.generated.model.CreateAssistantRequestToolResourcesCodeInterpreter
import com.xebia.functional.openai.generated.model.CreateAssistantRequestToolResourcesFileSearch
import com.xebia.functional.openai.generated.model.FunctionObject
import com.xebia.functional.openai.generated.model.ModifyAssistantRequest
import com.xebia.functional.openai.generated.model.ModifyAssistantRequestToolResources
import com.xebia.functional.openai.generated.model.ModifyAssistantRequestToolResourcesCodeInterpreter
import com.xebia.functional.openai.generated.model.ModifyAssistantRequestToolResourcesFileSearch
import com.xebia.functional.xef.Config
import com.xebia.functional.xef.OpenAI
import com.xebia.functional.xef.llm.assistants.AssistantThread.Companion.defaultConfig
Expand Down Expand Up @@ -51,14 +65,14 @@ class Assistant(
val toolConfig = toolsConfig.firstOrNull { it.functionObject.name == name }

val toolSerializer = toolConfig?.serializers ?: error("Function $name not registered")
val input = config.json.decodeFromString(toolSerializer.inputSerializer, args)
val input = toolConfig.json.decodeFromString(toolSerializer.inputSerializer, args)

val tool: Tool<Any?, Any?> = toolConfig.tool as Tool<Any?, Any?>

val schema = buildJsonSchema(toolSerializer.outputSerializer.descriptor)
val output: Any? = tool(input)
val result =
config.json.encodeToJsonElement(
toolConfig.json.encodeToJsonElement(
toolSerializer.outputSerializer as KSerializer<Any?>,
output
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ 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.json.Json
import kotlinx.serialization.serializer

fun interface Tool<Input, out Output> {
Expand All @@ -14,7 +15,8 @@ fun interface Tool<Input, out Output> {
data class ToolConfig<Input, out Output>(
val functionObject: FunctionObject,
val serializers: ToolSerializer,
val tool: Tool<Input, Output>
val tool: Tool<Input, Output>,
val json: Json = Json.Default
)

data class ToolSerializer(
Expand Down

0 comments on commit 46a4d4f

Please sign in to comment.