From bf480d21ca183bf2d9d743c9810d3c44470658c5 Mon Sep 17 00:00:00 2001 From: Anthony Wang Date: Sun, 4 Feb 2024 20:12:19 -0500 Subject: [PATCH] Filter out all non-JSON output lines to prevent exceptions --- engine.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/engine.py b/engine.py index fcd1213..6d88cdb 100644 --- a/engine.py +++ b/engine.py @@ -35,7 +35,7 @@ while c["player"]["health"] > 0 and c["player"][c["win"]["property"]] < c["win"] prompt += "NPC statuses:\n" + json.dumps({k: v for k, v in c["npc"].items() if v["location"] == c["player"]["location"]}) + "\n" prompt += "Player action: " + act + "\n" prompt += "Please write one single paragraph about what happens due to this action referring to me, the player, in second person. Remember, do not write more than one paragraph! Then, return the player status and NPC statuses updated to reflect changes due to this action. Make sure to meticulously update the states of both the player and the NPCs!" - # cprint(prompt, "green") + cprint(prompt, "green") stream = client.chat.completions.create(messages=[{"role": "user", "content": prompt}], model="", stream=True) updated = "" for chunk in stream: @@ -47,12 +47,12 @@ while c["player"]["health"] > 0 and c["player"][c["win"]["property"]] < c["win"] print(text, end="") else: updated += text - # cprint(updated, "red") - lines = list(filter(lambda i: i != "", updated.split("\n"))) - newp = json.loads(lines[1]) + cprint(updated, "red") + lines = list(filter(lambda i: "{" in i, updated.split("\n"))) + newp = json.loads(lines[0]) newp["location"] = c["player"]["location"] c["player"] = newp - for k, v in json.loads(lines[3]).items(): + for k, v in json.loads(lines[1]).items(): c["npc"][k] = v print(c["player"])