中文亚洲精品无码_熟女乱子伦免费_人人超碰人人爱国产_亚洲熟妇女综合网

當(dāng)前位置: 首頁(yè) > news >正文

新疆烏魯木齊醫(yī)院網(wǎng)站建設(shè)東莞網(wǎng)絡(luò)推廣營(yíng)銷

新疆烏魯木齊醫(yī)院網(wǎng)站建設(shè),東莞網(wǎng)絡(luò)推廣營(yíng)銷,WordPress 大數(shù)據(jù),小程序如何開(kāi)發(fā)制作大家好,我是易安! Chat GPT 是當(dāng)今著名的人工智能工具,就像聊天機(jī)器人一樣。Chat GPT會(huì)回答發(fā)送給它的所有查詢。今天,我將通過(guò)集成 OpenAI API (ChatGPT)構(gòu)建一個(gè)簡(jiǎn)單的類似 ChatGPT 的 android 應(yīng)用程序,我們可以在其…

大家好,我是易安!

Chat GPT 是當(dāng)今著名的人工智能工具,就像聊天機(jī)器人一樣。Chat GPT會(huì)回答發(fā)送給它的所有查詢。今天,我將通過(guò)集成 OpenAI API (ChatGPT)構(gòu)建一個(gè)簡(jiǎn)單的類似 ChatGPT 的 android 應(yīng)用程序,我們可以在其中提出任何問(wèn)題并獲得答案。

如何使 ChatGPT 像 Android 應(yīng)用程序一樣
如何使 ChatGPT 像 Android 應(yīng)用程序一樣

詳細(xì)步驟

第 1 步:在 Android Studio 中創(chuàng)建一個(gè)新項(xiàng)目

要在 Android Studio 中創(chuàng)建新項(xiàng)目,以 Kotlin 作為編程語(yǔ)言為例。

第 2 步:在 build.gradle 文件中添加以下依賴項(xiàng)

下面是 Volley 的依賴項(xiàng),我們將使用它從 API 獲取數(shù)據(jù)。要添加此依賴項(xiàng),請(qǐng)導(dǎo)航至 app > Gradle Scripts > build.gradle(app) 并在 dependencies 部分添加以下依賴項(xiàng)。我們使用 Picasso 依賴項(xiàng)從 URL 加載圖像。

//?下一行用于?volley?庫(kù)
實(shí)現(xiàn)?'com.android.volley:volley:1.2.0'

添加此依賴項(xiàng)后,同步您的項(xiàng)目,然后轉(zhuǎn)到 AndroidManifest.xml 部分。

第三步:在AndroidManifest.xml文件中添加上網(wǎng)權(quán)限

導(dǎo)航到應(yīng)用 > AndroidManifest.xml 并向其中添加以下代碼。

  • XML
<!--permissions?for?INTERNET-->
<uses-permission?android:name="android.permission.INTERNET"/>

第 4 步:使用 activity_main.xml 文件

導(dǎo)航到 app > res > layout > activity_main.xml 并將以下代碼添加到該文件。下面是 activity_main.xml 文件的代碼。

  • XML
<?xml?version="1.0"?encoding="utf-8"?>
<RelativeLayout?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"
?android:background="@color/back_color"
?tools:context=".MainActivity">

?<ScrollView
??android:layout_width="match_parent"
??android:layout_height="match_parent"
??android:layout_above="@id/idTILQuery"
??android:layout_alignParentTop="true"
??android:layout_margin="5dp"
??android:padding="5dp">

??<LinearLayout
???android:layout_width="match_parent"
???android:layout_height="match_parent"
???android:orientation="vertical">

???<!--?text?view?for?displaying?question-->
???<TextView
????android:id="@+id/idTVQuestion"
????android:layout_width="match_parent"
????android:layout_height="match_parent"
????android:layout_marginTop="30dp"
????android:padding="4dp"
????android:text="Question"
????android:textColor="@color/white"
????android:textSize="17sp"?/>

???<!--?text?view?for?displaying?response-->
???<TextView
????android:id="@+id/idTVResponse"
????android:layout_width="match_parent"
????android:layout_height="match_parent"
????android:layout_marginTop="5dp"
????android:padding="4dp"
????android:text="Response"
????android:textColor="@color/white"
????android:textSize="15sp"?/>
??</LinearLayout>

?</ScrollView>
?<!--?text?field?for?asking?question-->
?<com.google.android.material.textfield.TextInputLayout
??android:id="@+id/idTILQuery"
??style="@style/TextInputLayoutStyle"
??android:layout_width="match_parent"
??android:layout_height="wrap_content"
??android:layout_alignParentBottom="true"
??android:layout_margin="5dp"
??android:hint="Enter?your?query"
??android:padding="5dp"
??android:textColorHint="@color/white"
??app:hintTextColor="@color/white">

??<com.google.android.material.textfield.TextInputEditText
???android:id="@+id/idEdtQuery"
???android:layout_width="match_parent"
???android:layout_height="match_parent"
???android:background="@color/edt_back_color"
???android:drawableEnd="@drawable/ic_send"
???android:drawableTint="@color/white"
???android:ems="10"
???android:imeOptions="actionSend"
???android:importantForAutofill="no"
???android:inputType="textEmailAddress"
???android:textColor="@color/white"
???android:textColorHint="@color/white"
???android:textSize="14sp"?/>
?</com.google.android.material.textfield.TextInputLayout>
</RelativeLayout>

第 5 步:生成使用 API 的不記名令牌。

導(dǎo)航到以下URL (openai獲取你的api key),只需使用您的電子郵件和密碼注冊(cè)即可。在此屏幕上單擊創(chuàng)建新密鑰以生成新密鑰。生成您的密鑰后,我們必須將其用作制作 API 密鑰的令牌。

第 6 步:使用 MainActivity.kt 文件。

導(dǎo)航到 app > java > 你的應(yīng)用程序包名稱 > MainActivity.kt 文件并向其中添加以下代碼。

這里選擇的模型text-davinci-003,當(dāng)然你可以選擇其他3.5的模型

  • Kotlin
import?android.content.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.material.textfield.TextInputEditText
import?org.json.JSONObject

class?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()
????}
????return@OnEditorActionListener?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.MutableMap<kotlin.String,?kotlin.String>?{
?????val?params:?MutableMap<String,?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)
?}
}

最終運(yùn)行結(jié)果:

alt

多年沒(méi)開(kāi)發(fā)安卓的我,也能在很短的時(shí)間不費(fèi)吹飛之力搭建出來(lái),你也趕緊去試試吧!之后我還會(huì)出一些更加詳細(xì)的搭建教程,感謝閱讀!

本文由 mdnice 多平臺(tái)發(fā)布

http://www.risenshineclean.com/news/61950.html

相關(guān)文章:

  • 給小公司做網(wǎng)站賺錢么江西seo推廣
  • 西寧做腋臭北大網(wǎng)站lseo排名快速刷
  • 怎么做網(wǎng)站超鏈接外媒頭條最新消息
  • 手機(jī)視頻網(wǎng)站開(kāi)發(fā)廠房網(wǎng)絡(luò)推廣平臺(tái)
  • 深圳龍崗做網(wǎng)站公司哪家好seo排名首頁(yè)
  • 網(wǎng)站界面設(shè)計(jì)總結(jié)活動(dòng)策劃
  • 宿遷做網(wǎng)站電話市場(chǎng)調(diào)研報(bào)告模板ppt
  • wordpress響應(yīng)式主題制作教程seo社區(qū)
  • asp動(dòng)態(tài)網(wǎng)站開(kāi)發(fā)課后答案廣告引流推廣平臺(tái)
  • 網(wǎng)站開(kāi)發(fā)的方案最火的網(wǎng)絡(luò)銷售平臺(tái)
  • 看房子建設(shè)進(jìn)度的網(wǎng)站在線客服系統(tǒng)平臺(tái)有哪些
  • 網(wǎng)站建設(shè)相關(guān)業(yè)務(wù)百度搜索推廣方法
  • 公司網(wǎng)站去哪里做網(wǎng)站關(guān)鍵詞排名查詢
  • 鄭州市網(wǎng)站百度指數(shù)功能模塊
  • 2019廣東省工程建設(shè)網(wǎng)站搜索引擎優(yōu)化代理
  • 任何判斷網(wǎng)站SEO做的好壞中小企業(yè)管理培訓(xùn)課程
  • 蘇州網(wǎng)站設(shè)計(jì)價(jià)格seo整站怎么優(yōu)化
  • 設(shè)計(jì)圖片logo免費(fèi)安康地seo
  • 做招聘網(wǎng)站需要營(yíng)業(yè)執(zhí)照嗎網(wǎng)站改版seo建議
  • 昌吉建設(shè)局網(wǎng)站廣州seo推廣營(yíng)銷
  • 成品網(wǎng)站源碼多少錢云建站
  • 網(wǎng)站設(shè)計(jì)日志seo搜索引擎優(yōu)化策略
  • 邢臺(tái)網(wǎng)站建設(shè)服務(wù)商網(wǎng)絡(luò)軟文怎么寫(xiě)
  • 網(wǎng)站建設(shè)售后培訓(xùn)東莞疫情最新消息今天新增
  • 垂直類網(wǎng)站怎么做推廣發(fā)布外鏈的平臺(tái)有哪些
  • 專業(yè)做簡(jiǎn)歷的網(wǎng)站百度站長(zhǎng)平臺(tái)app
  • 網(wǎng)站建設(shè) 數(shù)據(jù)上傳 查詢中國(guó)十大網(wǎng)站
  • 單頁(yè)面網(wǎng)站做排名網(wǎng)絡(luò)推廣外包搜索手機(jī)蛙軟件
  • 當(dāng)日網(wǎng)站收錄查詢統(tǒng)計(jì)網(wǎng)店推廣軟文范例
  • 收到一張網(wǎng)站服務(wù)費(fèi)怎么做憑證在線的crm系統(tǒng)軟件