Spring AI ChatModel and EmbeddingModel implementations for OpenAI using
the official SDK.
The motivation of this ChatModel and EmbeddingModel implementations is to use Spring AI with
Spring 5.
Add Maven dependency of the latest version.
<dependency>
<groupId>com.javaaidev</groupId>
<artifactId>springai-openai-client</artifactId>
<version>${VERSION}</version>
</dependency>Supported features:
- Chat completions
- Function calling
- Streaming mode
To use this ChatModel,
- Create an
OpenAIClient, - Create an
OpenAIChatModel, - Create a Spring AI
ChatClient.Builderwith thisChatModel, - Create a Spring AI
ChatClientfromChatClient.Builder.
See the code below:
val client = OpenAIOkHttpClient.fromEnv()
val chatModel = OpenAIChatModel(client,
DefaultToolCallingManager.builder().toolCallbackResolver(CustomToolCallbackResolver()).build())
val chatOptions = OpenAiChatOptions.builder()
.model("gpt-4o-mini")
.build()
chatClient =
ChatClient.builder(chatModel).defaultOptions(chatOptions).build()
val response = chatClient.prompt().user("tell me a joke")
.call().content()Streaming mode is also supported.
val builder = StringBuilder()
chatClient.prompt().user("tell me a joke")
.stream().chatResponse().doOnNext {
builder.append(it.result.output.text)
}.blockLast()
val result = builder.toString()To use this EmbeddingModel,
- Create an
OpenAIClient, - Create an
OpenAIEmbeddingModel
See the code below:
val client = OpenAIOkHttpClient.fromEnv()
val embeddingModel = OpenAIEmbeddingModel(client)
val response = embeddingModel.call(
EmbeddingRequest(
listOf("hello", "world"),
OpenAIEmbeddingOptions.builder()
.model("text-embedding-3-small")
.build()
)
)