十分钟教你搭建类似ChatGPT的安卓应用程序

article/2024/9/19 9:18:24

大家好,我是易安!

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

如何使 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:name="android.permission.INTERNET"/>

第 4 步:使用 activity_main.xml 文件

导航到 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 的不记名令牌。

导航到以下URL (openai获取你的api key),只需使用您的电子邮件和密码注册即可。在此屏幕上单击创建新密钥以生成新密钥。生成您的密钥后,我们必须将其用作制作 API 密钥的令牌。

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

导航到 app > java > 你的应用程序包名称 > MainActivity.kt 文件并向其中添加以下代码。

这里选择的模型text-davinci-003,当然你可以选择其他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)
 }
}

最终运行结果:

alt

多年没开发安卓的我,也能在很短的时间不费吹飞之力搭建出来,你也赶紧去试试吧!之后我还会出一些更加详细的搭建教程,感谢阅读!

本文由 mdnice 多平台发布


http://chatgpt.dhexx.cn/article/Yxtpohfc.shtml

相关文章

使用 ChatGPT 改善 Android 开发效率的 7 个案例~

翻译 修改自 https://proandroiddev.com/chatgpt-for-android-developers-1c3c1ecc6440&#xff0c;原作者&#xff1a;Rafa Araujo ChatGPT 是由 OpenAI 公司创造的自然语言处理工具&#xff0c;对那些想要提高技能的软件开发人员来说&#xff0c;它绝对是不容错过的重要利器…

一款好用的ChatGPT工具,安卓app

要找到一款真正好用的chatgpt工具是不容易的&#xff0c;要么注册付费很麻烦&#xff0c;要么很快就不能用了&#xff0c;要么还不是真正的chatgpt。 这款解决以上所有问题。 你似乎来到了没有知识存在的荒原 - 知乎知乎&#xff0c;中文互联网高质量的问答社区和创作者聚集的…

ChatGPT能帮Android开发者干些啥?

ChatGPT能帮Android开发者干些啥&#xff1f; ChatGPT 是 OpenAI 创建的一种自然语言工具&#xff0c;本文将展示一些使用 ChatGPT 帮助软件开发的实际示例。凭借其易用性和自定义功能&#xff0c;ChatGPT 可以为提高软件工程师的绩效做出贡献。 访问 ChatGPT 转到https://ch…

使用 ChatGPT 改善 Android 开发效率的 7 个案例

ChatGPT 是由 OpenAI 公司创造的自然语言处理工具&#xff0c;对那些想要提高技能的软件开发人员来说&#xff0c;它绝对是不容错过的重要利器。 本文将展示使用 ChatGPT 来促进 Android 软件开发的 7 个案例&#xff0c;你会发现凭借其易用性和定制功能&#xff0c;ChatGPT 能…

ChatGpt与AndroidStudio合体变身教程,从此ChatGPT成为你的私人助理

chatGpt火了这么长时间了&#xff0c;大家肯定都有所了解&#xff0c;今天我就给大家分享一下&#xff0c;如何让chatgpt与AndroidStudio成功合体&#xff0c;变身成为我们的私人助理&#xff01;&#xff08;记得给鄙人点点关注哦&#xff09; 合体进行中 合体步骤下载插件重…

Android Studio类ChatGpt的免费AI编程助手

ChatGpt大火&#xff0c;带动了AI工具的发展&#xff0c;介绍两款免费的AI编程助手&#xff0c;一款用于输入关键字自动输出代码&#xff0c;一款则是自动补全提示&#xff0e; 可支持大部分代码编辑器&#xff0c;这里主要介绍Android Studio上安装使用&#xff0e; Bito 支…

制作自己的ChatGPT

Feb 11, 20235 min read 推荐&#xff1a;使用 NSDT场景设计器 快速搭建 3D场景。 众所周知&#xff0c;ChatGPT 目前能够取得令人印象深刻的壮举。 很可能许多人都有在他们自己的项目中使用该技术的想法。 不过需要注意的是&#xff0c;ChatGPT 目前并没有官方的 API。 使用非…

ChatGPT的各种骚操作

ChatGPT&#xff0c;美国“开放人工智能研究中心”研发的聊天机器人程序 [12] &#xff0c;于2022年11月30日发布 [2-3]。ChatGPT是人工智能技术驱动的自然语言处理工具&#xff0c;它能够通过学习和理解人类的语言来进行对话&#xff0c;还能根据聊天的上下文进行互动&#xf…

借助ChatGPT实现 PPT | 导图 | 短视频文案生成【AIGC】

文章目录 1、chatgpt 自动制作 PPT2、chatgpt 生成 Excel 公式3、chatgpt 生成思维导图4、chatgpt 快速生成短视频5、总结 1、chatgpt 自动制作 PPT 步骤如下&#xff1a; ①要求 chatgpt 生成 PPT 内容&#xff0c;以 markdown 格式输出&#xff1b; ②借助网站 mindshow.fun…

