Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
KoboldAI
GitHub Repository: KoboldAI/KoboldAI-Client
Path: blob/main/test_aiserver.py
471 views
1
import pytest, time
2
import aiserver
3
4
#Test Model List:
5
test_models = [
6
('EleutherAI/gpt-neo-1.3B', {'key': False, 'gpu': False, 'layer_count': 24, 'breakmodel': True, 'url': False}),
7
('gpt2', {'key': False, 'gpu': False, 'layer_count': 12, 'breakmodel': True, 'url': False}),
8
('facebook/opt-350m', {'key': False, 'gpu': False, 'layer_count': 24, 'breakmodel': True, 'url': False})
9
]
10
11
@pytest.fixture
12
def client_data():
13
app = aiserver.app
14
#app.test_client_class = FlaskLoginClient
15
client_conn = app.test_client()
16
socketio_client = aiserver.socketio.test_client(app, flask_test_client=client_conn)
17
#Clear out the connection message
18
response = socketio_client.get_received()
19
return (client_conn, app, socketio_client)
20
21
22
def get_model_menu(model):
23
for menu in aiserver.model_menu:
24
for item in aiserver.model_menu[menu]:
25
if item[1] == model:
26
for main_menu_line in aiserver.model_menu['mainmenu']:
27
if main_menu_line[1] == menu:
28
return (menu, main_menu_line, item)
29
return None
30
31
def generate_story_data(client_data):
32
(client, app, socketio_client) = client_data
33
socketio_client.emit('message',{'cmd': 'submit', 'allowabort': False, 'actionmode': 0, 'chatname': None, 'data': ''})
34
35
#wait until the game state turns back to start
36
state = 'wait'
37
new_text = None
38
start_time = time.time()
39
timeout = time.time() + 60*1
40
while state == 'wait':
41
if time.time() > timeout:
42
break
43
responses = socketio_client.get_received()
44
for response in responses:
45
response = response['args'][0]
46
print(response)
47
if response['cmd'] == 'setgamestate':
48
state = response['data']
49
elif response['cmd'] == 'updatechunk' or response['cmd'] == 'genseqs':
50
new_text = response['data']
51
time.sleep(0.1)
52
53
assert new_text is not None
54
55
def test_basic_connection(client_data):
56
(client, app, socketio_client) = client_data
57
response = client.get("/")
58
assert response.status_code == 200
59
60
def test_load_story_from_web_ui(client_data):
61
(client, app, socketio_client) = client_data
62
63
#List out the stories and make sure we have the sample story
64
socketio_client.emit('message',{'cmd': 'loadlistrequest', 'data': ''})
65
response = socketio_client.get_received()[0]['args'][0]['data']
66
found_sample_story = False
67
for story in response:
68
if story['name'] == 'sample_story':
69
found_sample_story = True
70
assert found_sample_story
71
72
#Click on the sample story, then click load
73
socketio_client.emit('message',{'cmd': 'loadselect', 'data': 'sample_story'})
74
socketio_client.emit('message',{'cmd': 'loadrequest', 'data': ''})
75
76
#Wait until we get the data back from the load
77
loaded_story = False
78
timeout = time.time() + 60*2
79
while not loaded_story:
80
if time.time() > timeout:
81
break
82
responses = socketio_client.get_received()
83
for response in responses:
84
response = response['args'][0]
85
if 'cmd' not in response:
86
print(response)
87
assert False
88
if response['cmd'] == 'updatescreen':
89
loaded_story = True
90
story_text = response['data']
91
break
92
assert loaded_story
93
94
#Verify that it's the right story data
95
assert story_text == '<chunk n="0" id="n0" tabindex="-1">Niko the kobold stalked carefully down the alley, his small scaly figure obscured by a dusky cloak that fluttered lightly in the cold winter breeze. Holding up his tail to keep it from dragging in the dirty snow that covered the cobblestone, he waited patiently for the butcher to turn his attention from his stall so that he could pilfer his next meal: a tender-looking</chunk><chunk n="1" id="n1" tabindex="-1"> chicken. He crouched just slightly as he neared the stall to ensure that no one was watching, not that anyone would be dumb enough to hassle a small kobold. What else was there for a lowly kobold to</chunk><chunk n="2" id="n2" tabindex="-1"> do in a city? All that Niko needed to know was</chunk><chunk n="3" id="n3" tabindex="-1"> where to find the chicken and then how to make off with it.<br/><br/>A soft thud caused Niko to quickly lift his head. Standing behind the stall where the butcher had been cutting his chicken,</chunk>'
96
97
@pytest.mark.parametrize("model, expected_load_options", test_models)
98
def test_load_model_from_web_ui(client_data, model, expected_load_options):
99
(client, app, socketio_client) = client_data
100
101
#Clear out any old messages
102
response = socketio_client.get_received()
103
104
(menu, menu_line, model_line) = get_model_menu(model)
105
106
#Send the ai load model menu option
107
socketio_client.emit('message',{'cmd': 'list_model', 'data': 'mainmenu'})
108
response = socketio_client.get_received()[0]['args'][0]['data']
109
assert menu_line in response
110
111
#Send the click model menu option
112
socketio_client.emit('message',{'cmd': 'list_model', 'data': menu, 'pretty_name': ""})
113
response = socketio_client.get_received()[0]['args'][0]['data']
114
assert model_line in response
115
116
#Click the model
117
socketio_client.emit('message',{'cmd': 'selectmodel', 'data': model})
118
response = socketio_client.get_received()[0]['args'][0]
119
#Check that we're getting the right load options
120
print(response)
121
assert response['key'] == expected_load_options['key']
122
assert response['gpu'] == expected_load_options['gpu']
123
assert response['layer_count'] == expected_load_options['layer_count']
124
assert response['breakmodel'] == expected_load_options['breakmodel']
125
assert response['url'] == expected_load_options['url']
126
127
#Now send the load
128
socketio_client.emit('message',{'cmd': 'load_model', 'use_gpu': True, 'key': '', 'gpu_layers': str(expected_load_options['layer_count']), 'disk_layers': '0', 'url': '', 'online_model': ''})
129
#wait until the game state turns back to start
130
state = 'wait'
131
start_time = time.time()
132
timeout = time.time() + 60*2
133
while state == 'wait':
134
if time.time() > timeout:
135
break
136
responses = socketio_client.get_received()
137
for response in responses:
138
response = response['args'][0]
139
if response['cmd'] == 'setgamestate':
140
state = response['data']
141
time.sleep(0.1)
142
143
#Give it a second to get all of the settings, etc and clear out the messages
144
responses = socketio_client.get_received()
145
146
#check the model info to see if it's loaded
147
socketio_client.emit('message',{'cmd': 'show_model', 'data': ''})
148
response = socketio_client.get_received()[0]['args'][0]
149
assert response == {'cmd': 'show_model_name', 'data': model}
150
151
generate_story_data(client_data)
152
153
def test_load_GooseAI_from_web_ui(client_data):
154
155
pytest.skip("unsupported configuration")
156
157
@pytest.mark.parametrize("model, expected_load_options", test_models)
158
def test_load_model_from_command_line(client_data, model, expected_load_options):
159
(client, app, socketio_client) = client_data
160
161
#Clear out any old messages
162
response = socketio_client.get_received()
163
164
(menu, menu_line, model_line) = get_model_menu(model)
165
166
aiserver.general_startup("--model {}".format(model))
167
168
aiserver.load_model(initial_load=True)
169
170
#check the model info to see if it's loaded
171
socketio_client.emit('message',{'cmd': 'show_model', 'data': ''})
172
response = socketio_client.get_received()[0]['args'][0]
173
assert response == {'cmd': 'show_model_name', 'data': model}
174
175
generate_story_data(client_data)
176
177
def test_back_redo(client_data):
178
(client, app, socketio_client) = client_data
179
180
181
#Make sure we have known story in the ui
182
test_load_story_from_web_ui(client_data)
183
184
#Clear out any old messages
185
response = socketio_client.get_received()
186
187
#run a back action
188
socketio_client.emit('message',{'cmd': 'back', 'data': ''})
189
response = socketio_client.get_received()[0]['args'][0]
190
assert response == {'cmd': 'removechunk', 'data': 3}
191
192
#Run a redo action
193
socketio_client.emit('message',{'cmd': 'redo', 'data': ''})
194
response = socketio_client.get_received()[0]['args'][0]
195
assert response == {'cmd': 'updatechunk', 'data': {'index': 3, 'html': '<chunk n="3" id="n3" tabindex="-1"> where to find the chicken and then how to make off with it.<br/><br/>A soft thud caused Niko to quickly lift his head. Standing behind the stall where the butcher had been cutting his chicken,</chunk>'}}
196
197
#Go all the way back, then all the way forward
198
socketio_client.emit('message',{'cmd': 'back', 'data': ''})
199
response = socketio_client.get_received()[0]['args'][0]
200
assert response == {'cmd': 'removechunk', 'data': 3}
201
socketio_client.emit('message',{'cmd': 'back', 'data': ''})
202
response = socketio_client.get_received()[0]['args'][0]
203
assert response == {'cmd': 'removechunk', 'data': 2}
204
socketio_client.emit('message',{'cmd': 'back', 'data': ''})
205
response = socketio_client.get_received()[0]['args'][0]
206
assert response == {'cmd': 'removechunk', 'data': 1}
207
socketio_client.emit('message',{'cmd': 'back', 'data': ''})
208
response = socketio_client.get_received()[0]['args'][0]
209
assert response == {'cmd': 'errmsg', 'data': 'Cannot delete the prompt.'}
210
socketio_client.emit('message',{'cmd': 'redo', 'data': ''})
211
response = socketio_client.get_received()
212
assert response == [{'name': 'from_server', 'args': [{'cmd': 'updatescreen', 'gamestarted': True, 'data': '<chunk n="0" id="n0" tabindex="-1">Niko the kobold stalked carefully down the alley, his small scaly figure obscured by a dusky cloak that fluttered lightly in the cold winter breeze. Holding up his tail to keep it from dragging in the dirty snow that covered the cobblestone, he waited patiently for the butcher to turn his attention from his stall so that he could pilfer his next meal: a tender-looking</chunk><chunk n="1" id="n1" tabindex="-1"> chicken. He crouched just slightly as he neared the stall to ensure that no one was watching, not that anyone would be dumb enough to hassle a small kobold. What else was there for a lowly kobold to</chunk>'}], 'namespace': '/'},
213
{'name': 'from_server', 'args': [{'cmd': 'texteffect', 'data': 1}], 'namespace': '/'}]
214
socketio_client.emit('message',{'cmd': 'redo', 'data': ''})
215
response = socketio_client.get_received()
216
assert response == [{'name': 'from_server', 'args': [{'cmd': 'updatechunk', 'data': {'index': 2, 'html': '<chunk n="2" id="n2" tabindex="-1"> do in a city? All that Niko needed to know was</chunk>'}}], 'namespace': '/'},
217
{'name': 'from_server', 'args': [{'cmd': 'texteffect', 'data': 2}], 'namespace': '/'}]
218
socketio_client.emit('message',{'cmd': 'redo', 'data': ''})
219
response = socketio_client.get_received()
220
assert response == [{'name': 'from_server', 'args': [{'cmd': 'updatechunk', 'data': {'index': 3, 'html': '<chunk n="3" id="n3" tabindex="-1"> where to find the chicken and then how to make off with it.<br/><br/>A soft thud caused Niko to quickly lift his head. Standing behind the stall where the butcher had been cutting his chicken,</chunk>'}}], 'namespace': '/'},
221
{'name': 'from_server', 'args': [{'cmd': 'texteffect', 'data': 3}], 'namespace': '/'}]
222
223
224
225
226
227