The 2.5k ChatGPT-Java version SDK upgrade 1.1.2-beta0 supports GPT-4V, Dall-e-3 model, ToolCalls, fine-tuning Job, TTS…

1. Project Introduction

Chatgpt-Java is the Java SDK of OpenAI’s official API, which can be quickly accessed for use in projects. Supports all official OpenAI interfaces.
The current harvest will be 2500 + star.

  • Open source address: https://github.com/Grt1228/chatgpt-java
  • Official documentation: https://chatgpt-java.unfbx.com/
  • Latest version: 1.1.2-beta0
<dependency>
    <groupId>com.unfbx</groupId>
    <artifactId>chatgpt-java</artifactId>
    <version>1.1.2-beta0</version>
</dependency>

Currently supported features:

Dall-e-3 FineTuneJob TTS ChatCompletionWithPicture
AI generated pictures Fine-tuned job Text to speech ChatCompletion of attached pictures
TikToken Chat Completions Images Speech To Text Balance query
Token calculation GPT-3.5, 4.0 dialogue model GPT-3.0 dialogue Picture model Speech to text, voice translation Balance query
Embeddings Files Moderations Fine-tune Models
Embedding Customized training model Text review, sensitive word identification Fine-tuning Model retrieval related

OpenAi updated a new version last week and released many new features, including GPT-4V, ChatCompletion for attaching images, specifying return data format, Tool Call, Dall-e-3 generated images, FineTuneJob, text-to-speech TTS, etc. and other functions.

Chatgpt-Java is updated simultaneously this week to support the latest API.

2. New version update

Therefore, the new version of the API update is based on the original OpenAiClient and OpenAiStreamClient, so there is no change when building the client.

Create Client as follows:

@Slf4j
public class OpenAiClientTest {<!-- -->

    private OpenAiClient client;
    private OpenAiStreamClient streamClient;
    
    @Before
    public void before() {<!-- -->
        HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor(new OpenAILogger());
        //! ! ! ! Never turn on BODY level logs in production or test environments! ! ! !
        //! ! ! It is recommended that the production or test environment be set to these three levels: NONE, BASIC, HEADERS,! ! !
        httpLoggingInterceptor.setLevel(HttpLoggingInterceptor.Level.HEADERS);
        OkHttpClient okHttpClient = new OkHttpClient
                .Builder()
                .addInterceptor(httpLoggingInterceptor)
                .addInterceptor(new OpenAiResponseInterceptor())
                .connectTimeout(10, TimeUnit.SECONDS)
                .writeTimeout(30, TimeUnit.SECONDS)
                .readTimeout(30, TimeUnit.SECONDS)
                .build();
        client = OpenAiClient.builder()
                .apiKey(Arrays.asList("************************"))
                .okHttpClient(okHttpClient)
                //If you have become a proxy yourself, just pass the proxy address. If you don’t have it, don’t pass it in. (Follow the official account reply: openai to get a free test proxy address)
                .apiHost("https://*******/")
                .build();

        streamClient = OpenAiStreamClient.builder()
                //Supports multiple keys to be passed in, randomly selected when requesting
                .apiKey(Arrays.asList("************************"))
                .okHttpClient(okHttpClient)
                //If you have become a proxy yourself, just pass the proxy address. If you don’t have it, don’t pass it in. (Follow the official account reply: openai to get a free test proxy address)
                .apiHost("https://*******/")
                .build();
    }
}

2.1. ChatCompletion example of attaching images

2.1.1, blocking request

/**
 * Chat model supports image streaming example
 */
