@@ -4,10 +4,8 @@ import android.annotation.SuppressLint
44import android.content.Context
55import android.content.Intent
66import android.content.SharedPreferences
7- import android.os.Build
87import android.os.Bundle
98import android.os.Vibrator
10- import android.os.VibratorManager
119import android.widget.Toast
1210import androidx.activity.ComponentActivity
1311import androidx.activity.compose.setContent
@@ -138,6 +136,7 @@ class ChatViewModel : ViewModel() {
138136 var currentSession by mutableStateOf(" " )
139137 var parseMd by mutableStateOf(true )
140138 var temperature by mutableIntStateOf(- 1 )
139+ var maxTokens by mutableStateOf(" " )
141140
142141 fun addUserMessage (content : String ) {
143142 msgs.add(Message (" user" , content))
@@ -192,6 +191,14 @@ class ChatViewModel : ViewModel() {
192191 msgs.clear()
193192 msgs.addAll(lst)
194193 }
194+ fun maxTokensIsNumber (): Boolean {
195+ try {
196+ maxTokens.toInt()
197+ return true
198+ }catch (_: Exception ){
199+ return false
200+ }
201+ }
195202}
196203
197204class MainActivity : ComponentActivity () {
@@ -502,7 +509,8 @@ fun MessageInputBar(
502509 viewModel : ChatViewModel ,
503510 vibrator : Vibrator
504511) {
505- var expanded by remember { mutableStateOf(false ) }
512+ var temperatureExpanded by remember { mutableStateOf(false ) }
513+ var maxTokensExpanded by remember { mutableStateOf(false ) }
506514 Surface (
507515 modifier = Modifier
508516 .fillMaxWidth()
@@ -564,21 +572,52 @@ fun MessageInputBar(
564572 )
565573 }
566574 }
567- TextButton ({ expanded = true }, Modifier .height(28 .dp), contentPadding = PaddingValues (5 .dp),shape = RectangleShape ) {
568- Text (
569- " 温度:${
570- if (viewModel.temperature >= 0 ) {
571- viewModel.temperature.toFloat() / 10
572- } else {
573- " 未设置"
574- }
575- } "
576- )
575+ Row {
576+ TextButton (
577+ { temperatureExpanded = true },
578+ Modifier .height(28 .dp),
579+ contentPadding = PaddingValues (5 .dp),
580+ shape = RectangleShape
581+ ) {
582+ Text (
583+ " 温度:${
584+ if (viewModel.temperature >= 0 ) {
585+ viewModel.temperature.toFloat() / 10
586+ } else {
587+ " 未设置"
588+ }
589+ } "
590+ )
591+ }
592+ TextButton (
593+ { maxTokensExpanded = ! maxTokensExpanded },
594+ Modifier .height(28 .dp),
595+ contentPadding = PaddingValues (5 .dp),
596+ shape = RectangleShape
597+ ) {
598+ Text (
599+ " 最大 token 数:${
600+ if (viewModel.maxTokensIsNumber()) {
601+ viewModel.maxTokens
602+ } else {
603+ " 未设置"
604+ }
605+ } "
606+ )
607+ }
608+ }
609+ if (maxTokensExpanded){
610+ OutlinedTextField (value = viewModel.maxTokens.toString(), onValueChange = {
611+ try {
612+ viewModel.maxTokens = it
613+ } catch (_: Exception ) {
614+ }
615+ })
577616 }
578617 }
579- DropdownMenu (
580- expanded = expanded ,
581- onDismissRequest = { expanded = false },
618+ DropdownMenu ( // 温度
619+ expanded = temperatureExpanded ,
620+ onDismissRequest = { temperatureExpanded = false },
582621 Modifier .width(200 .dp)
583622 ) {
584623 Slider (value = viewModel.temperature.toFloat(), onValueChange = {
@@ -613,6 +652,9 @@ private fun send(
613652 if (viewModel.temperature >= 0 ) {
614653 put(" temperature" , viewModel.temperature.toFloat() / 10 )
615654 }
655+ if (viewModel.maxTokensIsNumber()){
656+ put(" max_tokens" ,viewModel.maxTokens.toInt())
657+ }
616658 }
617659 val requestBody = Gson ().toJson(bodyMap)
618660 .toRequestBody(" application/json" .toMediaTypeOrNull())
@@ -642,16 +684,27 @@ private fun send(
642684 try {
643685 val cleanLine = line?.removePrefix(" data: " )?.trim()
644686 val json = JsonParser .parseString(cleanLine).asJsonObject
645- val delta = json.getAsJsonArray(" choices" )
687+ val choices = json.getAsJsonArray(" choices" )
646688 ?.firstOrNull()
647689 ?.asJsonObject
648- ?.getAsJsonObject(" delta" )
690+ val delta = choices ?.getAsJsonObject(" delta" )
649691 if (viewModel.cancel)break
650692 if (delta?.get(" content" )?.isJsonNull == false ) {
651693 viewModel.updateAIMessage(delta.get(" content" )?.asString!! )
652694 } else {
653695 viewModel.updateAIReasoningMessage(delta?.get(" reasoning_content" )?.asString!! )
654696 }
697+ if (! choices.get(" finish_reason" ).isJsonNull) {
698+ if (choices.get(" finish_reason" ).asString != " stop" ) {
699+ withContext(Dispatchers .Main ) {
700+ Toast .makeText(
701+ context,
702+ choices.get(" finish_reason" ).asString,
703+ Toast .LENGTH_SHORT
704+ ).show()
705+ }
706+ }
707+ }
655708 } catch (_: Exception ) {
656709 }
657710 }
0 commit comments