Giter Club home page Giter Club logo

cohere4j's Introduction

cohere4j

build Jitpack Javadoc License

This is an unofficial Java client library that you can use to interact with Cohere API. It can be used in Android or any Java and Kotlin Project.

Add Dependency

Gradle

To use library in your gradle project follow the steps below:

  1. Add this in your root build.gradle at the end of repositories:
    allprojects {
        repositories {
            ...
            maven { url 'https://jitpack.io' }
        }
    }
  2. Add the dependency
    dependencies {
        def COHERE4J_VERSION = "..."
        implementation "com.github.llmjava:cohere4j:$COHERE4J_VERSION"
    }

Maven

To use the library in your Maven project, follow the steps below:

  1. Add the JitPack repository to your build file:
    <repositories>
        <repository>
            <id>jitpack.io</id>
            <url>https://jitpack.io</url>
        </repository>
    </repositories>
  2. Add the dependency
    <dependency>
        <groupId>com.github.llmjava</groupId>
        <artifactId>cohere4j</artifactId>
        <version>${COHERE4J_VERSION}</version>
    </dependency>

Usage

Example code to use the Cohere API:

Create a configuration file

# Set API key using env variable or put actual value
cohere.apiKey=${env:COHERE_API_KEY}

Create an instance of CohereClient and submit text generation requests

import com.github.llmjava.cohere4j.*;

public class Main {
   public static void main(String[] args) {
       CohereConfig config = CohereConfig.fromProperties("cohere.properties");
       CohereClient client = new CohereClient.Builder().withConfig(config).build();

       String text = "tell me a joke";
       GenerateRequest request = new GenerateRequest.Builder()
               .withPrompt(text)
               .withConfig(config)
               .build();

       System.out.println(client.generate(request).getTexts().get(0));
   }
}

Find more usage examples under examples folder.

Text generation

Synchronously

Customizable way

GenerateRequest request = new GenerateRequest.Builder()
    .withPrompt(text)
    .withConfig(config)
    .build();

GenerateResponse response = client.generate(request);

Asynchronously

Customizable way

GenerateRequest request = new GenerateRequest.Builder()
    .withPrompt(text)
    .withConfig(config)
    .build();

client.generateAsync(request, new AsyncCallback<GenerateResponse>() {
    @Override public void onSuccess(GenerateResponse response) {
    }
    @Override public void onFailure(Throwable throwable) {
    }
});

Streaming

Customizable way

GenerateRequest request = new GenerateRequest.Builder()
     .withPrompt(text)
     .withConfig(config)
     .withStream(true) // set the request to streaming
     .build();

client.generateStream(request, new StreamingCallback<StreamGenerateResponse>() {
      @Override public void onPart(StreamGenerateResponse response) {
      }
      @Override public void onComplete(StreamGenerateResponse response) {
      }
      @Override public void onFailure(Throwable throwable) {
      }
});

Embeddings

Create an embedding request

EmbedRequest request = new EmbedRequest.Builder()
    .withText(text)
    .build();

Synchronously

Customizable way

EmbedResponse response = client.embed(request);

Asynchronously

Customizable way

client.embedAsync(request, new AsyncCallback<EmbedResponse>() {
    @Override public void onSuccess(EmbedResponse response) {
    }
    @Override public void onFailure(Throwable throwable) {
    }
});

Classification

Create a classification request

ClassifyRequest request = new ClassifyRequest.Builder()
    .withExample("Dermatologists don't like her!", "Spam")
    .withExample("Hello, open to this?", "Spam")
    .withExample("I need help please wire me $1000 right now", "Spam")
    .withExample("Nice to know you ;)", "Spam")
    .withExample("Please help me?", "Spam")
    .withExample("Your parcel will be delivered today", "Not spam")
    .withExample("Review changes to our Terms and Conditions", "Not spam")
    .withExample("Weekly sync notes", "Not spam")
    .withExample("Re: Follow up from today’s meeting", "Not spam")
    .withExample("Pre-read for tomorrow", "Not spam")
    .withInput("Confirm your email address")
    .withInput("hey i need u to send some $")
    .withTruncate("END")
    .build();

Synchronously

Customizable way

ClassifyResponse response = client.classify(request);

Asynchronously

Customizable way

client.classifyAsync(request, new AsyncCallback<ClassifyResponse>() {
    @Override public void onSuccess(ClassifyResponse response) {
    }
    @Override public void onFailure(Throwable throwable) {
    }
});

Tokenization

Create a tokenization request

TokenizeRequest request = new TokenizeRequest.Builder()
    .withText("tokenize me! :D")
    .withModel("command")
    .build();

Synchronously

Customizable way

TokenizeResponse response = client.tokenize(request);

Asynchronously

Customizable way

client.tokenizeAsync(request, new AsyncCallback<TokenizeResponse>() {
    @Override public void onSuccess(TokenizeResponse response) {
    }
    @Override public void onFailure(Throwable throwable) {
    }
});

Build Project

Clone the repository and import as Maven project in IntelliJ IDEA or Eclipse

Before building the project, make sure you have the following things installed.

  • Maven
  • Java 8

To install the API client library to your local Maven repository, simply execute:

mvn install

To build the library using Gradle, execute the following command

./gradlew build

Refer to the official documentation for more information.

cohere4j's People

Contributors

dzlab avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.