Skip to content

Improved Error handling and added Test cases

Latest
Compare
Choose a tag to compare
@daniel-iroka daniel-iroka released this 05 Oct 21:24
· 3 commits to ktor-migration since this release

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