Files
NexQuant/rdagent/components/coder/factor_coder/CoSTEER/scheduler.py
T

90 lines
2.8 KiB
Python
Raw Normal View History

2024-06-14 12:59:44 +08:00
import json
from pathlib import Path
2024-07-05 17:42:00 +08:00
from typing import Dict
2024-06-14 12:59:44 +08:00
from jinja2 import Environment, StrictUndefined
2024-07-05 17:42:00 +08:00
from rdagent.components.coder.factor_coder.CoSTEER.evolvable_subjects import (
FactorEvolvingItem,
)
2024-07-05 17:42:00 +08:00
from rdagent.components.coder.factor_coder.utils import get_data_folder_intro
from rdagent.core.conf import RD_AGENT_SETTINGS
from rdagent.core.prompts import Prompts
2024-07-05 17:42:00 +08:00
from rdagent.core.scenario import Scenario
2024-07-17 15:00:13 +08:00
from rdagent.log import rdagent_logger as logger
from rdagent.oai.llm_utils import APIBackend
2024-06-14 12:59:44 +08:00
scheduler_prompts = Prompts(file_path=Path(__file__).parent.parent / "prompts.yaml")
2024-06-14 12:59:44 +08:00
def RandomSelect(to_be_finished_task_index, implementation_factors_per_round):
import random
2024-06-14 12:59:44 +08:00
to_be_finished_task_index = random.sample(
to_be_finished_task_index,
implementation_factors_per_round,
)
2024-07-16 20:35:42 +08:00
logger.info(f"The random selection is: {to_be_finished_task_index}")
2024-06-14 12:59:44 +08:00
return to_be_finished_task_index
2024-07-05 17:42:00 +08:00
def LLMSelect(
to_be_finished_task_index,
implementation_factors_per_round,
evo: FactorEvolvingItem,
former_trace: Dict,
scen: Scenario,
):
2024-06-14 12:59:44 +08:00
tasks = []
for i in to_be_finished_task_index:
# find corresponding former trace for each task
target_factor_task_information = evo.sub_tasks[i].get_task_information()
2024-06-14 12:59:44 +08:00
if target_factor_task_information in former_trace:
tasks.append((i, evo.sub_tasks[i], former_trace[target_factor_task_information]))
2024-06-14 12:59:44 +08:00
system_prompt = (
Environment(undefined=StrictUndefined)
.from_string(
scheduler_prompts["select_implementable_factor_system"],
)
.render(
2024-07-05 17:42:00 +08:00
scenario=scen.get_scenario_all_desc(),
)
2024-06-14 12:59:44 +08:00
)
for _ in range(10): # max attempt to reduce the length of user_prompt
user_prompt = (
Environment(undefined=StrictUndefined)
.from_string(
scheduler_prompts["select_implementable_factor_user"],
)
.render(
factor_num=implementation_factors_per_round,
sub_tasks=tasks,
)
2024-06-14 12:59:44 +08:00
)
if (
2024-07-05 17:42:00 +08:00
APIBackend().build_messages_and_calculate_token(
user_prompt=user_prompt,
system_prompt=system_prompt,
2024-06-14 12:59:44 +08:00
)
< RD_AGENT_SETTINGS.chat_token_limit
2024-06-14 12:59:44 +08:00
):
break
2024-07-05 17:42:00 +08:00
response = APIBackend().build_messages_and_create_chat_completion(
2024-06-14 12:59:44 +08:00
user_prompt=user_prompt,
2024-07-05 17:42:00 +08:00
system_prompt=system_prompt,
2024-06-14 12:59:44 +08:00
json_mode=True,
)
try:
selection = json.loads(response)["selected_factor"]
if not isinstance(selection, list):
return to_be_finished_task_index
selection_index = [x for x in selection if isinstance(x, int)]
except:
return to_be_finished_task_index
2024-06-14 12:59:44 +08:00
return selection_index