Skip to content

Commit

Permalink
feat: expose and add default Json serialization to ToolConfig
Browse files Browse the repository at this point in the history
  • Loading branch information
realdavidvega committed Aug 21, 2024
1 parent f741c95 commit 498314e
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 498314e

Please sign in to comment.