-
Notifications
You must be signed in to change notification settings - Fork 489
Expand file tree
/
Copy pathevaluators_router.py
More file actions
282 lines (229 loc) · 8.8 KB
/
evaluators_router.py
File metadata and controls
282 lines (229 loc) · 8.8 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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
from typing import List, Optional
from fastapi import HTTPException, Request
from fastapi.responses import JSONResponse
from oss.src.utils.logging import get_module_logger
from oss.src.utils.common import APIRouter, is_ee
from oss.src.services import (
evaluator_manager,
db_manager,
evaluators_service,
)
from oss.src.models.api.evaluation_model import (
LegacyEvaluator,
EvaluatorConfig,
NewEvaluatorConfig,
UpdateEvaluatorConfig,
EvaluatorInputInterface,
EvaluatorOutputInterface,
EvaluatorMappingInputInterface,
EvaluatorMappingOutputInterface,
)
from oss.src.core.secrets.utils import get_llm_providers_secrets
if is_ee():
from ee.src.models.shared_models import Permission
from ee.src.utils.permissions import check_action_access
router = APIRouter()
log = get_module_logger(__name__)
@router.get("/", response_model=List[LegacyEvaluator])
async def get_evaluators_endpoint():
"""
Endpoint to fetch a list of evaluators.
Returns:
List[Evaluator]: A list of evaluator objects.
"""
evaluators = evaluator_manager.get_evaluators()
if evaluators is None:
raise HTTPException(status_code=500, detail="Error processing evaluators file")
if not evaluators:
raise HTTPException(status_code=404, detail="No evaluators found")
return evaluators
@router.post("/map/", response_model=EvaluatorMappingOutputInterface)
async def evaluator_data_map(request: Request, payload: EvaluatorMappingInputInterface):
"""Endpoint to map the experiment data tree to evaluator interface.
Args:
request (Request): The request object.
payload (EvaluatorMappingInputInterface): The payload containing the request data.
Returns:
EvaluatorMappingOutputInterface: the evaluator mapping output object
"""
mapped_outputs = await evaluators_service.map(mapping_input=payload)
return mapped_outputs
@router.post("/{evaluator_key}/run/", response_model=EvaluatorOutputInterface)
async def evaluator_run(
request: Request, evaluator_key: str, payload: EvaluatorInputInterface
):
"""Endpoint to evaluate LLM app run
Args:
request (Request): The request object.
evaluator_key (str): The key of the evaluator.
payload (EvaluatorInputInterface): The payload containing the request data.
Returns:
result: EvaluatorOutputInterface object containing the outputs.
"""
providers_keys_from_vault = await get_llm_providers_secrets(
project_id=request.state.project_id
)
payload.credentials = providers_keys_from_vault
try:
result = await evaluators_service.run(
evaluator_key=evaluator_key,
evaluator_input=payload,
)
except Exception as e:
log.warning(f"Error with evaluator /run", exc_info=True)
raise HTTPException(status_code=424 if "401" in str(e) else 500, detail=str(e))
return result
@router.get("/configs/", response_model=List[EvaluatorConfig])
async def get_evaluator_configs(
request: Request,
app_id: Optional[str] = None,
):
"""Endpoint to fetch evaluator configurations for a specific app.
Args:
app_id (str): The ID of the app.
Returns:
List[EvaluatorConfigDB]: A list of evaluator configuration objects.
"""
project_id: Optional[str] = None
if app_id:
app_db = await db_manager.fetch_app_by_id(app_id=app_id)
project_id = str(app_db.project_id)
else:
project_id = getattr(request.state, "project_id", None)
if project_id is None:
raise HTTPException(status_code=400, detail="project_id is required")
if is_ee():
has_permission = await check_action_access(
user_uid=request.state.user_id,
project_id=project_id,
permission=Permission.VIEW_EVALUATION,
)
if not has_permission:
error_msg = (
"You do not have permission to perform this action. "
"Please contact your organization admin."
)
log.error(error_msg)
return JSONResponse(
{"detail": error_msg},
status_code=403,
)
evaluators_configs = await evaluator_manager.get_evaluators_configs(project_id)
return evaluators_configs
@router.get("/configs/{evaluator_config_id}/", response_model=EvaluatorConfig)
async def get_evaluator_config(
evaluator_config_id: str,
request: Request,
):
"""Endpoint to fetch evaluator configurations for a specific app.
Returns:
List[EvaluatorConfigDB]: A list of evaluator configuration objects.
"""
evaluator_config_db = await db_manager.fetch_evaluator_config(evaluator_config_id)
if is_ee():
has_permission = await check_action_access(
user_uid=request.state.user_id,
project_id=str(evaluator_config_db.project_id),
permission=Permission.VIEW_EVALUATION,
)
if not has_permission:
error_msg = f"You do not have permission to perform this action. Please contact your organization admin."
log.error(error_msg)
return JSONResponse(
{"detail": error_msg},
status_code=403,
)
evaluators_configs = await evaluator_manager.get_evaluator_config(
evaluator_config_db
)
return evaluators_configs
@router.post("/configs/", response_model=EvaluatorConfig)
async def create_new_evaluator_config(
payload: NewEvaluatorConfig,
request: Request,
):
"""Endpoint to fetch evaluator configurations for a specific app.
Args:
app_id (str): The ID of the app.
Returns:
EvaluatorConfigDB: Evaluator configuration api model.
"""
if is_ee():
has_permission = await check_action_access(
user_uid=request.state.user_id,
project_id=request.state.project_id,
permission=Permission.CREATE_EVALUATION,
)
if not has_permission:
error_msg = f"You do not have permission to perform this action. Please contact your organization admin."
log.error(error_msg)
return JSONResponse(
{"detail": error_msg},
status_code=403,
)
evaluator_config = await evaluator_manager.create_evaluator_config(
project_id=request.state.project_id,
name=payload.name,
evaluator_key=payload.evaluator_key,
settings_values=payload.settings_values,
)
return evaluator_config
@router.put("/configs/{evaluator_config_id}/", response_model=EvaluatorConfig)
async def update_evaluator_config(
evaluator_config_id: str,
payload: UpdateEvaluatorConfig,
request: Request,
):
"""Endpoint to update evaluator configurations for a specific app.
Returns:
List[EvaluatorConfigDB]: A list of evaluator configuration objects.
"""
evaluator_config = await db_manager.fetch_evaluator_config(
evaluator_config_id=evaluator_config_id
)
if is_ee():
has_permission = await check_action_access(
user_uid=request.state.user_id,
project_id=str(evaluator_config.project_id),
permission=Permission.EDIT_EVALUATION,
)
if not has_permission:
error_msg = f"You do not have permission to perform this action. Please contact your organization admin."
log.error(error_msg)
return JSONResponse(
{"detail": error_msg},
status_code=403,
)
evaluators_configs = await evaluator_manager.update_evaluator_config(
evaluator_config_id=evaluator_config_id, updates=payload.model_dump()
)
return evaluators_configs
@router.delete("/configs/{evaluator_config_id}/", response_model=bool)
async def delete_evaluator_config(
evaluator_config_id: str,
request: Request,
):
"""Endpoint to delete a specific evaluator configuration.
Args:
evaluator_config_id (str): The unique identifier of the evaluator configuration.
Returns:
bool: True if deletion was successful, False otherwise.
"""
evaluator_config = await db_manager.fetch_evaluator_config(
evaluator_config_id=evaluator_config_id
)
if is_ee():
has_permission = await check_action_access(
user_uid=request.state.user_id,
project_id=str(evaluator_config.project_id),
permission=Permission.DELETE_EVALUATION,
)
if not has_permission:
error_msg = f"You do not have permission to perform this action. Please contact your organization admin."
log.error(error_msg)
return JSONResponse(
{"detail": error_msg},
status_code=403,
)
success = await evaluator_manager.delete_evaluator_config(evaluator_config_id)
return success