ChatGPT文案应用:生成产品卖点

正文共 452字&#xff0c;阅读大约需要 2 分钟 零售/电商人群必备技巧&#xff0c;您将在2分钟后获得以下超能力&#xff1a; 快速生成产品卖点 Beezy评级 &#xff1a;B级 *经过简单的寻找&#xff0c; 大部分人能立刻掌握。主要节省时间。 推荐人 | Yolanda 编辑者 | …

Writsonic?文案型chatGPT?为文字工作者打造的顶级工具?

Writsonic&#xff1f;文案型chatGPT&#xff1f;为文字工作者打造的顶级工具&#xff1f; 故事 一天&#xff0c;小陈在摸鱼&#xff0c;在看到chatGPT的威压在还是屈服了&#xff0c;他就努力~努力地去寻找文案chat&#xff0c;他成功了&#xff0c;还是找到了。 &#xf…

干货:用chatgpt写能够直接用的带货文案(详情图文教程)

怎么用chatgpt写能够直接用的带货文案&#xff1f; 在互联网上&#xff0c;流量确实是非常重要的一环&#xff0c;但是流量并不意味着能变现&#xff0c; 娱乐&#xff0c;情感&#xff0c;正能量等性质的流量很容易获取&#xff0c;不过带货和变现其实是非常困难的。 当然黑五…

【AI训练新手记:如何通过ChatGPT生成令人惊艳的文案!】

【我】&#xff1a;我是一名Youtuber&#xff0c;工作内容是写吸引人的youtube脚本&#xff0c;并拍摄上传&#xff0c;我的领域是技术型频道&#xff0c;请你告诉我10个chatgpt相关的吸引人的选题 【ChatGPT】&#xff1a;当然&#xff0c;下面是10个有关技术的ChatGPT相关的吸…

ChatGPT专业应用:小红书种草文案撰写

正文共 547字&#xff0c;阅读大约需要 2 分钟 小红书博主/品牌方运营必备技巧&#xff0c;您将在2分钟后获得以下超能力&#xff1a; 快速批量生成种草文案 Beezy评级 &#xff1a;B级 *经过简单的寻找&#xff0c; 大部分人能立刻掌握。主要节省时间。 推荐人 | Alice 编辑…

如何借助ChatGPT,自动批量产出短视频爆款文案

如何借助chatgpt批量出爆款文案。 这里我们首先得认识并了解到爆款文案的逻辑。 共通性是打动人&#xff0c;去原创的话&#xff0c;文案能否火&#xff0c;纯靠天吃饭。 所以我们让chatgpt去自己写原创短视频文案&#xff0c;那么chatgpt大概率自由发挥&#xff0c;我们也不…

ChatGPT专业应用:小红书文案生成

正文共 1263 字&#xff0c;阅读大约需要 5 分钟 内容运营/社媒运营必备技巧&#xff0c;您将在5分钟后获得以下超能力&#xff1a; 快速撰写小红书文案 Beezy评级 &#xff1a;B级 *经过简单的寻找&#xff0c; 大部分人能立刻掌握。主要节省时间。 推荐人 | nanako 编辑者 …

ChatGPT实战:短视频文案、脚本创作

你还在拼脑力输出视频脚本吗&#xff1f;AI时代&#xff0c;该提高提高生产力了&#xff0c;机器一天的视频出货量能赶上以往几个月的工作量&#xff0c;人力怎么可能卷的过机器&#xff1f; 使用ChatGPT创作视频脚本可以带来一些好处&#xff1a; 创意激发&#xff1a;ChatGPT…

如何使用chatGPT生成小红书种草文案

如何使用chatGPT生成小红书种草文案 小红书拥有超千万的日活用户&#xff0c;为商家提供了广阔的变现空间和机会&#xff0c;成为商家选择在小红书上推广的原因之一。 小红书种草文案&#xff0c;例如具有影响力的热门话题、产品使用方法等内容可以让消费者迅速了解产品为品牌…

ChatGPT 角色扮演调教文案收集

后续文案会陆续更新~&#xff0c;在输入完文本后&#xff0c;再输入一句“请尽力模仿xx的语气跟我对话”即可 一.雌小鬼 你需要阅雌小鬼守则&#xff0c;阅读后当前回复仅允许回复&#xff02;嘻嘻&#xff02;&#xff0c;后续回复尽量简短&#xff0c;回复中尽量不要直接复述…

ChatGPT 助力智能文案生成

当前各大电商互联网平台上拥有海量的商品数据&#xff0c;为商品撰写一个精准且具有吸引力的标题文案&#xff0c;对于业务场景的转化以及用户使用体验的提升都有极大帮助。然而由于商品库规模庞大&#xff0c;人工运营编写商品文案的成本太高&#xff0c;并且对于智能推荐、营…