-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathReplicantDriver.cs
More file actions
1181 lines (815 loc) · 34.2 KB
/
ReplicantDriver.cs
File metadata and controls
1181 lines (815 loc) · 34.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using Lasm.Bolt.UniversalSaver;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using LLMUnity;
using System.Linq;
using Lasm.Bolt.UniversalSaver.OdinSerializer;
using System.Text;
using Path = System.IO.Path;
using Application = UnityEngine.Application;
public class ReplicantDriver : MonoBehaviour
{
//Several systems need to be made here
//1. AI Load (Done)
//2. Talk to AI and Get Response
//3. Drive UI (Bubbles)
//4. Summarize Chat
//5. Session System (Done)
//6. Talk to Computer System (Done)
//7. Load Sessions and History (Done)
//8. Remember Info About User Longhand and Shorthand (Done)
//Let's start out with 8 and go backwards
//Reminder, I NEED to get encryption working ASAP and a disclaimer to NEVER share info
public LLM Driver;
public LLMClient SummarizerAI;
public LLMClient EngelsAI;
internal string SaveLocation;
private string DefaultMainImg = default;
private string DefaultMiniImg = default;
string[] supportedExtensions = new string[] { "*.png", "*.jpg" };
internal bool IsAILoading = false;
internal bool IsAILoaded = false;
internal TreeNode<Message> CurrentMainSession; //Holds everything
internal TreeNode<Message> CurrentMiniSession; //Holds the path being trained, AKA latest path
internal string SessionUUID;
internal string PDP;
internal string SaveLoc;
public AI mainAI;
internal TextAsset sumtext;
internal TextAsset engelstext;
public ReplicantUIWithBackend UIDriver;
#region Remember Info About User Longhand and Shorthand
public struct AIMemoryUser
{
string name;
List<string> aliases;
string favalias;
public string gender;
DateTime birthdate;
public string placeOfBirth;
public string nationality;
public string ethnicity;
List<string> Likes;
List<string> Dislikes;
public string physicalDescription;
public Notes GeneralNotes;
public Notes ImportantNotes;
}
public enum RelationshipType { Friend, Acquantince, BestFriend, Family, RomanticInterest, ProfessionalRelationship, Enemy, Pet }
public enum RespectLevel { Unrespected, SlightlyRespected, GeneralRespect, GreatRespect, GreatestRespect }
public enum RivalryType { Nonexistent, Malicious, Friendly, Professional, Frenemies, Other }
public enum Likability { Hated, Disliked, Ignored, Neutral, Liked, GreatlyLiked, Loved }
public enum Trustworthiness { Untrustworthy, Trustworthy, UntrustworthyWithGreaterSecrets, UntrustworthyWithLesserSecrets, Unknown }
public enum Reliability { Unreliable, SlightlyReliable, Reliable, VeryReliable, ExtremelyReliable }
public enum Loyalty { Disloyal, SlightlyLoyal, Neutral, VeryLoyal, ExtremelyLoyal }
public enum CommunicationStyle { Direct, Indirect, Open, Reserved, Assertive, Passive }
public enum ConflictResolution { Collaborative, Competitive, Avoidant, Compromising, Accommodating }
public enum EmotionalSupport { Strong, Moderate, Limited, OneSided }
public enum FrequencyOfInteraction { Daily, Weekly, Monthly, Occasionally, Rarely }
public enum SharedInterests { Common, Divergent, Limited }
public enum PowerDynamics { Equal, Imbalanced, Shifts }
public enum EmotionalIntimacy { High, Moderate, Low, SurfaceLevel }
public enum Animosity { None, Present }
public struct Notes
{
public string notetitle;
public string note;
public DateTime CreatedWhen;
public List<string> keywords;
}
public struct Relationship
{
string personname;
string name;
List<string> aliases;
string favalias;
DateTime birthdate;
List<string> Likes;
List<string> Dislikes;
RelationshipType Type;
//Personality Details
RespectLevel Respect;
RivalryType Rivalry;
Likability Likability;
Trustworthiness Trustworthiness;
Reliability Reliability;
Loyalty Loyalty;
CommunicationStyle Communication;
ConflictResolution ConflictResolution;
EmotionalSupport EmotionalSupport;
FrequencyOfInteraction InteractionFrequency;
SharedInterests Interests;
PowerDynamics Dynamics;
EmotionalIntimacy Intimacy;
Notes RelationshipNotes;
}
#endregion
//Saving the remember info for 1.1/1.2
#region Save, Get, Delete and Update ChatML Sessions
public void SaveSessionHistory(AI LLM, DateTime RecordTime, Dictionary<int, string> AI, Dictionary<int, string> Humans, Dictionary<int, string> Computer, Dictionary<int, string> Grandma, int Inputs, string SessionID, bool isNewSession)
{
string ChatML;
string SaveLoc = SaveLocation + @"/DataBunker" + LLM.AITruename + "GeneralSessions" + "/" + SessionID;
UniversalSave saveitem;
if (isNewSession)
{
ChatML = "Session" + SessionID + " = [" + System.Environment.NewLine;
saveitem = new UniversalSave();
saveitem.Set("SessionStart", RecordTime);
}
else
{
saveitem = UniversalSave.Load(SaveLoc, Lasm.Bolt.UniversalSaver.OdinSerializer.DataFormat.JSON);
ChatML = (string)saveitem.Get("ChatMLFile");
ChatML = ChatML.TrimEnd();
int lastIndex = ChatML.LastIndexOf(Environment.NewLine);
if (lastIndex != -1)
{
ChatML = ChatML.Remove(lastIndex);
}
}
for (int i = 0; i < Inputs; i++)
{
string jsonString = "{\"role\":\"default\",\"content\":\"This is a template, if you can see this ignore it!\"}";
Dictionary<string, string> jsonDict = JsonUtility.FromJson<Dictionary<string, string>>(jsonString);
if (AI.ContainsKey(i))
{
jsonDict["role"] = "Assistant";
jsonDict["content"] = AI[i];
}
else if (Humans.ContainsKey(i))
{
jsonDict["role"] = "User";
jsonDict["content"] = Humans[i];
}
else if (Computer.ContainsKey(i))
{
jsonDict["role"] = "Computer";
jsonDict["content"] = Computer[i];
}
else if (Grandma.ContainsKey(i))
{
jsonDict["role"] = "GRANDMOTHER";
jsonDict["content"] = Grandma[i];
}
string modifiedJsonString = JsonUtility.ToJson(jsonDict);
ChatML += modifiedJsonString + System.Environment.NewLine;
}
ChatML += "]";
saveitem.Set("ChatMLFile", ChatML);
UniversalSave.Save(SaveLoc, saveitem);
}
public void DeleteSession(string SessionID)
{
}
public List<Dictionary<int, string>> ConvertChatToDicts(TreeNode<Message> currentMessage)
{
Dictionary<int, string> userMessages = new Dictionary<int, string>();
Dictionary<int, string> assistantMessages = new Dictionary<int, string>();
Dictionary<int, string> computerMessages = new Dictionary<int, string>();
Dictionary<int, string> grandmotherMessages = new Dictionary<int, string>();
List<TreeNode<Message>> messages = GetDirectPathToMessage(currentMessage);
int order = 0;
foreach (TreeNode<Message> node in messages)
{
string message = $"({node.Data.Name}) {node.Data.Content}";
switch (node.Data.Role)
{
case "User":
userMessages.Add(order, message);
break;
case "Assistant":
assistantMessages.Add(order, message);
break;
case "Computer":
computerMessages.Add(order, message);
break;
case "GRANDMOTHER":
grandmotherMessages.Add(order, message);
break;
}
order++;
}
return new List<Dictionary<int, string>> { userMessages, assistantMessages, computerMessages, grandmotherMessages };
}
#endregion
#region Load AI
public struct AI
{
public string MainImg;
public string MiniImg;
public string AIName;
public string Version;
public string AIDescription;
public string Author;
public string AuthorDetails;
public string AITruename; //We use this name when making files
public AI(string mainImg, string miniImg, string aiName, string version, string aiDescription, string author, string authorDetails, string aiTruename)
{
MainImg = mainImg;
MiniImg = miniImg;
AIName = aiName;
Version = version;
AIDescription = aiDescription;
Author = author;
AuthorDetails = authorDetails;
AITruename = aiTruename;
}
public bool Equals(AI other)
{
// Compare all fields for equality
return MainImg == other.MainImg &&
MiniImg == other.MiniImg &&
AIName == other.AIName &&
Version == other.Version &&
AIDescription == other.AIDescription &&
Author == other.Author &&
AuthorDetails == other.AuthorDetails &&
AITruename == other.AITruename;
}
}
public void UpdateAIList()
{
//Side note, maybe make the AITruename a UUID, in the future add an AIVerify thing
//Now we will assume that the AIs folder has a list of folders. The name of the folder is the AITruename (which never changes) and contains the following:
//A UniversalSave.JSON file called KnowledgeCore within a folder named the AI's true name and a variable called "AI" containing the AIName, Version, Description, Author and Author Details
//An image called "MainImg" which is used as the main banner and
//An image caleld "MiniImg" which is used as a shortcut
//Any training data under files labeled "TrainingData-"InsertTitleHere".txt
//When updating the list we are adding AIs which did not exist, udpating Ais with new versions in them and deleting AIs that no logner exist in the lost
//We start out by getting all folders in the appropriate save spot to add to the list
var AIFolders = Directory.GetDirectories(SaveLocation + @"/AIs"); //The location of all our AI folders (Cataphract, Aiclia, etc), when you want to add a new AI just make a folder here with the basics
var SaveFile = UniversalSave.Load(SaveLocation + @"/Pandora" + @"/AICore", DataFormat.JSON); //This holds the values for Project Replicant to work, AKA the SaveData
//The list of AIs as the program sees it
List<AI> AIDB = (List<AI>)SaveFile.Get("AICache"); //The List of AIs we currently have saved within Project Replicant, whether up to date or not
List<string> KnownAIPaths = new List<string>(); //AI folders we are aware of Replicant wise, we remove the SARCOPHAGUS
KnownAIPaths.Remove((AIFolders + @"/SARCOPHAGUS"));
//We fill KnownAIPaths
foreach (AI ai in AIDB)
{
KnownAIPaths.Add(AIFolders + "/" + ai.AITruename);
}
//First off let's check if any files should be added to the AIDB, we know all the folders exist in general to appear here
foreach (string Folder in AIFolders)
{
//If the path already exists in the AIDB we don't do anything yet
if (KnownAIPaths.Contains(Folder))
{
continue;
}
//Else we add it if the JSON exists and ignore it if it doesn't
//Again the JSON should be called KnowledgeCore
else
{
//If the file we want exists, setup and add to list
if (File.Exists(Folder + @"/KnowledgeCore"))
{
//Get MainImg and MiniImg
string[] files = supportedExtensions.SelectMany(ext => Directory.GetFiles(Folder, ext)).ToArray();
string MainImg = DefaultMainImg;
string MiniImg = DefaultMiniImg;
foreach (string file in files)
{
if (System.IO.Path.GetFileNameWithoutExtension(file).StartsWith("MainImg"))
{
MainImg = file;
}
if (System.IO.Path.GetFileNameWithoutExtension(file).StartsWith("MiniImg"))
{
MiniImg = file;
}
}
//Get AI Data from KnowledgeCore
var AISaveData = UniversalSave.Load(Folder + @"/KnowledgeCore", DataFormat.JSON);
string AIName = (string)SaveFile.Get("AIName");
string Version = (string)SaveFile.Get("Version");
string AIDescription = (string)SaveFile.Get("AIDescription");
string Author = (string)SaveFile.Get("Author");
string AuthorDetails = (string)SaveFile.Get("AuthorDetails");
string AITruename = (string)SaveFile.Get("AITruename");
AIDB.Add(new AI(MainImg, MiniImg, AIName, Version, AIDescription, Author, AuthorDetails, AITruename));
}
//If not we ignore it
else
{
continue;
}
}
}
//Now to delete AIs which no longer exist from the DB
//For each known existing apth
foreach (string Folder in KnownAIPaths)
{
//If the directory for the AI still exists we do nothing
if (Directory.Exists(Folder))
{
continue;
}
//Else we delete it from AIDB and KnownAIPaths
else
{
KnownAIPaths.Remove(Folder);
//Find the matching AI object in AIDB and delete
foreach (AI Personality in AIDB)
{
string[] parts = Folder.Split('/');
string aiTruename = parts[parts.Length - 1];
if (aiTruename == Personality.AITruename)
{
AIDB.Remove(Personality);
}
}
}
}
//Finally we check all Ais which currently exist and update them if need be
foreach (AI Personality in AIDB)
{
var AISaveData = UniversalSave.Load(SaveLocation + "/" + Personality.AITruename + @"/KnowledgeCore", DataFormat.JSON);
string[] files = supportedExtensions.SelectMany(ext => Directory.GetFiles(SaveLocation + "/" + Personality.AITruename, ext)).ToArray();
string MainImg = DefaultMainImg;
string MiniImg = DefaultMiniImg;
foreach (string file in files)
{
if (System.IO.Path.GetFileNameWithoutExtension(file).StartsWith("MainImg"))
{
MainImg = file;
}
if (System.IO.Path.GetFileNameWithoutExtension(file).StartsWith("MiniImg"))
{
MiniImg = file;
}
}
//Get AI Data from KnowledgeCore
string AIName = (string)AISaveData.Get("AIName");
string Version = (string)AISaveData.Get("Version");
string AIDescription = (string)AISaveData.Get("AIDescription");
string Author = (string)AISaveData.Get("Author");
string AuthorDetails = (string)AISaveData.Get("AuthorDetails");
string AITruename = (string)AISaveData.Get("AITruename");
var tempAI = new AI(MainImg, MiniImg, AIName, Version, AIDescription, Author, AuthorDetails, AITruename);
bool areEqual = Personality.Equals(tempAI);
if (areEqual)
{
//We do nothing, the AI is up to date
}
else
{
//The AI is not up to date, Update it
var itemnumber = AIDB.IndexOf(Personality);
AIDB[itemnumber] = tempAI;
}
}
}
public void PreloadAI(AI AI)
{
mainAI = AI;
string TrainingSet = default;
//To create the AI, we first take all the basic training data from SARCHOPAGUS
TrainingSet = TrainingSet + LoadSarchopagus();
//Now we take the ChatML folder if it exists
string FolderToCheck = Path.Combine(SaveLoc, "AIs", AI.AITruename, "ChatML");
//Let's check if the directory exists
if (Directory.Exists(FolderToCheck))
{
//We get each txt file (Which we assume is ChatML) and add it
string[] txtFiles = Directory.GetFiles(FolderToCheck, "*.txt");
//First do txt files exist
if (txtFiles.Length > 0)
{
//We remove the final ] on the string
//Now we add the files to the TrainingSet
foreach (string item in txtFiles)
{
//We get the text
TrainingSet = TrainingSet + File.ReadAllText(item);
}
}
}
//Now to load
IsAILoading = true;
IsAILoaded = false;
Debug.Log(TrainingSet);
string sumpath = "Project Replicant/SummarizerChatML";
sumtext = Resources.Load<TextAsset>(sumpath);
// var summy = SummarizerAI.Complete(sumtext.text + "Make the response to this as short as possible, this is just to preload", ShowAILoadingText);
string engelspath = "Project Replicant/EngelsChatML";
engelstext = Resources.Load<TextAsset>(engelspath);
// _ = EngelsAI.Complete(engelstext.text + "Make the response to this as short as possible, this is just to preload", ShowAILoadingText);
// _ = Driver.Complete(TrainingSet + "Make the response to this as short as possible, this is just to preload", null, LoadedAI);
LoadedAI();
}
public void LoadedAI()
{
IsAILoaded = true;
IsAILoading = false;
Debug.Log("AILoaded!");
UIDriver.GoToChat();
}
public void ShowAILoadingText(string input)
{
Debug.Log(input);
}
public void StopAI(string Path)
{
Driver.CancelRequests();
}
public string LoadSarchopagus()
{
SaveLoc = System.IO.Path.Combine(UnityEngine.Application.persistentDataPath, "Project Replicant");
string sarpath = Path.Combine(SaveLoc, "AIs", "SARCOPHAGUS");
string[] txtFiles = Directory.GetFiles(sarpath, "*.txt");
string OurReturn = default;
//First do txt files exist
if (txtFiles.Length > 0)
{
//We add the files to the TrainingSet
foreach (string item in txtFiles)
{
//We get the text
OurReturn = OurReturn + File.ReadAllText(item);
}
}
return OurReturn;
}
#endregion
#region Save, Get, Delete and Update Current DataTree Sessions
public struct Message
{
public string Role;
public string Content;
public string Name;
public string DateTime;
public Message(string role, string content, string name, string datetime)
{
Role = role;
Content = content;
Name = name;
DateTime = datetime;
}
}
public string StartNewSession()
{
string SessionID = default;
for (int i = 0; i < 20; i++)
{
var tempitem = UnityEngine.Random.Range(0, 20);
var tempstring = tempitem.ToString();
SessionID = string.Concat(SessionID, tempstring);
}
SessionUUID = SessionID;
var currentdatetime = DateTime.Now.ToString();
var Session = new TreeNode<Message>(new Message("Computer", "Chat " + SessionUUID + " started at "
+ DateTime.Now.ToString(), "GRANDMOTHER", DateTime.Now.ToString()));
CurrentMainSession = Session;
return SessionUUID;
}
// Method to add a message to the conversation session
public TreeNode<Message> AddMessageToList(string role, string content, string name, TreeNode<Message> parentNode, string datetime)
{
TreeNode<Message> treemessage;
if (parentNode == null)
{
var message = new Message(role, content, name, datetime);
treemessage = CurrentMainSession.AddChild(message);
}
else
{
var message = new Message(role, content, name, datetime);
treemessage = parentNode.AddChild(message);
}
Debug.Log("CurrentNewMessageIs" + treemessage.ToString());
return treemessage;
}
public TreeNode<Message> AddUserMessage(string content, string name, TreeNode<Message> parentNode, string datetime)
{
TreeNode<Message> message = AddMessageToList("User", content, name, parentNode, datetime);
return message;
}
public TreeNode<Message> AddAssistantMessage(string content, string name, TreeNode<Message> parentNode, string datetime)
{
TreeNode<Message> message = AddMessageToList("Assistant", content, name, parentNode, datetime);
return message;
}
public TreeNode<Message> AddComputerMessage(string content, string name, TreeNode<Message> parentNode, string datetime)
{
TreeNode<Message> message = AddMessageToList("Computer", content, name, parentNode, datetime);
return message;
}
public TreeNode<Message> AddGRANDMOTHERMessage(string content, string name, TreeNode<Message> parentNode, string datetime)
{
TreeNode<Message> message = AddMessageToList("GRNADMOTHER", content, name, parentNode, datetime);
return message;
}
public TreeNode<Message> SelectConversationTree(TreeNode<Message> selectedTree)
{
CurrentMiniSession = selectedTree;
return CurrentMiniSession;
}
public void DeleteMessageAndSubConversations(Message selectedMessage)
{
// Find the node containing the message
TreeNode<Message> nodeToDelete = CurrentMainSession.FindInChildren(selectedMessage);
if (nodeToDelete != null)
{
// Remove the node and all its children
nodeToDelete.Parent.RemoveChild(nodeToDelete);
}
}
public void DeletedSubConversations(Message selectedMessage)
{
// Find the node containing the message
TreeNode<Message> nodeToDelete = CurrentMainSession.FindInChildren(selectedMessage);
nodeToDelete.Clear();
}
public TreeNode<Message> ShowNextIteration(Message selectedMessage)
{
TreeNode<Message> IterationToCheck = CurrentMainSession.FindInChildren(selectedMessage);
var ItemsToCheck = IterationToCheck.Parent.GetImmediateChildren();
var totalcount = ItemsToCheck.Count;
int iterationCount = -1;
bool valuesmatch = false;
foreach (TreeNode<Message> Item in ItemsToCheck)
{
iterationCount++;
if (Item.Data.Role == selectedMessage.Role && Item.Data.Name == selectedMessage.Name && Item.Data.Content == selectedMessage.Content && Item.Data.DateTime == selectedMessage.DateTime)
{
valuesmatch = true;
break;
}
}
if (iterationCount > totalcount || !valuesmatch)
{
return null;
}
else if (valuesmatch)
{
return ItemsToCheck[iterationCount];
}
else
{
return null;
}
}
public TreeNode<Message> ShowPreviousIteration(Message selectedMessage)
{
TreeNode<Message> IterationToCheck = CurrentMainSession.FindInChildren(selectedMessage);
var ItemsToCheck = IterationToCheck.Parent.GetImmediateChildren();
var totalcount = ItemsToCheck.Count;
int iterationCount = -1;
bool valuesmatch = false;
foreach (TreeNode<Message> Item in ItemsToCheck)
{
iterationCount++;
if (Item.Data.Role == selectedMessage.Role && Item.Data.Name == selectedMessage.Name && Item.Data.Content == selectedMessage.Content)
{
valuesmatch = true;
break;
}
}
if (iterationCount <= 0 || !valuesmatch)
{
return null;
}
else if (valuesmatch)
{
return ItemsToCheck[iterationCount];
}
else
{
return null;
}
}
public TreeNode<Message> ShowCertainIteration(Message selectedMessage, int Iteration)
{
TreeNode<Message> IterationToCheck = CurrentMainSession.FindInChildren(selectedMessage);
var ItemsToCheck = IterationToCheck.Parent.GetImmediateChildren();
var totalcount = ItemsToCheck.Count;
if (totalcount < Iteration)
{
return null;
}
else
{
return ItemsToCheck[Iteration];
}
}
public int GetCurrentIteration(Message selectedMessage)
{
TreeNode<Message> IterationToCheck = CurrentMainSession.FindInChildren(selectedMessage);
var ItemsToCheck = IterationToCheck.Parent.GetImmediateChildren();
var totalcount = ItemsToCheck.Count;
int iterationCount = -1;
bool valuesmatch = false;
foreach (TreeNode<Message> Item in ItemsToCheck)
{
iterationCount++;
if (Item.Data.Role == selectedMessage.Role && Item.Data.Name == selectedMessage.Name && Item.Data.Content == selectedMessage.Content)
{
valuesmatch = true;
break;
}
}
if (!valuesmatch)
{
return -999;
}
else
{
return iterationCount + 1;
}
}
public int GetTotalIterations(Message selectedMessage)
{
TreeNode<Message> IterationToCheck = CurrentMainSession.FindInChildren(selectedMessage);
var ItemsToCheck = IterationToCheck.Parent.GetImmediateChildren();
var totalcount = ItemsToCheck.Count;
return totalcount;
}
public List<TreeNode<Message>> GetDirectPathToMessage(TreeNode<Message> currentNode)
{
List<TreeNode<Message>> path = new List<TreeNode<Message>>();
// Start from the current node
TreeNode<Message> current = currentNode;
// Traverse upwards to the root
while (current != null)
{
// Add the current node to the path
path.Add(current);
// Move to the parent node
current = current.Parent;
}
// Reverse the path to get it from root to current node
path.Reverse();
return path;
}
public List<string> ConvertTreeToChatML(TreeNode<Message> currentMessage)
{
List<string> chatMLList = new List<string>();
List<TreeNode<Message>> messages = GetDirectPathToMessage(currentMessage);
chatMLList.Add("chat = [" + System.Environment.NewLine);
foreach (TreeNode<Message> node in messages)
{
string json = $"{{\"role\": \"{node.Data.Role}\", \"content\": \" ({node.Data.Name}) {node.Data.Content}\"}}";
chatMLList.Add(json + System.Environment.NewLine);
}
chatMLList.Add("]");
return chatMLList;
}
public string ConvertTreeToString(TreeNode<Message> currentMessage)
{
List<TreeNode<Message>> messages = GetDirectPathToMessage(currentMessage);
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.Append("chat = [" + System.Environment.NewLine);
foreach (TreeNode<Message> node in messages)
{
string json = $"{{\"role\": \"{node.Data.Role}\", \"content\": \" ({node.Data.Name}) {node.Data.Content}\"}}";
stringBuilder.Append(json + System.Environment.NewLine);
}
stringBuilder.Append("]");
return stringBuilder.ToString();
}
#endregion
//Make a way for a session to be loaded by doing the following;
//1. Get the conversation from ML
//2. Check if over 6k characters; if so, check summarization file for lines. If lines not included, summarize and add to summarization file
#region Talk To AI and Get Response
//This is going to be tricky, since people can choose to talk to an AI and then say "Oh actually I would rather now deal with this convo!" and then go all the way to the start with a new slider
//To account for this, we make a line system. Basically oficially? The AI only sees the current conversation you're having. However, each conversation is nested
//For example in your normal conversation it's "1. 2. 3. 4. 5". but you can go to 2 and reload. Everything becomes Convo 2: Start at 4(2), 1, 2, 3, 4, so omm
//Let's make a struct to represent and hold this
private GameObject CurrentTextBox = null;
private TMPro.TMP_InputField InputBox;
// public void OnEnable()
// {
// InputBox.interactable = false;
// }
public struct AIHistory
{
List<string> caca;