Skip to content

Releases: hngx-org/openai-api-library

Improved Error handling and added Test cases

05 Oct 21:24
Compare
Choose a tag to compare

OpenAi Api Library v0.1.4

In this new version of the Open Ai Api Library, we have improved the error handling and have added test cases for each request being made.

New Changes

We Improved the error handling and by adding a HttpResponse validator which is part of the Ktor library. This helps us to check the state of the statusCode and handle it uniformly. This is an example of what it looks like :

HttpResponseValidator {
    validateResponse {
        when (val v = it.status.value) {
            400 -> throw BadRequestsException(
                "Bad Request"
            )
         }
    }
} 

We also added Test cases for each request as proof of valid implementation which can be found in the OpenAiClientTest.kt file.

Installation Guide

To use this Library in your Project, you will need to follow carefully the listed guides below and step by step.

  • First, you will need to add Internet Permissions to your Manifest.xml file to be able to make the Internet call.
<uses-permission android:name="android.permission.INTERNET" />
  • Second, you will need to add these listed dependencies in your apps build.gradle.kts file. If you are using Groovy for your scripts, then you can solve this by simply using single quotes and removing the brackets.
dependencies {
    implementation("com.github.hngx-org:openai-api-library:0.1.3")
}

Maven Repository

Now you will need to add the maven repository to your Top Level Gradle file(if you are using older android studio versions) or your settings.gradle kts for kotlin or settings.gradle for Groovy.
For Kotlin Gradle(settings.gradle.kts) add the below inside repositories{ } which is inside the dependencyResolutionManagement{ } :
For Kotlin gradle

repositories {
    maven("https://jitpack.io")
}

For groovy

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

Usage Guide

Library Information and Usage

The library has been created in a such a way that all you have to do is call an Object OpenAiCaller which contains two functions. One function for the generated response and one for generated response and user chat logs. The library also makes use of kotlin coroutines(which is also embedded in ktor) to asynchronously handle the Api calls in a background thread, so you can safely call it in your main thread or a coroutineScope.

Library Usage

In order for a request to be successful, you will need to pass along a 'Cookie' to validate that the user has the rights to make use of this service. Please make reference to the Auth library on how to retrieve the cookies for each request. It is under the Fetching User Profile section.

First, you can instantiate the class like so
val openApiClient = OpenAiCaller

Or you may not even need to instantiate the class as the functions are static because of the companion Object used. So you can just directly call it like this:
val response = OpenAiCaller.generateChatResponse()

Next for a generated response, you will need to pass along the user prompt and cookie:

 viewModelScope.launch {
    val response = OpenApiCaller.generateChatResponse("What is a tank like?", "session="RandomGeneratedCookie"")
     // Do something with response
 }

And then lastly for a generated response with user chat logs(the stored user sessions) or history, you will need to pass along the current prompt, the user chat logs and history and lastly the cookie again. Like the below :

viewModelScope.launch {
    val response = openApiClient.generateUserChatResponse("What was my last question?", 
            arrayOf("User : What color is an Apple?", "Ai : An Apple is red in color."), "session="RandomGeneratedCookie"")
            
    // Do something with response
}

Contributors and Collaborators of this Project.

Slack Usernames.

Daniel Chinedum Iroka
Lara
ChinazaBlossom

Github usernames

@daniel-iroka
@OmolaraIdowu
@Chinazablossom

Working Release!

04 Oct 16:10
Compare
Choose a tag to compare

OpenAi Api Library v0.1.3

This is the new and IMPROVED Open Ai Api library which uses Kotlin's Ktor library that makes good use of coroutines to build asynchronous client and server side applications, as well as make HTTP calls and requests at a breeze.

Installation Guide

To use this Library in your Project, you will need to follow carefully the listed guides below and step by step.

  • First, you will need to add Internet Permissions to your Manifest.xml file to be able to make the Internet call.
<uses-permission android:name="android.permission.INTERNET" />
  • Second, you will need to add these listed dependencies in your apps build.gradle.kts file. If you are using Groovy for your scripts, then you can solve this by simply using single quotes and removing the brackets.
dependencies {
    implementation("com.github.hngx-org:openai-api-library:0.1.3")
}

Maven Repository

Now you will need to add the maven repository to your Top Level Gradle file(if you are using older android studio versions) or your settings.gradle kts for kotlin or settings.gradle for Groovy.
For Kotlin Gradle(settings.gradle.kts) add the below inside repositories{ } which is inside the dependencyResolutionManagement{ } :
For Kotlin gradle

repositories {
    maven("https://jitpack.io")
}

