Kunal Pai
commited on
Commit
·
81fafc1
1
Parent(s):
1d27ba2
Refactor get_last_assistant_content function to improve response handling and support various response formats
Browse files- bench/benchmarking_hle.py +32 -14
bench/benchmarking_hle.py
CHANGED
@@ -7,23 +7,41 @@ import os
|
|
7 |
from datetime import datetime
|
8 |
import re
|
9 |
|
10 |
-
def get_last_assistant_content(
|
11 |
"""
|
12 |
-
|
13 |
-
|
14 |
-
Returns the content string if found, otherwise an empty string.
|
15 |
"""
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
for turn in reversed(agent_response_json['agent_response']):
|
20 |
-
# Check for 'assistant' role and if the turn has content
|
21 |
-
turn_content = turn.get('content')
|
22 |
-
if turn.get('role') == 'assistant' and turn_content is not None and turn_content != "":
|
23 |
-
content = turn_content
|
24 |
-
break # Found the last assistant turn with non-empty content
|
25 |
|
26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
27 |
|
28 |
def benchmark_hle(num_samples=20, categories=None):
|
29 |
"""
|
|
|
7 |
from datetime import datetime
|
8 |
import re
|
9 |
|
10 |
+
def get_last_assistant_content(resp):
|
11 |
"""
|
12 |
+
Return the last assistant utterance from the response object
|
13 |
+
produced by `client.predict`.
|
|
|
14 |
"""
|
15 |
+
# ❶ If the server wraps things in a (messages, meta) tuple
|
16 |
+
if isinstance(resp, tuple):
|
17 |
+
resp = resp[0]
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
|
19 |
+
# ❷ At this point `resp` must be the list of message dicts
|
20 |
+
if not isinstance(resp, list):
|
21 |
+
return ""
|
22 |
+
|
23 |
+
for turn in reversed(resp):
|
24 |
+
if turn.get("role") != "assistant":
|
25 |
+
continue
|
26 |
+
|
27 |
+
# a) plain messages
|
28 |
+
if turn.get("content"):
|
29 |
+
return turn["content"]
|
30 |
+
|
31 |
+
# b) tool / function_response wrapper
|
32 |
+
fr = turn.get("function_response", {})
|
33 |
+
out = fr.get("result", {}).get("output")
|
34 |
+
if out:
|
35 |
+
return out
|
36 |
+
|
37 |
+
# c) messages stored as Part objects inside `content`
|
38 |
+
cont = turn.get("content")
|
39 |
+
if isinstance(cont, dict):
|
40 |
+
parts = cont.get("parts", [])
|
41 |
+
if parts and parts[0].get("text"):
|
42 |
+
return parts[0]["text"]
|
43 |
+
|
44 |
+
return ""
|
45 |
|
46 |
def benchmark_hle(num_samples=20, categories=None):
|
47 |
"""
|