Skip to content

OpenAi GPT API for Java. Including all API from OpenAI except deprecated. It especially includes stream client and jtokkit with function calculation. Including Baidu AI.

License

Notifications You must be signed in to change notification settings

forestwanglin/openai-java

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

openai-java

OpenAi API for Java. Including all API from OpenAI official document, and the counting token method.

GitHub version License

Example Application

Supported APIs

Important update

  • [2023-06-18] Support function call by API, and update the method to count tokens for functions after 0613 update by OpenAI
  • [2023-07-25] Return model limit information by passing consumer to OpenAiService instructor.
  • [2023-08-23] Remove api for Fine-tunes and Edits
  • [2023-08-24] Support Fine-tuning
  • [2023-11-08] Add Model Type gpt-4-1106-preview/gpt-4-vision-preview/gpt-3.5-turbo-instruct. Add param tools and tool_call instead of functions and function_call when send create completion request.
  • [2023-11-10] Reconstruct Completion and ChatCompletion
  • [2023-11-11] Update Image API to support dall-e-3
  • [2023-11-13] Update model with latest API
  • [2023-11-14] Add API support for Assistants, Threads, Messages and Runs, all of these are Beta version.
  • [2023-11-24] Add create speech api - Generates audio from the input text.
  • [2023-11-28] Remove api for Completions
  • [2023-12-04] Remove ModeType.GPT_3_5_TURBO_16K stead of GPT_3_5_TURBO_1106 which is the same length but cheaper.
  • [2024-02-02] Add model gpt-3.5-turbo-0125, ```gpt-4-0125-preview, text-embedding-3-small`, `text-embedding-3-large`.
  • [2024-02-07] Support running tool_calls in background which means that client needn't handle the tool_calls at the first response.
  • [2024-04-10] Support stream event for Assistant.
  • [2024-04-17] Add model gpt-4-turbo-2024-04-09. Remove gpt-3-turbo-1106, gpt-4-vision-preview, gpt-4-1106-preview, gpt-4-0125-preview.
  • [2024-04-21] Add api for Batch from version 3.9.2024042101
  • [2024-04-21] Replace Assistants, Thread, Messages, Runs with new version on Apr 17th, 2024 in version 4.0.2024102501
  • [2024-05-13] Add model gpt-4o-2024-05-13 and its new tokenizer o200k_base. Add parameter stream_options, logprobs, top_logprobs for createChatCompletion.
  • [2024-06-08] Add parameter parallel_tool_calls when calling create chat completion.

  • [2024-06-28] Support Baidu AI API, chat(include stream) only for now. see Baidu AI API.
  • [2024-07-19] Add model gpt-4o-mini-2024-07-18 to instead of gpt-3.5-turbo with smarter and cheaper.
  • [2024-08-08] Add model gpt-4o-2024-08-06 to instead of gpt-4o-2024-05-13 to support structure output.
  • [2024-08-08] Add model o1-preview-2024-09-12 and o1-mini-2024-09-12.
  • [2024-10-24] Add parameter input_audio when creating chat completion and support generate audio by new model gpt-4o-audio-preview-2024-10-01.

How to use

Maven

<dependency>
    <groupId>xyz.felh</groupId>
    <artifactId>service</artifactId>
    <version>4.0.2024102501</version>
</dependency>
<!-- get tokens count -->
<dependency>
    <groupId>xyz.felh</groupId>
    <artifactId>jtokkit</artifactId>
    <version>4.0.2024102501</version>
</dependency>

Gradle

implementation group: 'xyz.felh', name: 'service', version: '4.0.2024102501'
implementation group: 'xyz.felh', name: 'jtokkit', version: '4.0.2024102501'

sbt

libraryDependencies += "xyz.felh" % "service" % "4.0.2024102501"
libraryDependencies += "xyz.felh" % "jtokkit" % "4.0.2024102501"

Example (Spring Boot 3)

  • 1. Add maven dependency

<dependency>
    <groupId>xyz.felh</groupId>
    <artifactId>service</artifactId>
    <version>4.0.2024102501</version>
</dependency>
  • 2. Init openAIService

There are multiple ways to init openAIService. Create OpenAiService by passing token, or you can init it with your own OkHttpClient settings.

@Data
@Component
@ConfigurationProperties(prefix = "openai")
public class OpenAiApiConfig {
    // OpenAI API token
    private String token;

    // Init directly with token only
    @Bean(name = "openAiService")
    public OpenAiService openAiService() {
        return new OpenAiService(token);
    }
}
@Data
@Component
@ConfigurationProperties(prefix = "openai")
public class OpenAiApiConfig {
    // OpenAI API token
    private String token;
    // OpenAI API orgId
    private String orgId;
    // OpenAI API timeout
    private Long timeout;