@Test
public void pictureChat() {<!-- -->
    Content textContent = Content.builder().text("What’s in this image?").type(Content.Type.TEXT.getName()).build();
    ImageUrl imageUrl = ImageUrl.builder().url("https://upload.wikimedia.org/wikipedia/commons/thumb/d/dd/Gfp-wisconsin-madison-the-nature-boardwalk.jpg/2560px-Gfp -wisconsin-madison-the-nature-boardwalk.jpg").build();
    Content imageContent = Content.builder().imageUrl(imageUrl).type(Content.Type.IMAGE_URL.getName()).build();
    List<Content> contentList = new ArrayList<>();
    contentList.add(textContent);
    contentList.add(imageContent);
    MessagePicture message = MessagePicture.builder().role(Message.Role.USER).content(contentList).build();
    //#####Request parameters use the ChatCompletionWithPicture class
    ChatCompletionWithPicture chatCompletion = ChatCompletionWithPicture
            .builder()
            .messages(Collections.singletonList(message))
            .model(ChatCompletion.Model.GPT_4_VISION_PREVIEW.getName())
            .build();
    ChatCompletionResponse chatCompletionResponse = client.chatCompletion(chatCompletion);
    chatCompletionResponse.getChoices().forEach(e -> System.out.println(e.getMessage()));
}

2.1.2, Streaming request

/**
 * Chat model supports image streaming example
 */
@Test
public void pictureChatV2() {<!-- -->
    Content textContent = Content.builder().text("What’s in this image?").type(Content.Type.TEXT.getName()).build();
    ImageUrl imageUrl = ImageUrl.builder().url("https://upload.wikimedia.org/wikipedia/commons/thumb/d/dd/Gfp-wisconsin-madison-the-nature-boardwalk.jpg/2560px-Gfp -wisconsin-madison-the-nature-boardwalk.jpg").build();
    Content imageContent = Content.builder().imageUrl(imageUrl).type(Content.Type.IMAGE_URL.getName()).build();
    List<Content> contentList = new ArrayList<>();
    contentList.add(textContent);
    contentList.add(imageContent);
    MessagePicture message = MessagePicture.builder().role(Message.Role.USER).content(contentList).build();
    ChatCompletionWithPicture chatCompletion = ChatCompletionWithPicture
            .builder()
            .messages(Collections.singletonList(message))
            .model(ChatCompletion.Model.GPT_4_VISION_PREVIEW.getName())
            .build();
    ChatCompletionResponse chatCompletionResponse = client.chatCompletion(chatCompletion);
    chatCompletionResponse.getChoices().forEach(e -> System.out.println(e.getMessage()));
}

2.1.3. Precautions

The chatCompletion of attached images does not currently support the following simple interface requests.

  • public void streamChatCompletion(List messages, EventSourceListener el)
  • public ChatCompletionResponse chatCompletion(List messages)

2.2, ChatGPT specifies return Json format

The latest version of the OpenAi interface supports execution data return format. The following only gives examples of blocking output solutions. The same method of using streaming output will not be given.

The specified data format currently supports:

  • json_object
  • text

Reference source code: com/unfbx/chatgpt/entity/chat/ResponseFormat.java

2.2.1, Request

/**
 * Customized return data format
 */
@Test
public void diyReturnDataModelChat() {<!-- -->
    Message message = Message.builder().role(Message.Role.USER).content("Randomly output 10 words, use json output").build();
    ChatCompletion chatCompletion = ChatCompletion
        .builder()
        .messages(Collections.singletonList(message))
        .responseFormat(ResponseFormat.builder().type(ResponseFormat.Type.JSON_OBJECT.getName()).build())
        .model(ChatCompletion.Model.GPT_4_1106_PREVIEW.getName())
        .build();
    ChatCompletionResponse chatCompletionResponse = client.chatCompletion(chatCompletion);
    chatCompletionResponse.getChoices().forEach(e -> System.out.println(e.getMessage()));
}

{<!-- -->
  "words": [
    "aberration",
    "nostalgia",
    "quintessential",
    "harmony",
    "serendipity",
    "benevolent",
    "ephemeral",
    "labyrinth",
    "zenith",
    "vivacious"
  ]
}

2.3, Dall-e-3 generates pictures (AI painting)