For groovy

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

Usage Guide

Library Information and Usage

The library has been created in a such a way that all you have to do is call an Object OpenAiCaller which contains two functions. One function for the generated response and one for generated response and user chat logs. The library also makes use of kotlin coroutines(which is also embedded in ktor) to asynchronously handle the Api calls in a background thread, so you can safely call it in your main thread or a coroutineScope.

Library Usage

In order for a request to be successful, you will need to pass along a 'Cookie' to validate that the user has the rights to make use of this service. Please make reference to the Auth library on how to retrieve the cookies for each request. It is under the Fetching User Profile section.

First, you can instantiate the class like so
val openApiClient = OpenAiCaller

Or you may not even need to instantiate the class as the functions are static because of the companion Object used. So you can just directly call it like this:
val response = OpenAiCaller.generateChatResponse()

Next for a generated response, you will need to pass along the user prompt and cookie:

 viewModelScope.launch {
    val response = OpenApiCaller.generateChatResponse("What is a tank like?", "session="RandomGeneratedCookie"")
     // Do something with response
 }

And then lastly for a generated response with user chat logs(the stored user sessions) or history, you will need to pass along the current prompt, the user chat logs and history and lastly the cookie again. Like the below :

viewModelScope.launch {
    val response = openApiClient.generateUserChatResponse("What was my last question?", 
            arrayOf("User : What color is an Apple?", "Ai : An Apple is red in color."), "session="RandomGeneratedCookie"")
            
    // Do something with response
}

Contributors and Collaborators of this Project.

@daniel-iroka
@OmolaraIdowu
@Chinazablossom

OpenAi Api Package Library v1.2

03 Oct 22:46
Compare
Choose a tag to compare

OpenAi Api Library v1.2

The OpenAi Api Library v1.2 is an Android library that abstracts and connects the logic behind making network calls to OpenAi's Api, thereby simplifying the process of passing and generating responses to the OpenAi Api. This Project is for the HNGx 2023 Native Mobile Track.

Installation Guide

To use this Library in your Project, you will need to follow carefully the listed guides below and step by step.

  • First, you will need to add Internet Permissions to your Manifest.xml file to be able to make the Internet call.
<uses-permission android:name="android.permission.INTERNET" />
  • Second, you will need to add these listed dependencies in your apps build.gradle.kts file. If you are using Groovy for your scripts, then you can solve this by simply using single quotes and removing the brackets.
dependencies {
    implementation("com.github.hngx-org:openai-api-library:1.2")
}

Important Note!
You will also need to add the Retrofit Dependency in your project to instantiate the Retrofit Class on the library. A better Improvement on this will come in the future where you will not need to instantiate the library yourself.

implementation("com.squareup.retrofit2:retrofit:2.9.0")

Maven Repository

Now you will need to add the maven repository to your Top Level Gradle file(if you are using older android studio versions) or your settings.gradle kts for kotlin or settings.gradle for Groovy.
For Kotlin Gradle(settings.gradle.kts) add the below inside repositories{ } which is inside the dependencyResolutionManagement{ } :
For Kotlin gradle

repositories {
    maven("https://jitpack.io")
}

For groovy

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

Usage Guide

Library Usage and Information

The library has been created in a such a way that all you have to do is call an Object OpenAiCaller which contains two functions. One function for the generated response and one for generated response and user chat logs. The library also makes use of kotlin coroutines to asynchronously handle the Api calls in a background thread, so you can safely call it in your main thread or a coroutineScope like this :

For generated response :

 viewModelScope.launch {
    val response = OpenAiCaller.generateChatResponse("What is a tank like?")
     // Do something with response
 }

The for generated response with user chat logs(the stored user sessions)

viewModelScope.launch {
    val response = OpenAiCaller.generateUserChatResponse("What was my last question?", 
            arrayOf("User : What color is an Apple?", "Ai : An Apple is red in color."))
            
    // Do something with response
}

Request Validation.

Before a request can be accepted, the server will need to check the User logged in status, this is what will be gotten from the userId in the auth package. When this is retrieved, you can easily call this variable and pass it. This is what must be done to receive any reasonable response from the server.

USER_COOKIE = "The user id or generated cookie from the auth package"

Note This must be done before making any request.

This is still under development and will be migrated to use ktor and not retrofit.

OpenAi Api Package Library

02 Oct 23:58
Compare
Choose a tag to compare
Pre-release

This is the first release for the OpenAi Api Android Library, that contains the code and abstracts the logic behind the process of making a Network request to the Open Ai Api.