Skip to content
This repository has been archived by the owner on Nov 15, 2024. It is now read-only.

Commit

Permalink
Add alpha json_schema support to OpenAI
Browse files Browse the repository at this point in the history
  • Loading branch information
daviddenton committed Aug 10, 2024
1 parent 51c27f9 commit dffe898
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 14 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
This list is not intended to be all-encompassing - it will document major and breaking API
changes with their rationale when appropriate. Given version `A.B.C.D`, breaking changes are to be expected in version number increments where changes in the `A` or `B` sections:

### v5.20.1.0 (uncut)
### v5.21.0.0 (uncut)
- **http4k-connect-*** - Upgrade dependencies including Kotlin to 2.0.10
- **http4k-connect-ai-openai-**** - [Breaking] Model ResponseFormat as a sealed class hierarchy. Removed ResponseFormatType as now inherent in the JSON marshalling. Alpha support for `json_schema` response format, but it's just a map right now with no class structure.

### v5.20.0.0
- **http4k-connect-*** - Upgrade dependencies, including Kotshi to 3.0.0. Version bump to highlight that this could be a breaking change if you are using the Kotshi annotation processor.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ object OpenAIMoshi : ConfigurableMoshi(
.value(Timestamp)
.value(TokenId)
.value(User)
.value(ResponseFormatType)
.done()
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@ import org.http4k.connect.Http4kConnectAction
import org.http4k.connect.asRemoteFailure
import org.http4k.connect.model.ModelName
import org.http4k.connect.openai.CompletionId
import org.http4k.connect.openai.GPT3_5
import org.http4k.connect.openai.ObjectType
import org.http4k.connect.openai.OpenAIAction
import org.http4k.connect.openai.OpenAIMoshi
import org.http4k.connect.openai.OpenAIMoshi.autoBody
import org.http4k.connect.openai.ResponseFormatType
import org.http4k.connect.openai.Role
import org.http4k.connect.openai.Role.Companion.System
import org.http4k.connect.openai.Role.Companion.User
import org.http4k.connect.openai.Timestamp
import org.http4k.connect.openai.TokenId
import org.http4k.connect.openai.User
Expand All @@ -28,6 +30,8 @@ import org.http4k.lens.Header.CONTENT_TYPE
import se.ansman.kotshi.ExperimentalKotshiApi
import se.ansman.kotshi.JsonProperty
import se.ansman.kotshi.JsonSerializable
import se.ansman.kotshi.Polymorphic
import se.ansman.kotshi.PolymorphicLabel
import java.io.BufferedReader
import java.io.InputStreamReader
import kotlin.text.Charsets.UTF_8
Expand Down Expand Up @@ -103,10 +107,23 @@ data class ChatCompletion(
}
}


@JsonSerializable
data class ResponseFormat(
val type: ResponseFormatType
)
@Polymorphic("type")
sealed class ResponseFormat {
@JsonSerializable
@PolymorphicLabel("json_object")
data object Json : ResponseFormat()

@JsonSerializable
@PolymorphicLabel("url")
data object Url : ResponseFormat()

@JsonSerializable
@PolymorphicLabel("json_schema")
data class JsonSchema(val strict: Boolean, val json_schema: Map<String, Any>) : ResponseFormat()
}


@JsonSerializable
data class Message(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,6 @@ val ModelName.Companion.GPT4_TURBO_PREVIEW get() = ModelName.of("gpt-4-turbo-pre
val ModelName.Companion.GPT3_5 get() = ModelName.of("gpt-3.5-turbo")
val ModelName.Companion.TEXT_EMBEDDING_ADA_002 get() = ModelName.of("text-embedding-ada-002")

class ResponseFormatType private constructor(value: String) : StringValue(value) {
companion object : NonBlankStringValueFactory<ResponseFormatType>(::ResponseFormatType) {
val JsonObject = ResponseFormatType.of("json_object")
val url = ResponseFormatType.of("url")
}
}

class Role private constructor(value: String) : StringValue(value) {
companion object : NonBlankStringValueFactory<Role>(::Role) {
val System = Role.of("system")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class RealOpenAITest : OpenAIContract {

override val openAi = OpenAI.Http(
apiKey(ENV)!!,
JavaHttpClient().debug(debugStream = true),
JavaHttpClient().debug(),
OpenAIOrg.of("org-Ydjc9eGanqJtCP70yPUwZsvs")
)
}

0 comments on commit dffe898

Please sign in to comment.