Skip to content

Commit e26ffc4

Browse files
authored
Merge pull request #90 from neph1/update-v0.33.1
add some checks for empty responses and tests
2 parents f48995c + f1a8c8a commit e26ffc4

File tree

4 files changed

+35
-2
lines changed

4 files changed

+35
-2
lines changed

tale/llm/responses/WorldCreaturesResponse.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@
33
class WorldCreaturesResponse():
44

55
def __init__(self, response: dict = {}):
6-
self.creatures = response["creatures"]
6+
self.creatures = response.get("creatures", [])
77
self.valid = len(self.creatures) > 0

tale/llm/responses/WorldItemsResponse.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@
33
class WorldItemsResponse():
44

55
def __init__(self, response: dict = {}):
6-
self.items = response["items"]
6+
self.items = response.get("items", [])
77
self.valid = len(self.items) > 0

tale/llm/world_building.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,8 +186,12 @@ def generate_start_location(self, location: Location, zone_info: dict, story_typ
186186
if self.json_grammar_key:
187187
request_body[self.json_grammar_key] = self.json_grammar
188188
result = self.io_util.synchronous_request(request_body, prompt=prompt)
189+
if not result:
190+
return LocationResponse.empty()
189191
try:
190192
json_result = json.loads(parse_utils.sanitize_json(result))
193+
if not json_result.get('name', None):
194+
return LocationResponse.empty()
191195
location.name=json_result['name']
192196
return LocationResponse(json_result=json_result, location=location, exit_location_name='', item_types=self.item_types)
193197
except Exception as exc:

tests/test_llm_utils.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,12 @@ def test_generate_world_items(self):
251251
shield = result.items[1]
252252
assert(shield['name'] == 'shield')
253253

254+
self.llm_util._world_building.io_util.response = ''
255+
result = self.llm_util._world_building.generate_world_items(world_generation_context=WorldGenerationContext(story_context='',story_type='',world_info='',world_mood=0))
256+
assert not result.valid
257+
assert(len(result.items) == 0)
258+
259+
254260
def test_generate_world_creatures(self):
255261
# mostly for coverage
256262
self.llm_util._world_building.io_util.response = '{"creatures":[{"name": "dragon", "body": "Creature", "unarmed_attack": "BITE", "hp":100, "level":10}]}'
@@ -263,6 +269,10 @@ def test_generate_world_creatures(self):
263269
assert(dragon["level"] == 10)
264270
assert(dragon["unarmed_attack"] == UnarmedAttack.BITE.name)
265271

272+
self.llm_util._world_building.io_util.response = ''
273+
result = self.llm_util._world_building.generate_world_creatures(world_generation_context=WorldGenerationContext(story_context='',story_type='',world_info='',world_mood=0))
274+
assert not result.valid
275+
assert(len(result.creatures) == 0)
266276

267277
def test_get_neighbor_or_generate_zone(self):
268278
""" Tests the get_neighbor_or_generate_zone method of llm_utils.
@@ -472,6 +482,25 @@ def test_issue_overwriting_exits(self):
472482
assert((len(rocky_cliffs.exits) == 6))
473483
assert((len(location_response.new_locations) == 2))
474484

485+
def test_generate_location_empty_response(self):
486+
self.llm_util._world_building.io_util.response=''
487+
location = Location(name='Outside')
488+
result = self.llm_util.generate_start_location(location,
489+
story_type='',
490+
story_context='',
491+
zone_info={},
492+
world_info='')
493+
assert result.empty()
494+
495+
self.llm_util._world_building.io_util.response='{}'
496+
location = Location(name='Outside')
497+
result = self.llm_util.generate_start_location(location,
498+
story_type='',
499+
story_context='',
500+
zone_info={},
501+
world_info='')
502+
assert result.empty()
503+
475504
def test_generate_note_lore(self):
476505
self.llm_util._quest_building.io_util.response = 'A long lost tale of a hero who saved the world from a great evil.'
477506
world_generation_context = WorldGenerationContext(story_context=self.story.config.context, story_type=self.story.config.type, world_info='', world_mood=0)

0 commit comments

Comments
 (0)