Filter out all non-JSON output lines to prevent exceptions
This commit is contained in:
parent
92b1251dbb
commit
bf480d21ca
1 changed files with 5 additions and 5 deletions
10
engine.py
10
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"])
|
||||
|
||||
|
|
Loading…
Reference in a new issue