|
| 1 | +""" |
| 2 | +DeepEval Metrics - 메트릭 생성 및 설정 |
| 3 | +
|
| 4 | +DeepEval 메트릭의 팩토리 로직과 설정 상수를 제공합니다. |
| 5 | +deepeval_wrapper.py에서 평가 실행 시 이 모듈을 사용합니다. |
| 6 | +""" |
| 7 | + |
| 8 | +from typing import Any, Dict, Type |
| 9 | + |
| 10 | +# DeepEval 메트릭 이름 → 설명 (list_tasks 등에서 사용) |
| 11 | +DEEPEVAL_METRIC_DESCRIPTIONS: Dict[str, str] = { |
| 12 | + "answer_relevancy": "답변이 질문과 얼마나 관련있는지", |
| 13 | + "faithfulness": "답변이 컨텍스트에 충실한지 (Hallucination 방지)", |
| 14 | + "contextual_precision": "검색된 컨텍스트의 정밀도", |
| 15 | + "contextual_recall": "검색된 컨텍스트의 재현율", |
| 16 | + "hallucination": "환각 감지", |
| 17 | + "toxicity": "독성 평가", |
| 18 | + "bias": "편향 평가", |
| 19 | + "summarization": "요약 품질", |
| 20 | + "geval": "커스텀 평가 기준", |
| 21 | +} |
| 22 | + |
| 23 | + |
| 24 | +def get_deepeval_metric_map() -> Dict[str, Type[Any]]: |
| 25 | + """ |
| 26 | + DeepEval 메트릭 클래스 매핑 반환. |
| 27 | +
|
| 28 | + Returns: |
| 29 | + metric_name → Metric class 매핑 |
| 30 | + """ |
| 31 | + from deepeval.metrics import ( # type: ignore[import-untyped] |
| 32 | + AnswerRelevancyMetric, |
| 33 | + BiasMetric, |
| 34 | + ContextualPrecisionMetric, |
| 35 | + ContextualRecallMetric, |
| 36 | + FaithfulnessMetric, |
| 37 | + GEval, |
| 38 | + HallucinationMetric, |
| 39 | + SummarizationMetric, |
| 40 | + ToxicityMetric, |
| 41 | + ) |
| 42 | + |
| 43 | + return { |
| 44 | + "answer_relevancy": AnswerRelevancyMetric, |
| 45 | + "faithfulness": FaithfulnessMetric, |
| 46 | + "contextual_precision": ContextualPrecisionMetric, |
| 47 | + "contextual_recall": ContextualRecallMetric, |
| 48 | + "hallucination": HallucinationMetric, |
| 49 | + "toxicity": ToxicityMetric, |
| 50 | + "bias": BiasMetric, |
| 51 | + "summarization": SummarizationMetric, |
| 52 | + "geval": GEval, |
| 53 | + } |
| 54 | + |
| 55 | + |
| 56 | +def create_deepeval_metric( |
| 57 | + metric_name: str, |
| 58 | + model: str = "gpt-4o-mini", |
| 59 | + threshold: float = 0.5, |
| 60 | + include_reason: bool = True, |
| 61 | + async_mode: bool = True, |
| 62 | + **kwargs: Any, |
| 63 | +) -> Any: |
| 64 | + """ |
| 65 | + DeepEval 메트릭 인스턴스 생성. |
| 66 | +
|
| 67 | + Args: |
| 68 | + metric_name: 메트릭 이름 (answer_relevancy, faithfulness 등) |
| 69 | + model: LLM 모델 |
| 70 | + threshold: 통과 임계값 |
| 71 | + include_reason: 평가 이유 포함 여부 |
| 72 | + async_mode: 비동기 모드 사용 |
| 73 | + **kwargs: 메트릭별 추가 파라미터 |
| 74 | +
|
| 75 | + Returns: |
| 76 | + DeepEval Metric 인스턴스 |
| 77 | +
|
| 78 | + Raises: |
| 79 | + ImportError: deepeval 미설치 시 |
| 80 | + ValueError: 알 수 없는 메트릭 이름 |
| 81 | + """ |
| 82 | + metric_map = get_deepeval_metric_map() |
| 83 | + |
| 84 | + if metric_name not in metric_map: |
| 85 | + raise ValueError(f"Unknown metric: {metric_name}. Available: {list(metric_map.keys())}") |
| 86 | + |
| 87 | + metric_class = metric_map[metric_name] |
| 88 | + |
| 89 | + return metric_class( |
| 90 | + model=model, |
| 91 | + threshold=threshold, |
| 92 | + include_reason=include_reason, |
| 93 | + async_mode=async_mode, |
| 94 | + **kwargs, |
| 95 | + ) |
0 commit comments