Skip to content

A java client for the Cloudflare Workers AI REST API

License

Notifications You must be signed in to change notification settings

taraskovaliv/ai-client

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Cloudflare Workers AI API client for Java

Cloudflare is testing its Workers AI API. Hopefully this project makes it easier for java developers to consume Cloudflare's latest and greatest.

Maven Central Version GitHub License

Supported features

Table of Contents

Installation

Add dependency to your pom.xml file:

<dependency>
    <groupId>dev.kovaliv.cloudflare</groupId>
    <artifactId>ai-client</artifactId>
    <version>0.1.1</version>
</dependency>

Usage

Cloudflare Workers AI

Please visit the Cloudflare Workers AI website for more details. This lib provides a client that wraps around Cloudflare's REST API.

Client

CloudflareClient client = new CloudflareClient(getenv("CLOUDFLARE_ACCOUNT_ID"), getenv("CLOUDFLARE_AUTH_TOKEN"));

Text Generation

String prompt = "Hello, how are you?";
TextGenerationRequest request = new TextGenerationRequest(prompt);
TextGenerationResponse response = client.generate(request, TextModel.OPENCHAT_3_5_AWQ);
System.out.println(response.getResult().getResponse());

Text-to-Image

String prompt = "Curly dog";
ImageRequest request = new ImageRequest(prompt);
File image = client.generate(request, TextToImageModel.STABLE_DIFFUSION_XL_LIGHTNING);

Translation

String text = "Hello, how are you?";
TranslationRequest request = new TranslationRequest(text, "en", "es");
TranslationResponse response = client.generate(request, TranslationModel.M2M_100_1_2B);
System.out.println(response.getResult().getTranslatedText());

Summarization

String text = "Big text";
SummarizationRequest request = new SummarizationRequest(text);
SummarizationResponse response = client.generate(request, SummarizationModel.BART_LARGE_CNN);
System.out.println(response.getResult().getSummary());

Speech-to-Text

SpeechRecognitionResponse response = client.generate(new File("audio.ogg"), SpeechRecognitionModel.WHISPER);
System.out.println(response.getResult().getText());

Image Classification

ImageClassificationResponse response = client.generate(new File("image.jpg"), ImageClassificationModel.RESNET_50);

Image-to-Text

ImageToTextResponse response = client.generate(new File("image.jpg"), ImageToTextModel.UFORM_GEN2_QWEN_500M);
System.out.println(response.getResult().getDescription());

Object Detection

ObjectDetectionResponse response = client.generate(new File("image.jpg"), ObjectDetectionModel.DETR_RESNET_50);

Text Embedding

With a single text:

String text = "Hello, how are you?";
TextEmbeddingsRequest request = new TextEmbeddingsRequest(text);
TextEmbeddingsResponse response = client.generate(request, TextEmbeddingsModel.BG_BASE_EN_V1_5);

With multiple texts:

List<String> text = List.of("Hello, how are you?", "I am fine, thank you.", "Goodbye!");
TextEmbeddingsMultiRequest request = new TextEmbeddingsMultiRequest(text);
TextEmbeddingsResponse response = client.generate(request, TextEmbeddingsModel.BG_LARGE_EN_V1_5);

Text Classification

String text = "Hello, how are you?";
TextClassificationRequest request = new TextClassificationRequest(text);
TextClassificationResponse response = client.generate(request, TextClassificationModel.DISTILBERT_SST_2_INT8);

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/taraskovaliv/ai-client.

License

This lib is available as open source under the terms of the MIT License.