The latest version of the OpenAi interface supports the Dall-e-3 model to generate images, and the function is more powerful.

2.3.1, Request

/**
 * Examples of using the new version of the image generation model
 */
@Test
public void generateImageByDall_e_3() {<!-- -->
    Image image = Image.builder()
        .responseFormat(com.unfbx.chatgpt.entity.images.ResponseFormat.URL.getName())
        .model(Image.Model.DALL_E_3.getName())
        .prompt("A coffee cup with the four letters Unfbx printed on it.")
        .n(1)
        .quality(Image.Quality.HD.getName())
        .size(SizeEnum.size_1024_1792.getName())
        .style(Image.Style.NATURAL.getName())
        .build();
    ImageResponse imageResponse = client.genImages(image);
// ImageResponse imageResponse = client.genImages("A coffee cup with the four letters Unfbx printed on it.");
    System.out.println(imageResponse.getData().get(0).getUrl());
    }

2.4, FineTuneJob fine-tuning job

OpenAi supports custom training of exclusive models and fine-tuning of models through FineTuneJob.
The models supported by FineTuneJob are:

  • gpt-3.5-turbo-1106
  • gpt-3.5-turbo-0613
  • babbage-002
  • gpt-4-0613
  • davinci-002

For more detailed reference source code: com/unfbx/chatgpt/entity/fineTune/job/FineTuneJob.java

2.4.1. Create training data files

The created file format is json1The following is a sample file: fine_tune_test_file.json1
The file is in the github source code root directory and the file content;

{"messages": [{"role": "system", "content": "OnBot is a chatbot."}, {"role": \ "user", "content": "Is chagpt-java easy to use?"}, {"role": "assistant", "content": "It's okay." "}]}
{"messages": [{"role": "system", "content": "OnBot is a chatbot."}, {"role": "user\ ", "content": "What is the latest version of chagpt-java?"}, {"role": "assistant", "content": "Version 1.1.2"} ]}
{"messages": [{"role": "system", "content": "OnBot is a chatbot."}, {"role": "user\ ", "content": "Does chagpt-java support streaming output?"}, {"role": "assistant", "content": "The latest version supports streaming output "}]}
{"messages": [{"role": "system", "content": "OnBot is a chatbot."}, {"role": "user\ ", "content": "Who is the author of chagpt-java?"}, {"role": "assistant", "content": "The author is Unfbx, personal website: https://www.unfbx.com"}]}
{"messages": [{"role": "system", "content": "OnBot is a chatbot."}, {"role": "user\ ", "content": "Unfbx is the author of chagpt-java, what is the WeChat official account?"}, {"role": "assistant", "content": "program member's black hole."}]}
{"messages": [{"role": "system", "content": "OnBot is a chatbot."}, {"role": "user\ ", "content": "Unfbx is the author of chagpt-java, what is his personal blog address?"}, {"role": "assistant", "content": "https ://www.unfbx.com"}]}
{"messages": [{"role": "system", "content": "OnBot is a chatbot."}, {"role": "user\ ", "content": "Unfbx is the author of chagpt-java, what is the official address of chagpt-java?"}, {"role": "assistant", "content": \ "https://chatgpt-java.unfbx.com"}]}
{"messages": [{"role": "system", "content": "OnBot is a chatbot."}, {"role": "user\ ", "content": "Unfbx is the author of chagpt-java, what is the github address of chagpt-java?"}, {"role": "assistant", "content": "https://github.com/Grt1228/chatgpt-java"}]}
{"messages": [{"role": "system", "content": "OnBot is a chatbot."}, {"role": "user\ ", "content": "Unfbx is the author of chagpt-java, a brief introduction to chagpt-java."}, {"role": "assistant", "content": " The Java SDK of OpenAI official Api can be quickly accessed for project use. Currently, all official OpenAI interfaces are supported, and ** also supports Tokens calculation. Reference document: [Tokens_README.md](https://github.com/Grt1228/chatgpt-java /blob/main/Tokens_README.md)**"}]}
{"messages": [{"role": "system", "content": "OnBot is a chatbot."}, {"role": "user\ ", "content": "What is OpenAi's official Java Sdk?"}, {"role": "assistant", "content": "chagpt-java, github address: https://github.com/Grt1228/chatgpt-java"}]}

2.4.2. Upload files

/**
 * fineTuneJob usage example
 */
@Test
public void uploadFile() {<!-- -->
    UploadFileResponse uploadFileResponse = client.uploadFile(new java.io.File("fine_tune_test_file.json1"));
    //Return the id of the file: file-6KaBdtVlaassk9Y2P5ZjTqIC
    System.out.println(uploadFileResponse.getId());
}

2.4.3. Create fine-tuning job

@Test
public void fineTuneJob() {<!-- -->
    //file-KaNQn5V9YHlLqVQzo8CUMdIr is the file id returned by file upload
    FineTuneJobResponse fineTuneJobResponse = client.fineTuneJob("file-KaNQn5V9YHlLqVQzo8CUMdIr");
    //Return job id = ftjob-5WQr0bZ7grvjnY3Or2sqiixl
    System.out.println(fineTuneJobResponse.toString());
}

2.4.4, View fine-tuning job

2.4.4.1, detailed information
  • The fineTunedModel attribute of FineTuneJobResponse is the fine-tuned model id, which is also the model id used subsequently.

The fineTunedModel attribute will only have a value if the FineTuneJob is executed. If the job fails or is not executed, this attribute will be null.

Combined with Chapter 4.0, you can see the job execution information.

@Test
public void retrieveFineTuneJob() {<!-- -->
    //Pass in job id
    FineTuneJobResponse fineTuneJobResponse = client.retrieveFineTuneJob("ftjob-5WQr0bZ7grvjnY3Or2sqiixl");
    System.out.println(fineTuneJobResponse);
}
2.4.4.2, job list

Support paging query

@Test
public void retrieveFineTuneJob() {<!-- -->

// FineTuneJobListResponse<FineTuneJobResponse> jobListResponse = client.fineTuneJobs("ftjob-cG7zIraBhAkq5Ybs7311lH7t", 5);
    FineTuneJobListResponse<FineTuneJobResponse> jobListResponse = client.fineTuneJobs(null, 20);
    System.out.println(jobListResponse);
}

2.4.5. Fine-tuning job execution progress query

Support paging query, support paging

@Test
public void fineTuneJobEvents() {<!-- -->
    FineTuneJobListResponse<FineTuneJobEvent> listResponse = client.fineTuneJobEvents("ftjob-5WQr0bZ7grvjnY3Or2sqiixl", null, 20);
// FineTuneJobListResponse<FineTuneJobEvent> listResponse = client.fineTuneJobEvents("ftjob-5WQr0bZ7grvjnY3Or2sqiixl", "ftevent-WwB8lpWxhjgUJX9DYdb47zJe", 20);
    listResponse.getData().forEach(e -> System.out.println(e.getMessage()));
}

Output information. The output information will return the created model ID, which is the model ID used subsequently.

 The job has successfully completed
    New fine-tuned model created: ft:gpt-3.5-turbo-1106:personal::8K5KwJTU
    Step 91/100: training loss=0.45
    Step 81/100: training loss=0.00
    Step 71/100: training loss=0.00
    Step 61/100: training loss=0.94
    Step 51/100: training loss=0.19
    Step 41/100: training loss=0.06
    Step 31/100: training loss=0.95
    Step 21/100: training loss=1.99
    Step 11/100: training loss=2.50
    Step 1/100: training loss=5.42
    Fine-tuning job started
    Files validated, moving job to queued state
    Validating training file: file-KaNQn5V9YHlLqVQzo8CUMdIr
    Created fine-tuning job: ftjob-5WQr0bZ7grvjnY3Or2sqiixl

2.4.6, Fine-tuning model use

Note that the model parameter is a custom model id. This id will be returned after fineTuneJobEvents is completed.

There are several ways to obtain this ID:

  • 1. The fineTuneJobEvents interface returns after completion.
  • 2. Query job details through Chapter 3.1 to obtain the model id: fineTunedModel attribute.
  • 3. Models interface returns, refer to Chapter 5
@Test
public void fineTuneJobModelChat() {<!-- -->
    Message message1 = Message.builder().role(Message.Role.SYSTEM).content("OnBot is a chatbot.").build();
    Message message2 = Message.builder().role(Message.Role.USER).content("OnBot, please ask: Who is the author of Chatgpt-java?").build();
    List<Message> messages = new ArrayList<>(2);
    messages.add(message1);
    messages.add(message2);
    ChatCompletion chatCompletion = ChatCompletion
            .builder()
            .messages(messages)
            .model("ft:gpt-3.5-turbo-1106:personal::8K5KwJTU")
            .build();
    ChatCompletionResponse chatCompletionResponse = client.chatCompletion(chatCompletion);
    chatCompletionResponse.getChoices().forEach(e -> {<!-- -->
        System.out.println(e.getMessage());
    });
}

Output information

The author is Unfbx, personal website: https://www.unfbx.com

2.5, Text to Speech TTS

OpenAi’s latest interface supports TTS, high-definition voice, and six types of human voices.

2.5.1, text-to-speech

/**
 * tts usage example
 */
@Test
public void textToSpeed() {<!-- -->
    TextToSpeech textToSpeech = TextToSpeech.builder()
            .model(TextToSpeech.Model.TTS_1_HD.getName())
            .input("OpenAI official Api's Java SDK can be quickly accessed for project use. Currently it supports all official OpenAI interfaces and also supports Tokens calculation. Official github address: https://github.com/Grt1228/chatgpt-java. Welcome star.")
            .voice(TtsVoice.NOVA.getName())
            .responseFormat(TtsFormat.MP3.getName())
            .build();
    File file = new File("C:\Users\***\Desktop\test.mp3");
    client.textToSpeech(textToSpeech, new Callback<ResponseBody>() {<!-- -->
        @SneakyThrows
        @Override
        public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {<!-- -->
            InputStream inputStream = response.body().byteStream();
            //Create a file
            if (!file.exists()) {<!-- -->
                if (!file.getParentFile().exists())
                    file.getParentFile().mkdir();
                try {<!-- -->
                    file.createNewFile();
                } catch (IOException e) {<!-- -->
                    e.printStackTrace();
                    log.error("createNewFile IOException");
                }
            }

            OutputStream os = null;
            try {<!-- -->
                os = new BufferedOutputStream(new FileOutputStream(file));
                byte data[] = new byte[8192];
                int len;
                while ((len = inputStream.read(data, 0, 8192)) != -1) {<!-- -->
                    os.write(data, 0, len);
                }
            } catch (IOException e) {<!-- -->
                e.printStackTrace();
            } finally {<!-- -->
                try {<!-- -->
                    inputStream.close();
                } catch (IOException e) {<!-- -->
                    e.printStackTrace();
                }
                try {<!-- -->
                    if (os != null) {<!-- -->
                        os.close();
                    }
                } catch (IOException e) {<!-- -->
                    e.printStackTrace();
                }
            }
        }

        @Override
        public void onFailure(Call<ResponseBody> call, Throwable t) {<!-- -->

        }
    });
    CountDownLatch countDownLatch = new CountDownLatch(1);
    try {<!-- -->
        countDownLatch.await();
    } catch (InterruptedException e) {<!-- -->
        e.printStackTrace();
    }
}

3. More

Visit the project open source address: https://github.com/Grt1228/chatgpt-java
Click a little free star
Get more SDK features