Skip to content

Commit 8d1acf9

Browse files
committed
fn-calling: backfill missing functions and tests.
1 parent 4a67f86 commit 8d1acf9

File tree

4 files changed

+115
-39
lines changed

4 files changed

+115
-39
lines changed

openai-java-core/src/main/kotlin/com/openai/models/responses/ResponseCreateParams.kt

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -908,21 +908,6 @@ private constructor(
908908
/** Alias for calling [addTool] with `Tool.ofFunction(function)`. */
909909
fun addTool(function: FunctionTool) = apply { body.addTool(function) }
910910

911-
/** Alias for calling [addTool] with `Tool.ofFileSearch(fileSearch)`. */
912-
fun addTool(fileSearch: FileSearchTool) = apply { body.addTool(fileSearch) }
913-
914-
/**
915-
* Alias for calling [addTool] with the following:
916-
* ```java
917-
* FileSearchTool.builder()
918-
* .vectorStoreIds(vectorStoreIds)
919-
* .build()
920-
* ```
921-
*/
922-
fun addFileSearchTool(vectorStoreIds: List<String>) = apply {
923-
body.addFileSearchTool(vectorStoreIds)
924-
}
925-
926911
/**
927912
* Adds a single [FunctionTool] where the JSON schema describing the function parameters is
928913
* derived from the fields of a given class. Local validation of that JSON schema can be
@@ -940,6 +925,21 @@ private constructor(
940925
body.addTool(responseFunctionToolFromClass(functionParametersType, localValidation))
941926
}
942927

928+
/** Alias for calling [addTool] with `Tool.ofFileSearch(fileSearch)`. */
929+
fun addTool(fileSearch: FileSearchTool) = apply { body.addTool(fileSearch) }
930+
931+
/**
932+
* Alias for calling [addTool] with the following:
933+
* ```java
934+
* FileSearchTool.builder()
935+
* .vectorStoreIds(vectorStoreIds)
936+
* .build()
937+
* ```
938+
*/
939+
fun addFileSearchTool(vectorStoreIds: List<String>) = apply {
940+
body.addFileSearchTool(vectorStoreIds)
941+
}
942+
943943
/** Alias for calling [addTool] with `Tool.ofWebSearch(webSearch)`. */
944944
fun addTool(webSearch: WebSearchTool) = apply { body.addTool(webSearch) }
945945

openai-java-core/src/main/kotlin/com/openai/models/responses/StructuredResponseCreateParams.kt

Lines changed: 55 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,22 @@ class StructuredResponseCreateParams<T : Any>(
9090
/** @see ResponseCreateParams.Builder.model */
9191
fun model(only: ResponsesModel.ResponsesOnlyModel) = apply { paramsBuilder.model(only) }
9292

93+
/** @see ResponseCreateParams.Builder.background */
94+
fun background(background: Boolean?) = apply { paramsBuilder.background(background) }
95+
96+
/** @see ResponseCreateParams.Builder.background */
97+
fun background(background: Boolean) = apply { paramsBuilder.background(background) }
98+
99+
/** @see ResponseCreateParams.Builder.background */
100+
fun background(background: Optional<Boolean>) = apply {
101+
paramsBuilder.background(background)
102+
}
103+
104+
/** @see ResponseCreateParams.Builder.background */
105+
fun background(background: JsonField<Boolean>) = apply {
106+
paramsBuilder.background(background)
107+
}
108+
93109
/** @see ResponseCreateParams.Builder.include */
94110
fun include(include: List<ResponseIncludable>?) = apply { paramsBuilder.include(include) }
95111

@@ -286,14 +302,6 @@ class StructuredResponseCreateParams<T : Any>(
286302
/** @see ResponseCreateParams.Builder.addTool */
287303
fun addTool(tool: Tool) = apply { paramsBuilder.addTool(tool) }
288304

289-
/** @see ResponseCreateParams.Builder.addTool */
290-
fun addTool(fileSearch: FileSearchTool) = apply { paramsBuilder.addTool(fileSearch) }
291-
292-
/** @see ResponseCreateParams.Builder.addFileSearchTool */
293-
fun addFileSearchTool(vectorStoreIds: List<String>) = apply {
294-
paramsBuilder.addFileSearchTool(vectorStoreIds)
295-
}
296-
297305
/** @see ResponseCreateParams.Builder.addTool */
298306
fun addTool(function: FunctionTool) = apply { paramsBuilder.addTool(function) }
299307

@@ -304,6 +312,14 @@ class StructuredResponseCreateParams<T : Any>(
304312
localValidation: JsonSchemaLocalValidation = JsonSchemaLocalValidation.YES,
305313
) = apply { paramsBuilder.addTool(functionParametersType, localValidation) }
306314

315+
/** @see ResponseCreateParams.Builder.addTool */
316+
fun addTool(fileSearch: FileSearchTool) = apply { paramsBuilder.addTool(fileSearch) }
317+
318+
/** @see ResponseCreateParams.Builder.addFileSearchTool */
319+
fun addFileSearchTool(vectorStoreIds: List<String>) = apply {
320+
paramsBuilder.addFileSearchTool(vectorStoreIds)
321+
}
322+
307323
/** @see ResponseCreateParams.Builder.addTool */
308324
fun addTool(webSearch: WebSearchTool) = apply { paramsBuilder.addTool(webSearch) }
309325

@@ -312,6 +328,37 @@ class StructuredResponseCreateParams<T : Any>(
312328
paramsBuilder.addTool(computerUsePreview)
313329
}
314330

331+
/** @see ResponseCreateParams.Builder.addTool */
332+
fun addTool(mcp: Tool.Mcp) = apply { paramsBuilder.addTool(mcp) }
333+
334+
/** @see ResponseCreateParams.Builder.addTool */
335+
fun addTool(codeInterpreter: Tool.CodeInterpreter) = apply {
336+
paramsBuilder.addTool(codeInterpreter)
337+
}
338+
339+
/** @see ResponseCreateParams.Builder.addCodeInterpreterTool */
340+
fun addCodeInterpreterTool(container: Tool.CodeInterpreter.Container) = apply {
341+
paramsBuilder.addCodeInterpreterTool(container)
342+
}
343+
344+
/** @see ResponseCreateParams.Builder.addCodeInterpreterTool */
345+
fun addCodeInterpreterTool(string: String) = apply {
346+
paramsBuilder.addCodeInterpreterTool(string)
347+
}
348+
349+
/** @see ResponseCreateParams.Builder.addCodeInterpreterTool */
350+
fun addCodeInterpreterTool(
351+
codeInterpreterToolAuto: Tool.CodeInterpreter.Container.CodeInterpreterToolAuto
352+
) = apply { paramsBuilder.addCodeInterpreterTool(codeInterpreterToolAuto) }
353+
354+
/** @see ResponseCreateParams.Builder.addTool */
355+
fun addTool(imageGeneration: Tool.ImageGeneration) = apply {
356+
paramsBuilder.addTool(imageGeneration)
357+
}
358+
359+
/** @see ResponseCreateParams.Builder.addToolLocalShell */
360+
fun addToolLocalShell() = apply { paramsBuilder.addToolLocalShell() }
361+
315362
/** @see ResponseCreateParams.Builder.topP */
316363
fun topP(topP: Double?) = apply { paramsBuilder.topP(topP) }
317364

openai-java-core/src/test/kotlin/com/openai/core/StructuredOutputsTestUtils.kt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -175,12 +175,11 @@ internal fun checkOneDelegationWrite(
175175

176176
private fun invokeMethod(method: Method, target: Any, testCase: DelegationWriteTestCase) {
177177
val numParams = testCase.inputValues.size
178-
val inputValue1 = testCase.inputValues[0]
179-
val inputValue2 = testCase.inputValues.getOrNull(1)
180178

181179
when (numParams) {
182-
1 -> method.invoke(target, inputValue1)
183-
2 -> method.invoke(target, inputValue1, inputValue2)
180+
0 -> method.invoke(target)
181+
1 -> method.invoke(target, testCase.inputValues[0])
182+
2 -> method.invoke(target, testCase.inputValues[0], testCase.inputValues.getOrNull(1))
184183
else -> fail { "Unexpected number of function parameters ($numParams)." }
185184
}
186185
}
@@ -191,11 +190,12 @@ private fun invokeMethod(method: Method, target: Any, testCase: DelegationWriteT
191190
*/
192191
internal fun findDelegationMethod(target: Any, testCase: DelegationWriteTestCase): Method {
193192
val numParams = testCase.inputValues.size
194-
val inputValue1: Any? = testCase.inputValues[0]
193+
val inputValue1: Any? = if (numParams > 0) testCase.inputValues[0] else null
195194
val inputValue2 = if (numParams > 1) testCase.inputValues[1] else null
196195

197196
val method =
198197
when (numParams) {
198+
0 -> findJavaMethod(target.javaClass, testCase.functionName)
199199
1 ->
200200
if (inputValue1 != null) {
201201
findJavaMethod(

openai-java-core/src/test/kotlin/com/openai/models/responses/StructuredResponseCreateParamsTest.kt

Lines changed: 40 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,14 @@ internal class StructuredResponseCreateParamsTest {
7878
.environment(ComputerTool.Environment.LINUX)
7979
.build()
8080
private val TOOL = Tool.ofFunction(FUNCTION_TOOL)
81+
private val MCP_TOOL = Tool.Mcp.builder().serverLabel(STRING).serverUrl(STRING).build()
82+
private val CODE_INTERPRETER_TOOL_AUTO =
83+
Tool.CodeInterpreter.Container.CodeInterpreterToolAuto.builder().build()
84+
private val CODE_INTERPRETER_CONTAINER =
85+
Tool.CodeInterpreter.Container.ofCodeInterpreterToolAuto(CODE_INTERPRETER_TOOL_AUTO)
86+
private val CODE_INTERPRETER_TOOL =
87+
Tool.CodeInterpreter.builder().container(CODE_INTERPRETER_CONTAINER).build()
88+
private val IMAGE_GENERATION_TOOL = Tool.ImageGeneration.builder().build()
8189

8290
private val HEADERS = Headers.builder().build()
8391
private val QUERY_PARAMS = QueryParams.builder().build()
@@ -99,6 +107,10 @@ internal class StructuredResponseCreateParamsTest {
99107
DelegationWriteTestCase("model", STRING),
100108
DelegationWriteTestCase("model", CHAT_MODEL),
101109
DelegationWriteTestCase("model", RESPONSES_ONLY_MODEL),
110+
DelegationWriteTestCase("background", NULLABLE_BOOLEAN),
111+
DelegationWriteTestCase("background", BOOLEAN),
112+
DelegationWriteTestCase("background", OPTIONAL),
113+
DelegationWriteTestCase("background", JSON_FIELD),
102114
DelegationWriteTestCase("include", LIST),
103115
DelegationWriteTestCase("include", OPTIONAL),
104116
DelegationWriteTestCase("include", JSON_FIELD),
@@ -143,12 +155,19 @@ internal class StructuredResponseCreateParamsTest {
143155
DelegationWriteTestCase("tools", LIST),
144156
DelegationWriteTestCase("tools", JSON_FIELD),
145157
DelegationWriteTestCase("addTool", TOOL),
158+
DelegationWriteTestCase("addTool", FUNCTION_TOOL),
159+
DelegationWriteTestCase("addTool", CLASS, VALIDATION),
146160
DelegationWriteTestCase("addTool", FILE_SEARCH_TOOL),
147161
DelegationWriteTestCase("addFileSearchTool", LIST),
148-
DelegationWriteTestCase("addTool", FUNCTION_TOOL),
149162
DelegationWriteTestCase("addTool", WEB_SEARCH_TOOL),
150163
DelegationWriteTestCase("addTool", COMPUTER_TOOL),
151-
DelegationWriteTestCase("addTool", CLASS, VALIDATION),
164+
DelegationWriteTestCase("addTool", MCP_TOOL),
165+
DelegationWriteTestCase("addTool", CODE_INTERPRETER_TOOL),
166+
DelegationWriteTestCase("addCodeInterpreterTool", CODE_INTERPRETER_CONTAINER),
167+
DelegationWriteTestCase("addCodeInterpreterTool", STRING),
168+
DelegationWriteTestCase("addCodeInterpreterTool", CODE_INTERPRETER_TOOL_AUTO),
169+
DelegationWriteTestCase("addTool", IMAGE_GENERATION_TOOL),
170+
DelegationWriteTestCase("addToolLocalShell"),
152171
DelegationWriteTestCase("topP", NULLABLE_DOUBLE),
153172
DelegationWriteTestCase("topP", DOUBLE),
154173
DelegationWriteTestCase("topP", OPTIONAL),
@@ -199,20 +218,30 @@ internal class StructuredResponseCreateParamsTest {
199218

200219
@Test
201220
fun allBuilderDelegateFunctionsExistInDelegator() {
202-
// The delegator class does not implement various functions from the delegate class:
203-
// - text functions and body function
204-
// - addCodeInterpreterTool methods
205-
// - various tool-related methods (addTool variations, addToolLocalShell)
206-
// - background-related methods
207221
checkAllDelegation(
208222
mockBuilderDelegate::class,
209223
builderDelegator::class,
224+
// ************************************************************************************
225+
// NOTE: THIS TEST EXISTS TO ENSURE THAT WHEN NEW FUNCTIONS ARE ADDED MANUALLY OR VIA
226+
// CODE GEN TO `ResponseCreateParams.Builder`, THAT THOSE FUNCTIONS ARE _ALSO_ ADDED
227+
// _MANUALLY_ TO `StructuredResponseCreateParams.Builder`. FAILURE TO ADD THOSE
228+
// FUNCTIONS RESULTS IN _MISSING_ FUNCTIONALITY WHEN USING STRUCTURED OUTPUTS.
229+
// EXCEPTIONS ADDED TO THIS LIST ARE PRESENT BY DESIGN, NOT BECAUSE THE FUNCTIONS ARE
230+
// SIMPLY NOT YET IMPLEMENTED IN THE DELEGATOR CLASS.
231+
//
232+
// DO NOT ADD EXCEPTIONS TO THIS LIST SIMPLY BECAUSE TESTS ARE FAILING. THE TESTS ARE
233+
// SUPPOSED TO FAIL. ADD THE NEW FUNCTIONS TO `StructuredResponseCreateParams.Builder`
234+
// AND ADD A PARAMETERIZED TEST TO `builderDelegationTestCases` (above) TO ENSURE
235+
// CORRECT DELEGATION BEHAVIOR.
236+
// ************************************************************************************
237+
238+
// For Structured Outputs, setting `body` would overwrite the previously set `text`
239+
// property, which would break the Structured Outputs behavior.
210240
"body",
241+
// For Structured Outputs, a new type-safe generic`text` function replaces all existing
242+
// text functions, as they are mutually incompatible. This function has its own
243+
// dedicated unit tests.
211244
"text",
212-
"addCodeInterpreterTool",
213-
"addTool",
214-
"addToolLocalShell",
215-
"background",
216245
)
217246
}
218247

0 commit comments

Comments
 (0)