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
Files changed (1) hide show
  1. 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(agent_response_json):
11
  """
12
- Parses the agent's full response JSON to find the content of the last
13
- turn with the 'assistant' role that contains content.
14
- Returns the content string if found, otherwise an empty string.
15
  """
16
- content = ""
17
- # Find the content of the last turn with the 'assistant' role
18
- if agent_response_json and 'agent_response' in agent_response_json and isinstance(agent_response_json['agent_response'], list):
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
- return content
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
  """