Skip to main content

Spring AI Client for OpenAI with Official SDK

· 2 min read

I'm working on a project which is still using Spring 5. When I was trying to use Spring AI in this project, it turned out that the default OpenAIChatModel couldn't be used. This is because OpenAIChatModel uses RestClient, which is only available in Spring 6.

While there are many good reasons to upgrade from Spring 5 to Spring 6, there are equally same number of reasons not to upgrade. Sometimes it's not just about the technical issues, but also non-technical difficulties. For the project I'm working on, the major obstacle is the using of javax.servlet package. Spring 6 has migrated to use Jakarta EE, so there is no usage of javax.servlet. This requires code changes in some core components of the project. So upgrading Spring version is not a good choice.

OpenAI has an official Java SDK. So I created a ChatModel implementation using this SDK. This ChatModel has basic chat completion and function calling support. It's good enough for basic integration scenarios.

Source code of this ChatModel implementation can be found on GitHub.

To use this model, add its Maven dependency first.

Maven dependency

<dependency>
<groupId>com.javaaidev</groupId>
<artifactId>springai-openai-client</artifactId>
<version>0.4.1</version>
</dependency>

Then create ChatClient using this ChatModel,

  1. Create an OpenAIClient.
  2. Create an OpenAIChatModel.
  3. Create a Spring AI ChatClient.Builder with this ChatModel.
  4. Create a Spring AI ChatClient from ChatClient.Builder.

See the code below:

Use ChatModel
val client = OpenAIOkHttpClient.fromEnv()
val chatModel = OpenAIChatModel(client)
val chatOptions = OpenAiChatOptions.builder()
.model("gpt-3.5-turbo")
.build()
val chatClient =
ChatClient.builder(chatModel).defaultOptions(chatOptions).build()
val response = chatClient.prompt().user("tell me a joke")
.call().content()

If you are stuck with Spring 5 and want to try Spring AI with OpenAI, use this library.

Update

I added EmbeddingModel implementation using the same OpenAI client.