BedrockでQAタスクを実行する

  •  
 
ズィスト2024年4月11日 - 17:11 に投稿

タグ

QAを実行する方法を紹介する。 今回は特定の車種のタイヤの交換方法を答えるQAを作成する。

モデルは知識ベースのデータに対してクエリを実行して正確な情報を含む応答を生成する。 生成AIモデルは確率分布を使用して質問に対する応答を生成する。モデルは膨大なテキストデータでトレーニングされているため、シーケンスや単語の次に何が続くかを予測できる。 ただし、モデルには確実性がないためすべての質問に対して正確な答えを返すことはできない。

プロンプトに知識ベースを追加することで正確な答えを返すことができるようになる。 この方法は短い知識ベースでで機能するが、知識ベースが長い場合は使用できない可能性がある。

シナリオ

以下の順番でモデルに対して質問を行う。

  1. モデルに対して実在する車種のタイヤの交換に関する質問をする
  2. モデルに対して実在しない車種のタイヤの交換に関する質問をする
  3. プロンプトにタイヤの交換方法に関するマニュアルを追加した状態で1と同じ質問をする

上記の2つにおいてモデルは正確な答えを返すことができないことがわかる。 また、モデルが正確に回答をしているように見えても実際は架空の答えであることがわかる。

正確な答えを返すようにするために特定のメーカーと車のモデルに関する追加データを使用してモデルをトレーニングする必要がある。 今回はプロンプトにプロンプトにタイヤの交換方法に関するマニュアルを追加してモデルを学習させる。 これはZero Shotと呼ばれている方法である。

モデルに対して実在する車種のタイヤの交換に関する質問をする

以下のようにAudiのタイヤ修理方法についてモデルに質問する。

prompt_data = """You are an helpful assistant. Answer questions in a concise way. If you are unsure about the
answer say 'I am unsure'

Question: How can I fix a flat tire on my Audi A8?
Answer:"""
parameters = {
    "maxTokenCount":512,
    "stopSequences":[],
    "temperature":0,
    "topP":0.9
    }

body = json.dumps({"inputText": prompt_data, "textGenerationConfig": parameters})
modelId = "amazon.titan-tg1-large"  # change this to use a different version from the model provider
accept = "application/json"
contentType = "application/json"
try:

    response = boto3_bedrock.invoke_model(
        body=body, modelId=modelId, accept=accept, contentType=contentType
    )
    response_body = json.loads(response.get("body").read())
    answer = response_body.get("results")[0].get("outputText")
    print_ww(answer.strip())

except botocore.exceptions.ClientError as error:
    if  error.response['Error']['Code'] == 'AccessDeniedException':
        print(f"\x1b[41m{error.response['Error']['Message']}\
        \nTo troubeshoot this issue please refer to the following resources.\
         \nhttps://docs.aws.amazon.com/IAM/latest/UserGuide/troubleshoot_access-denied.html\
         \nhttps://docs.aws.amazon.com/bedrock/latest/userguide/security-iam.html\x1b[0m\n")      
        class StopExecution(ValueError):
            def _render_traceback_(self):
                pass
        raise StopExecution        
    else:
        raise error

これを実行すると以下のようなレスポンスになる。

I am unsure.

モデルに対して実在しない車種のタイヤの交換に関する質問をする

架空のメーカーと車種について同様のことを行うと以下のようになる。

プログラム

prompt_data = "How can I fix a flat tire on my Amazon Tirana?"
body = json.dumps({"inputText": prompt_data, 
                   "textGenerationConfig": parameters})
modelId = "amazon.titan-tg1-large"  # change this to use a different version from the model provider
accept = "application/json"
contentType = "application/json"

response = boto3_bedrock.invoke_model(
    body=body, modelId=modelId, accept=accept, contentType=contentType
)
response_body = json.loads(response.get("body").read())
answer = response_body.get("results")[0].get("outputText")
print_ww(answer.strip())

応答

Here are the steps to fix a flat tire on your Amazon Tirana:
1. Locate the spare tire, jack, and lug wrench in the trunk of your Amazon Tirana.
2. Use the lug wrench to loosen the lug nuts on the flat tire, but do not remove them yet.
3. Position the jack under the car near the flat tire.
4. Use the jack handle to raise the car until the flat tire is off the ground.
5. Completely remove the lug nuts and the flat tire.
6. Mount the spare tire on the wheel hub, making sure the lug bolts are properly aligned.
7. Tighten the lug nuts by hand until they are snug.
8. Use the lug wrench to tighten the lug nuts in a star pattern until they are very tight.
9. Lower the car back to the ground using the jack.
10. Use the lug wrench to tighten the lug nuts again in a star pattern.
11. Check the tire pressure of the spare tire to make sure it is properly inflated.

それらしい答えになっているが、存在しない車のため間違っていることは明らかである。

プロンプトにマニュアルを追加する

モデルが回答できなかったり、架空の答えを返す問題を解決するためには、プロンプトの一部として追加の知識を提供し、LLMの知識を補強すればよい。

以下はAudi A8のマニュアルの一部である。

Tires and tire pressure:

Tires are made of black rubber and are mounted on the wheels of your vehicle. They provide the necessary grip for driving, cornering, and braking. Two important factors to consider are tire pressure and tire wear, as they can affect the performance and handling of your car.

Where to find recommended tire pressure:

You can find the recommended tire pressure specifications on the inflation label located on the driver's side B-pillar of your vehicle. Alternatively, you can refer to your vehicle's manual for this information. The recommended tire pressure may vary depending on the speed and the number of occupants or maximum load in the vehicle.

Reinflating the tires:

When checking tire pressure, it is important to do so when the tires are cold. This means allowing the vehicle to sit for at least three hours to ensure the tires are at the same temperature as the ambient temperature.

To reinflate the tires:

    Check the recommended tire pressure for your vehicle.
    Follow the instructions provided on the air pump and inflate the tire(s) to the correct pressure.
    In the center display of your vehicle, open the "Car status" app.
    Navigate to the "Tire pressure" tab.
    Press the "Calibrate pressure" option and confirm the action.
    Drive the car for a few minutes at a speed above 30 km/h to calibrate the tire pressure.

Note: In some cases, it may be necessary to drive for more than 15 minutes to clear any warning symbols or messages related to tire pressure. If the warnings persist, allow the tires to cool down and repeat the above steps.

Flat Tire:

If you encounter a flat tire while driving, you can temporarily seal the puncture and reinflate the tire using a tire mobility kit. This kit is typically stored under the lining of the luggage area in your vehicle.

Instructions for using the tire mobility kit:

    Open the tailgate or trunk of your vehicle.
    Lift up the lining of the luggage area to access the tire mobility kit.
    Follow the instructions provided with the tire mobility kit to seal the puncture in the tire.
    After using the kit, make sure to securely put it back in its original location.
    Contact Rivesla or an appropriate service for assistance with disposing of and replacing the used sealant bottle.

Please note that the tire mobility kit is a temporary solution and is designed to allow you to drive for a maximum of 10 minutes or 8 km (whichever comes first) at a maximum speed of 80 km/h. It is advisable to replace the punctured tire or have it repaired by a professional as soon as possible.

プロンプトにこのテキストを追加する。また、このテキストに従って回答するようにプロントに指示を追加する。

プログラム

context = """Tires and tire pressure:

Tires are made of black rubber and are mounted on the wheels of your vehicle. They provide the necessary grip for driving, cornering, and braking. Two important factors to consider are tire pressure and tire wear, as they can affect the performance and handling of your car.

Where to find recommended tire pressure:

You can find the recommended tire pressure specifications on the inflation label located on the driver's side B-pillar of your vehicle. Alternatively, you can refer to your vehicle's manual for this information. The recommended tire pressure may vary depending on the speed and the number of occupants or maximum load in the vehicle.

Reinflating the tires:

When checking tire pressure, it is important to do so when the tires are cold. This means allowing the vehicle to sit for at least three hours to ensure the tires are at the same temperature as the ambient temperature.

To reinflate the tires:

    Check the recommended tire pressure for your vehicle.
    Follow the instructions provided on the air pump and inflate the tire(s) to the correct pressure.
    In the center display of your vehicle, open the "Car status" app.
    Navigate to the "Tire pressure" tab.
    Press the "Calibrate pressure" option and confirm the action.
    Drive the car for a few minutes at a speed above 30 km/h to calibrate the tire pressure.

Note: In some cases, it may be necessary to drive for more than 15 minutes to clear any warning symbols or messages related to tire pressure. If the warnings persist, allow the tires to cool down and repeat the above steps.

Flat Tire:

If you encounter a flat tire while driving, you can temporarily seal the puncture and reinflate the tire using a tire mobility kit. This kit is typically stored under the lining of the luggage area in your vehicle.

Instructions for using the tire mobility kit:

    Open the tailgate or trunk of your vehicle.
    Lift up the lining of the luggage area to access the tire mobility kit.
    Follow the instructions provided with the tire mobility kit to seal the puncture in the tire.
    After using the kit, make sure to securely put it back in its original location.
    Contact Audi or an appropriate service for assistance with disposing of and replacing the used sealant bottle.

Please note that the tire mobility kit is a temporary solution and is designed to allow you to drive for a maximum of 10 minutes or 8 km (whichever comes first) at a maximum speed of 80 km/h. It is advisable to replace the punctured tire or have it repaired by a professional as soon as possible."""

question = "How can I fix a flat tire on my Audi A8?"
prompt_data = f"""Answer the question based only on the information provided between ## and give step by step guide.
#
{context}
#

Question: {question}
Answer:"""

body = json.dumps({"inputText": prompt_data, "textGenerationConfig": parameters})
modelId = "amazon.titan-tg1-large"  # change this to use a different version from the model provider
accept = "application/json"
contentType = "application/json"

response = boto3_bedrock.invoke_model(
    body=body, modelId=modelId, accept=accept, contentType=contentType
)
response_body = json.loads(response.get("body").read())
answer = response_body.get("results")[0].get("outputText")
print_ww(answer.strip())

応答

If you encounter a flat tire while driving, you can temporarily seal the puncture and reinflate the
tire using a tire mobility kit. This kit is typically stored under the lining of the luggage area in
your vehicle.

Instructions for using the tire mobility kit:

1. Open the tailgate or trunk of your vehicle.
2. Lift up the lining of the luggage area to access the tire mobility kit.
3. Follow the instructions provided with the tire mobility kit to seal the puncture in the tire.
4. After using the kit, make sure to securely put it back in its original location.
5. Contact Audi or an appropriate service for assistance with disposing of and replacing the used
sealant bottle.

Please note that the tire mobility kit is a temporary solution and is designed to allow you to drive
for a maximum of 10 minutes or 8 km (whichever comes first) at a maximum speed of 80 km/h. It is
advisable to replace the punctured tire or have it repaired by a professional as soon as possible.

プロンプトに知識ベースを追加することで正確な答えを返すことがわかる。

まとめ

今回は特定の車種についてのタイヤ交換の方法についてモデルに回答を生成してもらったが、他のタスクでも同様に学習データが存在しない場合は正確な答えを生成できない可能性がある。
その場合は、プロンプトに学習データを追加することで解決することがわかった。
しかし、学習データが大きい場合はこの方法を使用することはできない。これを解決するための方法としてRAGがある。次回はRAGについて紹介する。

コメントを追加

プレーンテキスト

  • HTMLタグは利用できません。
  • 行と段落は自動的に折り返されます。
  • ウェブページのアドレスとメールアドレスは自動的にリンクに変換されます。
CAPTCHA
この質問はあなたが人間の訪問者であるかどうかをテストし、自動化されたスパム送信を防ぐためのものです。