2024-08-23 20:43:29 +08:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
# @Date : 8/23/2024 20:00 PM
|
|
|
|
|
# @Author : didi
|
2024-10-17 15:47:09 +08:00
|
|
|
# @Desc : Entrance of AFlow.
|
2024-08-23 20:43:29 +08:00
|
|
|
|
2024-10-16 11:44:01 +08:00
|
|
|
from examples.aflow.scripts.optimizer import Optimizer
|
2024-10-21 23:08:51 +08:00
|
|
|
from examples.aflow.data.download_data import download
|
2024-08-26 08:40:10 +08:00
|
|
|
from metagpt.configs.models_config import ModelsConfig
|
2024-10-16 11:44:01 +08:00
|
|
|
from typing import Literal
|
2024-08-26 08:40:10 +08:00
|
|
|
|
2024-10-16 11:44:01 +08:00
|
|
|
# DatasetType, QuestionType, and OptimizerType definitions
|
2024-10-16 19:50:50 +08:00
|
|
|
DatasetType = Literal["HumanEval", "MBPP", "GSM8K", "MATH", "HotpotQA", "DROP"]
|
2024-10-18 13:50:51 +08:00
|
|
|
QuestionType = Literal["math", "code", "qa"]
|
2024-10-16 11:44:01 +08:00
|
|
|
OptimizerType = Literal["Graph", "Test"]
|
2024-08-26 08:40:10 +08:00
|
|
|
|
2024-10-21 23:08:51 +08:00
|
|
|
# When you fisrt use, please download the datasets and initial rounds; If you want to get a look of the results, please download the results.
|
|
|
|
|
# download(["datasets", "results", "initial_rounds"])
|
|
|
|
|
|
2024-09-25 16:46:20 +08:00
|
|
|
# Crucial Parameters
|
2024-10-21 23:08:51 +08:00
|
|
|
dataset: DatasetType = "GSM8K" # Ensure the type is consistent with DatasetType
|
2024-10-16 11:44:01 +08:00
|
|
|
sample: int = 4 # Sample Count, which means how many workflows will be resampled from generated workflows
|
2024-10-21 23:08:51 +08:00
|
|
|
question_type: QuestionType = "code" # Ensure the type is consistent with QuestionType
|
2024-10-16 11:44:01 +08:00
|
|
|
optimized_path: str = "examples/aflow/scripts/optimized" # Optimized Result Save Path
|
|
|
|
|
initial_round: int = 1 # Corrected the case from Initial_round to initial_round
|
|
|
|
|
max_rounds: int = 20
|
|
|
|
|
check_convergence: bool = True
|
2024-09-25 16:46:20 +08:00
|
|
|
|
2024-10-17 15:47:09 +08:00
|
|
|
# Config llm model, you can modify `config/config2.yaml` to use more llms.
|
2024-09-25 16:46:20 +08:00
|
|
|
mini_llm_config = ModelsConfig.default().get("gpt-4o-mini")
|
2024-09-02 16:47:03 +08:00
|
|
|
claude_llm_config = ModelsConfig.default().get("claude-3-5-sonnet-20240620")
|
2024-09-25 16:46:20 +08:00
|
|
|
|
2024-10-17 15:47:09 +08:00
|
|
|
# Config operators.
|
2024-09-25 16:46:20 +08:00
|
|
|
operators = [
|
2024-10-18 13:50:51 +08:00
|
|
|
"Custom", # It's basic unit of a fixed node. optimizer can modify its prompt to get vairous nodes.
|
|
|
|
|
# "AnswerGenerate" # It's for qa
|
|
|
|
|
# "CustomCodeGenerate", # It's for code
|
2024-10-21 23:08:51 +08:00
|
|
|
"ScEnsemble", # It's for code, math and qa
|
2024-10-18 13:50:51 +08:00
|
|
|
# "Test", # It's for code
|
2024-10-21 23:08:51 +08:00
|
|
|
"Programmer", # It's for math
|
2024-08-26 08:40:10 +08:00
|
|
|
]
|
|
|
|
|
|
2024-09-25 16:46:20 +08:00
|
|
|
# Create an optimizer instance
|
2024-08-26 08:40:10 +08:00
|
|
|
optimizer = Optimizer(
|
2024-10-17 15:47:09 +08:00
|
|
|
dataset=dataset, # Config dataset
|
|
|
|
|
question_type=question_type, # Config Question Type
|
|
|
|
|
opt_llm_config=claude_llm_config, # Config Optimizer LLM
|
|
|
|
|
exec_llm_config=mini_llm_config, # Config Execution LLM
|
|
|
|
|
check_convergence=check_convergence, # Whether Early Stop
|
|
|
|
|
operators=operators, # Config Operators you want to use
|
|
|
|
|
optimized_path=optimized_path, # Config Optimized workflow's file path
|
|
|
|
|
sample=sample, # Only Top(sample) rounds will be selected.
|
|
|
|
|
initial_round=initial_round, # Optimize from initial round
|
|
|
|
|
max_rounds=max_rounds # The max iteration of AFLOW.
|
2024-08-26 08:40:10 +08:00
|
|
|
)
|
|
|
|
|
|
2024-10-16 11:44:01 +08:00
|
|
|
if __name__ == "__main__":
|
2024-10-17 15:47:09 +08:00
|
|
|
# Optimize workflow via setting the optimizer's mode to 'Graph'
|
2024-10-16 11:44:01 +08:00
|
|
|
optimizer.optimize("Graph")
|
2024-10-17 15:47:09 +08:00
|
|
|
# Test workflow via setting the optimizer's mode to 'Test'
|
2024-10-21 23:08:51 +08:00
|
|
|
# optimizer.optimize("Test")
|