-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_live_call.py
More file actions
122 lines (102 loc) · 4.41 KB
/
Copy pathtest_live_call.py
File metadata and controls
122 lines (102 loc) · 4.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
"""
test_live_call.py
------------------
Minimal live test: one LLM call, full diagnostics.
Prints:
- Exact prompt sent
- Exact JSON body sent to Ollama (intercepted)
- response.thinking (empty = thinking mode OFF = fix worked)
- response.response (the actual text)
- Whether JSON parsed correctly
Run with Ollama running:
python test_live_call.py
"""
import sys, json, time, httpx
from pathlib import Path
sys.path.insert(0, str(Path(__file__).resolve().parent))
# ── Intercept HTTP to capture exact request body ──────────────────────────────
original_send = httpx.Client.send
captured_body = {}
def patched_send(self, request, *a, **kw):
if "/api/generate" in str(request.url):
try:
captured_body.update(json.loads(request.content))
except Exception:
pass
return original_send(self, request, *a, **kw)
httpx.Client.send = patched_send
# ── Test prompt (same structure as all our agents) ────────────────────────────
PROMPT = (
"You are a creative assistant.\n\n"
"Respond with ONLY this JSON -- no prose, no markdown:\n"
'{\n "title": "A short film title",\n "genre": "one word genre"\n}'
)
print("=" * 60)
print("Scriptify AI -- Live LLM Diagnostic (generate/raw)")
print("=" * 60)
print(f"\nPrompt ({len(PROMPT)} chars):")
print("-" * 40)
print(PROMPT)
print("-" * 40)
# ── Make the call ─────────────────────────────────────────────────────────────
from backend.config import OLLAMA_MODEL, LLM_TIMEOUT_SECS, LLM_TEMPERATURE, LLM_TOP_P
from backend.agents._llm import _get_client, _extract_json
tokens = 120
num_ctx = tokens + 600
options = {"temperature": LLM_TEMPERATURE, "top_p": LLM_TOP_P,
"num_predict": tokens, "num_ctx": num_ctx}
print(f"\nModel : {OLLAMA_MODEL}")
print(f"Endpoint : generate (raw=True) -- bypasses modelfile template")
print(f"Options : {options}")
print(f"\nSending...", flush=True)
t0 = time.time()
response = None
error = None
try:
client = _get_client()
response = client.generate(
model=OLLAMA_MODEL,
prompt=PROMPT,
raw=True,
options=options,
keep_alive="10m",
)
except Exception as e:
error = e
elapsed = time.time() - t0
print(f"Elapsed : {elapsed:.2f}s")
# ── Show captured request body ────────────────────────────────────────────────
if captured_body:
print("\n--- Exact JSON body sent to Ollama ---")
display = {k: v for k, v in captured_body.items() if k != "prompt"}
display["prompt_len"] = len(captured_body.get("prompt", ""))
print(json.dumps(display, indent=2))
if error:
print(f"\n[ERROR] {type(error).__name__}: {error}")
if elapsed >= LLM_TIMEOUT_SECS - 2:
print("\n TIMEOUT -- model still generating after", LLM_TIMEOUT_SECS, "s")
print(" Check: ollama ps")
sys.exit(1)
# ── Inspect response ──────────────────────────────────────────────────────────
thinking_content = getattr(response, "thinking", None) or ""
text_content = response.response or ""
print("\n--- Response fields ---")
print(f" .thinking : {repr(thinking_content[:200] if thinking_content else None)}")
print(f" .response : {repr(text_content[:300])}")
print()
if thinking_content:
print(f"[FAIL] thinking is NOT empty ({len(thinking_content)} chars)")
print(" Thinking mode is still ON -- raw=True did not suppress it.")
print(" Consider using a different model tag or upgrading Ollama.")
else:
print(f"[OK] thinking is empty/None -- thinking mode OFF")
print(f" Response came back in {elapsed:.1f}s")
# ── JSON parse check ──────────────────────────────────────────────────────────
if text_content:
try:
parsed = _extract_json(text_content)
print(f"\n[OK] JSON parsed: {parsed}")
except Exception as e:
print(f"\n[WARN] JSON parse failed: {e}")
print(f" Raw output: {text_content[:300]}")
print()