Skip to content

OpenAI API returns body as empty #3528

Open
@799452017

Description

@799452017

Please do a quick search on GitHub issues first, there might be already a duplicate issue for the one you are about to create.
If the bug is trivial, just go ahead and create the issue. Otherwise, please take a few moments and fill in the following sections:

Bug description
There is a problem calling my private cloud OpenAI service using OpenAiApi

Image
But using openai4j is normal

Image
Using Postman is also normal

Image

Environment
<java.version>17</java.version>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<spring-framework.version>6.2.4</spring-framework.version>
<spring-boot.version>3.4.3</spring-boot.version>
<spring-ai.version>1.0.0-M6</spring-ai.version>
<spring-cloud.version>2024.0.0</spring-cloud.version>
Steps to reproduce

    @Test
    public void openaiTest() {
        OpenAiApi openAiApi = OpenAiApi.builder()
                .apiKey("")
                .baseUrl("")
                .completionsPath("chat/completions")
                .build();

        try {
            OpenAiApi.ChatCompletionRequest chatRequest = new OpenAiApi.ChatCompletionRequest(
                    Arrays.asList(new OpenAiApi.ChatCompletionMessage("你是谁", OpenAiApi.ChatCompletionMessage.Role.USER))
                    , "DeepSeek-R1", 0.7, true);
            Flux<OpenAiApi.ChatCompletionChunk> chatCompletionChunkFlux = openAiApi.chatCompletionStream(chatRequest);
            System.out.println(chatCompletionChunkFlux.blockLast());
        } catch (WebClientResponseException e) {
            System.err.println(e.getResponseBodyAsString());

            e.printStackTrace();
        }
    }
<dependency>
            <groupId>dev.ai4j</groupId>
            <artifactId>openai4j</artifactId>
            <version>0.17.0</version>
        </dependency>
    @Test
    public void test1() {

        OpenAiClient client = OpenAiClient.builder()
                .baseUrl("")
                .openAiApiKey("")
                .logStreamingResponses()
                .logLevel(LogLevel.DEBUG)
                .logRequests()
                .logResponses()
                // other customizations coming soon!
                .build();

        ChatCompletionRequest request = ChatCompletionRequest.builder()
                .model("DeepSeek-R1")
                .stream(true)
                .addUserMessage("你是谁")
                .temperature(0.7)
                .build();

        client.chatCompletion(request)
                .onResponse(response -> {
                    System.out.println(response.content());
                })
                .onError(error -> {
                    System.out.println("Error: " + error.getMessage());
                })
                .execute();
    }

Expected behavior
What problem causes the body to be empty

Minimal Complete Reproducible example
Please provide a failing test or a minimal complete verifiable example that reproduces the issue.
Bug reports that are reproducible will take priority in resolution over reports that are not reproducible.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions