Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose and add default Json serialization to ToolConfig #778

Merged
merged 2 commits into from
Aug 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading