自動プロンプト最適化ツール「Prompt Fix AGI」とは!?
開発者や研究者が日々直面する課題の一つに、大規模言語モデル(LLM)へのどのようにプロンプトを入れたら期待した回答が得られるかといった課題があります。正確で詳細な回答を得るためには、プロンプトの質が非常に重要です。この問題を解決するために開発されたのが、Prompt Fix AGIです。
こちらが、Githubのリポジトリです。
(もしこのリポジトリが少しでも役に立った、または誤って役に立ったと錯覚した場合は、忘れぬようスターを付けてくれたら幸いです!あなたのスターが開発者のモチベーションに繋がります)
Prompt Fix AGIとは?
Prompt Fix AGIは、ユーザーが入力したプロンプトと期待されるLLMの回答を基に、プロンプトを自動的に調整し、改善するツールです。このプロセスを通じて、プロンプトを明確かつ詳細にし、LLMが期待される回答に近い形で応答できるようにします。
動作プロセス
- プロンプトと期待されるLLMの回答を
prompt_fix_agent
に入力します。 -
prompt_fix_agent
から修正されたプロンプトが返ってきます。 - 修正されたプロンプトを使用してLLMから実際の回答を取得します。
- 実際の回答と期待される回答を比較し、
answer_check_agent
が類似度に基づいてスコアを割り当てます。 - スコアが設定された閾値を超えると、プロセスが終了します。
- 閾値以下の場合、
prompt_fix_agent
へ修正されたプロンプトと期待されるLLMの回答を入力とし、ステップ2から繰り返します。
prompt_fix_agent()
関数は、ユーザーのプロンプトとLLMからの期待される回答を入力として受け取り、修正されたプロンプトを生成します。修正されたプロンプトは、与えられたプロンプトの潜在的な問題を特定し、より適切な形に修正します。
answer_check_agent()
関数は、LLMから得られた回答と期待される回答を入力として受け取り、その内容の類似性に基づいてスコアを割り当てます。具体的には、回答の内容と表現の類似性を比較し、期待される回答との一致度を評価します。
変数
minimum_point
は、処理を停止する最小の閾値ポイントです。実際の回答と期待される回答を比較した類似度のポイントで、0〜10点で表されます。
retry
はリトライの回数です。Noneの場合、リトライの回数は無限です。retryが設定されている場合、ポイントが最小閾値ポイントを超えなくても、設定された回数に達した時点でループから抜けます。
test_case
は、ユーザープロンプトとLLMからの期待される回答のリストです。
使い方
- GitHubからリポジトリをクローンし、クローンしたリポジトリに移動する。
git clone https://github.com/mk668a/prompt-fix-agi.git
cd prompt-fix-agi
- 必要なパッケージをインストールする。
pip install -r requirements.txt
- .env.exampleファイルを.envにコピーする。
cp .env.example .env
- OpenAI APIキーをOPENAI_API_KEY、モデルをOPENAI_API_MODELに設定。
-
prompt-fix-agi.py
内の変数retry
、minimum_point
、test_case
を設定。 - スクリプトを実行する。
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