大規模言語モデル (LLM) を使用したシステムの構築 (7): 評価 2

今日はDeepLearning.AIのBuilding Systems with LLMのオンラインコースを受講したので、その主な内容の一部を共有したいと思います。私たちは以前に次の知識を学びました。

大規模言語モデル (LLM) を使用したシステムの構築 (1): 分類
大規模言語モデル (LLM) を使用したシステムの構築 (2): コンテンツの監査とプロンプト インジェクションの防止
大規模言語モデル (LLM) を使用したシステムの構築 (3) : 思考連鎖推論
大規模言語モデル (LLM) 構築システムの使用 (4): 連鎖プロンプト
大規模言語モデル (LLM) 構築システムの使用 (5): 出力チェック
大型言語モデル (LLM) 構築システムの使用 (6): エンドの構築Large Language Model (LLM) を用いたエンドツーエンドシステム構築システム(7): 評価1

LLM モデルにアクセスするためのメイン コードは次のとおりです。

import os
import openai
import sys
sys.path.append('../..')
import utils
from dotenv import load_dotenv, find_dotenv
_ = load_dotenv(find_dotenv()) # read local .env file

openai.api_key  = os.environ["OPENAI_API_KEY"]
 
def get_completion_from_messages(messages, 
                                 model="gpt-3.5-turbo", 
                                 temperature=0, 
                                 max_tokens=500):
    response = openai.ChatCompletion.create(
        model=model,
        messages=messages,
        temperature=temperature, 
        max_tokens=max_tokens,
    )
    return response.choices[0].message["content"]

エンドツーエンド システムを実行してユーザーの質問に回答します

ここでは、ツールキット utils を使用して、電子製品に関するユーザーの質問に対する一連の回答を実行します。utils の機能の原理については、以前のブログで説明されているため、ここでは繰り返しません。

customer_msg = f"""
tell me about the smartx pro phone and the fotosnap camera, the dslr one.
Also, what TVs or TV related products do you have?"""

products_by_category = utils.get_products_from_query(customer_msg)
category_and_product_list = utils.read_string_to_list(products_by_category)
product_info = utils.get_mentioned_product_info(category_and_product_list)
assistant_answer = utils.answer_user_msg(user_msg=customer_msg,
                                                   product_info=product_info)

