Skip to content

reactiveclown/openai-webflux-java

Repository files navigation

Maven Central Stars

Reactive OpenAI API client library.

This is the Reactive OpenAI client library.

It was written using WebFlux and Spring Boot. The library also contains pre-configured Spring Boot starter for easier usage.

The library is unofficial and community-maintained. Feel free to join the development.

Table of Contents

Supported APIs

Installation

Dependency import via Maven:

<dependency>
    <groupId>io.github.reactiveclown</groupId>
    <artifactId>openai-webflux-client-spring-boot-starter</artifactId>
    <version>0.9.1</version>
</dependency>

Dependency import via Gradle:

implementation 'io.github.reactiveclown:openai-webflux-client-spring-boot-starter:0.9.1'

After adding the dependency remember to add yor OpenAI api key to the application.properties.

com.github.reactiveclown.openai.apiKey=OPENAI_API_KEY

You can also add the Organization id and change baseUrl by providing next configuration variables. But this step is not mandatory one.

com.github.reactiveclown.openai.organizationId=OPENAI_ORGANIZATION_ID
com.github.reactiveclown.openai.baseUrl=OPENAI_CUSTOM_BASE_URL

Usage

Here you can find some usage examples. Also, kindly use the official documentation in order to get more information about parameters.


Models service

OpenAI Models Docs

Models service is used to retrieve the list of models or information about specific model.

@Service
public class ExampleService {

    private final ModelsService service;

    public ExampleService(ModelsService service) {
        this.service = service;
    }

    public Mono<ListModelsResponse> listModels() {
        return service.listModels();
    }

    public Mono<RetrieveModelResponse> retrieveModel(String modelName) {
        return service.retrieveModel(modelName);
    }
}

Completions service

OpenAI Completions Docs

Completions service is used to complete the text. Fell free to play around with builder parameters.

@Service
public class ExampleService {

    private final CompletionsService service;

    public ExampleService(CompletionsService service) {
        this.service = service;
    }

    public Mono<CreateCompletionResponse> createCompletion() {
        return service.createCompletion(
                CreateCompletionRequest
                        .builder("babbage")
                        .n(2)
                        .bestOf(1)
                        .build());
    }
}

Chat service

OpenAI Chat Docs

Chat service is used to chat with the chat models. Fell free to play around with builder parameters.

@Service
public class ExampleService {

    private final ChatService service;

    public ExampleService(ChatService service) {
        this.service = service;
    }

    public Mono<CreateChatCompletionResponse> createChatCompletion() {
        return service.createChatCompletion(
                CreateChatCompletionRequest
                        .builder("gpt-3.5-turbo", List.of(new MessageData("user","do something")))
                        .n(3)
                        .build());
    }
}

Edits service

OpenAI Edits Docs

Edits service is used to make an edits to the provided input. Fell free to play around with builder parameters.

@Service
public class ExampleService {

    private final EditsService service;

    public ExampleService(EditsService service) {
        this.service = service;
    }

    public Mono<CreateEditResponse> createEdit() {
        return service.createEdit(
                CreateEditRequest
                        .builder("babbage", "Add one digit after every word")
                        .input("One Two Three")
                        .build());
    }
}

Images service

OpenAI Images Docs

Image service is used to generate a different images and their transformations.

⚠️ As for now, methods that are requiring images are blocking. In the nearest future it is planned to add async FilePart implementation.

@Service
public class ExampleService {

    private final ImagesService service;

    public ExampleService(ImagesService service) {
        this.service = service;
    }

    //Non-blocking
    public Mono<CreateImageResponse> createImage() {
        return service.createImage(
                CreateImageRequest
                        .builder("Generate a digital art of Ukraine")
                        .build());
    }

    //Blocking
    public Mono<CreateImageVariationResponse> createImageVariation() {
        return service.createImageVariation(
                CreateImageVariationRequest
                        .builder("src/main/resources/exampleImage.png")
                        .size("512x512")
                        .build());
    }

    //Blocking
    public Mono<CreateImageEditResponse> createImageEdit() {
        return service.createImageEdit(
                CreateImageEditRequest
                        .builder("src/main/resources/exampleImage.png", "Generate a green fields")
                        .mask("src/main/resources/exampleMask.png")
                        .build());
    }
}

Embeddings service

OpenAI Embeddings Docs

Embedding service is used to create embeddings. Fell free to play around with builder parameters.

@Service
public class ExampleService {

    private final EmbeddingsService service;

    public ExampleService(EmbeddingsService service) {
        this.service = service;
    }

    public Mono<CreateEmbeddingsResponse> createEmbeddings(){
        return service.createEmbeddings(
                CreateEmbeddingsRequest
                        .builder("babbage", "example input")
                        .build());
    }
}

Audio service

OpenAI Audio Docs

Audio service is used to turn audio into text, also to make a translations into English. Fell free to play around with builder parameters.

⚠️ As for now, methods that are requiring audio files are blocking. In the nearest future it is planned to add async FilePart implementation.

@Service
public class ExampleService {

    private final AudioService service;

    public ExampleService(AudioService service) {
        this.service = service;
    }

    //Blocking
    public Mono<CreateTranscriptionResponse> createTranscription() {
        return service.createTranscription(
                CreateTranscriptionRequest
                        .builder("src/main/resources/exampleAudio.mp3", "whisper-1")
                        .build());
    }

    //Blocking
    public Mono<CreateTranslationResponse> createTranslation(){
        return service.createTranslation(CreateTranslationRequest
                .builder("src/main/resources/exampleAudio.mp3", "whisper-1")
                .build());
    }
}

Files service

OpenAI Files Docs

File service is used to upload and work with files. Fell free to play around with builder parameters.

⚠️ As for now, methods that are requiring files are blocking. In the nearest future it is planned to add async FilePart implementation.

@Service
public class ExampleService {

    private final FilesService service;

    public ExampleService(FilesService service) {
        this.service = service;
    }

    public Mono<ListFilesResponse> listFilesResponse() {
        return service.listFiles();
    }
    
    //Blocking
    public Mono<UploadFileResponse> uploadFile() {
        return service.uploadFile(
                UploadFileRequest
                        .builder("src/main/resources/exampleFile.jsonl", "finetune")
                        .build());
    }

    public Mono<DeleteFileResponse> deleteFile() {
        return service.deleteFile("fileId");
    }

    public Mono<RetrieveFileResponse> retrieveFile() {
        return service.retrieveFile("fileId");
    }

    public Mono<String> retrieveFileContent() {
        return service.retrieveFileContent("fileId");
    }
}

Fine-tunes service

OpenAI Fine-tunes Docs

Fine-tunes service is used to work with fine-tune files and models. Fell free to play around with builder parameters.

@Service
public class ExampleService {

    private final FineTunesService service;

    public ExampleService(FineTunesService service) {
        this.service = service;
    }

    public Mono<CreateFineTuneResponse> createFineTune(){
        return service.createFineTune(
                CreateFineTuneRequest
                        .builder("trainingFileId")
                        .build());
    }

    public Mono<ListFineTunesResponse> listFineTunes(){
        return service.listFineTunes();
    }

    public Mono<RetrieveFineTuneResponse> retrieveFineTune(){
        return service.retrieveFineTunes("fineTuneId");
    }

    public Mono<CancelFineTuneResponse> cancelFineTune(){
        return service.cancelFineTune("fineTuneId");
    }

    public Mono<ListFineTuneEventsResponse> listFineTuneEvents(){
        return service.listFineTuneEvents("fineTuneId");
    }

    public Mono<DeleteFineTuneModelResponse> deleteFineTuneModel(){
        return service.deleteFineTuneModel("modelId");
    }
}

Moderations service

OpenAI Moderations Docs

Moderations service is used to check for moderation violations. Fell free to play around with builder parameters.

@Service
public class ExampleService {

    private final ModerationsService service;

    public ExampleService(ModerationsService service) {
        this.service = service;
    }

    public Mono<CreateModerationResponse> createModeration() {
        return service.createModeration(
                CreateModerationRequest.builder("violating input").build());
    }
}

How can I help?

This is the open-source library, any help will be much appreciated. If you can see that there is a way to improve the code or functionality, kindly fill out the issue or make a pull request with your changes. Also feel free to look at opened issues and submit a pull request if you have your solution.

License

MIT License

Copyright (c) 2023 Maksym Volkov

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.