
这是“立即:使用语言模型的战略计划实现专家级及时的优化”的官方回购。即将出现的是一种新型的自动及时及时的方法,自主的设计提示质量与专家手工制作的质量相当,即专家级别的提示。 [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 }
}