-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.py
More file actions
152 lines (137 loc) · 5.62 KB
/
main.py
File metadata and controls
152 lines (137 loc) · 5.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import os
import weave
import asyncio
import os, yaml
from dotenv import load_dotenv
from weave_utils import (
download_source_docs,
gen_data,
PromptTemplate,
EmbeddingModel,
VectorStore,
ChatModel,
RagModel
)
from evaluate import (
eval_retrieval,
HallucinationLLMJudge,
CorrectnessLLMJudge
)
def main(config):
# init weave experiment
weave.init(config["entity"] + "/" + config["project_name"])
if config["setup"]:
# data extraction, object: source table
download_source_docs(**config)
# dataset generation, object: generated dataset
gen_model_instance = ChatModel(
name="GenModel",
chat_model=config["gen_eval_model"],
cm_max_new_tokens=config["gm_max_new_tokens"],
cm_quantize=config["gm_quantize"],
cm_temperature=config["gm_temperature"],
inference_batch_size=config["inference_batch_size"],
device=config["device"],
)
prompt_template_instance = PromptTemplate(
system_prompt=config["eval_system_prompt"],
human_prompt=config["gen_eval_prompt"],
)
asyncio.run(gen_data(
gen_model=gen_model_instance,
prompt_template=prompt_template_instance,
raw_data_artifact=config["raw_data_artifact"],
dataset_artifact=config["dataset_artifact"],
questions_per_chunk=config["questions_per_chunk"],
max_chunks_considered=config["max_chunks_considered"],
source_chunk_size=config["source_chunk_size"],
source_chunk_overlap=config["source_chunk_overlap"],
))
if config["benchmark"]:
# create RAG Model #
# TODO: publish doesn't work because indx is not saved (model post doesn't work / save)
# TODO: is weave.publish(vdb) even necessary?
chat_model = ChatModel(
name = "ChatModelRag",
chat_model = config["chat_model"],
cm_max_new_tokens = config["cm_max_new_tokens"],
cm_quantize = config["cm_quantize"],
cm_temperature = config["cm_temperature"],
inference_batch_size = config["inference_batch_size"],
device = config["device"],
)
embedding_model = EmbeddingModel(
embedding_model = config["embedding_model"],
device = config["device"],
embedding_model_norm_embed = config["embedding_model_norm_embed"],
)
vdb = VectorStore(
#name = "ClimateVectorStore_NonAsync",
docs = weave.ref(config["raw_data_artifact"]).get(),
key = "page_content",
embedding_model = embedding_model,
limit = 100,
chunk_size = config["chunk_size"],
chunk_overlap = config["chunk_overlap"],
)
wf_rag_model = RagModel(
chat_model = chat_model,
vector_store = vdb,
raw_data_artifact = config["raw_data_artifact"],
rag_prompt_system = config["rag_prompt_system"],
rag_prompt_user = config["rag_prompt_user"],
retrieval_chain_type = config["retrieval_chain_type"],
inference_batch_size = config["inference_batch_size"],
)
# create scoring functions #
# TODO: when specifying a different name attr but with same model class will we see in Weave?
# TODO: think about offloading the prompt creation to the judges themselves
eval_models = []
for idx, model_name in enumerate(config["eval_model"]):
eval_model = ChatModel(
name=f"ChatModelEval_{idx}",
chat_model=model_name,
cm_max_new_tokens=config["cm_max_new_tokens"],
cm_quantize=config["cm_quantize"],
cm_temperature=config["cm_temperature"],
inference_batch_size=config["inference_batch_size"],
device=config["device"],
)
eval_models.append(eval_model)
correctness_prompt = PromptTemplate(
system_prompt=config["eval_system_prompt"],
human_prompt=config["eval_corr_prompt_user"],
)
hallucination_prompt = PromptTemplate(
system_prompt=config["eval_system_prompt"],
human_prompt=config["eval_hall_prompt_user"],
)
# run evaluation #
# TODO: restructure scorers to include the categories more intuitively
evaluation = weave.Evaluation(
dataset=weave.ref(config["dataset_artifact"]).get(),
scorers=[
CorrectnessLLMJudge(
name="Performance Metrics",
description="Performance metrics incl. Correctness.",
models=eval_models,
prompt=correctness_prompt,
),
HallucinationLLMJudge(
name="Safety Metrics",
description="Safety metrics incl. Hallucination.",
models=eval_models,
prompt=hallucination_prompt,
)
],
)
# NOTE: this has to be async in any case (even if scorer and model.predict are not async)
asyncio.run(evaluation.evaluate(wf_rag_model))
if __name__ == "__main__":
config = {}
file_path = os.path.dirname(os.path.realpath(__file__))
with open(os.path.join(file_path, 'configs/general_config.yaml'), 'r') as file:
config.update(yaml.safe_load(file))
if not load_dotenv(os.path.join(file_path, "configs/benchmark.env")):
print("Environment variables couldn't be found.")
main(config)