mirror of
https://github.com/NicolasBohn/NexQuant.git
synced 2026-07-28 16:07:46 +00:00
7f4c2d18c6
* refine ds modal for more cases: eval and es * update model template * prompts for model and ensemble * fix a bug * fix a bug * init: ds workflow evovingstrategy * Adding ensemble (#505) * Initial Draft * Updating logic for init * Revising * Successful Testing * Updating to use the latest & right class * bug: bug-fixing for testing * data science loop changes * data science loop base * ds loop feedback * fix * remove measure_time because it's duplicated (in LoopBase) * add the knowledge query for data_loader & feature * edit ds workflow evaluator * data_loader bug fix * stop evolving when all tasks completed * llm app change * fix break all complete strategy * Adding queried knowledge (#508) Co-authored-by: XianBW <36835909+XianBW@users.noreply.github.com> * fix loop bug * ds workflow evaluator; test; refine prompts * workflow spec * fix ci * feature task changes * ds loop change * fix a bug in feat * add query knowledge for model and workflow * llm_debug info(for show) using pickle instead of json * remove NextLoopException * loop change * coder raise CoderError when all sub_tasks failed * rename code_dict to file_dict in FBWorkspace * add CoSTEER unittest * now show self.version in Task.get_task_information(), simplify CoSTEER sub tasks definition * remove some properties in ModelTask, add model_type in it. * fix llm app bug * llm web app bug fix * ds loop bug fix * fix: give component code to feature&ens eval * loop catch error bug * rename load_from_raw_data to load_data * feat: Add debug data creation functionality for data science scenarios * support local folder (#511) * support local folder * remove unnecessary random * KaggleScen Subclass * small fix * use template for style description * update default scen to kaggle * update sample data script * make sure frac < 1 * fix a bug * feature spec changes * fix * changeimport order * clear unnecessary std outputs * fix a typo * create sample folder after unzip kaggle data * feature/model test script update * Align the data types across modules. * fix a bug in model eval * show line number * move sample entry point to app * spec & model prompt changes * Refine the competition specification to address the data type problem and the coherence issue. * fix some bugs * add file filter in FBworkspace.code property * support non-binary prediction * avoid too much warnings * fix a bug in ensemble module * filtered the knowledge query in all modules * delete RAG in idea proposal * refine the code in ensemble * show exp workspace in llm_st * exp_gen bug fix * feedback bug fix * use `feature` instead of `feat01` * Trace & method of judging if exp is completed change * fix a bug in package calling and execute ci * fix code * bug fix * bug fix * fix a bug * fix some bugs * fix a bug * refactor: Enhance error handling and feedback in data science loop * support different use_azure on chat and embedding models * multi-model proposal logic * fix a small syntax error * loopBase and some changes * ensemble scores change * fbworkspace.code -> .all_codes * use all model codes in workflow coder * check scores.csv's keys(model_names) * model name changes * add a todo in ensemble test * sota_exp changes * give model info in exp gen * add runner time limit * config using debug data or not in evals * exp to feedback base * add feature code when writing model task * small problem * copying during sampling * update * refactor: Simplify code handling and improve workspace management * model part output fix * print model's execution time * bug fix * ensemble test fix * ens small change * ens_test bug fix * Refine partial expansion logic to display only a few subfolders when their structure is uniform, improving readability in nested directories. * several update on prompts * sample subfolders * Filter the stdout after code execution to remove irrelevant information e.g. progress bars, whitespace characters, excessive line breaks. * Add some more prompts and comments * several update on the first init rounds * model timeout as error * fix pattern of getting model codes in workspace * small bux fix on model prompts * remove get_code_with_key since we have regex pattern * fix: Correct tqdm progress bar update logic in LoopBase class * feat: Add diff generation and enhance feedback mechanism in data science loop * update some fix to model and workflow prompts * refine the logic of progress bar filter * add last_successful_exp in exp_gen * fix a one line bug * add a hint in prompt * fix data sample for bms * fix data sample for bms * hypothesis small fix * crawler readme update * fix component gen * fix bug * annotation change * load description.md if it exists * refactor: Simplify SOTA description handling in feedback and prompts * refactor: Use shared templates for feedback and experiment descriptions * change webapp for model codes changes * update proposal * add timeout message for docker run output * fix * refine the code in docker time processing * use .shape instead of len() when do shape eval * won't change size during iteration * support bson sample * sample support jsonl and bson * add former_code to coder prompts * a little speed us in debug data creating * filter progress bar when eval ens and main * avoid costeer makes no change to former code * fix several log error * add timeout judge threshold * fix some bugs in the evaluation of component output shapes * File structure for supporting litellm (#517) Co-authored-by: Young <afe.young@gmail.com> * ignore submission and show processing * ignore submission and show processing * add efficiency notice * refactor: Enhance error message with detailed feedback summary * refactor: Simplify component handling in DSExpGen class * refactor: Update code structure and add docstring for clarity * reserve one sample to each label in data sampling * add Evaluation info * refine costeer code to avoid giving same code twice * use raw_description as plain text * add a prompt hint to avoid same dict key * model task name bug in first model exp gen * fix a typo * add some debug info in costeer tests * task init change * enhance data sampling * refine the code in data_loader * more reasonable loop * fix a bug in data folder description * add error msg & traceback to execution feedback * fix llm error msg detection * add task information to costeer eval & add cache to docker run(use zipfile to store the whole workspace) * fix CI first round * fix CI second round * use txt to store test script to avoid pytest * remove zipfile in requirements * add azure.identity to requirements * ignore debug web page * component test changes * remove redundent task_desc in model coder * feat: Add APE module and prompts for automated prompt engineering * fix: Update .gitignore and improve text formatting in eval.py * refactor: Update print output and improve code comments and imports * style: Fix string formatting and import order in ape.py and fmt.py * exclude ape * add a data folder notice * reduce unnecessary output to stdout * refine the code of describe_data_folder * fix ci * style: streamlit style update (#522) * streamlit style update * fix import * fix format * fix llm_st loop progress bar * debugapp small change * fix model str * refine some prompts * fix model str * fix CI * refine the logic associated with the data_folder * fix ci * small change * set filter_progress_bar as default in execute * model proposal with workflow * add submission check in workflow eval * fix bug * small change * fix CI * fix CI * refactor: Move generate_diff to utils and update DSExpGen logic * more reasonable prompt describing metric direction * fix a minor jinja2 bug * quick fix exp_gen bugs * fix the following bug * fix * fix some bugs * remove workflow from model * add pending_tasks_list in data science to enable coding model and workflow * refine the code for handling JSON-formatted data descriptions * assert with information * ensure correct csv file name * add logging to help record the output * log competition * add log tag for debug llm app * test: Test ds refactor ll (#523) * fix bugs to former scenario * fix a bug because coding in rdloop changed * fix the bug when feedback gets no hypothesis * fix trace structure * change all trace hist when merging hypothesis to experiments * ignore some error in ruff * fix kaggle scenario bugs * refine one line * another bug * another small bug * fix ui bugs * chage kaggle train.py path --------- Co-authored-by: Xu Yang <peteryang@vip.qq.com> * fix CI * Update rdagent/app/data_science/loop.py Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * add samplecsv into spec prompts * fix CI --------- Co-authored-by: TPLin22 <tplin2@163.com> Co-authored-by: yuanteli <1957922024@qq.com> Co-authored-by: Xisen Wang <118058822+xisen-w@users.noreply.github.com> Co-authored-by: Bowen Xian <xianbowen@outlook.com> Co-authored-by: Xu Yang <peteryang@vip.qq.com> Co-authored-by: XianBW <36835909+XianBW@users.noreply.github.com> Co-authored-by: Tim <illking@foxmail.com> Co-authored-by: 炼金术师华华 <37462254+YeewahChan@users.noreply.github.com> Co-authored-by: Linlang <30293408+SunsetWolf@users.noreply.github.com> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
577 lines
22 KiB
Python
577 lines
22 KiB
Python
import io
|
|
import json
|
|
from abc import abstractmethod
|
|
from pathlib import Path
|
|
from typing import Tuple
|
|
|
|
import pandas as pd
|
|
from jinja2 import Environment, StrictUndefined
|
|
|
|
from rdagent.components.coder.factor_coder.config import FACTOR_COSTEER_SETTINGS
|
|
from rdagent.components.coder.factor_coder.factor import FactorTask
|
|
from rdagent.core.experiment import Task, Workspace
|
|
from rdagent.core.prompts import Prompts
|
|
from rdagent.oai.llm_conf import LLM_SETTINGS
|
|
from rdagent.oai.llm_utils import APIBackend
|
|
|
|
evaluate_prompts = Prompts(file_path=Path(__file__).parent / "prompts.yaml")
|
|
|
|
|
|
class FactorEvaluator:
|
|
"""Although the init method is same to Evaluator, but we want to emphasize they are different"""
|
|
|
|
def __init__(self, scen=None) -> None:
|
|
self.scen = scen
|
|
|
|
@abstractmethod
|
|
def evaluate(
|
|
self,
|
|
target_task: Task,
|
|
implementation: Workspace,
|
|
gt_implementation: Workspace,
|
|
**kwargs,
|
|
) -> Tuple[str, object]:
|
|
"""You can get the dataframe by
|
|
|
|
.. code-block:: python
|
|
|
|
_, gen_df = implementation.execute()
|
|
_, gt_df = gt_implementation.execute()
|
|
|
|
Returns
|
|
-------
|
|
Tuple[str, object]
|
|
- str: the text-based description of the evaluation result
|
|
- object: a comparable metric (bool, integer, float ...) None for evaluator with only text-based result
|
|
|
|
"""
|
|
raise NotImplementedError("Please implement the `evaluator` method")
|
|
|
|
def _get_df(self, gt_implementation: Workspace, implementation: Workspace):
|
|
if gt_implementation is not None:
|
|
_, gt_df = gt_implementation.execute()
|
|
if isinstance(gt_df, pd.Series):
|
|
gt_df = gt_df.to_frame("gt_factor")
|
|
if isinstance(gt_df, pd.DataFrame):
|
|
gt_df = gt_df.sort_index()
|
|
else:
|
|
gt_df = None
|
|
|
|
_, gen_df = implementation.execute()
|
|
if isinstance(gen_df, pd.Series):
|
|
gen_df = gen_df.to_frame("source_factor")
|
|
if isinstance(gen_df, pd.DataFrame):
|
|
gen_df = gen_df.sort_index()
|
|
return gt_df, gen_df
|
|
|
|
def __str__(self) -> str:
|
|
return self.__class__.__name__
|
|
|
|
|
|
class FactorCodeEvaluator(FactorEvaluator):
|
|
def evaluate(
|
|
self,
|
|
target_task: FactorTask,
|
|
implementation: Workspace,
|
|
execution_feedback: str,
|
|
value_feedback: str = "",
|
|
gt_implementation: Workspace = None,
|
|
**kwargs,
|
|
):
|
|
factor_information = target_task.get_task_information()
|
|
code = implementation.all_codes
|
|
|
|
system_prompt = (
|
|
Environment(undefined=StrictUndefined)
|
|
.from_string(evaluate_prompts["evaluator_code_feedback_v1_system"])
|
|
.render(
|
|
scenario=(
|
|
self.scen.get_scenario_all_desc(
|
|
target_task,
|
|
filtered_tag="feature",
|
|
simple_background=FACTOR_COSTEER_SETTINGS.simple_background,
|
|
)
|
|
if self.scen is not None
|
|
else "No scenario description."
|
|
)
|
|
)
|
|
)
|
|
|
|
execution_feedback_to_render = execution_feedback
|
|
for _ in range(10): # 10 times to split the content is enough
|
|
user_prompt = (
|
|
Environment(undefined=StrictUndefined)
|
|
.from_string(
|
|
evaluate_prompts["evaluator_code_feedback_v1_user"],
|
|
)
|
|
.render(
|
|
factor_information=factor_information,
|
|
code=code,
|
|
execution_feedback=execution_feedback_to_render,
|
|
value_feedback=value_feedback,
|
|
gt_code=gt_implementation.code if gt_implementation else None,
|
|
)
|
|
)
|
|
if (
|
|
APIBackend().build_messages_and_calculate_token(
|
|
user_prompt=user_prompt,
|
|
system_prompt=system_prompt,
|
|
)
|
|
> LLM_SETTINGS.chat_token_limit
|
|
):
|
|
execution_feedback_to_render = execution_feedback_to_render[len(execution_feedback_to_render) // 2 :]
|
|
else:
|
|
break
|
|
critic_response = APIBackend().build_messages_and_create_chat_completion(
|
|
user_prompt=user_prompt,
|
|
system_prompt=system_prompt,
|
|
json_mode=False,
|
|
)
|
|
|
|
return critic_response, None
|
|
|
|
|
|
class FactorInfEvaluator(FactorEvaluator):
|
|
def evaluate(
|
|
self,
|
|
implementation: Workspace,
|
|
gt_implementation: Workspace,
|
|
) -> Tuple[str, object]:
|
|
_, gen_df = self._get_df(gt_implementation, implementation)
|
|
if gen_df is None:
|
|
return (
|
|
"The source dataframe is None. Please check the implementation.",
|
|
False,
|
|
)
|
|
INF_count = gen_df.isin([float("inf"), -float("inf")]).sum().sum()
|
|
if INF_count == 0:
|
|
return "The source dataframe does not have any infinite values.", True
|
|
else:
|
|
return (
|
|
f"The source dataframe has {INF_count} infinite values. Please check the implementation.",
|
|
False,
|
|
)
|
|
|
|
|
|
class FactorSingleColumnEvaluator(FactorEvaluator):
|
|
def evaluate(
|
|
self,
|
|
implementation: Workspace,
|
|
gt_implementation: Workspace,
|
|
) -> Tuple[str, object]:
|
|
_, gen_df = self._get_df(gt_implementation, implementation)
|
|
if gen_df is None:
|
|
return (
|
|
"The source dataframe is None. Please check the implementation.",
|
|
False,
|
|
)
|
|
if len(gen_df.columns) == 1:
|
|
return "The source dataframe has only one column which is correct.", True
|
|
else:
|
|
return (
|
|
"The source dataframe has more than one column. Please check the implementation. We only evaluate the first column.",
|
|
False,
|
|
)
|
|
|
|
|
|
class FactorOutputFormatEvaluator(FactorEvaluator):
|
|
def evaluate(
|
|
self,
|
|
implementation: Workspace,
|
|
gt_implementation: Workspace,
|
|
) -> Tuple[str, object]:
|
|
gt_df, gen_df = self._get_df(gt_implementation, implementation)
|
|
if gen_df is None:
|
|
return (
|
|
"The source dataframe is None. Skip the evaluation of the output format.",
|
|
False,
|
|
)
|
|
buffer = io.StringIO()
|
|
gen_df.info(buf=buffer)
|
|
gen_df_info_str = f"The user is currently working on a feature related task.\nThe output dataframe info is:\n{buffer.getvalue()}"
|
|
system_prompt = (
|
|
Environment(undefined=StrictUndefined)
|
|
.from_string(
|
|
evaluate_prompts["evaluator_output_format_system"],
|
|
)
|
|
.render(
|
|
scenario=(
|
|
self.scen.get_scenario_all_desc(implementation.target_task, filtered_tag="feature")
|
|
if self.scen is not None
|
|
else "No scenario description."
|
|
)
|
|
)
|
|
)
|
|
|
|
# TODO: with retry_context(retry_n=3, except_list=[KeyError]):
|
|
max_attempts = 3
|
|
attempts = 0
|
|
final_evaluation_dict = None
|
|
|
|
while attempts < max_attempts:
|
|
try:
|
|
api = APIBackend() if attempts == 0 else APIBackend(use_chat_cache=False)
|
|
resp = api.build_messages_and_create_chat_completion(
|
|
user_prompt=gen_df_info_str, system_prompt=system_prompt, json_mode=True
|
|
)
|
|
resp_dict = json.loads(resp)
|
|
resp_dict["output_format_decision"] = str(resp_dict["output_format_decision"]).lower() in ["true", "1"]
|
|
|
|
return (
|
|
str(resp_dict["output_format_feedback"]),
|
|
resp_dict["output_format_decision"],
|
|
)
|
|
except (KeyError, json.JSONDecodeError) as e:
|
|
attempts += 1
|
|
if attempts >= max_attempts:
|
|
raise KeyError(
|
|
"Wrong JSON Response or missing 'output_format_decision' or 'output_format_feedback' key after multiple attempts."
|
|
) from e
|
|
|
|
return "Failed to evaluate output format after multiple attempts.", False
|
|
|
|
|
|
class FactorDatetimeDailyEvaluator(FactorEvaluator):
|
|
def evaluate(
|
|
self,
|
|
implementation: Workspace,
|
|
gt_implementation: Workspace,
|
|
) -> Tuple[str | object]:
|
|
_, gen_df = self._get_df(gt_implementation, implementation)
|
|
if gen_df is None:
|
|
return "The source dataframe is None. Skip the evaluation of the datetime format.", False
|
|
|
|
if "datetime" not in gen_df.index.names:
|
|
return "The source dataframe does not have a datetime index. Please check the implementation.", False
|
|
|
|
try:
|
|
pd.to_datetime(gen_df.index.get_level_values("datetime"))
|
|
except Exception:
|
|
return (
|
|
f"The source dataframe has a datetime index but it is not in the correct format (maybe a regular string or other objects). Please check the implementation.\n The head of the output dataframe is: \n{gen_df.head()}",
|
|
False,
|
|
)
|
|
|
|
time_diff = pd.to_datetime(gen_df.index.get_level_values("datetime")).to_series().diff().dropna().unique()
|
|
if pd.Timedelta(minutes=1) in time_diff:
|
|
return (
|
|
"The generated dataframe is not daily. The implementation is definitely wrong. Please check the implementation.",
|
|
False,
|
|
)
|
|
return "The generated dataframe is daily.", True
|
|
|
|
|
|
class FactorRowCountEvaluator(FactorEvaluator):
|
|
def evaluate(
|
|
self,
|
|
implementation: Workspace,
|
|
gt_implementation: Workspace,
|
|
) -> Tuple[str, object]:
|
|
gt_df, gen_df = self._get_df(gt_implementation, implementation)
|
|
if gen_df is None:
|
|
return (
|
|
"The source dataframe is None. Please check the implementation.",
|
|
False,
|
|
)
|
|
ratio = min(len(gen_df), len(gt_df)) / max(len(gen_df), len(gt_df))
|
|
return (
|
|
(
|
|
f"The ratio of rows count in the source dataframe to the ground truth dataframe is {ratio:.2f}. "
|
|
+ "Please verify the implementation. "
|
|
if ratio <= 0.99
|
|
else ""
|
|
),
|
|
ratio,
|
|
)
|
|
|
|
|
|
class FactorIndexEvaluator(FactorEvaluator):
|
|
def evaluate(
|
|
self,
|
|
implementation: Workspace,
|
|
gt_implementation: Workspace,
|
|
) -> Tuple[str, object]:
|
|
gt_df, gen_df = self._get_df(gt_implementation, implementation)
|
|
if gen_df is None:
|
|
return (
|
|
"The source dataframe is None. Please check the implementation.",
|
|
False,
|
|
)
|
|
gen_index_set, gt_index_set = set(gen_df.index), set(gt_df.index)
|
|
similarity = len(gen_index_set.intersection(gt_index_set)) / len(gen_index_set.union(gt_index_set))
|
|
return (
|
|
(
|
|
f"The source dataframe and the ground truth dataframe have different index with a similarity of {similarity:.2%}. The similarity is calculated by the number of shared indices divided by the union indices. "
|
|
+ "Please check the implementation."
|
|
if similarity <= 0.99
|
|
else ""
|
|
),
|
|
similarity,
|
|
)
|
|
|
|
|
|
class FactorMissingValuesEvaluator(FactorEvaluator):
|
|
def evaluate(
|
|
self,
|
|
implementation: Workspace,
|
|
gt_implementation: Workspace,
|
|
) -> Tuple[str, object]:
|
|
gt_df, gen_df = self._get_df(gt_implementation, implementation)
|
|
if gen_df is None:
|
|
return (
|
|
"The source dataframe is None. Please check the implementation.",
|
|
False,
|
|
)
|
|
if gen_df.isna().sum().sum() == gt_df.isna().sum().sum():
|
|
return "Both dataframes have the same missing values.", True
|
|
else:
|
|
return (
|
|
f"The dataframes do not have the same missing values. The source dataframe has {gen_df.isna().sum().sum()} missing values, while the ground truth dataframe has {gt_df.isna().sum().sum()} missing values. Please check the implementation.",
|
|
False,
|
|
)
|
|
|
|
|
|
class FactorEqualValueRatioEvaluator(FactorEvaluator):
|
|
def evaluate(
|
|
self,
|
|
implementation: Workspace,
|
|
gt_implementation: Workspace,
|
|
) -> Tuple[str, object]:
|
|
gt_df, gen_df = self._get_df(gt_implementation, implementation)
|
|
if gen_df is None:
|
|
return (
|
|
"The source dataframe is None. Please check the implementation.",
|
|
-1,
|
|
)
|
|
try:
|
|
close_values = gen_df.sub(gt_df).abs().lt(1e-6)
|
|
result_int = close_values.astype(int)
|
|
pos_num = result_int.sum().sum()
|
|
acc_rate = pos_num / close_values.size
|
|
except:
|
|
close_values = gen_df
|
|
if close_values.all().iloc[0]:
|
|
return (
|
|
"All values in the dataframes are equal within the tolerance of 1e-6.",
|
|
acc_rate,
|
|
)
|
|
else:
|
|
return (
|
|
"Some values differ by more than the tolerance of 1e-6. Check for rounding errors or differences in the calculation methods.",
|
|
acc_rate,
|
|
)
|
|
|
|
|
|
class FactorCorrelationEvaluator(FactorEvaluator):
|
|
def __init__(self, hard_check: bool, *args, **kwargs) -> None:
|
|
super().__init__(*args, **kwargs)
|
|
self.hard_check = hard_check
|
|
|
|
def evaluate(
|
|
self,
|
|
implementation: Workspace,
|
|
gt_implementation: Workspace,
|
|
) -> Tuple[str, object]:
|
|
gt_df, gen_df = self._get_df(gt_implementation, implementation)
|
|
if gen_df is None:
|
|
return (
|
|
"The source dataframe is None. Please check the implementation.",
|
|
False,
|
|
)
|
|
concat_df = pd.concat([gen_df, gt_df], axis=1)
|
|
concat_df.columns = ["source", "gt"]
|
|
ic = concat_df.groupby("datetime").apply(lambda df: df["source"].corr(df["gt"])).dropna().mean()
|
|
ric = (
|
|
concat_df.groupby("datetime")
|
|
.apply(lambda df: df["source"].corr(df["gt"], method="spearman"))
|
|
.dropna()
|
|
.mean()
|
|
)
|
|
|
|
if self.hard_check:
|
|
if ic > 0.99 and ric > 0.99:
|
|
return (
|
|
f"The dataframes are highly correlated. The ic is {ic:.6f} and the rankic is {ric:.6f}.",
|
|
True,
|
|
)
|
|
else:
|
|
return (
|
|
f"The dataframes are not sufficiently high correlated. The ic is {ic:.6f} and the rankic is {ric:.6f}. Investigate the factors that might be causing the discrepancies and ensure that the logic of the factor calculation is consistent.",
|
|
False,
|
|
)
|
|
else:
|
|
return f"The ic is ({ic:.6f}) and the rankic is ({ric:.6f}).", ic
|
|
|
|
|
|
class FactorValueEvaluator(FactorEvaluator):
|
|
def evaluate(
|
|
self,
|
|
implementation: Workspace,
|
|
gt_implementation: Workspace,
|
|
version: int = 1, # 1 for qlib factors and 2 for kaggle factors
|
|
**kwargs,
|
|
) -> Tuple:
|
|
conclusions = []
|
|
|
|
# Initialize result variables
|
|
row_result = 0
|
|
index_result = 0
|
|
output_format_result = None
|
|
equal_value_ratio_result = 0
|
|
high_correlation_result = False
|
|
row_result = None
|
|
|
|
# Check if both dataframe has only one columns Mute this since factor task might generate more than one columns now
|
|
if version == 1:
|
|
feedback_str, _ = FactorSingleColumnEvaluator(self.scen).evaluate(implementation, gt_implementation)
|
|
conclusions.append(feedback_str)
|
|
elif version == 2:
|
|
input_shape = self.scen.input_shape
|
|
_, gen_df = self._get_df(gt_implementation, implementation)
|
|
if gen_df.shape[-1] > input_shape[-1]:
|
|
conclusions.append(
|
|
"Output dataframe has more columns than input feature which is not acceptable in feature processing tasks. Please check the implementation to avoid generating too many columns. Consider this implementation as a failure."
|
|
)
|
|
|
|
feedback_str, inf_evaluate_res = FactorInfEvaluator(self.scen).evaluate(implementation, gt_implementation)
|
|
conclusions.append(feedback_str)
|
|
|
|
# Check if the index of the dataframe is ("datetime", "instrument")
|
|
feedback_str, _ = FactorOutputFormatEvaluator(self.scen).evaluate(implementation, gt_implementation)
|
|
conclusions.append(feedback_str)
|
|
if version == 1:
|
|
feedback_str, daily_check_result = FactorDatetimeDailyEvaluator(self.scen).evaluate(
|
|
implementation, gt_implementation
|
|
)
|
|
conclusions.append(feedback_str)
|
|
else:
|
|
daily_check_result = None
|
|
|
|
# Check dataframe format
|
|
if gt_implementation is not None:
|
|
feedback_str, row_result = FactorRowCountEvaluator(self.scen).evaluate(implementation, gt_implementation)
|
|
conclusions.append(feedback_str)
|
|
|
|
feedback_str, index_result = FactorIndexEvaluator(self.scen).evaluate(implementation, gt_implementation)
|
|
conclusions.append(feedback_str)
|
|
|
|
feedback_str, output_format_result = FactorMissingValuesEvaluator(self.scen).evaluate(
|
|
implementation, gt_implementation
|
|
)
|
|
conclusions.append(feedback_str)
|
|
|
|
feedback_str, equal_value_ratio_result = FactorEqualValueRatioEvaluator(self.scen).evaluate(
|
|
implementation, gt_implementation
|
|
)
|
|
conclusions.append(feedback_str)
|
|
|
|
if index_result > 0.99:
|
|
feedback_str, high_correlation_result = FactorCorrelationEvaluator(
|
|
hard_check=True, scen=self.scen
|
|
).evaluate(implementation, gt_implementation)
|
|
else:
|
|
high_correlation_result = False
|
|
feedback_str = "The source dataframe and the ground truth dataframe have different index. Give up comparing the values and correlation because it's useless"
|
|
conclusions.append(feedback_str)
|
|
|
|
# Combine all conclusions into a single string
|
|
conclusion_str = "\n".join(conclusions)
|
|
|
|
if gt_implementation is not None and (equal_value_ratio_result > 0.99) or high_correlation_result:
|
|
decision_from_value_check = True
|
|
elif (
|
|
row_result is not None
|
|
and row_result <= 0.99
|
|
or output_format_result is False
|
|
or daily_check_result is False
|
|
or inf_evaluate_res is False
|
|
):
|
|
decision_from_value_check = False
|
|
else:
|
|
decision_from_value_check = None
|
|
return conclusion_str, decision_from_value_check
|
|
|
|
|
|
class FactorFinalDecisionEvaluator(FactorEvaluator):
|
|
def evaluate(
|
|
self,
|
|
target_task: FactorTask,
|
|
execution_feedback: str,
|
|
value_feedback: str,
|
|
code_feedback: str,
|
|
**kwargs,
|
|
) -> Tuple:
|
|
system_prompt = (
|
|
Environment(undefined=StrictUndefined)
|
|
.from_string(evaluate_prompts["evaluator_final_decision_v1_system"])
|
|
.render(
|
|
scenario=(
|
|
self.scen.get_scenario_all_desc(target_task, filtered_tag="feature")
|
|
if self.scen is not None
|
|
else "No scenario description."
|
|
)
|
|
)
|
|
)
|
|
execution_feedback_to_render = execution_feedback
|
|
|
|
for _ in range(10): # 10 times to split the content is enough
|
|
user_prompt = (
|
|
Environment(undefined=StrictUndefined)
|
|
.from_string(
|
|
evaluate_prompts["evaluator_final_decision_v1_user"],
|
|
)
|
|
.render(
|
|
factor_information=target_task.get_task_information(),
|
|
execution_feedback=execution_feedback_to_render,
|
|
code_feedback=code_feedback,
|
|
value_feedback=(
|
|
value_feedback
|
|
if value_feedback is not None
|
|
else "No Ground Truth Value provided, so no evaluation on value is performed."
|
|
),
|
|
)
|
|
)
|
|
if (
|
|
APIBackend().build_messages_and_calculate_token(
|
|
user_prompt=user_prompt,
|
|
system_prompt=system_prompt,
|
|
)
|
|
> LLM_SETTINGS.chat_token_limit
|
|
):
|
|
execution_feedback_to_render = execution_feedback_to_render[len(execution_feedback_to_render) // 2 :]
|
|
else:
|
|
break
|
|
|
|
# TODO: with retry_context(retry_n=3, except_list=[KeyError]):
|
|
final_evaluation_dict = None
|
|
attempts = 0
|
|
max_attempts = 3
|
|
|
|
while attempts < max_attempts:
|
|
try:
|
|
api = APIBackend() if attempts == 0 else APIBackend(use_chat_cache=False)
|
|
final_evaluation_dict = json.loads(
|
|
api.build_messages_and_create_chat_completion(
|
|
user_prompt=user_prompt,
|
|
system_prompt=system_prompt,
|
|
json_mode=True,
|
|
seed=attempts, # in case of useless retrying when cache enabled.
|
|
),
|
|
)
|
|
final_decision = final_evaluation_dict["final_decision"]
|
|
final_feedback = final_evaluation_dict["final_feedback"]
|
|
|
|
final_decision = str(final_decision).lower() in ["true", "1"]
|
|
return final_decision, final_feedback
|
|
|
|
except json.JSONDecodeError as e:
|
|
raise ValueError("Failed to decode JSON response from API.") from e
|
|
except KeyError as e:
|
|
attempts += 1
|
|
if attempts >= max_attempts:
|
|
raise KeyError(
|
|
"Response from API is missing 'final_decision' or 'final_feedback' key after multiple attempts."
|
|
) from e
|
|
|
|
return None, None
|