位置: IT常识 - 正文

如何在Android中使用OpenAI API构建一个ChatGPT类的应用程序(如何在Android中找到关闭的文件)

编辑:rootadmin
如何在Android中使用OpenAI API构建一个ChatGPT类的应用程序

推荐整理分享如何在Android中使用OpenAI API构建一个ChatGPT类的应用程序(如何在Android中找到关闭的文件),希望有所帮助,仅作参考,欢迎阅读内容。

文章相关热门搜索词:如何在Android中创建一个手机,如何在android中进行单元测试,如何在Android中找到关闭的文件,如何在Android中使用线性布局,如何在android中创建新的,如何在Android中找到关闭的文件,如何在android中进行单元测试,如何在android中恢复已删除的文件?,内容如对您有帮助,希望把文章链接给更多的朋友!

ChatGPT是当今著名的人工智能工具之一,类似于聊天机器人。这个聊天机器人回答所有发送给它的查询。在本文中,我们将通过集成OpenAI API(ChatGPT)来构建一个简单的类似ChatGPT的android应用程序,我们可以在其中提出任何问题并得到适当的答案

我已经创建了一个示例应用程序,并将看看它的输出,然后我们将进一步在android studio中创建一个新项目。

一步一步的实现步骤1:在Android Studio中创建一个新项目

要在Android Studio中创建一个新项目,请参考How to Create/Start a New Project in Android Studio。注意,选择Kotlin作为编程语言。

步骤2:添加以下依赖项build.gradle 文件

下面是Volley的依赖关系,我们将使用它从API获取数据。要添加此依赖项,app > Gradle Scripts > build.gradle(app),并在依赖项部分添加以下依赖项。我们已经使用了Picasso依赖项来从URL加载图像

// below line is used for volley library

implementation ‘com.android.volley:volley:1.2.0’

如何在Android中使用OpenAI API构建一个ChatGPT类的应用程序(如何在Android中找到关闭的文件)

添加这个依赖后,同步你的项目,现在转移到AndroidManifest.xml部分。

步骤3:在AndroidManifest.xml文件中添加网络访问权限

app > AndroidManifest.xml,并将以下代码添加到其中

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

步骤4:使用activity_main.xml文件

app > res > layout > activity_main.xml并将以下代码添加到该文件中。下面是activity_main.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"><ScrollViewandroid: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"><LinearLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"><!-- text view for displaying question--><TextViewandroid: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--><TextViewandroid: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.TextInputLayoutandroid: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.TextInputEditTextandroid: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的记名令牌。

导航到下面的 URL,只需注册您的电子邮件和密码。在此屏幕上单击Create a new secret key以生成新密钥。一旦你的密钥生成,我们必须使用它作为一个令牌,使我们的API密钥。

步骤6:使用MainActivity。kt文件。

