Search code examples
javaazuremavenazure-openai

SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder" and Resource not found with Azure OpenAI service


When I am using the quickstart demo code from Azure OpenAI website in OpenJDK 21.

package com.azure.ai.openai.usage;

import com.azure.ai.openai.OpenAIClient;
import com.azure.ai.openai.OpenAIClientBuilder;
import com.azure.ai.openai.models.Choice;
import com.azure.ai.openai.models.Completions;
import com.azure.ai.openai.models.CompletionsOptions;
import com.azure.ai.openai.models.CompletionsUsage;
import com.azure.core.credential.AzureKeyCredential;

import java.util.ArrayList;
import java.util.List;

public class GetCompletionsSample {

    public static void main(String[] args) {
        String azureOpenaiKey = "MY AZURE OPENAI API KEY";
        String endpoint = "MY AZURE OPENAI ENDPOINT";
        String deploymentOrModelId = "gpt-35-turbo-instruct";

        OpenAIClient client = new OpenAIClientBuilder()
                .endpoint(endpoint)
                .credential(new AzureKeyCredential(azureOpenaiKey))
                .buildClient();

        List<String> prompt = new ArrayList<>();
        prompt.add("When was Microsoft founded?");

        Completions completions = client.getCompletions(deploymentOrModelId, new CompletionsOptions(prompt));

        System.out.printf("Model ID=%s is created at %s.%n", completions.getId(), completions.getCreatedAt());
        for (Choice choice : completions.getChoices()) {
            System.out.printf("Index: %d, Text: %s.%n", choice.getIndex(), choice.getText());
        }

        CompletionsUsage usage = completions.getUsage();
        System.out.printf("Usage: number of prompt token is %d, "
                        + "number of completion token is %d, and number of total tokens in request and response is %d.%n",
                usage.getPromptTokens(), usage.getCompletionTokens(), usage.getTotalTokens());
    }
}

and show the error

SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Exception in thread "main" com.azure.core.exception.ResourceNotFoundException: Status code 404, "{"error":{"code":"404","message": "Resource not found"}}"
    at com.azure.core.implementation.http.rest.RestProxyBase.instantiateUnexpectedException(RestProxyBase.java:347)
    at com.azure.core.implementation.http.rest.SyncRestProxy.ensureExpectedStatus(SyncRestProxy.java:130)
    at com.azure.core.implementation.http.rest.SyncRestProxy.handleRestReturnType(SyncRestProxy.java:213)
    at com.azure.core.implementation.http.rest.SyncRestProxy.invoke(SyncRestProxy.java:81)
    at com.azure.core.implementation.http.rest.RestProxyBase.invoke(RestProxyBase.java:109)
    at com.azure.core.http.rest.RestProxy.invoke(RestProxy.java:91)
    at jdk.proxy2/jdk.proxy2.$Proxy3.getCompletionsSync(Unknown Source)
    at com.azure.ai.openai.implementation.OpenAIClientImpl.getCompletionsWithResponse(OpenAIClientImpl.java:904)
    at com.azure.ai.openai.OpenAIClient.getCompletionsWithResponse(OpenAIClient.java:222)
    at com.azure.ai.openai.OpenAIClient.getCompletions(OpenAIClient.java:587)
    at com.azure.ai.openai.usage.GetCompletionsSample.main(GetCompletionsSample.java:29)

Process finished with exit code 1

I have checked all the configuration and make sure the endpoint was correct, the deployment was created more than 10 hours, and my deployment ID is same as the model name. Am I missing something? This is the dependencies:

<dependency>
    <groupId>com.azure</groupId>
    <artifactId>azure-ai-openai</artifactId>
    <version>1.0.0-beta.5</version>
</dependency>

and I've tried the solution from How do I fix "SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder"." However, when I add dependencies to pom.xml, it still get the error in my IntelliJ IDEA

<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-api</artifactId>
    <version>2.0.7</version>
</dependency>
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-log4j12</artifactId>
    <version>2.0.7</version>
</dependency>

Solution

  • I have added the dependencies log4j-api, log4j-core, log4j-slf4j-impl in pom.xml file.

    pom.xml :

        <dependencies>
            <dependency>
                <groupId>com.azure</groupId>
                <artifactId>azure-ai-openai</artifactId>
                <version>1.0.0-beta.5</version>
            </dependency>
            <dependency>
                <groupId>org.apache.logging.log4j</groupId>
                <artifactId>log4j-api</artifactId>
                <version>2.20.0</version>
            </dependency>
            <dependency>
                <groupId>org.apache.logging.log4j</groupId>
                <artifactId>log4j-core</artifactId>
                <version>2.20.0</version>
            </dependency>
            <dependency>
                <groupId>org.apache.logging.log4j</groupId>
                <artifactId>log4j-slf4j-impl</artifactId>
                <version>2.20.0</version>
            </dependency>
        </dependencies>
    

    I have modified the code to handle the exception.

    Code :

    import com.azure.ai.openai.OpenAIClient;
    import com.azure.ai.openai.OpenAIClientBuilder;
    import com.azure.ai.openai.models.Choice;
    import com.azure.ai.openai.models.Completions;
    import com.azure.ai.openai.models.CompletionsOptions;
    import com.azure.ai.openai.models.CompletionsUsage;
    import com.azure.core.credential.AzureKeyCredential;
    
    import java.util.ArrayList;
    import java.util.List;
    
    public class Main {
    
        public static void main(String[] args) {
            String azureOpenaiKey = "<key>";
            String endpoint = "https://eastus.api.cognitive.microsoft.com";
            String deploymentOrModelId = "<Model_ID>";
    
            OpenAIClient client = new OpenAIClientBuilder()
                    .endpoint(endpoint)
                    .credential(new AzureKeyCredential(azureOpenaiKey))
                    .buildClient();
    
            List<String> prompt = new ArrayList<>();
            prompt.add("When was Microsoft founded?");
    
            try {
                Completions completions = client.getCompletions(deploymentOrModelId, new CompletionsOptions(prompt));
    
                if (completions != null) {
                    System.out.printf("Model ID=%s is created at %s.%n", completions.getId(), completions.getCreatedAt());
                    for (Choice choice : completions.getChoices()) {
                        System.out.printf("Index: %d, Text: %s.%n", choice.getIndex(), choice.getText());
                    }
    
                    CompletionsUsage usage = completions.getUsage();
                    System.out.printf("Usage: number of prompt tokens is %d, "
                                    + "number of completion tokens is %d, and the number of total tokens in the request and response is %d.%n",
                            usage.getPromptTokens(), usage.getCompletionTokens(), usage.getTotalTokens());
                } else {
                    System.err.println("The response from the API is null.");
                }
            } catch (Exception e) {
                System.err.println("Error: " + e.getMessage());
            }
        }
    }
    

    Output :

    It ran successfully as below.

    enter image description here