@@ -70,6 +70,9 @@ public class MainActivity extends AppCompatActivity implements Runnable, LlamaCa
70
70
private SettingsFields mCurrentSettingsFields ;
71
71
private Handler mMemoryUpdateHandler ;
72
72
private Runnable memoryUpdater ;
73
+ private int promptID = 0 ;
74
+
75
+ private static final int CONVERSATION_HISTORY_MESSAGE_LOOKBACK = 2 ;
73
76
74
77
@ Override
75
78
public void onResult (String result ) {
@@ -195,6 +198,11 @@ private void populateExistingMessages(String existingMsgJSON) {
195
198
mMessageAdapter .notifyDataSetChanged ();
196
199
}
197
200
201
+ private int setPromptID () {
202
+
203
+ return mMessageAdapter .getMaxPromptID () + 1 ;
204
+ }
205
+
198
206
@ Override
199
207
protected void onCreate (Bundle savedInstanceState ) {
200
208
super .onCreate (savedInstanceState );
@@ -216,6 +224,7 @@ protected void onCreate(Bundle savedInstanceState) {
216
224
String existingMsgJSON = mDemoSharedPreferences .getSavedMessages ();
217
225
if (!existingMsgJSON .isEmpty ()) {
218
226
populateExistingMessages (existingMsgJSON );
227
+ promptID = setPromptID ();
219
228
}
220
229
mSettingsButton = requireViewById (R .id .settings );
221
230
mSettingsButton .setOnClickListener (
@@ -552,6 +561,48 @@ private void addSelectedImagesToChatThread(List<Uri> selectedImageUri) {
552
561
mMessageAdapter .notifyDataSetChanged ();
553
562
}
554
563
564
+ private String getConversationHistory () {
565
+ String conversationHistory = "" ;
566
+
567
+ ArrayList <Message > conversations =
568
+ mMessageAdapter .getRecentSavedTextMessages (CONVERSATION_HISTORY_MESSAGE_LOOKBACK );
569
+ if (conversations .isEmpty ()) {
570
+ return conversationHistory ;
571
+ }
572
+
573
+ int prevPromptID = conversations .get (0 ).getPromptID ();
574
+ String conversationFormat =
575
+ PromptFormat .getConversationFormat (mCurrentSettingsFields .getModelType ());
576
+ String format = conversationFormat ;
577
+ for (int i = 0 ; i < conversations .size (); i ++) {
578
+ Message conversation = conversations .get (i );
579
+ int currentPromptID = conversation .getPromptID ();
580
+ if (currentPromptID != prevPromptID ) {
581
+ conversationHistory = conversationHistory + format ;
582
+ format = conversationFormat ;
583
+ prevPromptID = currentPromptID ;
584
+ }
585
+ if (conversation .getIsSent ()) {
586
+ format = format .replace (PromptFormat .USER_PLACEHOLDER , conversation .getText ());
587
+ } else {
588
+ format = format .replace (PromptFormat .ASSISTANT_PLACEHOLDER , conversation .getText ());
589
+ }
590
+ }
591
+ conversationHistory = conversationHistory + format ;
592
+
593
+ return conversationHistory ;
594
+ }
595
+
596
+ private String getTotalFormattedPrompt (String conversationHistory , String rawPrompt ) {
597
+ if (conversationHistory .isEmpty ()) {
598
+ return mCurrentSettingsFields .getFormattedSystemAndUserPrompt (rawPrompt );
599
+ }
600
+
601
+ return mCurrentSettingsFields .getFormattedSystemPrompt ()
602
+ + conversationHistory
603
+ + mCurrentSettingsFields .getFormattedUserPrompt (rawPrompt );
604
+ }
605
+
555
606
private void onModelRunStarted () {
556
607
mSendButton .setClickable (false );
557
608
mSendButton .setImageResource (R .drawable .baseline_stop_24 );
@@ -586,19 +637,19 @@ private void onModelRunStopped() {
586
637
+ image .getBytes ().length );
587
638
});
588
639
String rawPrompt = mEditTextMessage .getText ().toString ();
589
- String prompt = mCurrentSettingsFields .getFormattedSystemAndUserPrompt (rawPrompt );
590
640
// We store raw prompt into message adapter, because we don't want to show the extra
591
641
// tokens from system prompt
592
- mMessageAdapter .add (new Message (rawPrompt , true , MessageType .TEXT , 0 ));
642
+ mMessageAdapter .add (new Message (rawPrompt , true , MessageType .TEXT , promptID ));
593
643
mMessageAdapter .notifyDataSetChanged ();
594
644
mEditTextMessage .setText ("" );
595
- mResultMessage = new Message ("" , false , MessageType .TEXT , 0 );
645
+ mResultMessage = new Message ("" , false , MessageType .TEXT , promptID );
596
646
mMessageAdapter .add (mResultMessage );
597
647
// Scroll to bottom of the list
598
648
mMessagesView .smoothScrollToPosition (mMessageAdapter .getCount () - 1 );
599
649
// After images are added to prompt and chat thread, we clear the imageURI list
600
650
// Note: This has to be done after imageURIs are no longer needed by LlamaModule
601
651
mSelectedImageUri = null ;
652
+ promptID ++;
602
653
Runnable runnable =
603
654
new Runnable () {
604
655
@ Override
@@ -610,10 +661,10 @@ public void run() {
610
661
onModelRunStarted ();
611
662
}
612
663
});
613
- ETLogging .getInstance ().log ("Running inference.. prompt=" + prompt );
614
664
long generateStartTime = System .currentTimeMillis ();
615
665
if (ModelUtils .getModelCategory (mCurrentSettingsFields .getModelType ())
616
666
== ModelUtils .VISION_MODEL ) {
667
+ ETLogging .getInstance ().log ("Running inference.. prompt=" + rawPrompt );
617
668
if (!processedImageList .isEmpty ()) {
618
669
// For now, Llava only support 1 image.
619
670
ETImage img = processedImageList .get (0 );
@@ -622,7 +673,7 @@ public void run() {
622
673
img .getWidth (),
623
674
img .getHeight (),
624
675
ModelUtils .VISION_MODEL_IMAGE_CHANNELS ,
625
- prompt ,
676
+ rawPrompt ,
626
677
ModelUtils .VISION_MODEL_SEQ_LEN ,
627
678
false ,
628
679
MainActivity .this );
@@ -633,14 +684,20 @@ public void run() {
633
684
0 ,
634
685
0 ,
635
686
ModelUtils .VISION_MODEL_IMAGE_CHANNELS ,
636
- prompt ,
687
+ rawPrompt ,
637
688
ModelUtils .VISION_MODEL_SEQ_LEN ,
638
689
false ,
639
690
MainActivity .this );
640
691
}
641
692
} else {
693
+ String finalPrompt =
694
+ getTotalFormattedPrompt (getConversationHistory (), rawPrompt );
695
+ ETLogging .getInstance ().log ("Running inference.. prompt=" + finalPrompt );
642
696
mModule .generate (
643
- prompt , ModelUtils .TEXT_MODEL_SEQ_LEN , false , MainActivity .this );
697
+ finalPrompt ,
698
+ (int ) (finalPrompt .length () * 0.75 ) + 64 ,
699
+ false ,
700
+ MainActivity .this );
644
701
}
645
702
646
703
long generateDuration = System .currentTimeMillis () - generateStartTime ;
0 commit comments