Sunday, January 29, 2023
HomeSoftware DevelopmentMethods to Construct a ChatGPT Like App in Android utilizing OpenAI API?

Methods to Construct a ChatGPT Like App in Android utilizing OpenAI API?


import android.content material.Context

import android.os.Bundle

import android.util.Log

import android.view.inputmethod.EditorInfo

import android.widget.TextView

import android.widget.TextView.OnEditorActionListener

import android.widget.Toast

import androidx.appcompat.app.AppCompatActivity

import com.android.volley.RequestQueue

import com.android.volley.Response

import com.android.volley.RetryPolicy

import com.android.volley.VolleyError

import com.android.volley.toolbox.JsonObjectRequest

import com.android.volley.toolbox.Volley

import com.google.android.materials.textfield.TextInputEditText

import org.json.JSONObject

  

class MainActivity : AppCompatActivity() {

  

    

    lateinit var responseTV: TextView

    lateinit var questionTV: TextView

    lateinit var queryEdt: TextInputEditText

  

  

    override enjoyable onCreate(savedInstanceState: Bundle?) {

        tremendous.onCreate(savedInstanceState)

        setContentView(R.format.activity_main)

        

        responseTV = findViewById(R.id.idTVResponse)

        questionTV = findViewById(R.id.idTVQuestion)

        queryEdt = findViewById(R.id.idEdtQuery)

  

        

        queryEdt.setOnEditorActionListener(OnEditorActionListener { v, actionId, occasion ->

            if (actionId == EditorInfo.IME_ACTION_SEND) {

                

                responseTV.textual content = "Please wait.."

                

                if (queryEdt.textual content.toString().size > 0) {

                    

                    getResponse(queryEdt.textual content.toString())

                } else {

                    Toast.makeText(this, "Please enter your question..", Toast.LENGTH_SHORT).present()

                }

                return@OnEditorActionListener true

            }

            false

        })

    }

  

    personal enjoyable getResponse(question: String) {

        

        questionTV.textual content = question

        queryEdt.setText("")

        

        val queue: RequestQueue = Volley.newRequestQueue(applicationContext)

        

        val jsonObject: JSONObject? = JSONObject()

        

        jsonObject?.put("mannequin", "text-davinci-003")

        jsonObject?.put("immediate", question)

        jsonObject?.put("temperature", 0)

        jsonObject?.put("max_tokens", 100)

        jsonObject?.put("top_p", 1)

        jsonObject?.put("frequency_penalty", 0.0)

        jsonObject?.put("presence_penalty", 0.0)

  

        

        val postRequest: JsonObjectRequest =

            

            object : JsonObjectRequest(Technique.POST, url, jsonObject,

                Response.Listener { response ->

                    

                    val responseMsg: String =

                        response.getJSONArray("selections").getJSONObject(0).getString("textual content")

                    responseTV.textual content = responseMsg

                },

                

                Response.ErrorListener { error ->

                    Log.e("TAGAPI", "Error is : " + error.message + "n" + error)

                }) {

                override enjoyable getHeaders(): kotlin.collections.MutableMap<kotlin.String, kotlin.String> {

                    val params: MutableMap<String, String> = HashMap()

                    

                    params["Content-Type"] = "software/json"

                    params["Authorization"] =

                        "Bearer Enter your token right here"

                    return params;

                }

            }

  

        

        postRequest.setRetryPolicy(object : RetryPolicy {

            override enjoyable getCurrentTimeout(): Int {

                return 50000

            }

  

            override enjoyable getCurrentRetryCount(): Int {

                return 50000

            }

  

            @Throws(VolleyError::class)

            override enjoyable retry(error: VolleyError) {

            }

        })

        

        queue.add(postRequest)

    }

}

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments