🧑‍✈️

自動プロンプト最適化ツール「Prompt Fix AGI」とは!?

2024/02/13に公開

開発者や研究者が日々直面する課題の一つに、大規模言語モデル(LLM)へのどのようにプロンプトを入れたら期待した回答が得られるかといった課題があります。正確で詳細な回答を得るためには、プロンプトの質が非常に重要です。この問題を解決するために開発されたのが、Prompt Fix AGIです。

こちらが、Githubのリポジトリです。

https://github.com/mk668a/prompt-fix-agi/tree/main
(もしこのリポジトリが少しでも役に立った、または誤って役に立ったと錯覚した場合は、忘れぬようスターを付けてくれたら幸いです!あなたのスターが開発者のモチベーションに繋がります)

Prompt Fix AGIとは?

Prompt Fix AGIは、ユーザーが入力したプロンプトと期待されるLLMの回答を基に、プロンプトを自動的に調整し、改善するツールです。このプロセスを通じて、プロンプトを明確かつ詳細にし、LLMが期待される回答に近い形で応答できるようにします。

動作プロセス

  1. プロンプトと期待されるLLMの回答をprompt_fix_agentに入力します。
  2. prompt_fix_agentから修正されたプロンプトが返ってきます。
  3. 修正されたプロンプトを使用してLLMから実際の回答を取得します。
  4. 実際の回答と期待される回答を比較し、answer_check_agentが類似度に基づいてスコアを割り当てます。
  5. スコアが設定された閾値を超えると、プロセスが終了します。
  6. 閾値以下の場合、prompt_fix_agentへ修正されたプロンプトと期待されるLLMの回答を入力とし、ステップ2から繰り返します。

prompt_fix_agent()関数は、ユーザーのプロンプトとLLMからの期待される回答を入力として受け取り、修正されたプロンプトを生成します。修正されたプロンプトは、与えられたプロンプトの潜在的な問題を特定し、より適切な形に修正します。

answer_check_agent()関数は、LLMから得られた回答と期待される回答を入力として受け取り、その内容の類似性に基づいてスコアを割り当てます。具体的には、回答の内容と表現の類似性を比較し、期待される回答との一致度を評価します。

変数

minimum_pointは、処理を停止する最小の閾値ポイントです。実際の回答と期待される回答を比較した類似度のポイントで、0〜10点で表されます。

retryはリトライの回数です。Noneの場合、リトライの回数は無限です。retryが設定されている場合、ポイントが最小閾値ポイントを超えなくても、設定された回数に達した時点でループから抜けます。

test_caseは、ユーザープロンプトとLLMからの期待される回答のリストです。

使い方

  1. GitHubからリポジトリをクローンし、クローンしたリポジトリに移動する。
git clone https://github.com/mk668a/prompt-fix-agi.git
cd prompt-fix-agi
  1. 必要なパッケージをインストールする。
pip install -r requirements.txt
  1. .env.exampleファイルを.envにコピーする。
cp .env.example .env
  1. OpenAI APIキーをOPENAI_API_KEY、モデルをOPENAI_API_MODELに設定。
  2. prompt-fix-agi.py内の変数retryminimum_pointtest_caseを設定。
  3. スクリプトを実行する。
python prompt-fix-agi.py

Dockerを使用する場合

docker-compose up

デフォルトでは、Pizza Exampleが実行されます。

prompt

what is pizza.

ideal_answer

- 500g of strong flour
- 200ml of water
- 1 teaspoon of salt
- 1 teaspoon of dry yeast
- 2 tablespoons of olive oil.

この例では、ideal_answerはピザの材料のリストです。プロンプトが「what is pizza.」であるため、LLMはideal_answerのような回答を出力しません。そのため、プロンプトは自動的に修正され、ideal_answerに類似した回答を得るようになります。

Start.

[prompt]
what is pizza.

[ideal_answer]

- 500g of strong flour
- 200ml of water
- 1 teaspoon of salt
- 1 teaspoon of dry yeast
- 2 tablespoons of olive oil.

------------------ loop 1 ------------------

[prompt_fix_agent fixed prompt]
what is pizza.
↓
Write down the necessary ingredients and measurements needed to make a basic pizza dough.

[llm answer]
To make a basic pizza dough, you will need the following ingredients and measurements:

- 2 ½ cups (300g) all-purpose flour
- 1 teaspoon (5g) granulated sugar
- 2 ¼ teaspoons (7g) active dry yeast
- 1 cup (240ml) warm water
- 2 tablespoons (30ml) olive oil
- 1 teaspoon (5g) salt

These measurements will yield approximately two medium-sized pizza crusts.

【point: 4.0】

------------------ loop 2 ------------------

[prompt_fix_agent fixed prompt]
Write down the necessary ingredients and measurements needed to make a basic pizza dough.
↓
Write down the necessary ingredients and their specific measurements needed to make a basic pizza dough. Be sure to include the quantities of each ingredient required.

[llm answer]
To make a basic pizza dough, you will need the following ingredients with their corresponding measurements:

- 2 ¼ cups (280 grams) of all-purpose flour
- 1 teaspoon of instant yeast
- ½ teaspoon of salt
- 1 teaspoon of sugar
- ¾ cup (180 milliliters) of warm water
- 2 tablespoons of olive oil

Please note that these measurements yield approximately two medium-sized pizza doughs.

【point: 6.0】

------------------ loop 3 ------------------

[prompt_fix_agent fixed prompt]
Write down the necessary ingredients and their specific measurements needed to make a basic pizza dough. Be sure to include the quantities of each ingredient required.
↓
List the specific quantities of each ingredient required to make a basic pizza dough. Include 500g of strong flour, 200ml of water, 1 teaspoon of salt, 1 teaspoon of dry yeast, and 2 tablespoons of olive oil.

[llm answer]
To make a basic pizza dough, you will need the following specific quantities of each ingredient:

- 500g of strong flour
- 200ml of water
- 1 teaspoon of salt
- 1 teaspoon of dry yeast
- 2 tablespoons of olive oil

【point: 10.0】

Done.

最後に

Prompt Fix AGIは単なる構想からスタートしましたが、現在はまだ基礎的な段階にあります。応答の評価にLLMではなく部分的な論理を採用したり、応答を評価する軸(例えば、文の構造、内容、長さなど)を設けたりするなど、改善の余地がたくさんあります。さらに、精度を高めるためにプロンプト自体の改善も可能です。みなさんのPRを心からお待ちしています。

Discussion