mirror of
https://github.com/NicolasBohn/NexQuant.git
synced 2026-08-04 02:37:44 +00:00
factor extraction pipeline ready (#16)
* run the code * update code * remove some redundant code --------- Co-authored-by: xuyang1 <xuyang1@microsoft.com>
This commit is contained in:
@@ -3,28 +3,28 @@ from __future__ import annotations
|
||||
import re
|
||||
from typing import List
|
||||
|
||||
from pandas.core.api import DataFrame as DataFrame
|
||||
|
||||
from core.evolving_framework import Evaluator as EvolvingEvaluator
|
||||
from core.evolving_framework import Feedback, QueriedKnowledge
|
||||
from core.log import FinCoLog
|
||||
from core.utils import multiprocessing_wrapper
|
||||
from factor_implementation.evolving.evolvable_subjects import (
|
||||
FactorImplementationList,
|
||||
)
|
||||
from factor_implementation.share_modules.conf import FactorImplementSettings
|
||||
from factor_implementation.share_modules.evaluator import (
|
||||
from rdagent.core.evolving_framework import Evaluator as EvolvingEvaluator
|
||||
from rdagent.core.evolving_framework import Feedback, QueriedKnowledge
|
||||
from rdagent.core.log import FinCoLog
|
||||
from rdagent.core.utils import multiprocessing_wrapper
|
||||
from rdagent.factor_implementation.evolving.evolvable_subjects import FactorImplementationList
|
||||
from rdagent.factor_implementation.share_modules.evaluator import (
|
||||
Evaluator as FactorImplementationEvaluator,
|
||||
)
|
||||
from factor_implementation.share_modules.evaluator import (
|
||||
from rdagent.factor_implementation.share_modules.evaluator import (
|
||||
FactorImplementationCodeEvaluator,
|
||||
FactorImplementationFinalDecisionEvaluator,
|
||||
FactorImplementationValueEvaluator,
|
||||
)
|
||||
from factor_implementation.share_modules.factor import (
|
||||
from rdagent.factor_implementation.share_modules.factor import (
|
||||
FactorImplementation,
|
||||
FactorImplementationTask,
|
||||
)
|
||||
from pandas.core.api import DataFrame as DataFrame
|
||||
|
||||
from rdagent.factor_implementation.share_modules.factor_implementation_config import (
|
||||
FactorImplementSettings,
|
||||
)
|
||||
|
||||
|
||||
class FactorImplementationSingleFeedback:
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from core.evolving_framework import EvolvableSubjects
|
||||
from core.log import FinCoLog
|
||||
from factor_implementation.share_modules.factor import (
|
||||
from rdagent.core.evolving_framework import EvolvableSubjects
|
||||
from rdagent.core.log import FinCoLog
|
||||
from rdagent.factor_implementation.share_modules.factor import (
|
||||
FactorImplementation,
|
||||
FactorImplementationTask,
|
||||
)
|
||||
|
||||
@@ -1,26 +1,29 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
from pathlib import Path
|
||||
import random
|
||||
from abc import abstractmethod
|
||||
from copy import deepcopy
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
from jinja2 import Template
|
||||
|
||||
from core.evolving_framework import EvolvingStrategy, QueriedKnowledge
|
||||
from core.utils import multiprocessing_wrapper
|
||||
from factor_implementation.share_modules.conf import FactorImplementSettings
|
||||
from factor_implementation.share_modules.factor import (
|
||||
from rdagent.core.evolving_framework import EvolvingStrategy, QueriedKnowledge
|
||||
from rdagent.core.utils import multiprocessing_wrapper
|
||||
from rdagent.factor_implementation.share_modules.factor import (
|
||||
FactorImplementation,
|
||||
FactorImplementationTask,
|
||||
FileBasedFactorImplementation,
|
||||
)
|
||||
from factor_implementation.share_modules.prompt import (
|
||||
FactorImplementationPrompts,
|
||||
from rdagent.core.prompts import Prompts
|
||||
from jinja2 import Template
|
||||
from rdagent.oai.llm_utils import APIBackend
|
||||
|
||||
from rdagent.factor_implementation.share_modules.factor_implementation_config import (
|
||||
FactorImplementSettings,
|
||||
)
|
||||
from rdagent.factor_implementation.share_modules.factor_implementation_utils import (
|
||||
get_data_folder_intro,
|
||||
)
|
||||
from factor_implementation.share_modules.utils import get_data_folder_intro
|
||||
from oai.llm_utils import APIBackend
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from factor_implementation.evolving.evolvable_subjects import (
|
||||
@@ -115,7 +118,9 @@ class FactorEvolvingStrategy(MultiProcessEvolvingStrategy):
|
||||
queried_former_failed_knowledge_to_render = queried_former_failed_knowledge
|
||||
|
||||
system_prompt = Template(
|
||||
FactorImplementationPrompts()["evolving_strategy_factor_implementation_v1_system"],
|
||||
Prompts(file_path=Path(__file__).parent.parent / "prompts.yaml")[
|
||||
"evolving_strategy_factor_implementation_v1_system"
|
||||
],
|
||||
).render(
|
||||
data_info=get_data_folder_intro(),
|
||||
queried_former_failed_knowledge=queried_former_failed_knowledge_to_render,
|
||||
@@ -128,7 +133,9 @@ class FactorEvolvingStrategy(MultiProcessEvolvingStrategy):
|
||||
while True:
|
||||
user_prompt = (
|
||||
Template(
|
||||
FactorImplementationPrompts()["evolving_strategy_factor_implementation_v1_user"],
|
||||
Prompts(file_path=Path(__file__).parent.parent / "prompts.yaml")[
|
||||
"evolving_strategy_factor_implementation_v1_user"
|
||||
],
|
||||
)
|
||||
.render(
|
||||
factor_information_str=factor_information_str,
|
||||
@@ -202,7 +209,9 @@ class FactorEvolvingStrategyWithGraph(MultiProcessEvolvingStrategy):
|
||||
queried_former_failed_knowledge_to_render = queried_former_failed_knowledge
|
||||
|
||||
system_prompt = Template(
|
||||
FactorImplementationPrompts()["evolving_strategy_factor_implementation_v1_system"],
|
||||
Prompts(file_path=Path(__file__).parent.parent / "prompts.yaml")[
|
||||
"evolving_strategy_factor_implementation_v1_system"
|
||||
],
|
||||
).render(
|
||||
data_info=get_data_folder_intro(),
|
||||
queried_former_failed_knowledge=queried_former_failed_knowledge_to_render,
|
||||
@@ -222,7 +231,11 @@ class FactorEvolvingStrategyWithGraph(MultiProcessEvolvingStrategy):
|
||||
and len(queried_former_failed_knowledge_to_render) != 0
|
||||
):
|
||||
error_summary_system_prompt = (
|
||||
Template(FactorImplementationPrompts()["evolving_strategy_error_summary_v2_system"])
|
||||
Template(
|
||||
Prompts(file_path=Path(__file__).parent.parent / "prompts.yaml")[
|
||||
"evolving_strategy_error_summary_v2_system"
|
||||
]
|
||||
)
|
||||
.render(
|
||||
factor_information_str=target_factor_task_information,
|
||||
code_and_feedback=queried_former_failed_knowledge_to_render[
|
||||
@@ -236,7 +249,11 @@ class FactorEvolvingStrategyWithGraph(MultiProcessEvolvingStrategy):
|
||||
)
|
||||
while True:
|
||||
error_summary_user_prompt = (
|
||||
Template(FactorImplementationPrompts()["evolving_strategy_error_summary_v2_user"])
|
||||
Template(
|
||||
Prompts(file_path=Path(__file__).parent.parent / "prompts.yaml")[
|
||||
"evolving_strategy_error_summary_v2_user"
|
||||
]
|
||||
)
|
||||
.render(
|
||||
queried_similar_component_knowledge=queried_similar_component_knowledge_to_render,
|
||||
)
|
||||
@@ -256,7 +273,9 @@ class FactorEvolvingStrategyWithGraph(MultiProcessEvolvingStrategy):
|
||||
|
||||
user_prompt = (
|
||||
Template(
|
||||
FactorImplementationPrompts()["evolving_strategy_factor_implementation_v2_user"],
|
||||
Prompts(file_path=Path(__file__).parent.parent / "prompts.yaml")[
|
||||
"evolving_strategy_factor_implementation_v2_user"
|
||||
],
|
||||
)
|
||||
.render(
|
||||
factor_information_str=target_factor_task_information,
|
||||
|
||||
@@ -4,32 +4,29 @@ import subprocess
|
||||
from pathlib import Path
|
||||
|
||||
import pandas as pd
|
||||
from fire.core import Fire
|
||||
from tqdm import tqdm
|
||||
|
||||
from core.evolving_framework import EvoAgent, KnowledgeBase
|
||||
from core.utils import multiprocessing_wrapper
|
||||
from factor_implementation.evolving.evaluators import (
|
||||
from rdagent.core.evolving_framework import EvoAgent, KnowledgeBase
|
||||
from rdagent.core.utils import multiprocessing_wrapper
|
||||
from rdagent.factor_implementation.evolving.evaluators import (
|
||||
FactorImplementationEvaluatorV1,
|
||||
FactorImplementationsMultiEvaluator,
|
||||
)
|
||||
from factor_implementation.evolving.evolvable_subjects import (
|
||||
FactorImplementationList,
|
||||
)
|
||||
from factor_implementation.evolving.evolving_strategy import (
|
||||
from rdagent.factor_implementation.evolving.evolvable_subjects import FactorImplementationList
|
||||
from rdagent.factor_implementation.evolving.evolving_strategy import (
|
||||
FactorEvolvingStrategy,
|
||||
FactorEvolvingStrategyWithGraph,
|
||||
)
|
||||
from factor_implementation.evolving.knowledge_management import (
|
||||
from rdagent.factor_implementation.evolving.knowledge_management import (
|
||||
FactorImplementationGraphKnowledgeBase,
|
||||
FactorImplementationGraphRAGStrategy,
|
||||
FactorImplementationKnowledgeBaseV1,
|
||||
FactorImplementationRAGStrategyV1,
|
||||
)
|
||||
from factor_implementation.share_modules.factor import (
|
||||
from rdagent.factor_implementation.share_modules.factor import (
|
||||
FactorImplementationTask,
|
||||
FileBasedFactorImplementation,
|
||||
)
|
||||
from fire.core import Fire
|
||||
from tqdm import tqdm
|
||||
|
||||
ALPHA101_INIT_COMPONENTS = [
|
||||
"1. abs(): absolute value to certain columns",
|
||||
@@ -107,11 +104,17 @@ class FactorImplementationEvolvingCli:
|
||||
def load_or_init_knowledge_base(self, former_knowledge_base_path: Path = None, component_init_list: list = []):
|
||||
if former_knowledge_base_path is not None and former_knowledge_base_path.exists():
|
||||
factor_knowledge_base = pickle.load(open(former_knowledge_base_path, "rb"))
|
||||
if self.evolving_version == 1 and not isinstance(
|
||||
factor_knowledge_base, FactorImplementationKnowledgeBaseV1,
|
||||
) or self.evolving_version == 2 and not isinstance(
|
||||
factor_knowledge_base,
|
||||
FactorImplementationGraphKnowledgeBase,
|
||||
if (
|
||||
self.evolving_version == 1
|
||||
and not isinstance(
|
||||
factor_knowledge_base,
|
||||
FactorImplementationKnowledgeBaseV1,
|
||||
)
|
||||
or self.evolving_version == 2
|
||||
and not isinstance(
|
||||
factor_knowledge_base,
|
||||
FactorImplementationGraphKnowledgeBase,
|
||||
)
|
||||
):
|
||||
raise ValueError("The former knowledge base is not compatible with the current version")
|
||||
else:
|
||||
@@ -259,7 +262,11 @@ class FactorImplementationEvolvingCli:
|
||||
print([feedback.final_decision if feedback is not None else None for feedback in feedbacks].count(True))
|
||||
|
||||
def implement_amc(
|
||||
self, evo_sub_path_str, former_knowledge_base_path_str, implementation_dump_path_str, slice_index,
|
||||
self,
|
||||
evo_sub_path_str,
|
||||
former_knowledge_base_path_str,
|
||||
implementation_dump_path_str,
|
||||
slice_index,
|
||||
):
|
||||
factor_implementations: FactorImplementationList = pickle.load(open(evo_sub_path_str, "rb"))
|
||||
factor_implementations.target_factor_tasks = factor_implementations.target_factor_tasks[
|
||||
|
||||
@@ -8,10 +8,7 @@ from itertools import combinations
|
||||
from pathlib import Path
|
||||
from typing import Union
|
||||
|
||||
from finco.graph import UndirectedGraph, UndirectedNode
|
||||
from jinja2 import Template
|
||||
|
||||
from core.evolving_framework import (
|
||||
from rdagent.core.evolving_framework import (
|
||||
EvolvableSubjects,
|
||||
EvoStep,
|
||||
Knowledge,
|
||||
@@ -19,19 +16,20 @@ from core.evolving_framework import (
|
||||
QueriedKnowledge,
|
||||
RAGStrategy,
|
||||
)
|
||||
from core.log import FinCoLog
|
||||
from factor_implementation.evolving.evaluators import (
|
||||
FactorImplementationSingleFeedback,
|
||||
)
|
||||
from factor_implementation.share_modules.conf import FactorImplementSettings
|
||||
from factor_implementation.share_modules.factor import (
|
||||
from rdagent.core.log import FinCoLog
|
||||
from rdagent.factor_implementation.evolving.evaluators import FactorImplementationSingleFeedback
|
||||
from rdagent.factor_implementation.share_modules.factor import (
|
||||
FactorImplementation,
|
||||
FactorImplementationTask,
|
||||
)
|
||||
from factor_implementation.share_modules.prompt import (
|
||||
FactorImplementationPrompts,
|
||||
from rdagent.core.prompts import Prompts
|
||||
from rdagent.knowledge_management.graph import UndirectedGraph, UndirectedNode
|
||||
from jinja2 import Template
|
||||
from rdagent.oai.llm_utils import APIBackend, calculate_embedding_distance_between_str_list
|
||||
|
||||
from rdagent.factor_implementation.share_modules.factor_implementation_config import (
|
||||
FactorImplementSettings,
|
||||
)
|
||||
from oai.llm_utils import APIBackend, calculate_embedding_distance_between_str_list
|
||||
|
||||
|
||||
class FactorImplementationKnowledge(Knowledge):
|
||||
@@ -187,9 +185,9 @@ class FactorImplementationRAGStrategyV1(RAGStrategy):
|
||||
)[-1]
|
||||
for index in similar_indexes
|
||||
]
|
||||
queried_knowledge.working_task_to_similar_successful_knowledge_dict[
|
||||
target_factor_task_information
|
||||
] = similar_successful_knowledge
|
||||
queried_knowledge.working_task_to_similar_successful_knowledge_dict[target_factor_task_information] = (
|
||||
similar_successful_knowledge
|
||||
)
|
||||
return queried_knowledge
|
||||
|
||||
|
||||
@@ -212,7 +210,7 @@ class FactorImplementationGraphRAGStrategy(RAGStrategy):
|
||||
def __init__(self, knowledgebase: FactorImplementationGraphKnowledgeBase) -> None:
|
||||
super().__init__(knowledgebase)
|
||||
self.current_generated_trace_count = 0
|
||||
self.prompt = FactorImplementationPrompts()
|
||||
self.prompt = Prompts(file_path=Path(__file__).parent.parent / "prompts.yaml")
|
||||
|
||||
def generate_knowledge(
|
||||
self,
|
||||
|
||||
@@ -3,19 +3,19 @@ from abc import ABC, abstractmethod
|
||||
from typing import Tuple
|
||||
|
||||
import pandas as pd
|
||||
from finco.log import FinCoLog
|
||||
from jinja2 import Template
|
||||
|
||||
from factor_implementation.share_modules.conf import FactorImplementSettings
|
||||
from factor_implementation.share_modules.factor import (
|
||||
FactorImplementation,
|
||||
FactorImplementationTask,
|
||||
)
|
||||
from factor_implementation.share_modules.prompt import (
|
||||
FactorImplementationPrompts,
|
||||
)
|
||||
from factor_implementation.share_modules.prompt import FactorImplementationPrompts
|
||||
from finco.log import FinCoLog
|
||||
from jinja2 import Template
|
||||
from oai.llm_utils import APIBackend
|
||||
|
||||
from rdagent.factor_implementation.share_modules.factor_implementation_config import (
|
||||
FactorImplementSettings,
|
||||
)
|
||||
|
||||
|
||||
class Evaluator(ABC):
|
||||
@abstractmethod
|
||||
|
||||
@@ -6,17 +6,19 @@ from pathlib import Path
|
||||
from typing import Tuple, Union
|
||||
|
||||
import pandas as pd
|
||||
from filelock import FileLock
|
||||
from finco.log import FinCoLog
|
||||
|
||||
from factor_implementation.share_modules.conf import FactorImplementSettings
|
||||
from factor_implementation.share_modules.exception import (
|
||||
CodeFormatException,
|
||||
NoOutputException,
|
||||
RuntimeErrorException,
|
||||
)
|
||||
from filelock import FileLock
|
||||
from finco.log import FinCoLog
|
||||
from oai.llm_utils import md5_hash
|
||||
|
||||
from rdagent.factor_implementation.share_modules.factor_implementation_config import (
|
||||
FactorImplementSettings,
|
||||
)
|
||||
|
||||
|
||||
class FactorImplementationTask:
|
||||
# TODO: remove the factor_ prefix may be better
|
||||
@@ -122,7 +124,8 @@ class FileBasedFactorImplementation(FactorImplementation):
|
||||
raise ValueError(self.FB_CODE_NOT_SET)
|
||||
with FileLock(self.workspace_path / "execution.lock"):
|
||||
(Path.cwd() / "git_ignore_folder" / "factor_implementation_execution_cache").mkdir(
|
||||
exist_ok=True, parents=True,
|
||||
exist_ok=True,
|
||||
parents=True,
|
||||
)
|
||||
if FactorImplementSettings().enable_execution_cache:
|
||||
# NOTE: cache the result for the same code
|
||||
|
||||
+1
-1
@@ -1,6 +1,6 @@
|
||||
from pathlib import Path
|
||||
|
||||
from finco.conf import FincoSettings
|
||||
from core.conf import FincoSettings
|
||||
|
||||
|
||||
class FactorImplementSettings(FincoSettings):
|
||||
+1
-1
@@ -5,7 +5,7 @@ import pandas as pd
|
||||
# render it with jinja
|
||||
from jinja2 import Template
|
||||
|
||||
from factor_implementation.share_modules.conf import FIS
|
||||
from rdagent.factor_implementation.share_modules.factor_implementation_config import FIS
|
||||
|
||||
TPL = """
|
||||
{{file_name}}
|
||||
@@ -1,22 +0,0 @@
|
||||
from pathlib import Path
|
||||
from typing import Dict
|
||||
|
||||
import yaml
|
||||
from finco.utils import SingletonBaseClass
|
||||
|
||||
|
||||
class FactorImplementationPrompts(Dict, SingletonBaseClass):
|
||||
def __init__(self):
|
||||
super().__init__()
|
||||
prompt_yaml_path = Path(__file__).parent / "prompts.yaml"
|
||||
|
||||
prompt_yaml_dict = yaml.load(
|
||||
open(
|
||||
prompt_yaml_path,
|
||||
encoding="utf8",
|
||||
),
|
||||
Loader=yaml.FullLoader,
|
||||
)
|
||||
|
||||
for key, value in prompt_yaml_dict.items():
|
||||
self[key] = value
|
||||
Reference in New Issue
Block a user