@@ -6,6 +6,7 @@ import com.cjcrafter.openai.completions.CompletionRequest
66import com.cjcrafter.openai.completions.CompletionResponse
77import com.cjcrafter.openai.completions.CompletionResponseChunk
88import com.cjcrafter.openai.util.OpenAIDslMarker
9+ import com.fasterxml.jackson.annotation.JsonAutoDetect
910import com.fasterxml.jackson.annotation.JsonInclude
1011import com.fasterxml.jackson.databind.DeserializationFeature
1112import com.fasterxml.jackson.databind.ObjectMapper
@@ -14,6 +15,7 @@ import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
1415import okhttp3.OkHttpClient
1516import org.jetbrains.annotations.ApiStatus
1617import org.jetbrains.annotations.Contract
18+ import org.slf4j.LoggerFactory
1719
1820interface OpenAI {
1921
@@ -91,46 +93,49 @@ interface OpenAI {
9193 protected var apiKey: String? = null
9294 protected var organization: String? = null
9395 protected var client: OkHttpClient = OkHttpClient ()
96+ protected var baseUrl: String = " https://api.openai.com"
9497
9598 fun apiKey (apiKey : String ) = apply { this .apiKey = apiKey }
9699 fun organization (organization : String? ) = apply { this .organization = organization }
97100 fun client (client : OkHttpClient ) = apply { this .client = client }
101+ fun baseUrl (baseUrl : String ) = apply { this .baseUrl = baseUrl }
98102
99103 @Contract(pure = true )
100104 open fun build (): OpenAI {
101105 return OpenAIImpl (
102- apiKey ? : throw IllegalStateException (" apiKey must be defined to use OpenAI" ),
103- organization,
104- client
106+ apiKey = apiKey ? : throw IllegalStateException (" apiKey must be defined to use OpenAI" ),
107+ organization = organization,
108+ client = client,
109+ baseUrl = baseUrl,
105110 )
106111 }
107112 }
108113
109114 @OpenAIDslMarker
110115 class AzureBuilder internal constructor(): Builder() {
111- private var azureBaseUrl: String? = null
112116 private var apiVersion: String? = null
113117 private var modelName: String? = null
114118
115- fun azureBaseUrl (azureBaseUrl : String ) = apply { this .azureBaseUrl = azureBaseUrl }
116119 fun apiVersion (apiVersion : String ) = apply { this .apiVersion = apiVersion }
117120 fun modelName (modelName : String ) = apply { this .modelName = modelName }
118121
119122 @Contract(pure = true )
120123 override fun build (): OpenAI {
121124 return AzureOpenAI (
122- apiKey ? : throw IllegalStateException (" apiKey must be defined to use OpenAI" ),
123- organization,
124- client,
125- azureBaseUrl ? : throw IllegalStateException (" azureBaseUrl must be defined for azure" ) ,
126- apiVersion ? : throw IllegalStateException (" apiVersion must be defined for azure" ),
127- modelName ? : throw IllegalStateException (" modelName must be defined for azure" )
125+ apiKey = apiKey ? : throw IllegalStateException (" apiKey must be defined to use OpenAI" ),
126+ organization = organization ,
127+ client = client ,
128+ baseUrl = if (baseUrl == " https://api.openai.com " ) throw IllegalStateException (" baseUrl must be set to an azure endpoint " ) else baseUrl ,
129+ apiVersion = apiVersion ? : throw IllegalStateException (" apiVersion must be defined for azure" ),
130+ modelName = modelName ? : throw IllegalStateException (" modelName must be defined for azure" )
128131 )
129132 }
130133 }
131134
132135 companion object {
133136
137+ internal val logger = LoggerFactory .getLogger(OpenAI ::class .java)
138+
134139 /* *
135140 * Instantiates a builder for a default OpenAI instance. For Azure's
136141 * OpenAI, use [azureBuilder] instead.
@@ -155,6 +160,14 @@ interface OpenAI {
155160 setSerializationInclusion(JsonInclude .Include .NON_NULL )
156161 configure(DeserializationFeature .FAIL_ON_UNKNOWN_PROPERTIES , false )
157162
163+ // By default, Jackson can serialize fields AND getters. We just want fields.
164+ setVisibility(serializationConfig.getDefaultVisibilityChecker()
165+ .withFieldVisibility(JsonAutoDetect .Visibility .ANY )
166+ .withGetterVisibility(JsonAutoDetect .Visibility .NONE )
167+ .withSetterVisibility(JsonAutoDetect .Visibility .NONE )
168+ .withCreatorVisibility(JsonAutoDetect .Visibility .NONE )
169+ )
170+
158171 // Register modules with custom serializers/deserializers
159172 val module = SimpleModule ().apply {
160173 addSerializer(ToolChoice ::class .java, ToolChoice .serializer())
@@ -180,10 +193,4 @@ interface OpenAI {
180193 consumer(chunk)
181194 }
182195 }
183- }
184-
185- @Contract(pure = true )
186- fun openAI (init : OpenAI .Builder .() -> Unit ) = OpenAI .builder().apply (init ).build()
187-
188- @Contract(pure = true )
189- fun azureOpenAI (init : OpenAI .AzureBuilder .() -> Unit ) = OpenAI .azureBuilder().apply (init ).build()
196+ }
0 commit comments