-
-
Notifications
You must be signed in to change notification settings - Fork 177
Expand file tree
/
Copy pathChatBot.cs
More file actions
202 lines (184 loc) · 6.81 KB
/
ChatBot.cs
File metadata and controls
202 lines (184 loc) · 6.81 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
using UnityEngine;
using System.Collections.Generic;
using System.Threading.Tasks;
using UnityEngine.UI;
using LLMUnity;
#if ENABLE_INPUT_SYSTEM
using UnityEngine.InputSystem;
#endif
namespace LLMUnitySamples
{
public class ChatBot : MonoBehaviour
{
public Transform chatContainer;
public Color playerColor = new Color32(75, 70, 80, 255);
public Color aiColor = new Color32(70, 80, 80, 255);
public Color fontColor = Color.white;
public Font font;
public int fontSize = 16;
public int bubbleWidth = 600;
public LLMAgent llmAgent;
public float textPadding = 10f;
public float bubbleSpacing = 10f;
public Sprite sprite;
public Button stopButton;
private InputBubble inputBubble;
private List<Bubble> chatBubbles = new List<Bubble>();
private bool blockInput = true;
private BubbleUI playerUI, aiUI;
private bool warmUpDone = false;
private int lastBubbleOutsideFOV = -1;
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);
stopButton.gameObject.SetActive(true);
ShowLoadedMessages();
_ = llmAgent.Warmup(WarmUpCallback);
}
Bubble AddBubble(string message, bool isPlayerMessage)
{
Bubble bubble = new Bubble(chatContainer, isPlayerMessage ? playerUI : aiUI, isPlayerMessage ? "PlayerBubble" : "AIBubble", message);
chatBubbles.Add(bubble);
bubble.OnResize(UpdateBubblePositions);
return bubble;
}
void ShowLoadedMessages()
{
for (int i = 1; i < llmAgent.chat.Count; i++) AddBubble(llmAgent.chat[i].content, i % 2 == 1);
}
void OnInputFieldSubmit(string newText)
{
inputBubble.ActivateInputField();
#if ENABLE_INPUT_SYSTEM
// new input system for latest Unity version
bool shiftHeld = Keyboard.current != null && (Keyboard.current.leftShiftKey.isPressed || Keyboard.current.rightShiftKey.isPressed);
#else
// old input system
bool shiftHeld = Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift);
#endif
if (blockInput || newText.Trim() == "" || shiftHeld)
{
StartCoroutine(BlockInteraction());
return;
}
blockInput = true;
// replace vertical_tab
string message = inputBubble.GetText().Replace("\v", "\n");
AddBubble(message, true);
Bubble aiBubble = AddBubble("...", false);
Task chatTask = llmAgent.Chat(message, aiBubble.SetText, AllowInput);
inputBubble.SetText("");
}
public void WarmUpCallback()
{
warmUpDone = true;
inputBubble.SetPlaceHolderText("Message me");
AllowInput();
}
public void AllowInput()
{
blockInput = false;
inputBubble.ReActivateInputField();
}
public void CancelRequests()
{
llmAgent.CancelRequests();
AllowInput();
}
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)
{
// Remove newline added by Enter
#if ENABLE_INPUT_SYSTEM
// new input system for latest Unity version
bool enterPressed = Keyboard.current != null && Keyboard.current.enterKey.wasPressedThisFrame;
#else
// old input system
bool enterPressed = Input.GetKey(KeyCode.Return);
#endif
if (enterPressed)
{
if (inputBubble.GetText().Trim() == "")
inputBubble.SetText("");
}
}
public void UpdateBubblePositions()
{
float y = inputBubble.GetSize().y + inputBubble.GetRectTransform().offsetMin.y + bubbleSpacing;
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;
}
}
void Update()
{
if (!inputBubble.inputFocused() && warmUpDone)
{
inputBubble.ActivateInputField();
StartCoroutine(BlockInteraction());
}
if (lastBubbleOutsideFOV != -1)
{
// destroy bubbles outside the container
for (int i = 0; i <= lastBubbleOutsideFOV; i++)
{
chatBubbles[i].Destroy();
}
chatBubbles.RemoveRange(0, lastBubbleOutsideFOV + 1);
lastBubbleOutsideFOV = -1;
}
}
public void ExitGame()
{
Debug.Log("Exit button clicked");
Application.Quit();
}
bool onValidateWarning = true;
void OnValidate()
{
if (onValidateWarning && !llmAgent.remote && llmAgent.llm != null && llmAgent.llm.model == "")
{
Debug.LogWarning($"Please select a model in the {llmAgent.llm.gameObject.name} GameObject!");
onValidateWarning = false;
}
}
}
}