-
Notifications
You must be signed in to change notification settings - Fork 163
Closed
Description
Please provide examples for function calling as well
or provide a reference if there is documentation
of course documentation is not enough to use and reference for java code
iv tryed some examples but there is errors
fun aiFuncCalling() {
// Define function tool
val weatherFunction = ChatCompletionTool.builder()
.type(JsonValue.from("function"))
.function(
FunctionDefinition.builder()
.name("getWeather")
.description("Get current temperature for provided coordinates in celsius.")
.parameters(
FunctionParameters.builder()
.putAdditionalProperty("type",JsonValue.from("object"))
.putAdditionalProperty("latitude", JsonValue.from("number"))
.putAdditionalProperty("longitude", JsonValue.from("number"))
.build()
)
.strict(true)
.build()
)
.build()
// Create request
val createParams = ChatCompletionCreateParams.builder()
.model(ChatModel.GPT_4O)
.maxCompletionTokens(2048)
.addUserMessage("What's the weather like in Paris today?")
.addTool(weatherFunction)
.toolChoice(ChatCompletionToolChoiceOption.Auto.AUTO) // Allow AI to call the function
.build()
// Send request
val response = openAIClient.chat().completions().create(createParams)
// Handle response
response.choices().forEach { choice ->
val message = choice.message()
println("Assistant: ${message.content()}")
message.toolCalls().stream().forEach{ toolCall ->
if (toolCall[0].function().name() == "getWeather") {
val params = toolCall[0].function().arguments()
println("Function Call Detected: getWeather with params $params")
}
}
}
}
Exception in thread "main" com.openai.errors.BadRequestException: 400: OpenAIError{additionalProperties={error={message=Invalid schema for function 'getWeather': In context=(), 'additionalProperties' is required to be supplied and to be false., type=invalid_request_error, param=tools[0].function.parameters, code=invalid_function_parameters}}}
at com.openai.core.handlers.ErrorHandler$withErrorHandler$1.handle(ErrorHandler.kt:45)
at com.openai.services.blocking.chat.CompletionServiceImpl.create(CompletionServiceImpl.kt:59)
at com.openai.services.blocking.chat.CompletionService.create$default(CompletionService.kt:28)
at ca.dealertuoch.demos.spring.demoaikt.service.OpenAiCompletionService.aiFuncCalling(OpenAiCompletionService.kt:68)
at ca.dealertuoch.demos.spring.demoaikt.CompletionsExampleKt.main(CompletionsExample.kt:19)
Process finished with exit code 1
pauliustumas
Metadata
Metadata
Assignees
Labels
No labels