导航到app > java > your app’s package name > MainActivity.kt 文件,并添加下面的代码。代码中添加了注释以详细了解它。

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: TextViewlateinit var questionTV: TextViewlateinit var queryEdt: TextInputEditTextvar url = "https://api.openai.com/v1/completions"override fun onCreate(savedInstanceState: Bundle?) {super.onCreate(savedInstanceState)setContentView(R.layout.activity_main)// initializing variables on below line.responseTV = findViewById(R.id.idTVResponse)questionTV = findViewById(R.id.idTVQuestion)queryEdt = findViewById(R.id.idEdtQuery)// adding editor action listener for edit text on below line.queryEdt.setOnEditorActionListener(OnEditorActionListener { v, actionId, event ->if (actionId == EditorInfo.IME_ACTION_SEND) {// setting response tv on below line.responseTV.text = "Please wait.."// validating textif (queryEdt.text.toString().length > 0) {// calling get response to get the response.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) {// setting text on for question on below line.questionTV.text = queryqueryEdt.setText("")// creating a queue for request queue.val queue: RequestQueue = Volley.newRequestQueue(applicationContext)// creating a json object on below line.val jsonObject: JSONObject? = JSONObject()// adding params to json object.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)// on below line making json object request.val postRequest: JsonObjectRequest =// on below line making json object request.object : JsonObjectRequest(Method.POST, url, jsonObject,Response.Listener { response ->// on below line getting response message and setting it to text view.val responseMsg: String =response.getJSONArray("choices").getJSONObject(0).getString("text")responseTV.text = responseMsg},// adding on error listenerResponse.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()// adding headers on below line.params["Content-Type"] = "application/json"params["Authorization"] ="Bearer Enter your token here"return params;}}// on below line adding retry policy for our request.postRequest.setRetryPolicy(object : RetryPolicy {override fun getCurrentTimeout(): Int {return 50000}override fun getCurrentRetryCount(): Int {return 50000}@Throws(VolleyError::class)override fun retry(error: VolleyError) {}})// on below line adding our request to queue.queue.add(postRequest)}}输出参考

https://media.geeksforgeeks.org/wp-content/uploads/20230118003207/Screenrecorder-2023-01-18-00-28-57-186.mp4?_=1

参考文章:https://www.geeksforgeeks.org/how-to-build-a-chatgpt-like-app-in-android-using-openai-api/

本文链接地址:https://www.jiuchutong.com/zhishi/299788.html 转载请保留说明!

上一篇:【学Vue就跟玩一样】组件-单文件组件(vue该怎么学)

下一篇:谷歌研究员走火入魔事件曝光:认为AI已具备人格,被罚带薪休假,聊天记录让网友San值狂掉...(谷歌研究院)

  • 城建税的计税依据是增值税和消费税的和吗
  • 结转和分配制造费用会计分录
  • 无票收入也需要开票吗
  • 收入和费用类科目一般设置为什么辅助核算
  • 2019年所得税季度增值税怎么报
  • 应付职工薪酬代扣款项账务处理
  • 收到货款开的增值税发票怎么写分录
  • 餐补和车补可以税前扣除吗?
  • 实收资本未认缴资本还用填写吗
  • 收到的货款与发票数不符,怎么做账
  • 对方收到发票不付款怎么办
  • 企业固定资产出租取得的收入属于
  • 公司给员工的商业保险
  • 不动产60%和40%抵扣时间
  • 13个点的发票实际交多少钱
  • 对公银行转款备注重要吗
  • 为什么负债不等于亏损
  • 事业单位事业收入和经营收入要上缴财政
  • 股权转让流程详解
  • 收支利息税务如何处理
  • 房地产项目代建模式
  • Excel规划求解怎么做
  • 成本的分类有哪些如何分类
  • xbox无法连接无线网络
  • Mysql的GROUP_CONCAT()函数使用方法
  • ·exe是什么文件
  • windows无法连接到system Events
  • php字符串型数据的定义方式
  • 外籍人员一次性奖金
  • 减免税款账务处理
  • 奥克拉库克湾
  • php oracle 连接池
  • 公司法人和股东哪个承担的责任大
  • sql数据库语句基本语法
  • php邮件发送类
  • Web前端开发知识点总结
  • 公司抽奖奖品怎么做账
  • 公司承担员工的费用,员工违规吗
  • centos7自带yum吗
  • 生产费用在完工产品和在产品之间的方法
  • 事业单位成本核算具体指引—公立医院
  • SQL SERVER 2000 9003错误的解决方法(只适用于SQL2000)
  • android 矩阵运算
  • 政府补助的界定
  • 一般纳税人认定书是什么样子
  • 用友软件数据导出
  • 个人独资企业个税怎么交
  • 实收资本的会计编码
  • 应付账款长时间收不回怎么处理呢
  • 公司代缴的社保能不能取出来
  • 补缴以前年度的增值税以及附加税,还有罚款的账务处理
  • 管理不善造成的存货盘亏损失计入什么科目
  • 工会经费计提比例0.8%和2%有何区别
  • SQL 统计一个数据库中所有表记录的数量
  • win10系统如何关闭窗口特效
  • assoc.exe
  • win7系统电脑怎么连接热点
  • splash.exe - splash是什么进程 有什么作用
  • win10系统出现问题怎么办
  • windows局域网共享文件用的什么协议
  • win7系统怎么关闭屏幕保护
  • linux命令使用例子
  • 电脑导航阻止怎么办
  • 飞完整版歌曲
  • node执行js文件的命令是什么
  • div 绝对位置
  • android自动开关机失效
  • linux中date命令详解
  • DOS命令删除文件夹
  • 网页上面的收藏夹显示
  • 怎么把两个文件中的内容合并
  • python爬虫403解决方案
  • js判断上传图片宽高大小
  • js设计模型
  • 相机调取失败
  • jquery判断数据类型
  • 河北省国税局发展前景
  • 北京税务师取消了吗
  • 深圳市国家税务局网站
  • 科研用地是否缴纳土地使用税
  • 免责声明:网站部分图片文字素材来源于网络,如有侵权,请及时告知,我们会第一时间删除,谢谢! 邮箱:opceo@qq.com

    鄂ICP备2023003026号

    网站地图: 企业信息 工商信息 财税知识 网络常识 编程技术

    友情链接: 武汉网站建设