Coder Social home page Coder Social logo

openai-api-library's Introduction

OpenAi Api Library

v0.1.4

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. This Networking used behind this project is the kotlin ktor.

Installation Guide

Getting started

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.4")
}

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.

Examples

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
}

Error Handling

When an error occurs from a request, a HttpResponseValidator which is part of the ktor library checks the statusCode and returns an exception for each possible statusCode gotten due to an error that occurred from a request.

Example

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

we set a bunch of Exceptions for each possible statusCode, so you can easily catch or find these exceptions from the library if anything goes wrong on the server end. You can easily do this using a try catch {} block.

Example

try {
    viewModelScope.launch {
        val response = OpenApiCaller.generateChatResponse("What is a tank like?", "session="RandomGeneratedCookie"")
        // Do something with response
        Log.d(TAG, "The received response is $response")
    }
} catch(e : Exception) {
    Log.d(TAG, "Error received from server request is $e exception")
    // Example of this should be "Error received from server request is [PaymentRequiredException...]"
}

Contributors and Collaborators of this Project.

Slack Usernames.

Daniel Chinedum Iroka Lara ChinazaBlossom

Github usernames

@daniel-iroka @OmolaraIdowu @Chinazablossom

openai-api-library's People

Contributors

daniel-iroka avatar

Watchers

Williams Chukwuebuka Malachy avatar Giwa Jossy avatar  avatar

Forkers

daniel-iroka

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.