Filter out all non-JSON output lines to prevent exceptions

This commit is contained in:
Anthony Wang 2024-02-04 20:12:19 -05:00
parent 92b1251dbb
commit bf480d21ca
Signed by: a
SSH key fingerprint: SHA256:B5ADfMCqd2M7d/jtXDoihAV/yfXOAbWWri9+GdCN4hQ

View file

@ -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 =[{"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="")
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