DevTurtle logo DevTurtle

Spring AI – How to integrate Function Calling with OpenAI

guide/ Spring AI guide

Spring AI, the powerful framework for developing Artificial Intelligence applications, offers an interesting feature called "Function Calling", which allows you to easily integrate custom functions into conversations with AI. In this article, we will explore how Spring AI's "function calling" mechanism works and how you can leverage it to enrich conversations with AI.

What is Function Calling?

Function Calling is a Spring AI feature that allows you to record custom Java functions and allow the AI to intelligently choose to call them during a conversation. This means you can connect the language model’s capabilities with external tools and APIs, enabling deeper and more powerful integration of AI capabilities with other parts of the system.

Function Calling offers several significant benefits:

  • Flexibility: Allows you to easily integrate custom features with AI, allowing for greater flexibility and personalization of conversations.
  • Simplicity: Thanks to the direct integration with Spring AI and the support provided by the framework, the implementation of custom functions is simple and intuitive.
  • Save code: Spring AI takes care of much of the boilerplate code needed to support function invocation, allowing developers to focus on business logic.

Function Calling with OpenAI

Before starting this tutorial we recommend you read the previous article of this guide which explains how to create a Spring AI project with OpenAI.

So let’s start from our basic Spring Boot project and define a new Service that implements a function of our choice. In the case of this example we will create a function that calculates the area of a rectangle given the base and height:

Java
package com.example.aidemo.service;

import java.util.function.Function;

import com.example.aidemo.service.RectangleAreaService.Request;
import com.example.aidemo.service.RectangleAreaService.Response;

public class RectangleAreaService implements Function<Request, Response> {

	// Request for RectangleAreaService
	public record Request(double base, double height) {}
	public record Response(double area) {}

	@Override
	public Response apply(Request r) {
		return new Response(r.base()*r.height());
	}
	
}

After implementing the service logic, it is necessary to define the function which can then be invoked by the AI. In this regard, it is necessary to define a bean as follows:

Java
package com.example.aidemo.config;

import java.util.function.Function;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Description;

import com.example.aidemo.service.RectangleAreaService;

@Configuration
public class Functions {
	
	@Bean
	@Description("Calculate the area of a rectangle from its base and height")
	public Function<RectangleAreaService.Request, RectangleAreaService.Response> rectangeleAreaFunction() {
		return new RectangleAreaService();
	}

}

Compared to the definition of a generic Spring Bean, we have inserted the @Description annotation which provides the model with a description of what the function does.

At this point, we can add a new endpoint to the RestController that responds to prompts using the function we just defined:

Java
@GetMapping("/ai/function")
public Generation functionCalling(@RequestParam(value = "message") String message) {
	Prompt prompt = new Prompt(message,
			OpenAiChatOptions.builder().withFunction("rectangeleAreaFunction").build());
	ChatResponse response = chatModel.call(prompt);
	return response.getResult();
}

You will be able to test the application by invoking the endpoint:

Plaintext
localhost:8080/ai/function?message=Can you calculate the area of a rectangle with base 10 and height 4?

The response will have the following format:

JSON
{
  "metadata": {
    "finishReason": "STOP",
    "contentFilterMetadata": null
  },
  "output": {
    "messageType": "ASSISTANT",
    "properties": {
      "role": "ASSISTANT",
      "finishReason": "STOP",
      "id": "chatcmpl-9GTohEE6TtLVMhYPA6ZMF4oOUD633"
    },
    "content": "The area of a rectangle with a base of 10 and height of 4 is 40.",
    "media": [
      
    ]
  }
}
git icon
Git RepositoryDownload project source

Conclusions

The example must make us reflect on the great potential that this tool has as it allows us to integrate the AI model with parts of customized code. For example, you could invoke an external API or access a database to retrieve useful information.