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

Assistant JSON config #774

Merged
merged 1 commit into from
Aug 13, 2024
Merged
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
Expand Up @@ -9,7 +9,6 @@ import com.xebia.functional.xef.llm.models.functions.buildJsonSchema
import io.ktor.util.logging.*
import kotlinx.serialization.KSerializer
import kotlinx.serialization.Serializable
import kotlinx.serialization.json.Json
import kotlinx.serialization.json.JsonElement
import kotlinx.serialization.json.JsonObject
import kotlinx.serialization.json.JsonPrimitive
Expand Down Expand Up @@ -52,14 +51,17 @@ class Assistant(
val toolConfig = toolsConfig.firstOrNull { it.functionObject.name == name }

val toolSerializer = toolConfig?.serializers ?: error("Function $name not registered")
val input = Json.decodeFromString(toolSerializer.inputSerializer, args)
val input = config.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 =
Json.encodeToJsonElement(toolSerializer.outputSerializer as KSerializer<Any?>, output)
config.json.encodeToJsonElement(
toolSerializer.outputSerializer as KSerializer<Any?>,
output
)
ToolOutput(schema, result)
} catch (e: Exception) {
if (e is AssertionError) throw e
Expand Down Expand Up @@ -164,7 +166,7 @@ class Assistant(
functionObject.name,
functionObject.description ?: "",
functionObject.parameters?.let { el ->
Json.encodeToString(JsonObject.serializer(), el)
config.json.encodeToString(JsonObject.serializer(), el)
} ?: ""
)
} else {
Expand Down Expand Up @@ -195,7 +197,7 @@ class Assistant(
name = assistantRequest.name,
description = assistantRequest.description,
instructions = assistantRequest.instructions,
tools = assistantTools(assistantRequest),
tools = assistantTools(assistantRequest, config),
toolResources =
assistantRequest.toolResources?.let {
ModifyAssistantRequestToolResources(
Expand All @@ -220,7 +222,7 @@ class Assistant(
name = assistantRequest.name,
description = assistantRequest.description,
instructions = assistantRequest.instructions,
tools = assistantTools(assistantRequest),
tools = assistantTools(assistantRequest, config),
toolResources = assistantRequest.toolResources,
metadata =
assistantRequest.metadata
Expand All @@ -234,7 +236,8 @@ class Assistant(
}

private fun assistantTools(
assistantRequest: AssistantRequest
assistantRequest: AssistantRequest,
config: Config
): List<AssistantObjectToolsInner> =
assistantRequest.tools.orEmpty().map {
when (it) {
Expand All @@ -254,7 +257,7 @@ class Assistant(
FunctionObject(
name = it.name,
parameters =
Json.parseToJsonElement(it.parameters) as? JsonObject
config.json.parseToJsonElement(it.parameters) as? JsonObject
?: JsonObject(emptyMap()),
description = it.description
)
Expand Down
Loading