Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add function calling example #212

Closed
alirezaalj opened this issue Feb 11, 2025 · 2 comments
Closed

Add function calling example #212

alirezaalj opened this issue Feb 11, 2025 · 2 comments

Comments

@alirezaalj
Copy link

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
@TomerAberbach
Copy link
Collaborator

I'm writing an example here: #213

But note that it'll get easier later once we implement #139

@TomerAberbach TomerAberbach changed the title Exmaple for function calling Add function calling example Feb 12, 2025
@TomerAberbach
Copy link
Collaborator

This is now merged

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants