以下の内容はhttps://nikkie-ftnext.hatenablog.com/entry/run-geval-implementation-script-with-few-summevalより取得しました。


テキスト(要約など)をGPT-4 を使って評価する手法、G-Eval(2023年)の実装スクリプトを動かす

はじめに

七尾百合子さん、お誕生日 231日目 おめでとうございます! nikkieです。

G-Eval がどんな手法なのかをつかみたく、データを絞って実装スクリプトを動かしました。

目次

「G-Eval: NLG Evaluation using Gpt-4 with Better Human Alignment」

自然言語生成タスク(例:要約)の評価を GPT-4 を用いて行い、人間による評価とより一致する手法 G-Eval を提案した論文1
EMNLP 2023 採択

著者による実装。
SummEval という要約のデータセットで実験した結果を公開しています

手法の概要は Figure 1 (p.2)

SummEval には評価観点が4つあるのですが、それぞれでプロンプトを用意。
例えば coherence(一貫性)の評価プロンプトについて
https://github.com/nlpyang/geval/blob/6f84404ce4503d2ba0cde0293c5d402ecf2976a8/prompts/summeval/coh_detailed.txt

  • 著者が用意したのは2つ
    • Task Introduction (1~5行目)
    • Evaluation Criteria (7~9行目)
  • Chain-of-Thought で LLM が Evaluatin Steps を生成 (11~15行目)

評価手順を人間が用意するのではなくLLM に生成させるのが、この手法の工夫の1つです2

このプロンプトに元の文章{{Document}}と生成された要約{{Summary}}を加えて、GPT-4 に coherence の観点を評価させます。
評価は「- Coherence:」に続く形で 1-5 のスコアだけを返させます。
また、1回の評価で20の結果を返させ3、その平均とすることでトークン生成確率を考慮します。
この確率考慮も工夫の1つです

データを絞って gpt4_eval.py を動かす

coherence が高い例と低い例4に絞りました。
doc_id: dm-test-8764fb95bfad8ee849274873a92fb8d6b400eee2

source5

Paul Merson has restarted his row with Andros Townsend after the Tottenham midfielder was brought on with only seven minutes remaining in his team 's 0-0 draw with Burnley on Sunday . 'Just been watching the game , did you miss the coach ? # RubberDub # 7minutes , ' Merson put on Twitter . Merson initially angered Townsend for writing in his Sky Sports column that 'if Andros Townsend can get in ( the England team ) then it opens it up to anybody . ' Paul Merson had another dig at Andros Townsend after his appearance for Tottenham against Burnley Townsend was brought on in the 83rd minute for Tottenham as they drew 0-0 against Burnley Andros Townsend scores England 's equaliser in their 1-1 friendly draw with Italy in Turin on Tuesday night The former Arsenal man was proven wrong when Townsend hit a stunning equaliser for England against Italy and he duly admitted his mistake . 'It 's not as though I was watching hoping he would n't score for England , I 'm genuinely pleased for him and fair play to him – it was a great goal , ' Merson said . 'It 's just a matter of opinion , and my opinion was that he got pulled off after half an hour at Manchester United in front of Roy Hodgson , so he should n't have been in the squad . 'When I 'm wrong , I hold my hands up . I do n't have a problem with doing that - I 'll always be the first to admit when I 'm wrong . ' Townsend hit back at Merson on Twitter after scoring for England against Italy Sky Sports pundit Merson ( centre ) criticised Townsend 's call-up to the England squad last week Townsend hit back at Merson after netting for England in Turin on Wednesday , saying 'Not bad for a player that should be 'nowhere near the squad ' ay @ PaulMerse ? ' Any bad feeling between the pair seemed to have passed but Merson was unable to resist having another dig at Townsend after Tottenham drew at Turf Moor .

イギリスサッカー界の舌戦のようです(解説者 vs 選手)

M13 の要約 (system_output) は coherence 2.33(低め)

Paul merson has restarted his row with andros townsend . The tottenham midfielder was brought on with only seven minutes remaining in his team 's 0-0 draw with burnley . Andros townsend scores england 's equaliser in their 1-1 friendly draw with italy in turin .

M20 の要約は coherence 4.67(高め)

Paul Merson is not happy with Andros Townsend 's call-up to the England squad last week

この coherece が再現するのか確認します6

