cohere-ai/cohere-java

The error 403 when calling an API using Java language translates to "Forbidden".

Closed this issue · 4 comments

Hello, I'm a Java development engineer, and I've been encountering issues with using cohere(https://api.cohere.com/v1/rerank). Specifically, I'm getting a 403 error when calling an API interface, even though I'm able to successfully call it using Postman. Could you please help me troubleshoot this issue?

Please could you share a code snippet?

Hello, thank you very much for your reply. Actually, the code I'm using for [cohere-java] is very simple. I'm just running your sample code. Here's a screenshot from my side.
postman request:
image

java-api:
image
image

Hey apologies for delay getting back to you on this. I'm unable to reproduce on the latest version of the java sdk with the following snippet. Please reopen the ticket if you're still having issues!

/* (C)2024 */
import com.cohere.api.Cohere;
import com.cohere.api.requests.RerankRequest;
import com.cohere.api.types.RerankRequestDocumentsItem;
import com.cohere.api.types.RerankResponse;
import java.util.List;

public class RerankPost {
    public static void main(String[] args) {
        Cohere cohere = Cohere.builder().clientName("snippet").build();

        RerankResponse response =
                cohere.rerank(
                        RerankRequest.builder()
                                .query("What is the capital of the United States?")
                                .documents(
                                        List.of(
                                                RerankRequestDocumentsItem.of(
                                                        "Carson City is the capital city of the"
                                                                + " American state of Nevada."),
                                                RerankRequestDocumentsItem.of(
                                                        "The Commonwealth of the Northern Mariana"
                                                            + " Islands is a group of islands in"
                                                            + " the Pacific Ocean. Its capital is"
                                                            + " Saipan."),
                                                RerankRequestDocumentsItem.of(
                                                        "Capitalization or capitalisation in"
                                                            + " English grammar is the use of a"
                                                            + " capital letter at the start of a"
                                                            + " word. English usage varies from"
                                                            + " capitalization in other"
                                                            + " languages."),
                                                RerankRequestDocumentsItem.of(
                                                        "Washington, D.C. (also known as simply"
                                                            + " Washington or D.C., and officially"
                                                            + " as the District of Columbia) is the"
                                                            + " capital of the United States. It is"
                                                            + " a federal district."),
                                                RerankRequestDocumentsItem.of(
                                                        "Capital punishment (the death penalty) has"
                                                            + " existed in the United States since"
                                                            + " beforethe United States was a"
                                                            + " country. As of 2017, capital"
                                                            + " punishment is legal in 30 of the 50"
                                                            + " states.")))
                                .model("rerank-english-v3.0")
                                .topN(3)
                                .build());

        System.out.println(response);
    }
}

Apologies, I just saw the message. I will give it another try. Thank you very much for your response