    // Init with OkHttpClient settings
    @Bean(name = "openAiService")
    public OpenAiService openAiService() {
        ObjectMapper mapper = defaultObjectMapper();
        // Add proxy if need
        // Proxy proxy = new Proxy(Proxy.Type.SOCKS, new InetSocketAddress("127.0.0.1", 1086));
        OkHttpClient client = defaultClient(token, orgId, Duration.ofMillis(timeout))
                .newBuilder()
                .addInterceptor(new ExtractHeaderInterceptor(responseHeaders -> log.info("headers: {}", JSON.toJSONString(responseHeaders))))
                .proxy(proxy)
                .build();
        Retrofit retrofit = defaultRetrofit(client, mapper);
        OpenAiApi api = retrofit.create(OpenAiApi.class);
        return new OpenAiService(api, client);
    }
}

Here is the settings in yml file.

openai:
  token: OPEN_AI_API_TOKEN
  org-id: OPEN_AI_ORG_ID
  timeout: 60000

3. Call API

3.1.a Create Chat Completion (Without stream)

public class OpenAiService {

    public void createStreamChatCompletion() {
        CreateChatCompletionRequest request = CreateChatCompletionRequest.builder()
                .messages(Arrays.asList(new ChatMessage(ChatMessageRole.USER, "Hello, Please count 1 to 10")))
                .model("gpt-3.5-turbo")
                .maxTokens(2048)
                .temperature(0.6)
                .stream(false)
                .build();
        log.info("chatCompletion Request:\n{}", JsonUtils.toPrettyJSONString(request));
        ChatCompletion completionResult = openAiService.createChatCompletion(request);
        log.info("chatCompletion Response:\n{}", JsonUtils.toPrettyJSONString(completionResult));
    }
}

3.1.b Create Chat Completion (With stream)

3.1.b.1 Register listener for stream (use Flux to serve server-sent events)
public class OpenAiService {

    private Flux<ServerSentEvent<List<String>>> buildFlux(String messageId) {
        Flux<ServerSentEvent<List<String>>> flux = Flux.create(fluxSink -> {

            StreamChatCompletionListener listener = new StreamChatCompletionListener() {
                @Override
                public void onOpen(String requestId, Response response) {
                    log.debug("onOpen {}", requestId);
                }

                @Override
                public void onEvent(String requestId, xyz.felh.openai.completion.chat.ChatCompletion chatCompletion) {
                    ChatCompletionChoice chatCompletionChoice = chatCompletion.getChoices().get(0);
                    if ("stop".equalsIgnoreCase(chatCompletionChoice.getFinishReason())) {
                        log.info("chatCompletion stream is stopped");
                        // send stop signature to client
                        fluxSink.next(ServerSentEvent.<List<String>>builder()
                                .id(requestId)
                                .event("stop")
                                .data(Collections.singletonList("stop"))
                                .build());
                    } else {
                        if (chatCompletionChoice.getDelta() != null && chatCompletionChoice.getDelta().getContent() != null) {
                            // send delta message to client
                            fluxSink.next(ServerSentEvent.<List<String>>builder()
                                    .id(requestId)
                                    .event("message")
                                    .data(Collections.singletonList(chatCompletionChoice.getDelta().getContent()))
                                    .build());
                        }
                    }
                }
            };

            // create stream chat message
            CreateChatCompletionRequest request = CreateChatCompletionRequest.builder()
                    .messages(Arrays.asList(new ChatMessage(ChatMessageRole.USER, "Hello, Please count 1 to 10")))
                    .model("gpt-3.5-turbo")
                    .maxTokens(2048)
                    .temperature(0.8)
                    .stream(true)
                    .build();
            log.info("chatCompletion Request:\n{}", JsonUtils.toPrettyJSONString(request));
            openAiService.createSteamChatCompletion(messageId, request, listener);

            // unsubscribe when user disconnect
            fluxSink.onCancel(() -> {
                log.info("flux cancel {}", messageId);
                listener.close();
            });
        }, FluxSink.OverflowStrategy.LATEST);
        return flux;
    }
}

3.2 Create Image

public class OpenAiService {
    public void createImage() {
        CreateImageRequest createImageRequest = CreateImageRequest.builder()
                .prompt("A cute baby dea otter")
                .n(1)
                .size(ImageSize.R_1024X1024)
                .responseFormat(ImageResponseFormat.URL)
                .model(ImageModelType.DALL_E_2.value())
                .build();
        ImageResponse imageResponse = getOpenAiService().createImage(createImageRequest);
        log.info("imageResponse: {}", toJSONString(imageResponse));
    }
}

3.3 Create Embedding

public class OpenAiService {
  
    public void createEmbedding() {
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < 4095; i++) {
            sb.append("AGI ");
        }

        log.info("f:"+ TikTokenUtils.tokens(EncodingType.CL100K_BASE, sb.toString().trim()));

        List<String> inputs = new ArrayList<>();
        inputs.add(sb.toString().trim());
        CreateEmbeddingRequest createEmbeddingRequest = CreateEmbeddingRequest.builder()
            .input(inputs)
            .encodingFormat(CreateEmbeddingRequest.EncodingFormat.FLOAT)
            .model("text-embedding-ada-002")
            .build();
        CreateEmbeddingResponse createEmbeddingResponse = getOpenAiService().createEmbeddings(createEmbeddingRequest);
        log.info("createEmbeddingResponse:  {}", toJSONString(createEmbeddingResponse));
    }
}

3.4 Create Chat Completion with GPT_4_TURBO with Input Image

public class OpenAiService {

    public void createChatCompletionWithImage() {
        String model = "gpt-4-turbo-2024-04-09";
        ChatMessage chatMessage = new ChatMessage();
        chatMessage.setRole(ChatMessageRole.USER);
        chatMessage.addTextToContent("描述一下图片的内容");
        chatMessage.addImageUrlToContent("https://qn.felh.xyz/ai1.jpg", ChatMessage.ImageUrlDetail.LOW);
        // you can also set image with base64 format
        // chatMessage.addImageWithBase642ContentItem("data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAASABIAAD/4QCMRXhpZgAATU0AKgAAAAgABQESAAMAAAABAAEAAAEaAAUAAAABAAAASgEbAAUAAAABAAAAUgEoAAMAAAABAAIAAIdpAAQAAAABAAAAWgAAAAAAAABIAAAAAQAAAEgAAAABAAOgAQADAAAAAQABAACgAgAEAAAAAQAAAJagAwAEAAAAAQAAAJYAAAAA/8AAEQgAlgCWAwEiAAIRAQMRAf/EAB8AAAEFAQEBAQEBAAAAAAAAAAABAgMEBQYHCAkKC//EALUQAAIBAwMCBAMFBQQEAAABfQECAwAEEQUSITFBBhNRYQcicRQygZGhCCNCscEVUtHwJDNicoIJChYXGBkaJSYnKCkqNDU2Nzg5OkNERUZHSElKU1RVVldYWVpjZGVmZ2hpanN0dXZ3eHl6g4SFhoeIiYqSk5SVlpeYmZqio6Slpqeoqaqys7S1tre4ubrCw8TFxsfIycrS09TV1tfY2drh4uPk5ebn6Onq8fLz9PX29/j5+v/EAB8BAAMBAQEBAQEBAQEAAAAAAAABAgMEBQYHCAkKC//EALURAAIBAgQEAwQHBQQEAAECdwABAgMRBAUhMQYSQVEHYXETIjKBCBRCkaGxwQkjM1LwFWJy0QoWJDThJfEXGBkaJicoKSo1Njc4OTpDREVGR0hJSlNUVVZXWFlaY2RlZmdoaWpzdHV2d3h5eoKDhIWGh4iJipKTlJWWl5iZmqKj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NrV9FiZgRjNrav9wjs84z6Rkc1+f3xG+KnxA+LOuf2/wCP9am1a4QsYY2+S2tgx+7bwL8kYHTdguR952rz6vpMp4Oo0bTxHvS7dP8Ag/PTyPsMi8PqFC1TFvnl2+yv8/np5E91dXN7dT3t7M9xcXMjzSyyMXeSSRi7u7HkszEkk9Sa+yv2B/C9xr37ROn6ssZa38O6fe3kjY4V5VW2iyfVvMkx/umvi0kKCzHAHXgn9ByfYDkngc1+7/7EvwIvPhD8OZte8UW32fxP4saO5uYmHz2trGCLa3b0YBjJIM4EjsO1ejxRj40MHJdZaL57/cv0PX40zSGGwE4396a5UvXf7l+nc+0hR0oor8ZP54CiiigAooooA//X/fyiiigAooooAKMZoooAKKKKAPzf/be+FX7RnjaMXngfUJtd8GrGDcaDZEW9wrqPmd1yPtiN/cLDb/zzc4I/HS5t5rG9k0q8ia1vLc7HtpUaGePHZoXCuuB2KjjpxX9VdcT4u+G3w+8fQfZ/G3hvTtdQYwL21inxj0LqSK+zyXi36tTVGpTul1Wj+ff8D9D4d48eDpLD1KScV1Wj+fR/h5tn8xJilAyUYD6Guh8HeDvFfxD1NNG8CaRdeILxyB5djH5wXPeSTIijHvI6iv6DLf8AZV/ZxtZhcQ/DfQt6nI3WUbAH6MCP0r2zSdF0fQbNdP0Sxg0+1T7sVvEsUY+ioAK9bEcdwUf3VNt+f/Av+h72K8Tqaj+4otvzdl+F7/gfnr+zN+wzZeBL6x+IHxf8jUvENm4ms9OiYy2dlKvKyyMQonnXqp2hIzyoLAPX6O0UV8JmGZVsVU9pWd3+C9D8xzXOMRjavtcRK76dkuy/r1CiiiuE8wKKKKACiiigD//Q/fyiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigD/9k=", ChatMessage.IMG_DETAIL_HIGH);
        List<ChatMessage> chatMessages = Arrays.asList(
            ChatMessage.builder()
                    .role(ChatMessageRole.SYSTEM)
                    .content("You are a helpful assistant. Do not include pleasantries in your responses. Mark code language tag if there is code.")
                    .build(),
            chatMessage);
        CreateChatCompletionRequest chatCompletionRequest = CreateChatCompletionRequest.builder()
            .messages(chatMessages)
            .maxTokens(4096)
            .temperature(0.6)
            .stream(false)
            .user("FU92834923849328943824")
            .model(model)
            .build();
        ChatCompletion chatCompletion = getOpenAiService().createChatCompletion(chatCompletionRequest);
        log.info("chatCompletion: {}", toJSONString(chatCompletion));
    }
}

