Skip to content

JavaAIDev/springai-openai-client

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

31 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

SpringAI OpenAI Client using Official Java SDK

build Maven Central Version javadoc

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>

ChatModel

Supported features:

  • Chat completions
  • Function calling

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:

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()

EmbeddingModel

To use this EmbeddingModel,

  1. Create an OpenAIClient,
  2. 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()
    )
)

About

Spring AI ChatModel for OpenAI using official Java SDK, designed for Spring 5

Topics

Resources

License

Stars

Watchers

Forks

Sponsor this project

  •  

Packages

No packages published