Spring with ChatGPT

Last week I published another video on my YouTube channel. The title is Spring & AI without Spring AI: Chat with GPT. It’s part of my series on integrating the OpenAI services into Java systems without using the experimental Spring AI framework. Nothing against that framework, of course — it’s just very early in the alpha phase for it, and there’s still plenty you can do without it.

For example, Spring 3+ added in the HTTP exchange interfaces. With those interfaces, you can add methods annotated with @GetExchange, @PostExchange, etc, to abstract methods, and Spring will implement them for you. All you need to do is provide a bean that configures a WebClient and arranges for a proxy, and you’re good to go.

In this particular case, my interface looks like:

@HttpExchange("/v1")
public interface OpenAIInterface {

    @PostExchange("/chat/completions")
    ChatResponse getChatResponse(
                  @RequestBody ChatRequest chatRequest);
}

The WebClient bean is configured using an HttpServiceProxyFactory, which is mostly boilerplate that is included my @Configuration class called AppConfig.

For the details, see this GitHub repository.

Here’s the actual video:

YouTube video about using Spring to chat with GPT

I hope you find it entertaining. I’m publishing this blog post about it partly to show the new video, and partly because I just enabled my blog to work with the Fediverse. That means if you’re on Mastodon, you can look up my blog’s account there, @kousenit, subscribe to it, and you’ll get every post.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Discover more from Stuff I've learned recently...

Subscribe now to keep reading and get access to the full archive.

Continue reading