3.5 Create Chat Completion with Tool Calls in background.

public class OpenAiService {
    
    public void createFunctionCallStreamChatCompletion() {
        final List<ChatMessage> messages = new ArrayList<>();
        messages.add(new ChatMessage(ChatMessageRole.SYSTEM, "You are an assistant."));
        messages.add(new ChatMessage(ChatMessageRole.USER, "What is weather now in Shanghai?"));

        SchemaGeneratorConfigBuilder configBuilder = new SchemaGeneratorConfigBuilder(SchemaVersion.DRAFT_7, OptionPreset.PLAIN_JSON)
                .with(new JacksonModule());
        SchemaGeneratorConfig config = configBuilder.build();
        SchemaGenerator generator = new SchemaGenerator(config);
        JsonNode jsonSchema = generator.generateSchema(GetWeatherParam.class);
        JSONObject jsonObject = JSONObject.parseObject(jsonSchema.toString());

        CreateChatCompletionRequest chatCompletionRequest = CreateChatCompletionRequest.builder()
                .messages(messages)
                .model("gpt-3.5-turbo-0125")
                .tools(List.of(Tool.builder()
                        .type(Type.FUNCTION)
                        .function(Function.builder()
                                .name("get_weather")
                                .description("Get the current weather in a given location")
                                .parameters(jsonObject)
                                .build()).build()))
                .toolChoice("auto")
                .build();
        StreamChatCompletionListener listener = new StreamChatCompletionListener() {
            @Override
            public void onOpen(String requestId, Response response) {log.info("on onOpen {}", requestId);
            }

            @Override
            public void onEvent(String requestId, ChatCompletion chatCompletion) {log.info("chatCompletion: {}", JSON.toJSONString(chatCompletion));
            }

            @Override
            public void onEventDone(String requestId) {log.info("on onEventDone {}", requestId);
            }

            @Override
            public void onClosed(String requestId) {log.info("on onClosed {}", requestId);
            }

            @Override
            public void onFailure(String requestId, Throwable t, Response response) {log.info("on failure {} {}", requestId, JSON.toJSONString(response));
            }
        };
        getOpenAiService().createSteamChatCompletion("1234", chatCompletionRequest, listener,
                (requestId, chatCompletion) -> {
            log.info("request Id {}", requestId);
            log.info("chatCompletion {}", chatCompletion);
            if (Preconditions.isNotBlank(chatCompletion)
                    && Preconditions.isNotBlank(chatCompletion.getChoices())
                    && Preconditions.isNotBlank(chatCompletion.getChoices().get(0).getDelta())
                    && Preconditions.isNotBlank(chatCompletion.getChoices().get(0).getDelta().getToolCalls())) {
                List<ToolCall> toolCalls = chatCompletion.getChoices().get(0).getDelta().getToolCalls();
                messages.add(chatCompletion.getChoices().get(0).getDelta());
                for (ToolCall toolCall : toolCalls) {
                    ChatMessage chatMessage = new ChatMessage(ChatMessageRole.TOOL, "晴");
                    chatMessage.setToolCallId(toolCall.getId());
                    messages.add(chatMessage);
                }
            }
            return StreamToolCallsRequest.builder().request(CreateChatCompletionRequest.builder()
                            .messages(messages)
                            .model("gpt-3.5-turbo-0125")
                            .build())
                    .requestId("3444444").build();
        });
    }
}

You can find more examples in

License

Published under the MIT License (https://github.com/forestwanglin/openai-java/blob/main/LICENSE)

About

OpenAi GPT API for Java. Including all API from OpenAI except deprecated. It especially includes stream client and jtokkit with function calculation. Including Baidu AI.

Topics

Resources

License

Stars

Watchers

Forks

Languages