Skip to content

cohere-ai/cohere-java

Folders and files

NameName
Last commit message
Last commit date

Latest commit

10b39d6 · Jan 3, 2025

History

36 Commits
Apr 22, 2024
Apr 17, 2024
Aug 30, 2024
Dec 14, 2023
Jan 3, 2025
May 8, 2024
Dec 13, 2023
Apr 24, 2024
Jan 3, 2025
Jul 9, 2024
Dec 13, 2023
Jan 3, 2025
Apr 17, 2024
Aug 30, 2024
Apr 17, 2024
Dec 13, 2023

Repository files navigation

Cohere Java Library

Maven Central fern shield

✨🪩✨ Announcing Cohere's new Java SDK ✨🪩✨

We are very excited to publish this brand new Java SDK. We now officially support Java and will continuously update this library with all of the latest features in our API. Please create issues where you have feedback so that we can continue to improve the developer experience!

Documentation

API reference documentation is available here.

Installation

Gradle

Add the dependency in your build.gradle:

dependencies {
    implementation 'com.cohere:cohere-java:+'
}

Maven

Add the dependency in your pom.xml:

<dependency>
    <groupId>com.cohere</groupId>
    <artifactId>cohere-java</artifactId>
    <version>1.x.x</version>
</dependency>

Usage

import com.cohere.api.Cohere;
import com.cohere.api.requests.ChatRequest;
import com.cohere.api.types.ChatMessage;
import com.cohere.api.types.Message;
import com.cohere.api.types.NonStreamedChatResponse;

import java.util.List;


public class ChatPost {
    public static void main(String[] args) {
        Cohere cohere = Cohere.builder().token("<<apiKey>>").clientName("snippet").build();

        NonStreamedChatResponse response = cohere.chat(
                ChatRequest.builder()
                        .message("What year was he born?")
                        .chatHistory(
                                List.of(Message.user(ChatMessage.builder().message("Who discovered gravity?").build()),
                                        Message.chatbot(ChatMessage.builder().message("The man who is widely credited with discovering gravity is Sir Isaac Newton").build()))).build());

        System.out.println(response);
    }
}

Handling Errors

When the API returns a non-success status code (4xx or 5xx response), a subclass of ApiError will be thrown:

import com.cohere.api.core.ApiError;

try {
  cohere.generate(/* ... */);
} catch (ApiError error) {
  System.out.println(error.getBody());
  System.out.println(error.getStatusCode());
}

Beta status

This SDK is in beta, and there may be breaking changes between versions without a major version update. Therefore, we recommend pinning the package version to a specific version in your build.gradle file. This way, you can install the same version each time without breaking changes unless you are intentionally looking for the latest version.

Contributing

While we value open-source contributions to this SDK, this library is generated programmatically. Additions made directly to this library would have to be moved over to our generation code, otherwise they would be overwritten upon the next generated release. Feel free to open a PR as a proof of concept, but know that we will not be able to merge it as-is. We suggest opening an issue first to discuss with us!

On the other hand, contributions to the README are always very welcome!