Skip to content

Open AI Library helps developer interacts with API easily

License

Notifications You must be signed in to change notification settings

AppcentMobile/ACMOpenAI-Android

Repository files navigation

ACMOpenAI

License: MIT
ACMOpenAI is an unofficial library that help developers to use Open AI API easily.

Installation

Maven users

Add this dependency to your project's POM:

<repositories>
  <repository>
      <id>jitpack.io</id>
      <url>https://jitpack.io</url>
  </repository>
</repositories>

<dependency>
    <groupId>com.github.AppcentMobile</groupId>
    <artifactId>ACMOpenAI-Android</artifactId>
    <version>1.0.2</version>
</dependency>

Gradle users

Add this dependency to your project's build file:

allprojects {
  repositories {
    ...
    maven { url 'https://jitpack.io' }
  }
}

dependencies {
   implementation 'com.github.AppcentMobile:ACMOpenAI-Android:1.0.2'
}

Usage

Basicly create a instance of ACMOpenAI and set the apiKey

val openApi = ACMOpenAI.Builder()
    .apiKey("${YOUR_API_KEY}")
    .debugging(true)
    .build()

val response = openApi.chat().createChatCompletion(
    CreateChatCompletionRequest(
        model = "gpt-3.5-turbo",
        messages = arrayOf(
            ChatCompletionRequestMessage(
                role = ChatCompletionRequestMessage.Role.USER.value,
                content = "Hello ChatGPT"
            )
        )
    )
)

Stream support 🚀🚀

Chat and Completions apis has stream property to provide server-sent events to collectors.

val openApi = ACMOpenAI.Builder()
    .apiKey("${YOUR_API_KEY}")
    .build()

openApi.chat().createChatCompletion(
    CreateChatCompletionRequest(
        model = "gpt-3.5-turbo",
        messages = arrayOf(
            ChatCompletionRequestMessage(
                role = ChatCompletionRequestMessage.Role.USER.value,
                content = "What did Schrodinger's Cat 😺 experiment prove?"
            )
        ),
        stream = true
    )
).collect {
    print(it?.choices?.get(0)?.message?.content)
}

ACMOpenAI options

The ACMOpenAI supports the following options on initialization. These options are all keys in the single constructor parameter.

Option Default value Type Description
apiKey undefined string Bearer token for authentication. Should be given for every request
baseUrl "https://api.openai.com/v1" string Base URL
logLevel HttpLoggingInterceptor.Level.NONE HttpLoggingInterceptor.Level
client Default client generated by ACMOpenAI OkHttpClient Define OkHttpClient by yourself

Documentation for API Endpoints

Domentation All URIs are relative to https://api.openai.com/v1

License