Skip to content

Commit

Permalink
Merge pull request #90 from neph1/update-v0.33.1
Browse files Browse the repository at this point in the history
add some checks for empty responses and tests
  • Loading branch information
neph1 authored Sep 10, 2024
2 parents f48995c + f1a8c8a commit e26ffc4
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 2 deletions.
2 changes: 1 addition & 1 deletion tale/llm/responses/WorldCreaturesResponse.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
class WorldCreaturesResponse():

def __init__(self, response: dict = {}):
self.creatures = response["creatures"]
self.creatures = response.get("creatures", [])
self.valid = len(self.creatures) > 0
2 changes: 1 addition & 1 deletion tale/llm/responses/WorldItemsResponse.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
class WorldItemsResponse():

def __init__(self, response: dict = {}):
self.items = response["items"]
self.items = response.get("items", [])
self.valid = len(self.items) > 0
4 changes: 4 additions & 0 deletions tale/llm/world_building.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,8 +186,12 @@ def generate_start_location(self, location: Location, zone_info: dict, story_typ
if self.json_grammar_key:
request_body[self.json_grammar_key] = self.json_grammar
result = self.io_util.synchronous_request(request_body, prompt=prompt)
if not result:
return LocationResponse.empty()
try:
json_result = json.loads(parse_utils.sanitize_json(result))
if not json_result.get('name', None):
return LocationResponse.empty()
location.name=json_result['name']
return LocationResponse(json_result=json_result, location=location, exit_location_name='', item_types=self.item_types)
except Exception as exc:
Expand Down
29 changes: 29 additions & 0 deletions tests/test_llm_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,12 @@ def test_generate_world_items(self):
shield = result.items[1]
assert(shield['name'] == 'shield')

self.llm_util._world_building.io_util.response = ''
result = self.llm_util._world_building.generate_world_items(world_generation_context=WorldGenerationContext(story_context='',story_type='',world_info='',world_mood=0))
assert not result.valid
assert(len(result.items) == 0)


def test_generate_world_creatures(self):
# mostly for coverage
self.llm_util._world_building.io_util.response = '{"creatures":[{"name": "dragon", "body": "Creature", "unarmed_attack": "BITE", "hp":100, "level":10}]}'
Expand All @@ -263,6 +269,10 @@ def test_generate_world_creatures(self):
assert(dragon["level"] == 10)
assert(dragon["unarmed_attack"] == UnarmedAttack.BITE.name)

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

def test_get_neighbor_or_generate_zone(self):
""" Tests the get_neighbor_or_generate_zone method of llm_utils.
Expand Down Expand Up @@ -472,6 +482,25 @@ def test_issue_overwriting_exits(self):
assert((len(rocky_cliffs.exits) == 6))
assert((len(location_response.new_locations) == 2))

def test_generate_location_empty_response(self):
self.llm_util._world_building.io_util.response=''
location = Location(name='Outside')
result = self.llm_util.generate_start_location(location,
story_type='',
story_context='',
zone_info={},
world_info='')
assert result.empty()

self.llm_util._world_building.io_util.response='{}'
location = Location(name='Outside')
result = self.llm_util.generate_start_location(location,
story_type='',
story_context='',
zone_info={},
world_info='')
assert result.empty()

def test_generate_note_lore(self):
self.llm_util._quest_building.io_util.response = 'A long lost tale of a hero who saved the world from a great evil.'
world_generation_context = WorldGenerationContext(story_context=self.story.config.context, story_type=self.story.config.type, world_info='', world_mood=0)
Expand Down

0 comments on commit e26ffc4

Please sign in to comment.