@@ -6,6 +6,7 @@ import com.cjcrafter.openai.completions.CompletionRequest
6
6
import com.cjcrafter.openai.completions.CompletionResponse
7
7
import com.cjcrafter.openai.completions.CompletionResponseChunk
8
8
import com.cjcrafter.openai.util.OpenAIDslMarker
9
+ import com.fasterxml.jackson.annotation.JsonAutoDetect
9
10
import com.fasterxml.jackson.annotation.JsonInclude
10
11
import com.fasterxml.jackson.databind.DeserializationFeature
11
12
import com.fasterxml.jackson.databind.ObjectMapper
@@ -14,6 +15,7 @@ import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
14
15
import okhttp3.OkHttpClient
15
16
import org.jetbrains.annotations.ApiStatus
16
17
import org.jetbrains.annotations.Contract
18
+ import org.slf4j.LoggerFactory
17
19
18
20
interface OpenAI {
19
21
@@ -91,46 +93,49 @@ interface OpenAI {
91
93
protected var apiKey: String? = null
92
94
protected var organization: String? = null
93
95
protected var client: OkHttpClient = OkHttpClient ()
96
+ protected var baseUrl: String = " https://api.openai.com"
94
97
95
98
fun apiKey (apiKey : String ) = apply { this .apiKey = apiKey }
96
99
fun organization (organization : String? ) = apply { this .organization = organization }
97
100
fun client (client : OkHttpClient ) = apply { this .client = client }
101
+ fun baseUrl (baseUrl : String ) = apply { this .baseUrl = baseUrl }
98
102
99
103
@Contract(pure = true )
100
104
open fun build (): OpenAI {
101
105
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,
105
110
)
106
111
}
107
112
}
108
113
109
114
@OpenAIDslMarker
110
115
class AzureBuilder internal constructor(): Builder() {
111
- private var azureBaseUrl: String? = null
112
116
private var apiVersion: String? = null
113
117
private var modelName: String? = null
114
118
115
- fun azureBaseUrl (azureBaseUrl : String ) = apply { this .azureBaseUrl = azureBaseUrl }
116
119
fun apiVersion (apiVersion : String ) = apply { this .apiVersion = apiVersion }
117
120
fun modelName (modelName : String ) = apply { this .modelName = modelName }
118
121
119
122
@Contract(pure = true )
120
123
override fun build (): OpenAI {
121
124
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" )
128
131
)
129
132
}
130
133
}
131
134
132
135
companion object {
133
136
137
+ internal val logger = LoggerFactory .getLogger(OpenAI ::class .java)
138
+
134
139
/* *
135
140
* Instantiates a builder for a default OpenAI instance. For Azure's
136
141
* OpenAI, use [azureBuilder] instead.
@@ -155,6 +160,14 @@ interface OpenAI {
155
160
setSerializationInclusion(JsonInclude .Include .NON_NULL )
156
161
configure(DeserializationFeature .FAIL_ON_UNKNOWN_PROPERTIES , false )
157
162
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
+
158
171
// Register modules with custom serializers/deserializers
159
172
val module = SimpleModule ().apply {
160
173
addSerializer(ToolChoice ::class .java, ToolChoice .serializer())
@@ -180,10 +193,4 @@ interface OpenAI {
180
193
consumer(chunk)
181
194
}
182
195
}
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