Skip to content

Commit

Permalink
v1.7.0-beta08
Browse files Browse the repository at this point in the history
  • Loading branch information
AndraxDev committed Mar 16, 2023
1 parent dfb905c commit e7f7378
Show file tree
Hide file tree
Showing 10 changed files with 828 additions and 403 deletions.
Empty file.
4 changes: 1 addition & 3 deletions .idea/sonarlint/issuestore/index.pb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Empty file.
2 changes: 0 additions & 2 deletions .idea/sonarlint/securityhotspotstore/index.pb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ android {
applicationId "org.teslasoft.assistant"
minSdk 28
targetSdk 33
versionCode 6
versionName "1.6.0-beta07"
versionCode 7
versionName "1.7.0-beta08"
}

buildTypes {
Expand All @@ -35,7 +35,7 @@ dependencies {
implementation 'androidx.appcompat:appcompat:1.6.1'
implementation 'com.google.android.material:material:1.9.0-alpha02'
implementation 'com.google.code.gson:gson:2.10.1'
implementation 'com.aallam.openai:openai-client:3.0.0'
implementation 'com.aallam.openai:openai-client:3.1.0'
implementation 'io.ktor:ktor-client-android:2.2.3'
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.6.1'
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.6.4'
Expand Down
Binary file modified app/release/app-release.aab
Binary file not shown.
97 changes: 83 additions & 14 deletions app/src/main/java/org/teslasoft/assistant/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,14 @@ import com.aallam.openai.api.chat.ChatCompletionChunk
import com.aallam.openai.api.chat.ChatCompletionRequest
import com.aallam.openai.api.chat.ChatMessage
import com.aallam.openai.api.chat.ChatRole
import com.aallam.openai.api.completion.CompletionRequest
import com.aallam.openai.api.completion.TextCompletion
import com.aallam.openai.api.image.ImageCreation
import com.aallam.openai.api.image.ImageSize
import com.aallam.openai.api.model.Model
import com.aallam.openai.api.model.ModelId
import com.aallam.openai.client.OpenAI
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import io.ktor.util.reflect.Type
Expand Down Expand Up @@ -79,6 +83,7 @@ class MainActivity : FragmentActivity() {
// init AI
private var ai: OpenAI? = null
private var key: String? = null
private var model = ""

// Init DALL-e
private var resolution = "512x152"
Expand Down Expand Up @@ -360,9 +365,33 @@ class MainActivity : FragmentActivity() {
finish()
} else {
ai = OpenAI(key!!)
loadModel()
}
}

private fun loadModel() {
val settings: SharedPreferences = getSharedPreferences("settings", MODE_PRIVATE)
model = settings.getString("model", "gpt-3.5-turbo").toString()
}

private suspend fun getModels() {
val models: List<Model> = ai!!.models()

var string = "";

for (m: Model in models) {
val tmp: String = m.id.toString().replace(")", "").replace("ModelId(id=", "")

if (tmp.contains("gpt")) string += "$tmp\n"
}

MaterialAlertDialogBuilder(this, R.style.App_MaterialAlertDialog)
.setTitle("Debug")
.setMessage(string)
.setPositiveButton("Close") { _, _, -> }
.show()
}

// Init image resolutions
private fun loadResolution() {
val settings: SharedPreferences = getSharedPreferences("settings", MODE_PRIVATE)
Expand Down Expand Up @@ -448,7 +477,6 @@ class MainActivity : FragmentActivity() {
recognizer?.startListening(intent)
}

@OptIn(BetaOpenAI::class)
private fun putMessage(message: String, isBot: Boolean) {
val map: HashMap<String, Any> = HashMap()

Expand All @@ -465,27 +493,55 @@ class MainActivity : FragmentActivity() {

@OptIn(BetaOpenAI::class)
private suspend fun generateResponse(request: String, shouldPronounce: Boolean) {
val chatCompletionRequest = ChatCompletionRequest(
model = ModelId("gpt-3.5-turbo-0301"),
messages = chatMessages
)
putMessage("", true)
var response = ""

try {
val completions: Flow<ChatCompletionChunk> = ai!!.chatCompletions(chatCompletionRequest)
if (model.contains("davinci") || model.contains("curie") || model.contains("babbage") || model.contains("ada")) {

putMessage("", true)
var response = ""
var tokens = 0

tokens = if (model.contains("text-davinci") || model.contains("code-davinci")) {
2048
} else 1500

val completionRequest = CompletionRequest(
model = ModelId(model),
prompt = request,
maxTokens = tokens,
echo = false
)

completions.collect { v ->
run {
if (v.choices[0].delta != null) {
if (v.choices[0].delta?.content != null) {
response += v.choices[0].delta?.content
val completions: Flow<TextCompletion> = ai!!.completions(completionRequest)

completions.collect { v ->
run {
if (v.choices[0].text != null) {
response += v.choices[0].text
messages[messages.size - 1]["message"] = "$response"
adapter?.notifyDataSetChanged()
}
}
}
} else {
val chatCompletionRequest = ChatCompletionRequest(
model = ModelId(model),
messages = chatMessages
)

val completions: Flow<ChatCompletionChunk> = ai!!.chatCompletions(chatCompletionRequest)

completions.collect { v ->
run {
if (v.choices[0].delta != null) {
if (v.choices[0].delta?.content != null) {
response += v.choices[0].delta?.content
messages[messages.size - 1]["message"] = "$response"
adapter?.notifyDataSetChanged()
}
}
}
}
}

messages[messages.size - 1]["message"] = "$response\n"
Expand All @@ -500,7 +556,20 @@ class MainActivity : FragmentActivity() {
tts!!.speak(response, TextToSpeech.QUEUE_FLUSH, null,"")
}
} catch (e: Exception) {
putMessage(e.stackTraceToString(), true)
response += "[SYSTEM ERROR] "

if (e.stackTraceToString().contains("That model does not exist")) {
response += "Looks like this model (${model}) is not available to you right now. It can be because of high demand or this model is currently in limited beta."
} else if (e.stackTraceToString().contains("Connect timeout has expired")) {
response += "Could not connect to OpenAI servers. It may happen when your Internet speed is slow or too many users are using this model at the same time. Try to switch to another model."
} else if (e.stackTraceToString().contains("This model's maximum")) {
response += "Too many tokens. It is an internal error, please report it. Also try to truncate your input. Sometimes it may help."
} else {
response += e.stackTraceToString()
}

messages[messages.size - 1]["message"] = "${response}\n"
adapter?.notifyDataSetChanged()
}

saveSettings()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package org.teslasoft.assistant.adapters

import android.annotation.SuppressLint
import android.app.Activity
import android.content.Context
import android.content.Intent
import android.content.SharedPreferences
Expand Down Expand Up @@ -91,8 +90,8 @@ class ChatAdapter(data: ArrayList<HashMap<String, Any>>?, context: FragmentActiv
val src = dataArray?.get(position)?.get("message").toString()

val markwon: Markwon = Markwon.create(mContext);

markwon.setMarkdown(message, src)
message.setTextIsSelectable(true)

imageFrame.visibility = View.GONE
message.visibility = View.VISIBLE
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import android.content.SharedPreferences.Editor
import android.net.Uri
import android.os.Bundle
import android.widget.LinearLayout
import android.widget.RadioButton
import android.widget.Toast
import androidx.fragment.app.FragmentActivity
import com.google.android.material.button.MaterialButton
Expand All @@ -28,6 +29,24 @@ class SettingsActivity : FragmentActivity() {
private var r512: MaterialButton? = null
private var r1024: MaterialButton? = null

private var gpt_35_turbo: RadioButton? = null
private var gpt_35_turbo_0301: RadioButton? = null
private var gpt_4: RadioButton? = null
private var gpt_4_0314: RadioButton? = null
private var gpt_4_32k: RadioButton? = null
private var gpt_4_32k_0314: RadioButton? = null
private var text_davinci_003: RadioButton? = null
private var text_davinci_002: RadioButton? = null
private var code_davinci_002: RadioButton? = null
private var code_cushman_001: RadioButton? = null
private var text_curie_001: RadioButton? = null
private var text_babbage_001: RadioButton? = null
private var text_ada_001: RadioButton? = null
private var davinci: RadioButton? = null
private var curie: RadioButton? = null
private var babbage: RadioButton? = null
private var ada: RadioButton? = null

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_settings)
Expand All @@ -38,17 +57,54 @@ class SettingsActivity : FragmentActivity() {
btnClearChat = findViewById(R.id.btn_clear_chat)
btnDebugMenu = findViewById(R.id.btn_debug_menu)

gpt_35_turbo = findViewById(R.id.gpt_35_turbo)
gpt_35_turbo_0301 = findViewById(R.id.gpt_35_turbo_0301)
gpt_4 = findViewById(R.id.gpt_4)
gpt_4_0314 = findViewById(R.id.gpt_4_0314)
gpt_4_32k = findViewById(R.id.gpt_4_32k)
gpt_4_32k_0314 = findViewById(R.id.gpt_4_32k_0314)
text_davinci_003 = findViewById(R.id.text_davinci_003)
text_davinci_002 = findViewById(R.id.text_davinci_002)
code_davinci_002 = findViewById(R.id.code_davinci_002)
code_cushman_001 = findViewById(R.id.code_cushman_001)
text_curie_001 = findViewById(R.id.text_curie_001)
text_babbage_001 = findViewById(R.id.text_babbage_001)
text_ada_001 = findViewById(R.id.text_ada_001)
davinci = findViewById(R.id.davinci)
curie = findViewById(R.id.curie)
babbage = findViewById(R.id.babbage)
ada = findViewById(R.id.ada)

dalleResolutions = findViewById(R.id.resolution_choices)
r256 = findViewById(R.id.r256)
r512 = findViewById(R.id.r512)
r1024 = findViewById(R.id.r1024)

loadResolution()
loadModel()

r256?.setOnClickListener { saveResolution("256x256") }
r512?.setOnClickListener { saveResolution("512x512") }
r1024?.setOnClickListener { saveResolution("1024x1024") }

gpt_35_turbo?.setOnClickListener { setModel("gpt-3.5-turbo") }
gpt_35_turbo_0301?.setOnClickListener { setModel("gpt-3.5-turbo-0301") }
gpt_4?.setOnClickListener { setModel("gpt-4") }
gpt_4_0314?.setOnClickListener { setModel("gpt-4-0314") }
gpt_4_32k?.setOnClickListener { setModel("gpt-4-32k") }
gpt_4_32k_0314?.setOnClickListener { setModel("gpt-4-32k-0314") }
text_davinci_003?.setOnClickListener { setModel("text-davinci-003") }
text_davinci_002?.setOnClickListener { setModel("text-davinci-002") }
code_davinci_002?.setOnClickListener { setModel("code-davinci-002") }
code_cushman_001?.setOnClickListener { setModel("code-cushman-001") }
text_curie_001?.setOnClickListener { setModel("text-curie-001") }
text_babbage_001?.setOnClickListener { setModel("text-babbage-001") }
text_ada_001?.setOnClickListener { setModel("text-ada-001") }
davinci?.setOnClickListener { setModel("davinci") }
curie?.setOnClickListener { setModel("curie") }
babbage?.setOnClickListener { setModel("babbage") }
ada?.setOnClickListener { setModel("ada") }

btnChangeApi?.setOnClickListener {
startActivity(Intent(this, ActivationActivity::class.java))
finish()
Expand Down Expand Up @@ -89,7 +145,7 @@ class SettingsActivity : FragmentActivity() {

silenceSwitch?.setOnCheckedChangeListener { _, isChecked ->
run {
val editor: SharedPreferences.Editor = silenceSettings.edit()
val editor: Editor = silenceSettings.edit()
if (isChecked) {
editor.putBoolean("silence_mode", true)
} else {
Expand All @@ -101,11 +157,43 @@ class SettingsActivity : FragmentActivity() {
}
}

private fun setModel(model: String) {
val settings: SharedPreferences = getSharedPreferences("settings", MODE_PRIVATE)
val editor = settings.edit()

editor.putString("model", model)
editor.apply()
}

private fun loadModel() {
val settings: SharedPreferences = getSharedPreferences("settings", MODE_PRIVATE)

when (settings.getString("model", "gpt-3.5-turbo")) { // load default model if settings not found
"gpt-3.5-turbo" -> gpt_35_turbo?.isChecked = true
"gpt-3.5-turbo-0301" -> gpt_35_turbo_0301?.isChecked = true
"gpt-4" -> gpt_4?.isChecked = true
"gpt-4-0314" -> gpt_4_0314?.isChecked = true
"gpt-4-32k" -> gpt_4_32k?.isChecked = true
"gpt-4-32k-0314" -> gpt_4_32k_0314?.isChecked = true
"text-davinci-003" -> text_davinci_003?.isChecked = true
"text-davinci-002" -> text_davinci_002?.isChecked = true
"code-davinci-002" -> code_davinci_002?.isChecked = true
"code-cushman-001" -> code_cushman_001?.isChecked = true
"text-curie-001" -> text_curie_001?.isChecked = true
"text-babbage-001" -> text_babbage_001?.isChecked = true
"text-ada-001" -> text_ada_001?.isChecked = true
"davinci" -> davinci?.isChecked = true
"curie" -> curie?.isChecked = true
"babbage" -> babbage?.isChecked = true
"ada" -> ada?.isChecked = true
else -> gpt_35_turbo?.isChecked = true
}
}

private fun loadResolution() {
val settings: SharedPreferences = getSharedPreferences("settings", MODE_PRIVATE)
val r = settings.getString("resolution", "512x512")

when (r) {
when (settings.getString("resolution", "512x512")) {
"256x256" -> r256?.isChecked = true
"512x512" -> r512?.isChecked = true
"1024x1024" -> r1024?.isChecked = true
Expand All @@ -115,7 +203,7 @@ class SettingsActivity : FragmentActivity() {

private fun saveResolution(r: String) {
val settings: SharedPreferences = getSharedPreferences("settings", MODE_PRIVATE)
val editor: SharedPreferences.Editor = settings.edit()
val editor = settings.edit()
editor.putString("resolution", r)
editor.apply()
}
Expand Down
Loading

0 comments on commit e7f7378

Please sign in to comment.