forked from undreamai/LLMUnity
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ChatBot.cs
176 lines (159 loc) · 5.98 KB
/
ChatBot.cs
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
using UnityEngine;
using System.Collections.Generic;
using System.Threading.Tasks;
using LLMUnity;
using UnityEngine.UI;
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 LLMCharacter llmCharacter;
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);
_ = llmCharacter.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);
playerBubble.OnResize(UpdateBubblePositions);
aiBubble.OnResize(UpdateBubblePositions);
Task chatTask = llmCharacter.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()
{
llmCharacter.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)
{
// Get rid of newline character added when we press enter
if (Input.GetKey(KeyCode.Return))
{
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 && !llmCharacter.remote && llmCharacter.llm != null && llmCharacter.llm.model == "")
{
Debug.LogWarning($"Please select a model in the {llmCharacter.llm.gameObject.name} GameObject!");
onValidateWarning = false;
}
}
}
}