Git Product home page Git Product logo

javaai's Introduction

JavaAI logo

Maven Central GitHub Downloads (all assets, all releases) License: MIT Codacy Badge

About

JavaAI is a lightweight Java library with minimal third-party dependencies designed to interact with the OpenAI API. It provides an intuitive interface for accessing advanced AI capabilities in Java applications. With JavaAI, you can easily integrate state-of-the-art features into your projects, including chat with GPT, image generation in DALL-E, and text-to-speech with Whisper.

Usage

Prerequisites

  • Java 21
  • OpenAI API key

Integration

Maven

<dependency>
    <groupId>io.github.artemnefedov</groupId>
    <artifactId>javaai</artifactId>
    <version>0.4.1</version>
</dependency>

Gradle

implementation 'io.github.artemnefedov:javaai:0.4.1'

Initialize JavaAI

You can initialize JavaAI in two ways: by directly passing the API key to the constructor or by adding environment variables with the key to your system, naming it OPENAI_API_KEY as recommended by OpenAi

Passing the API key directly to the constructor

var javaAi = JavaAI.javaAiBuilder("YOUR_API_KEY");

Using an environment variable

var javaAI = JavaAI.javaAiBuilder();

Example

ChatGPT

You can use two ways to interact with ChatGPT:

  1. Pass the user's message, as a string, to the chat() method.
javaAi.chat("YOUR_QUESTION");
  1. Pass a saved conversation to the method as a List<ChatMessage>.
var messages = List.of(
       new ChatMessage("user", "what is the meaning of life?"),
       new ChatMessage("AI", "The meaning of life is to be happy."),
       new ChatMessage("user", "are you sure?")
);

javaAI.chat(messages);

Depending on the value of n you set, you can use either the chat() method, which returns a String response from the API, or the chatWithChoices() method, which returns multiple responses from the API as List<String>, depending on the value of n you set.


DALL-E

You can use the generateImage() method to generate an image from a text prompt. The model will return a URL to the result, as a List of String.

javaAI.generateImage("Computes science cat, photo on Fujifilm x100v, 2024");

Response CS cat


TTS

To translate text to speech, you must pass to the textToSpeech() method a string containing the text you want to voice and a string containing the location where the audio file will be saved.

javaAI.textToSpeech("Hi, my name is Artem, and I made this piece of... code.", "path/to/save/audio.mp3");

Response

piece_of_code.mp4

Configuration

You can specify different settings for each model, via the setChatConfig(), setDalleConfig(), and setTtsConfig() methods. You are accepting records ChatConfig, DalleConfig, and TtsConfig respectively.

Config records view


ChatConfig.java

public record ChatConfig(
        Model model,
        float temperature,
        int topP,
        int n,
        boolean stream,
        String stop,
        int maxTokens,
        float presencePenalty,
        float frequencyPenalty,
        Map<Integer, Integer> logitBias,
        String user) {
}

Parameters in OpenAI API docs


DalleConfig.java

public record DalleConfig(
        DalleModel model,
        int n,
        String quality,
        ResponseFormat responseFormat,
        Size size,
        Style style,
        String user) {
}

Parameters in OpenAI API docs


TtsConfig.java

public record TtsConfig(
        TtsModel model,
        Voice voice,
        VoiceResponseFormat responseFormat,
        float speed
) {
}

Parameters in OpenAI API docs


Example for Chat:

import io.github.artemnefedov.javaai.model.chat.ChatConfig;

var customChatConfig = new ChatConfig(
        ChatConfig.Model.GPT_3_5_TURBO,
        1F,
        1,
        1,
        false,
        "\n",
        2000,
        0F,
        0F,
        new HashMap<>(),
        UUID.randomUUID().toString()
);

javaAi.setChatConfig(customChatConfig);

Features


License

Distributed under the MIT License

javaai's People

Contributors

artemnefedov avatar dependabot[bot] avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  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.