LLM tests

This commit is contained in:
Dejvino
2026-06-30 21:44:57 +02:00
parent 6229e2e8c4
commit 6b277d725d
5 changed files with 412 additions and 12 deletions
+18 -10
View File
@@ -1,6 +1,7 @@
from __future__ import annotations
import json
import re
from .llm import call_llm
from .paths import CHAR_PATH, WORLD_PATH
@@ -9,11 +10,6 @@ from . import state
VALIDATION_PROMPT = """You are a strict RPG game master validating whether a player's action is possible given the game state. Be thorough — check inventory, stats, location, NPCs, and story logic.
Respond with JSON only:
{{"valid": true, "reason": "ok"}}
or
{{"valid": false, "reason": "brief explanation of why the action is impossible"}}
## Character
{character}
@@ -30,7 +26,15 @@ or
- Does the action make sense given the character's abilities and resources? -> valid
- If valid, also check: if they're using a consumable item, note that it must be removed from inventory.
Reply with ONLY the JSON object."""
Reply with ONLY the JSON object. Examples:
```
{{"valid": true, "reason": "ok"}}
```
or
```
{{"valid": false, "reason": "brief explanation of why the action is impossible"}}
```
"""
def validate_action(
@@ -48,17 +52,21 @@ def validate_action(
text = call_llm(
[{"role": "user", "content": prompt}],
max_tokens=256,
max_tokens=512,
temperature=0.2,
label="Action validation",
on_debug=on_debug,
)
if not text:
return True, ""
return False, "Not sure"
cleaned = text.strip()
m = re.search(r"```(?:json)?\s*\n?(.*?)```", cleaned, re.DOTALL)
if m:
cleaned = m.group(1).strip()
try:
data = json.loads(text.strip())
data = json.loads(cleaned)
valid = data.get("valid", True)
reason = data.get("reason", "")
if on_debug:
@@ -67,7 +75,7 @@ def validate_action(
except (json.JSONDecodeError, ValueError):
if on_debug:
on_debug("action_validation", {"valid": True, "reason": "parse_failed", "raw": text[:200]})
return True, ""
return False, "Unrecognized"
def auto_prompt(book_log: str = "") -> str: