
這是“立即:使用語言模型的戰略計劃實現專家級及時的優化”的官方回購。即將出現的是一種新型的自動及時及時的方法,自主的設計提示質量與專家手工製作的質量相當,即專家級別的提示。 [arxiv]


git clone https://github.com/XinyuanWangCS/PromptAgent.git
cd PromptAgent
conda create -n prompt_agent
conda activate prompt_agent
pip install -r requirements.txt以下命令可以立即運行以製定專家提示,以完成大型台式任務,Penguins_in_a_table。運行可能需要一些時間,具體取決於OpenAI API的推理速度和數據集的大小。
注意:在運行此命令之前,請在example_config.yaml文件(base_model_setting:api_key和optim_model_setting:api_key)中添加(OpenAI)API鍵。您還可以檢查YAML文件中的所有其他8個。
python src/main.py --config_dir example_config.yaml penguins_in_a_table是一項理解任務,以回答表中包含的動物問題。原始數據集中的一個示例如下:
Here is a table where the first line is a header and each subsequent line is a penguin:
name, age, height (cm), weight (kg)
Louis, 7, 50, 11
Bernard, 5, 80, 13
Vincent, 9, 60, 11
Gwen, 8, 70, 15
For example: the age of Louis is 7, the weight of Gwen is 15 kg, the height of
Bernard is 80 cm.
Which penguin is taller than the other ones? Answer:
然後,預期的結果是Bernard 。
大基礎數據集中的最初查詢是Answer questions about a table of penguins and their attributes.從如此普通的提示開始,即將立即採樣模型錯誤(來自基本模型),生成錯誤反饋(操作),模擬未來的獎勵,並蒐索導致專家提示的高回報路徑。 penguins_in_a_table的優化提示將看起來像這樣(確切的結果可能會有所不同,因為這不是確定性的):
As you delve into a dataset of penguins, assess essential attributes like names, ages,
and gender. Decode the significance of each attribute in the context of every penguin
while keeping in mind that the dataset may be modified, including addition or removal
of penguins. When such modifications are made, immediately revise your understanding,
redo your computations, and ensure that your subsequent calculations consider these
changes. The crux of your task is to identify relationships and patterns within
the attributes, giving special attention to the names and ages of the penguins.
For complex tasks, break them down into manageable chunks ensuring no essential detail
is missed. When a change is made to the dataset, recompute your values taking into
consideration these changes, paying extra attention to cumulative computations. Ensure
that your understanding of ’more than’, ’less than’, and ’equal to’ is precise and
that you correctly interpret these in context of the question.
...
運行上述實驗大約需要兩個小時,該實驗使用OpenAI API(GPT-4的4美元約為4美元,GPT-3.5 $ 4)。完成優化後,所有中間節點和路徑都將存儲在JSON文件中。我們將保持頂級獎勵節點,最後一個節點處於最高平均獎勵路徑中,而最高獎勵節點則是最高的平均獎勵路徑。在本文中,我們將最高獎勵節點在最高的平均獎勵路徑中用作選擇策略。
我們可以運行test.py來測試具有以下命令的任何提示性能:
在命令行中輸入提示:
python src/test.py --task_name bigbench --prompt " Answer questions about a table of penguins and their attributes. " --prompt_file " prompt file path " --train_size 70 --eval_size 50 --test_size 79 --seed 42 --base_model_type " openai " --base_model_name ' gpt-3.5-turbo ' --data_dir " datasets/penguins_in_a_table.json " --base_api_key " your_api "或者
如果提示很長,請在.txt文件中放置提示:
python src/test.py --task_name bigbench --prompt_file " prompt file path " --train_size 70 --eval_size 50 --test_size 79 --seed 42 --base_model_type " openai " --base_model_name ' gpt-3.5-turbo ' --data_dir " datasets/penguins_in_a_table.json " --base_api_key " your_api " 如果您使用的是HuggingFace TextGeneration模型,請在.YAML文件中修改base_model_setting或optim_model_setting。如果您打算使用開源型號,我們建議使用具有適度尺寸的指令調整模型,例如Mistralai/Mistral-7B-7B-Instruct-V0.2。正如我們在論文中提到的那樣,專家級提示已準備好用於相對高級的LLM。
注意:您可以修改擁抱面模型的參數(例如max_new_tokens),因為這些模型可能具有不同的輸入窗口或其他設置。
這是使用Mistralai/Mistral-7B-Instruct-V0.2:
base_model_setting:
model_type: hf_textgeneration # openai | palm | hf_text2text | hf_textgeneration | ct_model
model_name: mistralai/Mistral-7B-Instruct-v0.2 # api-based model'name or huggingface model name
temperature: 0.0
api_key: null # if need api key
device: cuda # cuda | cpu | cuda:x, e.g. 0,1,2...
model_path: null # ct model requires the downloaded model's path 您可以添加一個新的.py文件,包括新型號。模型的類需要兩個功能:batch_forward_func:輸入一批提示,輸出一批模型的響應。
def batch_forward_func(self, batch_prompts: List(str)):
...
return List(str)生成:輸入一個提示,輸出一個響應
def generate(self, input: str):
...
return str然後,您可以在Lagandy_model文件夾中的init .py中添加model_type名稱和類名。如果您遇到任何問題,也可以與我們聯繫,或者想添加到官方的即將起來的回購中。
我們的基本任務類可以在task/base_task.py文件中看到,其中詳細說明了任務特定功能。我們當前的任務包括選擇問題任務和NER任務。添加新的選擇任務相對容易。請參閱任務文件夾中的.py文件。首先,創建一個新的Task.py文件和一個新的CustomTask類。然後,在您的自定義任務中可以實現多個特定於任務的功能。
之後,您可以在自定義數據集上立即運行!
如果您發現紙張和代碼很有用,請友善地將此倉庫播放,並引用以下紙張。請隨時聯繫[email protected]和[email protected],如果您有任何疑問,請打開問題。非常感謝!
@article { wang2023promptagent ,
title = { PromptAgent: Strategic Planning with Language Models Enables Expert-level Prompt Optimization } ,
author = { Wang, Xinyuan and Li, Chenxi and Wang, Zhen and Bai, Fan and Luo, Haotian and Zhang, Jiayou and Jojic, Nebojsa and Xing, Eric P and Hu, Zhiting } ,
journal = { arXiv preprint arXiv:2310.16427 } ,
year = { 2023 }
}