Repository navigation
Expand file tree
/
Copy patheval.py
More file actions
155 lines (133 loc) · 6.18 KB
/
Copy patheval.py
File metadata and controls
155 lines (133 loc) · 6.18 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
153
154
155
"""Evaluation utilities for the Arklex framework.
This module provides functionality for evaluating the performance of the Arklex
framework through conversation simulation and metrics extraction. It includes
utilities for simulating first-pass and second-pass conversations, extracting
task completion metrics, and generating labeled conversation data for analysis.
"""
import os
import json
import argparse
from typing import Any, Dict, List, Tuple, Optional, cast
from arklex.evaluation.simulate_first_pass_convos import simulate_conversations
from arklex.evaluation.extract_conversation_info import extract_task_completion_metrics
from arklex.evaluation.simulate_second_pass_convos import get_labeled_convos
from arklex.utils.model_config import MODEL
from arklex.utils.model_provider_config import LLM_PROVIDERS
from arklex.evaluation.chatgpt_utils import create_client
def evaluate(
config: Dict[str, Any],
) -> Tuple[
List[Dict[str, Any]], List[Dict[str, Any]], Dict[str, Any], List[Dict[str, Any]]
]:
"""Evaluate the performance of the Arklex framework based on the provided configuration.
This function simulates conversations and extracts metrics based on the task specified in the config.
It supports first-pass and second-pass evaluations, and returns the results of the evaluation.
Args:
config (Dict[str, Any]): Configuration dictionary containing task, model API, model parameters,
synthetic data parameters, and other settings.
Returns:
Tuple[List[Dict[str, Any]], List[Dict[str, Any]], Dict[str, Any], List[Dict[str, Any]]]:
A tuple containing the first-pass data, labeled conversations, goal metrics, and goals.
"""
task: str = config["task"]
model_api: str = config["model_api"]
model_params: Dict[str, Any] = config["model_params"]
synthetic_data_params: Dict[str, Any] = config["synthetic_data_params"]
bot_goal: Optional[str] = config.get("builder_objective", None)
bot_goal = None if bot_goal == "" else bot_goal
first_pass_data: List[Dict[str, Any]]
goals: List[Dict[str, Any]]
goal_metrics: Dict[str, Any]
labeled_convos: List[Dict[str, Any]]
if task == "first_pass" or task == "simulate_conv_only":
# first pass
first_pass_data, goals = simulate_conversations(
model_api, model_params, synthetic_data_params, config
)
goal_metrics = extract_task_completion_metrics(
first_pass_data, config["client"], bot_goal
)
data = first_pass_data
# second pass
if task == "all":
labeled_convos = get_labeled_convos(
first_pass_data, model_api, synthetic_data_params, model_params, config
)
else:
labeled_convos = []
return data, labeled_convos, goal_metrics, goals
if __name__ == "__main__":
parser: argparse.ArgumentParser = argparse.ArgumentParser()
parser.add_argument("--model_api", type=str)
parser.add_argument("--model_params", type=dict, default={})
parser.add_argument("--num_convos", type=int, default=5)
parser.add_argument("--num_goals", type=int, default=5)
parser.add_argument("--max_turns", type=int, default=5)
parser.add_argument("--documents_dir", type=str)
parser.add_argument("--config", type=str)
parser.add_argument("--output_dir", type=str)
parser.add_argument("--model", type=str, default=MODEL["model_type_or_path"])
parser.add_argument(
"--llm-provider", type=str, default=MODEL["llm_provider"], choices=LLM_PROVIDERS
)
parser.add_argument(
"--customer_type", type=str, default=None, choices=["b2b", "b2c"]
)
parser.add_argument(
"--task",
type=str,
default="first_pass",
choices=["first_pass", "simulate_conv_only", "all"],
)
parser.add_argument(
"--user_attributes", type=str, default="arklex/evaluation/user_attributes.json"
)
parser.add_argument("--custom_profile", action="store_true")
parser.add_argument("--system_inputs", action="store_true")
parser.add_argument("--data_file", type=str, default=None)
args: argparse.Namespace = parser.parse_args()
MODEL["model_type_or_path"] = args.model
MODEL["llm_provider"] = args.llm_provider
client: Any = create_client()
assert args.model_api is not None, "Model api must be provided"
assert args.config is not None, "Config file must be provided"
assert args.user_attributes is not None, "User attribute file must be provided"
if not args.output_dir:
args.output_dir = os.path.join(args.documents_dir, "eval")
if not os.path.exists(args.output_dir):
os.makedirs(args.output_dir, exist_ok=True)
config: Dict[str, Any] = json.load(open(args.config))
user_attributes: Dict[str, Any] = json.load(open(args.user_attributes))
# if args.testset:
# testset = json.load(open(args.testset))
# else:
# testset = {}
config["model_api"] = args.model_api
config["documents_dir"] = args.documents_dir
config["output_dir"] = args.output_dir
config["model_params"] = args.model_params
config["synthetic_data_params"] = {
"num_convos": args.num_convos,
"num_goals": args.num_goals,
"max_turns": args.max_turns,
"customer_type": args.customer_type,
"data_file": args.data_file,
}
config["task"] = args.task
config["user_attributes"] = user_attributes
config["custom_profile"] = args.custom_profile
config["system_inputs"] = args.system_inputs
config["client"] = client
first_pass_data: List[Dict[str, Any]]
final_convos: List[Dict[str, Any]]
goal_metrics: Dict[str, Any]
goals: List[Dict[str, Any]]
first_pass_data, final_convos, goal_metrics, goals = evaluate(config)
with open(os.path.join(args.output_dir, "goals.json"), "w") as f:
json.dump(goals, f, indent=4)
with open(os.path.join(args.output_dir, "simulate_data.json"), "w") as f:
json.dump(first_pass_data, f, indent=4)
with open(os.path.join(args.output_dir, "labeled_data.json"), "w") as f:
json.dump(final_convos, f, indent=4)
with open(os.path.join(args.output_dir, "goal_completion.json"), "w") as f:
json.dump(goal_metrics, f, indent=4)