How to build an AI chatbot using Google's Gemini APIs in Kotlin in Android App?
Автор: Programmer World
Загружено: 2024-03-10
Просмотров: 1059
Описание:
This video shows the steps to build your own AI Chabot from scratch. In this demo it uses the Google's Gemini APIs to build the chatbot in Kotlin language.
It refers below document for quickstarter guide:
https://ai.google.dev/tutorials/andro...
For APIKey generation, one can use below link:
https://aistudio.google.com/app/apikey
I hope you like this video. For any questions, suggestions or appreciation please contact us at: https://programmerworld.co/contact/ or email at: [email protected]
Complete source code and other details/ steps of this video are posted in the below link:
https://programmerworld.co/android/ho...
However, the main code is copied below also for reference:
package com.programmerworld.geminiaichatbbot
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.EditText
import com.google.ai.client.generativeai.Chat
import com.google.ai.client.generativeai.GenerativeModel
import com.google.ai.client.generativeai.type.content
import kotlinx.coroutines.MainScope
import kotlinx.coroutines.launch
class MainActivity : AppCompatActivity() {
lateinit var editTextInput: EditText
lateinit var editTextOutput: EditText
lateinit var chat: Chat
var stringBuilder: StringBuilder = java.lang.StringBuilder()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
editTextInput = findViewById(R.id.editTextInput)
editTextOutput = findViewById(R.id.editTextOutput)
val generativeModel = GenerativeModel(
// For text-only input, use the gemini-pro model
modelName = "gemini-pro",
// Access your API key as a Build Configuration variable (see "Set up your API key" above)
apiKey = "AIzaSyDxTPhR0l6hQl5VrKjRsMn8aa-sSFL3kDg"
)
chat = generativeModel.startChat(
history = listOf(
content(role = "user") { text("Hello, I have 2 dogs in my house.") },
content(role = "model") { text("Great to meet you. What would you like to know?") }
)
)
stringBuilder.append("Hello, I have 2 dogs in my house.\n\n")
stringBuilder.append("Great to meet you. What would you like to know?\n\n")
editTextOutput.setText(stringBuilder.toString())
}
public fun buttonSendChat(view: View){
stringBuilder.append(editTextInput.text.toString() + "\n\n")
MainScope().launch {
var result = chat.sendMessage(editTextInput.text.toString())
stringBuilder.append(result.text + "\n\n")
editTextOutput.setText(stringBuilder.toString())
editTextInput.setText("")
}
}
}
--
Повторяем попытку...
Доступные форматы для скачивания:
Скачать видео
-
Информация по загрузке: