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.
<dependency>
<groupId>com.javaaidev</groupId>
<artifactId>springai-openai-client</artifactId>
<version>0.6.0</version>
</dependency>
Supported features:
- Chat completions
- Function calling
To use this ChatModel
,
- Create an
OpenAIClient
, - Create an
OpenAIChatModel
, - Create a Spring AI
ChatClient.Builder
with thisChatModel
, - Create a Spring AI
ChatClient
fromChatClient.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()
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()
)
)