forked from undreamai/LLMUnity
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChatBot.cs
More file actions
147 lines (131 loc) · 5.22 KB
/
ChatBot.cs
File metadata and controls
147 lines (131 loc) · 5.22 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
using UnityEngine;
using System.Collections.Generic;
using System.Threading.Tasks;
using LLMUnity;
namespace LLMUnitySamples
{
public class ChatBot : MonoBehaviour
{
public Transform chatContainer;
public Color playerColor = new Color32(81, 164, 81, 255);
public Color aiColor = new Color32(29, 29, 73, 255);
public Color fontColor = Color.white;
public Font font;
public int fontSize = 16;
public int bubbleWidth = 600;
public LLMClient llm;
public float textPadding = 10f;
public float bubbleSpacing = 10f;
public Sprite sprite;
private InputBubble inputBubble;
private List<Bubble> chatBubbles = new List<Bubble>();
private bool blockInput = true;
private BubbleUI playerUI, aiUI;
private bool updatePositions = false;
private bool warmUpDone = false;
void Start()
{
if (font == null) font = Resources.GetBuiltinResource<Font>("LegacyRuntime.ttf");
playerUI = new BubbleUI {
sprite=sprite,
font=font,
fontSize=fontSize,
fontColor=fontColor,
bubbleColor=playerColor,
bottomPosition=0,
leftPosition=0,
textPadding=textPadding,
bubbleOffset=bubbleSpacing,
bubbleWidth=bubbleWidth,
bubbleHeight=-1
};
aiUI = playerUI;
aiUI.bubbleColor = aiColor;
aiUI.leftPosition = 1;
inputBubble = new InputBubble(chatContainer, playerUI, "InputBubble", "Loading...", 4);
inputBubble.AddSubmitListener(onInputFieldSubmit);
inputBubble.AddValueChangedListener(onValueChanged);
inputBubble.setInteractable(false);
_ = llm.Warmup(WarmUpCallback);
}
void onInputFieldSubmit(string newText){
inputBubble.ActivateInputField();
if (blockInput || newText.Trim() == "" || Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift)){
StartCoroutine(BlockInteraction());
return;
}
blockInput = true;
// replace vertical_tab
string message = inputBubble.GetText().Replace("\v", "\n");
Bubble playerBubble = new Bubble(chatContainer, playerUI, "PlayerBubble", message);
Bubble aiBubble = new Bubble(chatContainer, aiUI, "AIBubble", "...");
chatBubbles.Add(playerBubble);
chatBubbles.Add(aiBubble);
SetUpdatePositions();
BubbleTextSetter aiBubbleTextSetter = new BubbleTextSetter(this, aiBubble);
Task chatTask = llm.Chat(message, aiBubbleTextSetter.SetText, AllowInput);
inputBubble.SetText("");
}
public void WarmUpCallback(){
warmUpDone = true;
inputBubble.SetPlaceHolderText("Message me");
AllowInput();
}
public void AllowInput(){
blockInput = false;
inputBubble.ReActivateInputField();
}
IEnumerator<string> BlockInteraction()
{
// prevent from change until next frame
inputBubble.setInteractable(false);
yield return null;
inputBubble.setInteractable(true);
// change the caret position to the end of the text
inputBubble.MoveTextEnd();
}
void onValueChanged(string newText){
// Get rid of newline character added when we press enter
if (Input.GetKey(KeyCode.Return)){
if(inputBubble.GetText().Trim() == "")
inputBubble.SetText("");
}
}
public void SetUpdatePositions(){
updatePositions = true;
}
public void UpdateBubblePositions()
{
float y = inputBubble.GetSize().y + inputBubble.GetRectTransform().offsetMin.y + bubbleSpacing;
int lastBubbleOutsideFOV = -1;
float containerHeight = chatContainer.GetComponent<RectTransform>().rect.height;
for (int i = chatBubbles.Count - 1; i >= 0; i--) {
Bubble bubble = chatBubbles[i];
RectTransform childRect = bubble.GetRectTransform();
childRect.anchoredPosition = new Vector2(childRect.anchoredPosition.x, y);
// last bubble outside the container
if (y > containerHeight && lastBubbleOutsideFOV == -1){
lastBubbleOutsideFOV = i;
}
y += bubble.GetSize().y + bubbleSpacing;
}
// destroy bubbles outside the container
for (int i = 0; i <= lastBubbleOutsideFOV; i++) {
chatBubbles[i].Destroy();
}
chatBubbles.RemoveRange(0, lastBubbleOutsideFOV+1);
}
void Update()
{
if(!inputBubble.inputFocused() && warmUpDone)
{
inputBubble.ActivateInputField();
StartCoroutine(BlockInteraction());
}
if(updatePositions){
UpdateBubblePositions();
updatePositions=false;
}
}
}
}