Cloudflare is testing its Workers AI API. Hopefully this project makes it easier for java developers to consume Cloudflare's latest and greatest.
- Text Generation
- Text-to-Image
- Translation
- Summarization
- Speech-to-Text
- Image Classification
- Image-to-Text
- Object Detection
- Text Embeddings
- Text Classification
Add dependency to your pom.xml
file:
<dependency>
<groupId>dev.kovaliv.cloudflare</groupId>
<artifactId>ai-client</artifactId>
<version>0.1.1</version>
</dependency>
Please visit the Cloudflare Workers AI website for more details. This lib provides a client that wraps around Cloudflare's REST API.
CloudflareClient client = new CloudflareClient(getenv("CLOUDFLARE_ACCOUNT_ID"), getenv("CLOUDFLARE_AUTH_TOKEN"));
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());
String prompt = "Curly dog";
ImageRequest request = new ImageRequest(prompt);
File image = client.generate(request, TextToImageModel.STABLE_DIFFUSION_XL_LIGHTNING);
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());
String text = "Big text";
SummarizationRequest request = new SummarizationRequest(text);
SummarizationResponse response = client.generate(request, SummarizationModel.BART_LARGE_CNN);
System.out.println(response.getResult().getSummary());
SpeechRecognitionResponse response = client.generate(new File("audio.ogg"), SpeechRecognitionModel.WHISPER);
System.out.println(response.getResult().getText());
ImageClassificationResponse response = client.generate(new File("image.jpg"), ImageClassificationModel.RESNET_50);
ImageToTextResponse response = client.generate(new File("image.jpg"), ImageToTextModel.UFORM_GEN2_QWEN_500M);
System.out.println(response.getResult().getDescription());
ObjectDetectionResponse response = client.generate(new File("image.jpg"), ObjectDetectionModel.DETR_RESNET_50);
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);
String text = "Hello, how are you?";
TextClassificationRequest request = new TextClassificationRequest(text);
TextClassificationResponse response = client.generate(request, TextClassificationModel.DISTILBERT_SST_2_INT8);
Bug reports and pull requests are welcome on GitHub at https://github.com/taraskovaliv/ai-client.
This lib is available as open source under the terms of the MIT License.