Skip to content

Commit 4295649

Browse files
he-yufengYunnglin
andauthored
fix(scicode): read assistant text blocks (modelscope#1381)
* fix(scicode): read assistant text blocks * style: fix yapf formatting --------- Co-authored-by: Yunnglin <mao.looper@qq.com>
1 parent 1feea77 commit 4295649

2 files changed

Lines changed: 22 additions & 8 deletions

File tree

evalscope/benchmarks/scicode/util.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
from typing import cast
2-
31
from evalscope.api.evaluator import TaskState
42

53

@@ -21,7 +19,7 @@ def extract_code(block: str) -> str:
2119
The extracted code with the code fences and surrounding whitespace removed.
2220
2321
"""
24-
return block.replace('```python', '').replace('```', '').strip()
22+
return block.replace("```python", "").replace("```", "").strip()
2523

2624

2725
def get_generated_code(state: TaskState) -> list[str]:
@@ -43,10 +41,8 @@ def get_generated_code(state: TaskState) -> list[str]:
4341
If the number of assistant messages does not match the number of subproblems in the task metadata.
4442
4543
"""
46-
assistant_messages = [
47-
extract_code(cast(str, message.content)) for message in state.messages if message.role == 'assistant'
48-
]
49-
assert len(assistant_messages) == len(state.metadata['sub_steps'])
44+
assistant_messages = [extract_code(message.text) for message in state.messages if message.role == "assistant"]
45+
assert len(assistant_messages) == len(state.metadata["sub_steps"])
5046
return assistant_messages
5147

5248

@@ -73,4 +69,4 @@ def subproblem_str_to_int(num: str) -> int:
7369
1
7470
7571
"""
76-
return int(num.split('.')[1])
72+
return int(num.split(".")[1])
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
from evalscope.api.dataset import Sample
2+
from evalscope.api.evaluator import TaskState
3+
from evalscope.api.messages import ChatMessageAssistant, ContentText
4+
from evalscope.benchmarks.scicode.util import get_generated_code
5+
6+
7+
def test_get_generated_code_accepts_text_content_blocks():
8+
state = TaskState(
9+
model="test",
10+
sample=Sample(input="77", metadata={"sub_steps": [{}]}),
11+
messages=[
12+
ChatMessageAssistant(content=[
13+
ContentText(text="```python\ndef answer():\n return 1\n```"),
14+
], ),
15+
],
16+
)
17+
18+
assert get_generated_code(state) == ["def answer():\n return 1"]

0 commit comments

Comments
 (0)