BioErrorLog Tech Blog

試行錯誤の記録

LLMの回答を自動評価するOpenAIプラクティス

OpenAIのプロンプトエンジニアリングガイドに従って、LLMの回答を自動評価するプラクティスを整理します。

はじめに

LLMアプリケーションの開発、あるいはプロンプトエンジニアリングの過程では、そのLLM出力を評価する仕組みが重要です。 出力評価の仕組みなくしては、チューニングの方針に迷うことになるでしょう。

前回は、LangCheckというツールを使ってLLM回答評価する方法を整理しました: www.bioerrorlog.work

今回は、特にツールは使わず、OpenAIがプロンプトエンジニアリングガイドの中で推奨している方法をもとにLLM回答評価を行うプラクティスをまとめます。

LLM回答の自動評価ベストプラクティス

プロンプトに変更を加えたとき、その変更が良い影響を及ぼすのかあるいは悪い影響を及ぼしてしまうのかを評価する重要なことですが、難しいことでもあります。

ある入力に対しては良い影響があったとしても他の入力には悪影響が出てたりしますし、テストの数が少なければその評価の信憑性も上がりません。

より精緻な回答評価をするには、多くのサンプルサイズが必要になる | 画像はOpenAIドキュメントより

良い回答評価の仕組みは、以下の特徴を持ちます。

  • 実際のユースケースを反映したものである
    (もしくは少なくとも多様なテストケースを含んでいる)
  • 統計的にも有意であるよう、十分な数のテストケースを持っている
  • 自動化、繰り返しが容易である

LLM回答評価は、コンピュータに自動でやらせることも、人間がやることも、またはその混合でも可能です。

コンピュータによる自動評価では、LLMモデルに回答評価をさせることで、客観的指標(例: 理想回答との照合)、あるいはより主観的で曖昧な基準で評価することが可能です。 そのようなモデルベース評価は、回答が多岐に渡りうる場合(例: 長文回答が必要な質問ケース)で特に有効です。

自動でモデルベース評価できるケースと、人間による評価が必要なケースの線引きは曖昧です。 モデルの性能が向上するに従って、モデルベース評価が可能な範囲も増えています。

どのようなテストケースを自動化するかは、都度ユースケースにしたがって実験することが推奨されます。

回答評価の具体例

では、具体的にLLMを使って回答評価する方法を見ていきます。

例1. ある既知の事実が回答に含まれているか評価する

例えば、LLMの回答にある既知の事実が含まれている必要があるときは、以下のようなシステムプロンプトでLLMに判定させます。

You will be provided with text delimited by triple quotes that is supposed to be the answer to a question. Check if the following pieces of information are directly contained in the answer:

- Neil Armstrong was the first person to walk on the moon.
- The date Neil Armstrong first walked on the moon was July 21, 1969.

For each of these points perform the following steps:

1 - Restate the point.  
2 - Provide a citation from the answer which is closest to this point.  
3 - Consider if someone reading the citation who doesn't know the topic could directly infer the point. Explain why or why not before making up your mind.  
4 - Write "yes" if the answer to 3 was yes, otherwise write "no".  

Finally, provide a count of how many "yes" answers there are. Provide this count as {"count": <insert count here>}.

プロンプト指示内容:

  • 3連クォートで質問への回答が与えられます。
  • 回答に以下の情報が含まれているかをチェックしてください。
    • ニールアームストロングは月の上を歩いた最初の人物である。
    • ニールアームストロングが初めて月を歩いた日付は1969年の7月21日である。
  • これらの確認ポイントそれぞれについて、以下のステップで評価を実行してください。
    • 1 - 確認ポイントを再度述べる。
    • 2 - 確認ポイントに最も近い部分を回答から引用する。
    • 3 - このトピックを知らない人が引用文を読んで、そのポイントを直接推測できるかどうかを考える。なぜそうなのか、なぜそうでないのかを説明した上で判断すること。
    • 4 - ステップ3がyesであれば"yes"と出力し、そうでなければ"no"と出力する。
  • 最後に、いくつ"yes"だったかをカウントしてください。
    • カウント結果を{"count": <insert count here>}で出力してください。


では、この評価プロンプトで実際に回答を評価してみます。

まずは、両方の事実を含む下記の回答をユーザープロンプトに与えて、回答評価してみます。

"""Neil Armstrong is famous for being the first human to set foot on the Moon. This historic event took place on July 21, 1969, during the Apollo 11 mission."""

結果がこちら:

1 - Neil Armstrong was the first person to walk on the moon.
- Citation: "Neil Armstrong is famous for being the first human to set foot on the Moon."
- Explanation: The citation directly states that Neil Armstrong was the first human to walk on the moon. Someone reading this sentence would not need any additional information to understand that Neil Armstrong was the first person to do so.
- Answer: yes

2 - The date Neil Armstrong first walked on the moon was July 21, 1969.
- Citation: "This historic event took place on July 21, 1969, during the Apollo 11 mission."
- Explanation: The citation directly states the date Neil Armstrong first walked on the moon. Someone reading this sentence would be able to understand the specific date of the event without needing further explanation.
- Answer: yes

{"count": 2}

評価実行結果のOpenAI Playground画面 | 事実を2つ含むケース

想定通り、指定した実行ステップに従った後、{"count": 2}として2つの事実が含まれていることが判定されています。


次は、片方の事実を含む下記の回答をユーザープロンプトに与えて、回答評価してみます。

"""Neil Armstrong made history when he stepped off the lunar module, becoming the first person to walk on the moon."""

結果はこちら:

1 - Neil Armstrong was the first person to walk on the moon.
- Citation: "Neil Armstrong made history when he stepped off the lunar module, becoming the first person to walk on the moon."
- Yes, someone reading the citation without prior knowledge on the topic could directly infer that Neil Armstrong was the first person to walk on the moon because it is explicitly stated.

2 - The date Neil Armstrong first walked on the moon was July 21, 1969.
- Citation: N/A
- No, the date of July 21, 1969, is not mentioned in the provided text, so someone without prior knowledge could not infer this information from the citation.

{"count": 1}

評価実行結果のOpenAI Playground画面 | 事実を1つ含むケース

こちらもまた指定した実行ステップに従った後、{"count": 1}として1つの事実が含まれていることが判定されました。


最後に、事実を含まない下記の回答をユーザープロンプトに与えて、回答評価してみます。

"""In the summer of '69, a voyage grand, Apollo 11, bold as legend's hand. Armstrong took a step, history unfurled, "One small step," he said, for a new world."""

結果がこちら:

1. Neil Armstrong was the first person to walk on the moon.
- Citation: "Armstrong took a step, history unfurled."
- Explanation: The citation implies that Armstrong's action of taking a step was historically significant, suggesting it was a first or pioneering action. However, without prior knowledge, one might not directly infer that he was the first person to walk on the moon, only that his action was important.
- Answer: no

2. The date Neil Armstrong first walked on the moon was July 21, 1969.
- Citation: "In the summer of '69, a voyage grand."
- Explanation: The citation provides the year (1969) and implies the event took place in the summer, but it does not specify the exact date of July 21. Without prior knowledge, one could not directly infer the specific date from this information.
- Answer: no

{"count": 0}

評価実行結果のOpenAI Playground画面 | 事実を含まないケース

{"count": 0}として、事実が含まれていないことが正しく判定されました。

例2. 理想回答との矛盾や重なりを評価する

リファレンスとする理想回答と、実際のLLM回答を突き合わせて評価するのも有効な方法です。

下記のシステムプロンプトは、理想回答と実際の回答を比較して、矛盾や重なり具合を評価させるための例です。

Use the following steps to respond to user inputs. Fully restate each step before proceeding. i.e. "Step 1: Reason...".

Step 1: Reason step-by-step about whether the information in the submitted answer compared to the expert answer is either: disjoint, equal, a subset, a superset, or overlapping (i.e. some intersection but not subset/superset).

Step 2: Reason step-by-step about whether the submitted answer contradicts any aspect of the expert answer.

Step 3: Output a JSON object structured like: {"type_of_overlap": "disjoint" or "equal" or "subset" or "superset" or "overlapping", "contradiction": true or false}

プロンプト指示内容:

  • 下記のステップでユーザーインプットを評価してください。
    • 実行前にそれぞれのステップを再記述してください。
  • ステップ1: 理想回答と比較して、提出された回答の情報が、不一致、一致、サブセット、スーパーセット、重複(交差はあるが、サブセット/スーパーセットではない)のいずれかであるかどうか、ステップバイステップで評価する。
  • ステップ2: 提出された回答が、理想回答と矛盾しているかどうかをステップバイステップで評価する。
  • ステップ3: 次のような構造のJSONを出力する:
    {"type_of_overlap": "disjoint" or "equal" or "subset" or "superset" or "overlapping", "contradiction": true or false}


では、この評価プロンプトで実際に回答を評価してみます。

まずは、理想回答と矛盾はしてないものの、基準には満たない回答を評価してみます。

Question: """What event is Neil Armstrong most famous for and on what date did it occur? Assume UTC time."""

Submitted Answer: """Didn't he walk on the moon or something?"""

Expert Answer: """Neil Armstrong is most famous for being the first person to walk on the moon. This historic event occurred on July 21, 1969."""

結果はこちら:

Step 1: Reason step-by-step about whether the information in the submitted answer compared to the expert answer is either: disjoint, equal, a subset, a superset, or overlapping (i.e. some intersection but not subset/superset).

- The submitted answer states that Neil Armstrong is known for walking on the moon, which is a part of the information provided in the expert answer.
- The expert answer provides additional details, including the specific date of the event (July 21, 1969) and the fact that Armstrong was the first person to walk on the moon.
- Therefore, the information in the submitted answer is a subset of the information provided in the expert answer because it contains less detail but is accurate within the context of the broader information provided by the expert.

Step 2: Reason step-by-step about whether the submitted answer contradicts any aspect of the expert answer.

