Skip to content

Add function calling example #212

@alirezaalj

Description

@alirezaalj

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions