Files
NexQuant/rdagent/components/proposal/factor_proposal.py
T

102 lines
3.4 KiB
Python
Raw Normal View History

from abc import abstractmethod
from pathlib import Path
2024-07-04 15:56:14 +08:00
from typing import Tuple
from jinja2 import Environment, StrictUndefined
2024-07-05 17:42:00 +08:00
from rdagent.components.coder.factor_coder.factor import FactorExperiment
from rdagent.core.prompts import Prompts
from rdagent.core.proposal import (
Hypothesis,
2024-07-04 15:56:14 +08:00
Hypothesis2Experiment,
HypothesisGen,
Scenario,
Trace,
)
from rdagent.oai.llm_utils import APIBackend
prompt_dict = Prompts(file_path=Path(__file__).parent / "prompts.yaml")
FactorHypothesis = Hypothesis
class FactorHypothesisGen(HypothesisGen):
def __init__(self, scen: Scenario):
super().__init__(scen)
# The following methods are scenario related so they should be implemented in the subclass
@abstractmethod
2024-07-17 15:00:13 +08:00
def prepare_context(self, trace: Trace) -> Tuple[dict, bool]:
...
@abstractmethod
2024-07-17 15:00:13 +08:00
def convert_response(self, response: str) -> FactorHypothesis:
...
def gen(self, trace: Trace) -> FactorHypothesis:
2024-07-04 15:56:14 +08:00
context_dict, json_flag = self.prepare_context(trace)
system_prompt = (
Environment(undefined=StrictUndefined)
2024-07-11 10:50:34 +08:00
.from_string(prompt_dict["hypothesis_gen"]["system_prompt"])
2024-07-04 15:56:14 +08:00
.render(
2024-07-11 10:50:34 +08:00
targets="factors",
2024-07-04 15:56:14 +08:00
scenario=self.scen.get_scenario_all_desc(),
hypothesis_output_format=context_dict["hypothesis_output_format"],
hypothesis_specification=context_dict["hypothesis_specification"],
2024-07-04 15:56:14 +08:00
)
)
user_prompt = (
Environment(undefined=StrictUndefined)
2024-07-11 10:50:34 +08:00
.from_string(prompt_dict["hypothesis_gen"]["user_prompt"])
2024-07-04 15:56:14 +08:00
.render(
2024-07-11 10:50:34 +08:00
targets="factors",
2024-07-04 15:56:14 +08:00
hypothesis_and_feedback=context_dict["hypothesis_and_feedback"],
RAG=context_dict["RAG"],
)
)
2024-07-04 15:56:14 +08:00
resp = APIBackend().build_messages_and_create_chat_completion(user_prompt, system_prompt, json_mode=json_flag)
2024-07-04 15:56:14 +08:00
hypothesis = self.convert_response(resp)
return hypothesis
2024-07-04 15:56:14 +08:00
class FactorHypothesis2Experiment(Hypothesis2Experiment[FactorExperiment]):
@abstractmethod
2024-07-17 15:00:13 +08:00
def prepare_context(self, hypothesis: Hypothesis, trace: Trace) -> Tuple[dict, bool]:
...
2024-07-04 15:56:14 +08:00
@abstractmethod
2024-07-17 15:00:13 +08:00
def convert_response(self, response: str, trace: Trace) -> FactorExperiment:
...
2024-07-04 15:56:14 +08:00
def convert(self, hypothesis: Hypothesis, trace: Trace) -> FactorExperiment:
context, json_flag = self.prepare_context(hypothesis, trace)
2024-07-04 15:56:14 +08:00
system_prompt = (
Environment(undefined=StrictUndefined)
2024-07-11 10:50:34 +08:00
.from_string(prompt_dict["hypothesis2experiment"]["system_prompt"])
2024-07-04 15:56:14 +08:00
.render(
2024-07-11 10:50:34 +08:00
targets="factors",
scenario=trace.scen.get_scenario_all_desc(),
2024-07-04 15:56:14 +08:00
experiment_output_format=context["experiment_output_format"],
)
)
user_prompt = (
Environment(undefined=StrictUndefined)
2024-07-11 10:50:34 +08:00
.from_string(prompt_dict["hypothesis2experiment"]["user_prompt"])
2024-07-04 15:56:14 +08:00
.render(
2024-07-11 10:50:34 +08:00
targets="factors",
target_hypothesis=context["target_hypothesis"],
2024-07-04 15:56:14 +08:00
hypothesis_and_feedback=context["hypothesis_and_feedback"],
2024-07-11 10:50:34 +08:00
target_list=context["target_list"],
2024-07-04 15:56:14 +08:00
RAG=context["RAG"],
)
)
resp = APIBackend().build_messages_and_create_chat_completion(user_prompt, system_prompt, json_mode=json_flag)
return self.convert_response(resp, trace)