Skip to content

Getting Started - with Java/Kotlin/JVM

We use the AIsBreaker Client API for Java/Kotlin/JVM now.

Setup

Add the required dependency to your build tool:

Groovy
dependencies {
    ...
    implementation 'org.aisbreaker:aisbreaker-api-java:0.1.2'
    ...
}
Kotlin
dependencies {
    ...
    implementation("org.aisbreaker:aisbreaker-api-java:0.1.2")
    ...
}
XML
<project ...>
  ...
  <dependencies>
    ...
    <dependency>
      <groupId>org.aisbreaker</groupId>
      <artifactId>aisbreaker-api-java</artifactId>
      <version>0.1.2</version>
    </dependency>
    ...
  </dependencies>
  ...
</project>
Scala
...
libraryDependencies += "org.aisbreaker" % "aisbreaker-api-java" % "0.1.2"
...

Initialize the AIsBreaker Client API

Import required libraries:

Java
import org.aisbreaker.api.*;
import org.aisbreaker.api.model.*;
Kotlin
import org.aisbreaker.api.*
import org.aisbreaker.api.model.*

Start definition of the main method/function, inclusive start of a try-catch block:

Java
public class AIsBreakerSimpleChat {
  public static void main(String[] args) {
    System.out.println("AIsBreakerSimpleChat");
    System.out.println("--------------------");
            
    try {
Kotlin
fun main() {
    println("AIsBreakerSimpleChat(kt)")
    println("------------------------")

    try {

Select your desired service and define respective Service Properties - mainly the serviceId:

Java
        // service configuration: select the service/serviceId you want to use
        AIsServiceProps serviceProps = new AIsServiceProps()
            .setServiceId("chat:dummy")
        
            //.setServiceId("chat:openai.com")
        
            //.setServiceId("chat:gemini.vertexai.google.com")
            //.setProject("<YOUR-GOOGLE-CLOUD-PROJECT>")          // optional for gemini.vertexai.google.com
            //.setLocation("<YOUR-GOOGLE-CLOUD-LOCATION>")        // optional for gemini.vertexai.google.com, e.g. "us-central1"
        
            //.setServiceId("chat:huggingface.co/<HF-ACCOUNT>/<HF-MODEL>")
            // e.g.:
            //.setServiceId("chat:huggingface.co/microsoft/DialoGPT-large")
        
            //.setServiceId("aisbreaker:mirror")
            //.setForward2ServiceProps(new AIsServiceProps()
            //    .setServiceId("chat:echo")
            //)
        ;
Kotlin
        // service configuration: select the service/serviceId you want to use
        val serviceProps = AIsServiceProps()
            .setServiceId("chat:dummy")

            //.setServiceId("chat:openai.com")

            //.setServiceId("chat:gemini.vertexai.google.com")
            //.setProject("<YOUR-GOOGLE-CLOUD-PROJECT>")          // optional for gemini.vertexai.google.com
            //.setLocation("<YOUR-GOOGLE-CLOUD-LOCATION>")        // optional for gemini.vertexai.google.com, e.g. "us-central1"

            //.setServiceId("chat:huggingface.co/<HF-ACCOUNT>/<HF-MODEL>")
            // e.g.:
            //.setServiceId("chat:huggingface.co/microsoft/DialoGPT-large")

            //.setServiceId("aisbreaker:mirror")
            //.setForward2ServiceProps(AIsServiceProps()
            //    .setServiceId("chat:echo")
            //)

Set authentication secrets (API Keys), if required by your desired service:

Java
        Auth auth = new Auth()
            // optionally, set your OpenAI API key:
            //.setSecret("sk-...")
        
            // optionally, set your Google Cloud (Vertext AI) API key:
            //.setSecret("googlecloud-account-json-base64_..")
        
            // optionally, set your Huggingface API key:
            //.setSecret("hf_...")
        
            // optionally, set your AIsBreaker API key:
            //.setSecret("aisbreaker_...")
        ;
Kotlin
        val auth = Auth()
            // optionally, set your OpenAI API key:
            //.setSecret("sk-...")

            // optionally, set your Google Cloud (Vertext AI) API key:
            //.setSecret("googlecloud-account-json-base64_..")

            // optionally, set your Huggingface API key:
            //.setSecret("hf_...")

            // optionally, set your AIsBreaker API key:
            //.setSecret("aisbreaker_...")

In a client-side app (e.g. Android app) read the API Key from an authentication flow or from configuration instead. But only use AIsBreaker API Keys to never expose your AI service API key to app users.

Select the AIsBreaker server to use (null defaults to https://api.demo.aisbreaker.org/) and get access to the API:

Java
        String aisbreakerServerURL =  null;//"https://api.demo.aisbreaker.org/";

        // service initialization
        AIsService aisService =
            AIsBreaker.getAIsService(aisbreakerServerURL, serviceProps, auth);
Kotlin
        val aisbreakerServerURL: String? = null //"https://api.demo.aisbreaker.org/"

        // service initialization
        val aisService = 
            AIsBreaker.getAIsService(aisbreakerServerURL, serviceProps, auth)

Use the AIsBreaker Client API

Most API functions can work asynchronously with Futures.

Below we implement a simple conversation example with blocking functions: 1st request/question/prompt + 1st response/answer + 2nd request/question/prompt + 2nd response/answer.

Java
        // 1st question/prompt
        String question1 = "What is NodeJS?";
        System.out.println("***** Question1 *****\n"+question1+"\n");
        
        // 1st answer
        ResponseFinal response1 = aisService.process(new Request()
            .addInput(new Input()
                .setText(new InputText()
                    .setRole("user")
                    .setContent(question1)
                )
            )
        );
        System.out.println("***** Answer1 *****\n"+response1.getOutputs().get(0).getText().getContent()+"\n");
        
        
        // 2nd question/prompt
        String question2 = "shorter please";
        System.out.println("***** Question2 *****\n"+question2+"\n");
        
        // 2nd answer
        ResponseFinal response2 = aisService.process(new Request()
            .addInput(new Input()
                .setText(new InputText()
                    .setRole("user")
                    .setContent(question2)
                )
            )
            .setConversationState(response1.getConversationState())
        );
        System.out.println("***** Answer2 *****\n"+response2.getOutputs().get(0).getText().getContent()+"\n");
Kotlin
        // 1st question/prompt
        val question1 = "What is NodeJS?"
        println("***** Question1 *****\n$question1\n")

        // 1st answer
        val response1 = aisService.process(Request()
            .addInput(Input()
                .setText(InputText()
                    .setRole("user")
                    .setContent(question1)
                )
            )
        )
        println("***** Answer1 *****\n${response1.outputs[0].text.content}\n")


        // 2nd question/prompt
        val question2 = "shorter please"
        println("***** Question2 *****\n$question2\n")

        // 2nd answer
        val response2 = aisService.process(Request()
            .addInput(Input()
                .setText(InputText()
                    .setRole("user")
                    .setContent(question2)
                )
            )
            .setConversationState(response1.conversationState)
        )
        println("***** Answer2 *****\n${response2.outputs[0].text.content}\n")

Details:

Finally, we need to catch potential AIsErrors and finish the definition of the main method/function:

Java
    } catch (AIsError e) {
        e.printStackTrace();
    }
  }
}
Kotlin
    } catch (e: AIsError) {
        e.printStackTrace()
    }
}

Examples

Some example apps are available:

Released under the MIT License.