Skip to content
January 4, 2025

Fluent APIs and Spring AI’s Chat Client API

· 3 min read · Algorithm & Design, GenAI, Spring AI
  • A fluent API is a style of designing software APIs where method calls are chained together in a way that resembles natural language.
  • Key Characteristics of Fluent API:
    • Method Chaining: This is the core principle. Each method in a fluent API typically returns an object of the same type (or a closely related type), allowing you to chain multiple method calls together in a single line of code.
    • Readability: Fluent APIs aim to improve code readability by making it resemble natural language. The chain of method calls often reads like a sentence, describing the desired outcome.
    • Domain-Specific Language (DSL): Fluent APIs can create a domain-specific language that is tailored to a particular problem or domain. This can make the code more concise and easier to understand for developers familiar with that domain
  • Example of a Fluent API in Java, demonstrating the Builder Pattern to create a Book object:

class Book {
    private String author;
    private String title;
    private String genre    ;

    // Private constructor to enforce the use of the Builder
    private Book(BookBuilder builder) {
        this.author = builder.author;
        this.title = builder.title;
        this.genre = builder.genre;
    }

    @Override
    public String toString() {
        return "Book [author=" + author + ", title=" + title + ", genre=" + genre + "]";
    }

    // Static nested Builder class
    public static class BookBuilder {
        private String author;
        private String title;
        private String genre;

        public BookBuilder setAuthor(String author) {
            this.author = author;
            return this; // Returning the builder object for chaining
        }

        public BookBuilder setTitle(String title) {
            this.title = title;
            return this;
        }

        public BookBuilder setGenre(String genre) {
            this.genre = genre;
            return this;
        }

        public Book build() {
            return new Book(this);
        }
    }
}

public class Main {
    public static void main(String[] args) {
        Book book = new Book.BookBuilder()
                        .setAuthor("ABC")
                        .setTitle("XYZ Book")
                        .setGenre("Comedy")
                        .build();

        System.out.println(book);
    }
}
  • Key Points:
    • Fluent Methods: setAuthor, setTitle, and setGenre return the builder itself, enabling chaining.
    • Readable Construction: The chained calls make it clear what properties are being set.
    • Builder Pattern: A common pattern in fluent APIs to construct complex objects step by step.
  • The Spring AI ChatClient is also called a fluent API because it leverages the concept of method chaining to construct and execute prompts for interacting with LLMs.  
  • Here’s why it fits the definition of a fluent API:
    • Method Chaining:
      • You start with chatClient.prompt(). Then, you chain methods like .user("..."), .system("..."), .stream(), and .content() to build the prompt and specify how the response should be handled. Each method returns an object (or a closely related object) of the same type, allowing you to continue chaining methods.
    • Readability:
      • This chaining creates a readable flow, resembling a sentence that describes the interaction with the AI model.
  • Example
chatClient.prompt() 
    .user("Tell me a joke") 
    .stream() 
    .content();
  • This reads almost like a natural language instruction: “Tell me a joke, stream the response, and give me the content
  • Domain-Specific Language (DSL):
    • While not a full-fledged DSL, the fluent API creates a domain-specific vocabulary for interacting with AI models within the Spring ecosystem.
  • In essence: The Spring AI ChatClient utilizes method chaining to provide a concise and readable way to define and execute prompts for AI models, making it a prime example of a fluent API.  

Spring AI Chat Client API

Thanks!

Tags: , , ,

Leave a Reply