0% found this document useful (0 votes)
48 views

Testgpt

The document contains code for an Android application that communicates with the OpenAI API to retrieve responses to user questions. It includes layout XML with TextViews and an EditText for input/output, and a MainActivity class that handles sending the request, receiving the response, and displaying it. The activity finds the relevant views, sets a click listener to send the question on button press, and makes the API request using OkHttp to get a response which is then passed to a callback and displayed.

Uploaded by

isdevacademy2005
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
48 views

Testgpt

The document contains code for an Android application that communicates with the OpenAI API to retrieve responses to user questions. It includes layout XML with TextViews and an EditText for input/output, and a MainActivity class that handles sending the request, receiving the response, and displaying it. The activity finds the relevant views, sets a click listener to send the question on button press, and makes the API request using OkHttp to get a response which is then passed to a callback and displayed.

Uploaded by

isdevacademy2005
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

<?xml version="1.0" encoding="utf-8"?

>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:padding="20dp"
android:orientation="vertical"
android:background="#0E193D"
android:gravity="bottom">

<TextView
android:id="@+id/txt_response"
android:layout_width="match_parent"
android:layout_height="250dp"
android:textStyle="bold"
android:textColor="@color/white"
android:textSize="20sp"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="20dp"
/>
<TextView
android:id="@+id/txt_my_response"
android:layout_width="match_parent"
android:layout_height="250dp"
android:textStyle="bold|italic"
android:textColor="#FFC107"
android:textSize="20sp"
/>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal">
<EditText
android:id="@+id/edt_question"
android:layout_width="330dp"
android:layout_height="wrap_content"
android:hint="Enter your question here !"
android:textColorHint="@color/white"
android:textColor="@color/white"
android:textSize="20dp"
android:textStyle="bold"
/>
<ImageView
android:id="@+id/btn_send"
android:layout_width="50dp"
android:layout_height="50dp"
android:src="@drawable/ic_send"

/>

</LinearLayout>
</LinearLayout>

package pro.developer.chatgpt

import android.annotation.SuppressLint
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.widget.Button
import android.widget.EditText
import android.widget.TextView
import okhttp3.*
import okhttp3.MediaType.Companion.toMediaType
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.RequestBody.Companion.toRequestBody
import org.json.JSONArray
import org.json.JSONObject
import java.io.IOException

class MainActivity : AppCompatActivity() {

@SuppressLint("CutPasteId", "WrongViewCast")
private val client = OkHttpClient()

@SuppressLint("WrongViewCast")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

val edt_question = findViewById<EditText>(R.id.edt_question)


val btn_send = findViewById<Button>(R.id.btn_send)
val txt_myresp = findViewById<TextView>(R.id.txt_my_response)
val txt_resp = findViewById<TextView>(R.id.txt_response)

btn_send.setOnClickListener {
val question = edt_question.text.toString()
txt_myresp.setText("$question")
getResponse(question) { response ->
runOnUiThread {
txt_resp.text = response
}
}

}
}

fun getResponse(question: String, callback: (String) -> Unit) {

val apiKey = "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"


val url = "https://api.openai.com/v1/completions"
val requestBody="""
{
"prompt": "$question",
"max_tokens": 500,
"temperature": 0
}
""".trimIndent()

val request = Request.Builder()


.url(url)
.addHeader("Content-Type", "application/json")
.addHeader("Authorization", "Bearer $apiKey")

.post(requestBody.toRequestBody("application/json".toMediaTypeOrNull()))
.build()

client.newCall(request).enqueue(object : Callback {
override fun onFailure(call: Call, e: IOException) {
Log.e("error","API failed",e)
}

override fun onResponse(call: Call, response: Response) {


val body=response.body?.string()
if (body != null) {
Log.v("data",body)
}
else{
Log.v("data","empty")
}
val jsonObject= JSONObject(body)
val jsonArray: JSONArray =jsonObject.getJSONArray("choices")
val textResult=jsonArray.getJSONObject(0).getString("text")
callback(textResult)
}
})
}

You might also like