- The submitted answer does not contradict the expert answer. It acknowledges Neil Armstrong's moonwalk, which aligns with the expert's identification of Armstrong's most famous event.
- The submitted answer lacks the specificity and additional details found in the expert answer but does not present any information that would directly conflict with the facts provided by the expert.

Step 3: Output a JSON object structured like: {"type_of_overlap": "disjoint" or "equal" or "subset" or "superset" or "overlapping", "contradiction": true or false}.

{"type_of_overlap": "subset", "contradiction": false}

評価実行結果のOpenAI Playground画面 | 理想回答と矛盾はしてないものの基準には満たない回答のケース

結果は{"type_of_overlap": "subset", "contradiction": false}ということで、実際の回答が理想回答のサブセットであり矛盾はしてない旨が正しく判定されました。


続いて、理想回答と矛盾している回答を評価してみます。

Question: """What event is Neil Armstrong most famous for and on what date did it occur? Assume UTC time."""

Submitted Answer: """On the 21st of July 1969, Neil Armstrong became the second person to walk on the moon, following after Buzz Aldrin."""

Expert Answer: """Neil Armstrong is most famous for being the first person to walk on the moon. This historic event occurred on July 21, 1969."""

結果はこちら:

Step 1: Reason step-by-step about whether the information in the submitted answer compared to the expert answer is either: disjoint, equal, a subset, a superset, or overlapping (i.e. some intersection but not subset/superset).

- Both answers identify the event Neil Armstrong is most famous for as walking on the moon.
- Both answers provide the same date for the event: July 21, 1969.
- The submitted answer incorrectly states that Neil Armstrong was the second person to walk on the moon, while the expert answer correctly identifies him as the first.
- Given that both answers discuss the same event and date, but differ in the detail about Armstrong's order of walking on the moon, the information is overlapping. They share common information about the event and its date but differ in a significant detail regarding Armstrong's role.

Step 2: Reason step-by-step about whether the submitted answer contradicts any aspect of the expert answer.

- The submitted answer contradicts the expert answer regarding who was the first person to walk on the moon. The submitted answer incorrectly states that Armstrong was the second, directly contradicting the expert answer's correct statement that he was the first.
- This contradiction is a direct opposition of facts between the two answers.

Step 3: Output a JSON object structured like: {"type_of_overlap": "overlapping", "contradiction": true}.

{"type_of_overlap": "overlapping", "contradiction": true}

評価実行結果のOpenAI Playground画面 | 理想回答と矛盾する回答のケース

結果は{"type_of_overlap": "overlapping", "contradiction": true}となり、重複するものの矛盾した回答であることが判定できています。


最後に、必要以上に詳細に説明した正解の回答を試してみます。

Question: """What event is Neil Armstrong most famous for and on what date did it occur? Assume UTC time."""

Submitted Answer: """At approximately 02:56 UTC on July 21st 1969, Neil Armstrong became the first human to set foot on the lunar surface, marking a monumental achievement in human history."""

Expert Answer: """Neil Armstrong is most famous for being the first person to walk on the moon. This historic event occurred on July 21, 1969."""

結果はこちら:

Step 1: Reason step-by-step about whether the information in the submitted answer compared to the expert answer is either: disjoint, equal, a subset, a superset, or overlapping (i.e. some intersection but not subset/superset).

- Both answers identify Neil Armstrong's most famous event as being the first person to walk on the moon.
- Both answers provide the same date for this event: July 21, 1969.
- The submitted answer provides additional information about the exact time the event occurred (approximately 02:56 UTC) and describes it as a "monumental achievement in human history."
- Since the submitted answer contains all the information found in the expert answer plus additional details, the submitted answer is a superset of the expert answer.

Step 2: Reason step-by-step about whether the submitted answer contradicts any aspect of the expert answer.

- There is no contradiction between the submitted answer and the expert answer. Both agree on the event Neil Armstrong is most famous for and the date it occurred. The submitted answer simply provides more detailed information without contradicting the core facts presented in the expert answer.

Step 3: Output a JSON object structured like:

{
  "type_of_overlap": "superset",
  "contradiction": false
}

評価実行結果のOpenAI Playground画面 | 理想回答とより詳細な、矛盾しない回答のケース

結果は{"type_of_overlap": "superset", "contradiction": false}ということで、矛盾せずに理想回答のスーパーセットが回答されていることが正しく評価されました。

おわりに

OpenAIの推奨するプラクティスで、LLM回答のModel-based評価をやってみました。

素朴にLLMとプロンプトを活用してLLM回答評価をするのも、シンプルで取り組みやすいですね。 またOpenAIもLLM回答評価用のツールとしてEvalsを公開しているので、そちらも活用していきたいところです。

以上、どなたかの参考になれば幸いです。

[関連記事]

www.bioerrorlog.work

www.bioerrorlog.work

www.bioerrorlog.work

参考

Test changes systematically - OpenAI Prompt Engineering Guide

GitHub - openai/evals: Evals is a framework for evaluating LLMs and LLM systems, and an open-source registry of benchmarks.