Skip to content

Add ReasoningParser#287

Merged
junya-takayama merged 4 commits into
mainfrom
add_reasoning_parser
Jun 2, 2026
Merged

Add ReasoningParser#287
junya-takayama merged 4 commits into
mainfrom
add_reasoning_parser

Conversation

@junya-takayama

@junya-takayama junya-takayama commented May 21, 2026

Copy link
Copy Markdown
Collaborator

Add a mechanism to the VLLM and HuggingFaceLM classes to extract the reasoning content and store it in LMOutput.reasoning_text.

Example

flexeval_lm --eval_setup rakuda-v2-ja --language_model VLLM --language_model.model Qwen/Qwen3-4B --language_model.reasoning_parser UnifiedRegexReasoningParser --language_model.reasoning_parser.pattern="<think>(?P<reasoning_content>.*?)</think>(?P<content>.*)"
...
2026-05-21 21:04:48.236 | INFO     | flexeval.core.evaluate_chat_response:evaluate_chat_response:181 - Example of the output
2026-05-21 21:04:48.236 | INFO     | flexeval.core.evaluate_chat_response:evaluate_chat_response:182 - {
    "lm_output": {
        "text": "\n\n四国地方には以下の4つの都道府県があります。それぞれの県庁所在地を以下に列挙します。\n\n1. **香川県(かわかん)**  \n   - **県庁所在地**:香川市(かわかし)\n\n2. **岡山県(おうさんけん)**  \n   - **県庁所在地**:岡山市(おうさんし)\n\n3. **徳島県(とくしまけん)**  \n   - **県庁所在地**:徳島市(とくしまし)\n\n4. **広島県(ひろしまけん)**  \n   - **県庁所在地**:広島市(ひろしまし)\n\n※四国地方は日本の四つの地方の一つで、これらの4県が含まれます。",
        "raw_text": "<think>\nOkay, the user is asking for the four prefectures in the Shikoku region and their respective prefectural capitals. Let me start by recalling the four prefectures in Shikoku. I think they are Kagawa, Okayama, Tokushima, and Hiroshima. Wait, but I need to make sure. Let me think again. Shikoku is the fourth largest island in Japan, right? The four prefectures there are Kagawa, Okayama, Tokushima, and Hiroshima. Yes, that's correct.\n\nNow, the prefectural capitals. For Kagawa, I believe the capital is Kagawa City. Okayama's capital is Okayama City. Tokushima's capital is Tokushima City. And Hiroshima's capital is Hiroshima City. Let me double-check each one to be sure. Kagawa City is indeed the capital of Kagawa Prefecture. Okayama City is the capital of Okayama Prefecture. Tokushima City is the capital of Tokushima Prefecture. Hiroshima City is the capital of Hiroshima Prefecture. That seems right. \n\nWait, but sometimes people might confuse the capitals. For example, Hiroshima is a major city, but is it the capital? Yes, Hiroshima City is the capital of Hiroshima Prefecture. Okay, so the answer should be the four prefectures and their capitals as listed. I think that's all. Let me just confirm once more. Yes, those are the four prefectures in Shikoku, and their capitals are as mentioned. No mistakes there.\n</think>\n\n四国地方には以下の4つの都道府県があります。それぞれの県庁所在地を以下に列挙します。\n\n1. **香川県(かわかん)**  \n   - **県庁所在地**:香川市(かわかし)\n\n2. **岡山県(おうさんけん)**  \n   - **県庁所在地**:岡山市(おうさんし)\n\n3. **徳島県(とくしまけん)**  \n   - **県庁所在地**:徳島市(とくしまし)\n\n4. **広島県(ひろしまけん)**  \n   - **県庁所在地**:広島市(ひろしまし)\n\n※四国地方は日本の四つの地方の一つで、これらの4県が含まれます。",
        "reasoning_text": "\nOkay, the user is asking for the four prefectures in the Shikoku region and their respective prefectural capitals. Let me start by recalling the four prefectures in Shikoku. I think they are Kagawa, Okayama, Tokushima, and Hiroshima. Wait, but I need to make sure. Let me think again. Shikoku is the fourth largest island in Japan, right? The four prefectures there are Kagawa, Okayama, Tokushima, and Hiroshima. Yes, that's correct.\n\nNow, the prefectural capitals. For Kagawa, I believe the capital is Kagawa City. Okayama's capital is Okayama City. Tokushima's capital is Tokushima City. And Hiroshima's capital is Hiroshima City. Let me double-check each one to be sure. Kagawa City is indeed the capital of Kagawa Prefecture. Okayama City is the capital of Okayama Prefecture. Tokushima City is the capital of Tokushima Prefecture. Hiroshima City is the capital of Hiroshima Prefecture. That seems right. \n\nWait, but sometimes people might confuse the capitals. For example, Hiroshima is a major city, but is it the capital? Yes, Hiroshima City is the capital of Hiroshima Prefecture. Okay, so the answer should be the four prefectures and their capitals as listed. I think that's all. Let me just confirm once more. Yes, those are the four prefectures in Shikoku, and their capitals are as mentioned. No mistakes there.\n",
...

@junya-takayama junya-takayama force-pushed the add_reasoning_parser branch from 3827b57 to 9661b17 Compare May 21, 2026 09:57
@junya-takayama junya-takayama marked this pull request as ready for review May 21, 2026 12:06
@junya-takayama junya-takayama changed the title [WIP] Add ReasoningParser Add ReasoningParser May 21, 2026
Comment thread flexeval/core/language_model/hf_lm.py Outdated
]
lm_outputs = self._batch_complete_text(chat_messages_as_string, **kwargs)
if self.reasoning_parser:
for lm_output in lm_outputs:

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ここではtoolのところにある

            if lm_output.text is None:
                continue

的な処理はいらないんですかね?

@junya-takayama junya-takayama Jun 1, 2026

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

そもそもtool, reasoning でfor文分かれてる意味ないなと思ったのでまとめた上で一番上にセットしました!
https://github.com/sbintuitions/flexeval/pull/287/changes#diff-9c4eb04cb0e3beb9678e4067391a29b6bfb2c25b6f1cf1de4729ddb6dba932deR429-R431

Comment thread flexeval/core/language_model/hf_lm.py Outdated
lm_output.text = reasoning.text
lm_output.reasoning_text = reasoning.reasoning_text

if self.tool_parser:

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

resoning parserとtool_parserの同時使用って想定されていますか?
想定しているなら下でraw_textとかが上書きされてしまうのでまずい気がします

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

同時使用はあり得ます。
想定としては raw_text は何であれ後処理が入る前のテキストが入るので、そうなるように修正しました
(commit 操作ミスって色々くっついちゃいましたが…)

https://github.com/sbintuitions/flexeval/pull/287/changes#diff-9c4eb04cb0e3beb9678e4067391a29b6bfb2c25b6f1cf1de4729ddb6dba932deR435-R437

base.py: https://github.com/sbintuitions/flexeval/pull/287/changes#diff-3f56c1a60f9931bd179b6a566c4391ce34db99296812dc1590020d26de1f69aaR240-R243

@junya-takayama junya-takayama force-pushed the add_reasoning_parser branch from 6a5523e to 7dc3f8c Compare June 1, 2026 05:59
@junya-takayama

Copy link
Copy Markdown
Collaborator Author

@Kotaro-Aono レビューありがとうございます!諸々対応しました

Comment thread tests/core/language_model/test_hf_lm.py Outdated
response = chat_lm.generate_chat_response([{"role": "user", "content": "test"}], max_new_tokens=1)
assert response.raw_text == raw_output
assert response.text is None
assert response.reasoning_text is raw_output

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ここが悪さしてそうです
reasoning_text == None
raw_output == "no think tags here"
なので

@Kotaro-Aono Kotaro-Aono left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@junya-takayama junya-takayama merged commit 9b0fe54 into main Jun 2, 2026
8 checks passed
@junya-takayama junya-takayama deleted the add_reasoning_parser branch June 2, 2026 04:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants