feat: add normal rag into framework (#360)

* add normal rag into framework

* fix a typo

* fix a bug
This commit is contained in:
WinstonLiyt
2024-09-27 01:36:12 +08:00
committed by GitHub
parent f5a2ea3aa1
commit 9c8fed3010
5 changed files with 19 additions and 18 deletions
@@ -161,6 +161,7 @@ class KGHypothesisExperiment2Feedback(HypothesisExperiment2Feedback):
if self.scen.if_using_vector_rag:
self.scen.vector_base.add_experience_to_vector_base(experiment_feedback)
self.scen.vector_base.save()
elif self.scen.if_using_graph_rag:
self.scen.trace.knowledge_base.load_from_documents([experiment_feedback], self.scen)
@@ -1,6 +1,7 @@
import io
import json
import pickle
from datetime import datetime, timezone
from pathlib import Path
import pandas as pd
@@ -43,8 +44,9 @@ class KGScenario(Scenario):
self.if_using_vector_rag = KAGGLE_IMPLEMENT_SETTING.if_using_vector_rag
if self.if_using_vector_rag and KAGGLE_IMPLEMENT_SETTING.rag_path:
self.vector_base = KaggleExperienceBase()
self.vector_base.load(KAGGLE_IMPLEMENT_SETTING.rag_path)
self.vector_base = KaggleExperienceBase(KAGGLE_IMPLEMENT_SETTING.rag_path)
self.vector_base.path = datetime.now(timezone.utc).strftime("%Y-%m-%d-%H-%M-%S") + "_kaggle_kb.pkl"
self.vector_base.save()
self._output_format = self.output_format
self._interface = self.interface
@@ -197,7 +199,7 @@ The model code should follow the simulator:
@property
def rich_style_description(self) -> str:
return f"""
This is the Kaggle scenario for the competition: {self.competitionn}
This is the Kaggle scenario for the competition: {self.competition}
"""
def get_scenario_all_desc(self) -> str:
@@ -70,18 +70,16 @@ def extract_knowledge_from_feedback(feedback_response: dict) -> dict:
def process_all_case_files(directory_path: str):
output_file = Path(directory_path) / "kaggle_experience_results.json"
json_output = []
for filename in os.listdir(directory_path):
if filename.endswith(".case"):
file_path = os.path.join(directory_path, filename)
with open(file_path, "r", encoding="utf-8") as file:
content = file.read()
knowladge = extract_knowledge_from_high_score_answers(content)
json_output.append(knowladge)
for file_path in Path(directory_path).rglob("*.case"):
with open(file_path, "r", encoding="utf-8") as file:
content = file.read()
knowledge = extract_knowledge_from_high_score_answers(content)
json_output.append(knowledge)
with open(output_file, "w", encoding="utf-8") as json_file:
json.dump(json_output, json_file, ensure_ascii=False)
if __name__ == "__main__":
process_all_case_files(directory_path="git_ignore_folder/experience/tabular_cases_all")
process_all_case_files(directory_path="git_ignore_folder/data/kaggle")
@@ -122,11 +122,8 @@ class KaggleExperienceBase(PDVectorBase):
super().__init__(vector_df_path)
self.kaggle_experience_path = kaggle_experience_path
self.kaggle_experience_data = []
# if path is not None and Path(path).exists():
# self.load_kaggle_experience(kaggle_experience_path)
# self.path = Path(path).parent / (datetime.now(timezone.utc).strftime("%Y-%m-%d-%H-%M-%S") + "_kaggle_kb.pkl")
# else:
# pass
if kaggle_experience_path:
self.load_kaggle_experience(kaggle_experience_path)
def add(self, document: Union[KGDocument, List[KGDocument]]):
document.split_into_trunk()
@@ -93,7 +93,10 @@ class KGHypothesisGen(ModelHypothesisGen):
self.confidence_parameter = 1.0
self.initial_performance = 0.0
def generate_RAG_content(self, trace: Trace) -> str:
def generate_RAG_content(self, trace: Trace, hypothesis_and_feedback: str) -> str:
if self.scem.if_using_vector_rag:
rag_results, _ = self.scen.vector_base.search_experience(hypothesis_and_feedback, topk_k=5)
return "\n".join([doc.content for doc in rag_results])
if self.scen.if_using_graph_rag is False or trace.knowledge_base is None:
return None
same_competition_node = trace.knowledge_base.get_node_by_content(trace.scen.get_competition_full_desc())
@@ -235,7 +238,7 @@ class KGHypothesisGen(ModelHypothesisGen):
context_dict = {
"hypothesis_and_feedback": hypothesis_and_feedback,
"RAG": self.generate_RAG_content(trace),
"RAG": self.generate_RAG_content(trace, hypothesis_and_feedback),
"hypothesis_output_format": Environment(undefined=StrictUndefined)
.from_string(prompt_dict["hypothesis_output_format"])
.render(if_using_feature_selection=KAGGLE_IMPLEMENT_SETTING.if_using_feature_selection),