$ uv run gpt4_eval.py --prompt prompts/summeval/coh_detailed.txt --summeval data/summeval2.json --save gpt4o_coh_detailed2.json --model gpt-4o

  • スクリプトで実装できているので inline script metadata を追加
  • openai ライブラリをメジャーバージョン 1 に上げました
  • API キーは、環境変数OPENAI_API_KEYで渡せます(引数で渡さない)
  • 論文の記載(3.1 p.3)に合わせて temperature を 2 から 1 に下げます7
  • max_token引数は「- Coherence: 3」という6トークンのケースも拾うために 5 から 6 に
    • max_completion_tokens引数8にすると動きが違うような

summeval2.json はこちら
https://gist.github.com/ftnext/ebe71cf756560b83a910c902db4885cb

動かせたが、再現している?

2件のデータについて3つのモデルで試しましたが、再現したとは言いがたい結果でした。
OpenAI が提供するモデルのアップデートのために、なかなか再現しないです。

gpt-4o
https://gist.github.com/ftnext/bfd92639f50c9937c868f10044d9a842
プロンプト無視。そもそも整数のスコアが出せずボロボロで、採用難しそう

gpt-4-turbo
https://gist.github.com/ftnext/911c366b2d8b54c1520f28b85d45123e
coherence が低い方を 3-4、高い方を2とスコア付け。
この2件では人間とは真逆

gpt-4.1
https://gist.github.com/ftnext/5098bc938bc4be6e9c480663cc3c0feb
どちらもcoherenceは2が多い

たった2件だけなので、もっと多くで試せば感触は変わるかもしれません。
プロンプトの Evaluation Steps を論文当時のモデルで生成している点が現行モデルと相性よくない可能性もありますよね。そこからやり直す?

ただ OpenAI のモデルや API の変更の影響が大きいために、再現性を高く維持するのが難しそうです。
GPT-5 ではこのスクリプトは動きませんでした(max_completion_tokensの指定に変える必要があるが、6トークンのままでは推論で使い尽くしてスコアが出力されず)

費用面では、1リクエストが800トークン程度でした

終わりに

G-Eval、論文を拾い読み & 実装を動かして理解した工夫は以下です。
生成された文章の評価において

  • 観点ごとの評価手順を LLM に生成させられる(Task Introduction と Evaluation Criteria から Evaluation Steps を作って、観点を評価するプロンプトができた)
  • 観点評価プロンプトに対して複数候補生成させ、スコアの平均を取る(自信のある 4 と、自信のない3~4をスコア上は区別できる)

今回はデータを絞ったため、論文の結果が2025年時点においても再現するかは、手を動かす余地があります(OpenAI の提供するモデルの変更が大きい)。
続きで手を動かすときには、SummEval データセットや人間による評価と一致の示し方についても今より理解したいですね

LLM をアプリケーションに組み込むに当たって、LLM が生成した文章の評価の機会はそれなりにあるととらえています。
評価手順作成に LLM を使ったり、複数候補でスコア付けさせて平均を取ったりという工夫は活かせるところがありそうに思いました


  1. 日本語で読めるまとめもありました
  2. 人間が考えた評価手順を伝えることも過去にやってました
  3. なお、APIパラメタにlogprobsを見つけました(top_logprobsは0から20まで) https://platform.openai.com/docs/api-reference/chat/create#chat_create-logprobs
  4. SummEval データセットはそこまで分かっていないですが、人間によるスコアの平均なのかなと思っています
  5. G-Eval では SummEval データセットの reference は見ていないようです
  6. argparse の引数指定は一意に決まるならば途中まででよいことを使っています。https://docs.python.org/ja/3/library/argparse.html#prefix-matching
  7. temperature=1 は「トレーニングデータ上の出現確率分布」(『LLMのプロンプトエンジニアリング』2.4 より)
  8. including visible output tokens and reasoning tokens.https://platform.openai.com/docs/api-reference/chat/create#chat_create-max_completion_tokens



以上の内容はhttps://nikkie-ftnext.hatenablog.com/entry/run-geval-implementation-script-with-few-summevalより取得しました。
このページはhttp://font.textar.tv/のウェブフォントを使用してます

不具合報告/要望等はこちらへお願いします。
モバイルやる夫Viewer Ver0.14