https://github.com/javaaidev/springai-openai-client
Spring AI ChatModel for OpenAI using official Java SDK
https://github.com/javaaidev/springai-openai-client
java openai openai-api spring-ai spring-ai-openai
Last synced: 3 months ago
JSON representation
Spring AI ChatModel for OpenAI using official Java SDK
- Host: GitHub
- URL: https://github.com/javaaidev/springai-openai-client
- Owner: JavaAIDev
- License: apache-2.0
- Created: 2025-02-13T17:55:49.000Z (12 months ago)
- Default Branch: main
- Last Pushed: 2025-02-18T22:36:06.000Z (11 months ago)
- Last Synced: 2025-02-18T23:28:34.743Z (11 months ago)
- Topics: java, openai, openai-api, spring-ai, spring-ai-openai
- Language: Java
- Homepage:
- Size: 28.3 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
README
# SpringAI OpenAI Client using Official Java SDK
[](https://github.com/JavaAIDev/springai-openai-client/actions/workflows/build.yaml)

[](https://javadoc.io/doc/com.javaaidev/springai-openai-client)
Spring AI `ChatModel` and `EmbeddingModel` implementations for OpenAI using
the [official SDK](https://github.com/openai/openai-java).
The motivation of this `ChatModel` and `EmbeddingModel` implementations is to use Spring AI with
Spring 5.
Add Maven dependency of the latest version.
```xml
com.javaaidev
springai-openai-client
${VERSION}
```
## ChatModel
Supported features:
- Chat completions
- Function calling
- Streaming mode
### Use ChatModel
To use 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:
```kotlin
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
Streaming mode is also supported.
```kotlin
val builder = StringBuilder()
chatClient.prompt().user("tell me a joke")
.stream().chatResponse().doOnNext {
builder.append(it.result.output.text)
}.blockLast()
val result = builder.toString()
```
## EmbeddingModel
To use this `EmbeddingModel`,
1. Create an `OpenAIClient`,
2. Create an `OpenAIEmbeddingModel`
See the code below:
```kotlin
val client = OpenAIOkHttpClient.fromEnv()
val embeddingModel = OpenAIEmbeddingModel(client)
val response = embeddingModel.call(
EmbeddingRequest(
listOf("hello", "world"),
OpenAIEmbeddingOptions.builder()
.model("text-embedding-3-small")
.build()
)
)
```