ここでは、誰もが意味をよりよく理解できるように、customer_msg を中国語に翻訳します。

 ここでは、まず utils.get_products_from_query メソッドを呼び出して、ユーザーの質問に関係する製品カテゴリのリストを取得します (このプロセスには LLM へのアクセスが必要です)。LLM から返される製品カタログ リストは文字列型であるため、utils.read_string_to_list メソッドを呼び出して文字列を Python の List 形式に変換し、すべての製品カタログ リストに含まれるすべての製品の特定の情報をクエリする必要もあります (これは(LLM にアクセスする必要はありません)、最後にユーザーの質問を特定の製品情報とともに LLM に送信し、最終応答を生成します。上記の変数の具体的な内容を見てみましょう。

 最後に、LLM の最終応答を見てみましょう。

 顧客の質問は複数の電子製品に関するものであるため、LLM の回答の情報量が多すぎます。LLM の回答が要件を満たしているかどうかをどのように評価できますか?

抽出された製品情報に基づいて、スコア基準を使用してユーザーに対する LLM の応答を評価します。

 ここでは、顧客からの質問、特定の製品情報、LLM の最終回答の 3 つの部分に基づいて、LLM の最終回答が適格であるかどうかを評価するためのスコア基準を作成する必要があります。つまり、これら 3 つの部分を統合し、LLM に前の応答が適格であるかどうかを評価させる必要があります。次に、これらの関数を実現するために eval_with_rubric 関数を定義します。

def eval_with_rubric(test_set, assistant_answer):

    cust_msg = test_set['customer_msg']
    context = test_set['context']
    completion = assistant_answer
    
    system_message = """\
    You are an assistant that evaluates how well the customer service agent \
    answers a user question by looking at the context that the customer service \
    agent is using to generate its response. 
    """

    user_message = f"""\
You are evaluating a submitted answer to a question based on the context \
that the agent uses to answer the question.
Here is the data:
    [BEGIN DATA]
    ************
    [Question]: {cust_msg}
    ************
    [Context]: {context}
    ************
    [Submission]: {completion}
    ************
    [END DATA]

Compare the factual content of the submitted answer with the context. \
Ignore any differences in style, grammar, or punctuation.
Answer the following questions:
    - Is the Assistant response based only on the context provided? (Y or N)
    - Does the answer include information that is not provided in the context? (Y or N)
    - Is there any disagreement between the response and the context? (Y or N)
    - Count how many questions the user asked. (output a number)
    - For each question that the user asked, is there a corresponding answer to it?
      Question 1: (Y or N)
      Question 2: (Y or N)
      ...
      Question N: (Y or N)
    - Of the number of questions asked, how many of these questions were addressed by the answer? (output a number)
"""

    messages = [
        {'role': 'system', 'content': system_message},
        {'role': 'user', 'content': user_message}
    ]

    response = get_completion_from_messages(messages)
    return response

 ここでは、誰もがよりよく理解できるように、system_message と user_message を中国語に翻訳します。

 次に、LLM の以前の応答を評価します。

cust_prod_info = {
    'customer_msg': customer_msg,
    'context': product_info
}

evaluation_output = eval_with_rubric(cust_prod_info, assistant_answer)
print(evaluation_output)

 ここでは評価基準を定義します。つまり、user_message で定義した 6 つの質問に LLM に回答させます。これらの 6 つの質問が採点基準となります。上記の出力から、LLM は 6 つの質問すべてに正解しました。

ユーザーに対する LLM の回答を、「理想的な」/「専門家」(人間が生成した)回答と比較して評価します。

LLM の回答の効果を評価するには、LLM に簡単な質問 (これらの質問は単純な統計の場合もあります) に回答するよう求めることに加えて、人間が生成した専門家や理想的な回答との違いを評価することも必要です。以下に理想的/専門的な答えを定義します。

test_set_ideal = {
    'customer_msg': """\
tell me about the smartx pro phone and the fotosnap camera, the dslr one.
Also, what TVs or TV related products do you have?""",
    'ideal_answer':"""\
Of course!  The SmartX ProPhone is a powerful \
smartphone with advanced camera features. \
For instance, it has a 12MP dual camera. \
Other features include 5G wireless and 128GB storage. \
It also has a 6.1-inch display.  The price is $899.99.

The FotoSnap DSLR Camera is great for \
capturing stunning photos and videos. \
Some features include 1080p video, \
3-inch LCD, a 24.2MP sensor, \
and interchangeable lenses. \
The price is 599.99.

For TVs and TV related products, we offer 3 TVs \


All TVs offer HDR and Smart TV.

The CineView 4K TV has vibrant colors and smart features. \
Some of these features include a 55-inch display, \
'4K resolution. It's priced at 599.

The CineView 8K TV is a stunning 8K TV. \
Some features include a 65-inch display and \
8K resolution.  It's priced at 2999.99

The CineView OLED TV lets you experience vibrant colors. \
Some features include a 55-inch display and 4K resolution. \
It's priced at 1499.99.

We also offer 2 home theater products, both which include bluetooth.\
The SoundMax Home Theater is a powerful home theater system for \
an immmersive audio experience.
Its features include 5.1 channel, 1000W output, and wireless subwoofer.
It's priced at 399.99.

The SoundMax Soundbar is a sleek and powerful soundbar.
It's features include 2.1 channel, 300W output, and wireless subwoofer.
It's priced at 199.99

Are there any questions additional you may have about these products \
that you mentioned here?
Or may do you have other questions I can help you with?
    """
}

LLM の回答を専門家の回答で評価する

以下では、 OpenAI evalsプロジェクトのプロンプトを使用して、 BLEU (バイリンガル評価理解)評価を実装します。

BLEUスコア: 2 つのテキストが類似しているかどうかを評価する別の方法。

def eval_vs_ideal(test_set, assistant_answer):

    cust_msg = test_set['customer_msg']
    ideal = test_set['ideal_answer']
    completion = assistant_answer
    
    system_message = """\
    You are an assistant that evaluates how well the customer service agent \
    answers a user question by comparing the response to the ideal (expert) response
    Output a single letter and nothing else. 
    """

    user_message = f"""\
You are comparing a submitted answer to an expert answer on a given question. Here is the data:
    [BEGIN DATA]
    ************
    [Question]: {cust_msg}
    ************
    [Expert]: {ideal}
    ************
    [Submission]: {completion}
    ************
    [END DATA]

Compare the factual content of the submitted answer with the expert answer. Ignore any differences in style, grammar, or punctuation.
    The submitted answer may either be a subset or superset of the expert answer, or it may conflict with it. Determine which case applies. Answer the question by selecting one of the following options:
    (A) The submitted answer is a subset of the expert answer and is fully consistent with it.
    (B) The submitted answer is a superset of the expert answer and is fully consistent with it.
    (C) The submitted answer contains all the same details as the expert answer.
    (D) There is a disagreement between the submitted answer and the expert answer.
    (E) The answers differ, but these differences don't matter from the perspective of factuality.
  choice_strings: ABCDE
"""

    messages = [
        {'role': 'system', 'content': system_message},
        {'role': 'user', 'content': user_message}
    ]

    response = get_completion_from_messages(messages)
    return response

誰もがよりよく理解できるように、上記を中国語に翻訳します。

 次に、LLM の答えと専門家の (理想的な) 答えを比較し、最終的にスコア (A、B、C、D、E) を与えます。

eval_vs_ideal(test_set_ideal, assistant_answer)

 

 評価結果から、LLM は自身の回答で A を獲得しました。これは、LLM の以前の回答が専門家の回答のサブセットであり、専門家の回答と完全に一致していることを意味します。各製品についての十分な理解. 非常に詳細な指示が作成されており、これらの指示の一部はシステムの製品情報に含まれていないため、LLM の回答は専門家の回答の一部ですが、これで問題はありません。LLM の回答は体系的な製品情報に基づいており、LLM が製品情報の内容を歪曲する可能性はなく、専門家の回答内容の一部と完全に一致しています。別の間違った LLM 応答を試して、専門家の回答と比較してみましょう。

assistant_answer_2 = "life is like a box of chocolates"
eval_vs_ideal(test_set_ideal, assistant_answer_2)

 ここでは、「人生はチョコレートの箱のようなものです」という商品情報とは関係のないLLM回答をしたとしますが、このときの評価結果はDスコアとなり、提出された回答と差があることになります。専門家の答え。

要約する

今日、私たちは LLM の応答を評価する 2 つの方法を使用する方法を学びました。1 つ目は、LLM が答えるための簡単な質問を設計することです。2 つ目は、人間の専門家の回答を使用して、LLM の回答と専門家の回答の違いを評価することです。 give スコア (A、B、C、D、E) を与えます。

おすすめ

転載: blog.csdn.net/weixin_42608414/article/details/131375176