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.
To use library in your gradle project follow the steps below:
- Add this in your root
build.gradle
at the end of repositories:allprojects { repositories { ... maven { url 'https://jitpack.io' } } }
- Add the dependency
dependencies { def COHERE4J_VERSION = "..." implementation "com.github.llmjava:cohere4j:$COHERE4J_VERSION" }
To use the library in your Maven project, follow the steps below:
- Add the JitPack repository to your build file:
<repositories> <repository> <id>jitpack.io</id> <url>https://jitpack.io</url> </repository> </repositories>
- Add the dependency
<dependency> <groupId>com.github.llmjava</groupId> <artifactId>cohere4j</artifactId> <version>${COHERE4J_VERSION}</version> </dependency>
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.
Customizable way
GenerateRequest request = new GenerateRequest.Builder()
.withPrompt(text)
.withConfig(config)
.build();
GenerateResponse response = client.generate(request);
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) {
}
});
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) {
}
});
Create an embedding request
EmbedRequest request = new EmbedRequest.Builder()
.withText(text)
.build();
Customizable way
EmbedResponse response = client.embed(request);
Customizable way
client.embedAsync(request, new AsyncCallback<EmbedResponse>() {
@Override public void onSuccess(EmbedResponse response) {
}
@Override public void onFailure(Throwable throwable) {
}
});
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();
Customizable way
ClassifyResponse response = client.classify(request);
Customizable way
client.classifyAsync(request, new AsyncCallback<ClassifyResponse>() {
@Override public void onSuccess(ClassifyResponse response) {
}
@Override public void onFailure(Throwable throwable) {
}
});
Create a tokenization request
TokenizeRequest request = new TokenizeRequest.Builder()
.withText("tokenize me! :D")
.withModel("command")
.build();
Customizable way
TokenizeResponse response = client.tokenize(request);
Customizable way
client.tokenizeAsync(request, new AsyncCallback<TokenizeResponse>() {
@Override public void onSuccess(TokenizeResponse response) {
}
@Override public void onFailure(Throwable throwable) {
}
});
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.