陕西西安网站建设公司,描述网站开发的过程,北京网站建设定制型报价,简单美食网站模板免费下载大家好#xff0c;我是易安#xff01; Chat GPT 是当今著名的人工智能工具#xff0c;就像聊天机器人一样。Chat GPT会回答发送给它的所有查询。今天#xff0c;我将通过集成 OpenAI API (ChatGPT)构建一个简单的类似 ChatGPT 的 android 应用程序#xff0c;我们可以在其… 大家好我是易安 Chat GPT 是当今著名的人工智能工具就像聊天机器人一样。Chat GPT会回答发送给它的所有查询。今天我将通过集成 OpenAI API (ChatGPT)构建一个简单的类似 ChatGPT 的 android 应用程序我们可以在其中提出任何问题并获得答案。 如何使 ChatGPT 像 Android 应用程序一样 详细步骤 第 1 步在 Android Studio 中创建一个新项目 要在 Android Studio 中创建新项目以 Kotlin 作为编程语言为例。 第 2 步在 build.gradle 文件中添加以下依赖项 下面是 Volley 的依赖项我们将使用它从 API 获取数据。要添加此依赖项请导航至 app Gradle Scripts build.gradle(app) 并在 dependencies 部分添加以下依赖项。我们使用 Picasso 依赖项从 URL 加载图像。 // 下一行用于 volley 库实现 com.android.volley:volley:1.2.0 添加此依赖项后同步您的项目然后转到 AndroidManifest.xml 部分。 第三步在AndroidManifest.xml文件中添加上网权限 导航到应用 AndroidManifest.xml 并向其中添加以下代码。 XML !--permissions for INTERNET--uses-permission android:nameandroid.permission.INTERNET/ 第 4 步使用 activity_main.xml 文件 导航到 app res layout activity_main.xml 并将以下代码添加到该文件。下面是 activity_main.xml 文件的代码。 XML ?xml version1.0 encodingutf-8?RelativeLayout xmlns:androidhttp://schemas.android.com/apk/res/android xmlns:apphttp://schemas.android.com/apk/res-auto xmlns:toolshttp://schemas.android.com/tools android:layout_widthmatch_parent android:layout_heightmatch_parent android:backgroundcolor/back_color tools:context.MainActivity ScrollView android:layout_widthmatch_parent android:layout_heightmatch_parent android:layout_aboveid/idTILQuery android:layout_alignParentToptrue android:layout_margin5dp android:padding5dp LinearLayout android:layout_widthmatch_parent android:layout_heightmatch_parent android:orientationvertical !-- text view for displaying question-- TextView android:idid/idTVQuestion android:layout_widthmatch_parent android:layout_heightmatch_parent android:layout_marginTop30dp android:padding4dp android:textQuestion android:textColorcolor/white android:textSize17sp / !-- text view for displaying response-- TextView android:idid/idTVResponse android:layout_widthmatch_parent android:layout_heightmatch_parent android:layout_marginTop5dp android:padding4dp android:textResponse android:textColorcolor/white android:textSize15sp / /LinearLayout /ScrollView !-- text field for asking question-- com.google.android.material.textfield.TextInputLayout android:idid/idTILQuery stylestyle/TextInputLayoutStyle android:layout_widthmatch_parent android:layout_heightwrap_content android:layout_alignParentBottomtrue android:layout_margin5dp android:hintEnter your query android:padding5dp android:textColorHintcolor/white app:hintTextColorcolor/white com.google.android.material.textfield.TextInputEditText android:idid/idEdtQuery android:layout_widthmatch_parent android:layout_heightmatch_parent android:backgroundcolor/edt_back_color android:drawableEnddrawable/ic_send android:drawableTintcolor/white android:ems10 android:imeOptionsactionSend android:importantForAutofillno android:inputTypetextEmailAddress android:textColorcolor/white android:textColorHintcolor/white android:textSize14sp / /com.google.android.material.textfield.TextInputLayout/RelativeLayout 第 5 步生成使用 API 的不记名令牌。 导航到以下URL openai获取你的api key只需使用您的电子邮件和密码注册即可。在此屏幕上单击创建新密钥以生成新密钥。生成您的密钥后我们必须将其用作制作 API 密钥的令牌。 第 6 步使用 MainActivity.kt 文件。 导航到 app java 你的应用程序包名称 MainActivity.kt 文件并向其中添加以下代码。 这里选择的模型text-davinci-003当然你可以选择其他3.5的模型 Kotlin import android.content.Contextimport android.os.Bundleimport android.util.Logimport android.view.inputmethod.EditorInfoimport android.widget.TextViewimport android.widget.TextView.OnEditorActionListenerimport android.widget.Toastimport androidx.appcompat.app.AppCompatActivityimport com.android.volley.RequestQueueimport com.android.volley.Responseimport com.android.volley.RetryPolicyimport com.android.volley.VolleyErrorimport com.android.volley.toolbox.JsonObjectRequestimport com.android.volley.toolbox.Volleyimport com.google.android.material.textfield.TextInputEditTextimport org.json.JSONObjectclass MainActivity : AppCompatActivity() { // creating variables on below line. lateinit var responseTV: TextView lateinit var questionTV: TextView lateinit var queryEdt: TextInputEditText var url https://api.openai.com/v1/completions override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) responseTV findViewById(R.id.idTVResponse) questionTV findViewById(R.id.idTVQuestion) queryEdt findViewById(R.id.idEdtQuery) queryEdt.setOnEditorActionListener(OnEditorActionListener { v, actionId, event - if (actionId EditorInfo.IME_ACTION_SEND) { responseTV.text Please wait.. if (queryEdt.text.toString().length 0) { getResponse(queryEdt.text.toString()) } else { Toast.makeText(this, Please enter your query.., Toast.LENGTH_SHORT).show() } returnOnEditorActionListener true } false }) } private fun getResponse(query: String) { questionTV.text query queryEdt.setText() val queue: RequestQueue Volley.newRequestQueue(applicationContext) val jsonObject: JSONObject? JSONObject() jsonObject?.put(model, text-davinci-003) jsonObject?.put(prompt, query) 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(Method.POST, url, jsonObject, Response.Listener { response - val responseMsg: String response.getJSONArray(choices).getJSONObject(0).getString(text) responseTV.text responseMsg }, Response.ErrorListener { error - Log.e(TAGAPI, Error is : error.message \n error) }) { override fun getHeaders(): kotlin.collections.MutableMapkotlin.String, kotlin.String { val params: MutableMapString, String HashMap() params[Content-Type] application/json params[Authorization] Bearer Enter your token here return params; } } postRequest.setRetryPolicy(object : RetryPolicy { override fun getCurrentTimeout(): Int { return 50000 } override fun getCurrentRetryCount(): Int { return 50000 } Throws(VolleyError::class) override fun retry(error: VolleyError) { } }) queue.add(postRequest) }} 最终运行结果 多年没开发安卓的我也能在很短的时间不费吹飞之力搭建出来你也赶紧去试试吧之后我还会出一些更加详细的搭建教程感谢阅读 本文由 mdnice 多平台发布