-
Notifications
You must be signed in to change notification settings - Fork 183
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(chat): add function calling (#200)
- Loading branch information
1 parent
a1a0c4c
commit 409d5bb
Showing
8 changed files
with
227 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
openai-core/src/commonMain/kotlin/com.aallam.openai.api/chat/ChatCompletionFunction.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package com.aallam.openai.api.chat | ||
|
||
import kotlinx.serialization.SerialName | ||
import kotlinx.serialization.Serializable | ||
|
||
@Serializable | ||
public data class ChatCompletionFunction( | ||
/** | ||
* The name of the function to be called. Must be a-z, A-Z, 0-9, or contain underscores and dashes, with a maximum | ||
* length of 64. | ||
*/ | ||
@SerialName("name") val name: String, | ||
/** | ||
* The description of what the function does. | ||
*/ | ||
@SerialName("description") val description: String? = null, | ||
/** | ||
* The parameters the functions accepts, described as a JSON Schema object. See the guide for examples and the | ||
* JSON Schema reference for documentation about the format. | ||
*/ | ||
@SerialName("parameters") val parameters: JsonData? = null, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
openai-core/src/commonMain/kotlin/com.aallam.openai.api/chat/FunctionCall.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package com.aallam.openai.api.chat | ||
|
||
import com.aallam.openai.api.BetaOpenAI | ||
import kotlinx.serialization.KSerializer | ||
import kotlinx.serialization.Serializable | ||
import kotlinx.serialization.descriptors.SerialDescriptor | ||
import kotlinx.serialization.descriptors.buildClassSerialDescriptor | ||
import kotlinx.serialization.encoding.Decoder | ||
import kotlinx.serialization.encoding.Encoder | ||
import kotlinx.serialization.json.JsonDecoder | ||
import kotlinx.serialization.json.JsonEncoder | ||
import kotlinx.serialization.json.JsonObject | ||
import kotlinx.serialization.json.JsonPrimitive | ||
import kotlin.jvm.JvmInline | ||
|
||
@BetaOpenAI | ||
@Serializable(with = FunctionCallSerializer::class) | ||
public sealed interface FunctionCall { | ||
public val name: String | ||
|
||
public companion object { | ||
public val Auto: FunctionCall = FunctionCallString("auto") | ||
public val None: FunctionCall = FunctionCallString("none") | ||
public fun forceCall(name: String): FunctionCall = FunctionCallObject(name) | ||
} | ||
} | ||
@OptIn(BetaOpenAI::class) | ||
internal object FunctionCallSerializer: KSerializer<FunctionCall>{ | ||
override val descriptor: SerialDescriptor = buildClassSerialDescriptor("FunctionCall") {} | ||
private val objectSerializer = FunctionCallObject.serializer() | ||
override fun deserialize(decoder: Decoder): FunctionCall { | ||
if(decoder is JsonDecoder){ | ||
return when(val json = decoder.decodeJsonElement()){ | ||
is JsonPrimitive -> FunctionCallString(json.content) | ||
is JsonObject -> objectSerializer.deserialize(decoder) | ||
else -> throw UnsupportedOperationException("Cannot deserialize Parameters") | ||
} | ||
} | ||
throw UnsupportedOperationException("Cannot deserialize Parameters") | ||
} | ||
|
||
override fun serialize(encoder: Encoder, value: FunctionCall) { | ||
if(encoder is JsonEncoder){ | ||
when(value){ | ||
is FunctionCallString -> encoder.encodeString(value.name) | ||
is FunctionCallObject -> objectSerializer.serialize(encoder, value) | ||
} | ||
return | ||
} | ||
throw UnsupportedOperationException("Cannot deserialize Parameters") | ||
} | ||
} | ||
|
||
@OptIn(BetaOpenAI::class) | ||
@JvmInline | ||
@Serializable | ||
internal value class FunctionCallString(override val name: String): FunctionCall | ||
|
||
@OptIn(BetaOpenAI::class) | ||
@Serializable | ||
internal data class FunctionCallObject(override val name: String): FunctionCall | ||
|
50 changes: 50 additions & 0 deletions
50
openai-core/src/commonMain/kotlin/com.aallam.openai.api/chat/JsonData.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package com.aallam.openai.api.chat | ||
|
||
import kotlinx.serialization.KSerializer | ||
import kotlinx.serialization.Serializable | ||
import kotlinx.serialization.descriptors.SerialDescriptor | ||
import kotlinx.serialization.descriptors.buildClassSerialDescriptor | ||
import kotlinx.serialization.encoding.Decoder | ||
import kotlinx.serialization.encoding.Encoder | ||
import kotlinx.serialization.json.Json | ||
import kotlinx.serialization.json.JsonDecoder | ||
import kotlinx.serialization.json.JsonElement | ||
import kotlinx.serialization.json.JsonEncoder | ||
import kotlinx.serialization.json.JsonObjectBuilder | ||
import kotlinx.serialization.json.buildJsonObject | ||
import kotlin.jvm.JvmInline | ||
|
||
@JvmInline | ||
@Serializable(with = JsonData.JsonDataSerializer::class) | ||
public value class JsonData(public val json: JsonElement){ | ||
public object JsonDataSerializer: KSerializer<JsonData>{ | ||
override val descriptor: SerialDescriptor = buildClassSerialDescriptor("JsonData") {} | ||
|
||
override fun deserialize(decoder: Decoder): JsonData { | ||
if(decoder is JsonDecoder){ | ||
return JsonData(decoder.decodeJsonElement()) | ||
} | ||
throw UnsupportedOperationException("Cannot deserialize Parameters") | ||
} | ||
|
||
override fun serialize(encoder: Encoder, value: JsonData) { | ||
if(encoder is JsonEncoder){ | ||
encoder.encodeJsonElement(value.json) | ||
return | ||
} | ||
} | ||
} | ||
public companion object{ | ||
public fun fromString(json: String): JsonData = fromJsonElement(Json.parseToJsonElement(json)) | ||
|
||
public fun fromJsonElement(json: JsonElement): JsonData = JsonData(json) | ||
|
||
public fun builder(block: JsonObjectBuilder.() -> Unit): JsonData{ | ||
val json = buildJsonObject ( | ||
block | ||
) | ||
return fromJsonElement(json) | ||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters