Convert a string or Metric enum to a Metric enum member.
(cls, metric: str | Metric)
| 66 | |
| 67 | @classmethod |
| 68 | def from_string(cls, metric: str | Metric) -> Metric: |
| 69 | """Convert a string or Metric enum to a Metric enum member.""" |
| 70 | if isinstance(metric, cls): |
| 71 | return metric |
| 72 | if isinstance(metric, str): |
| 73 | mapping = { |
| 74 | "cos": cls.COSINE, |
| 75 | "cosine": cls.COSINE, |
| 76 | "dot": cls.COSINE, |
| 77 | "euclidean": cls.EUCLIDEAN, |
| 78 | "l2": cls.EUCLIDEAN, |
| 79 | "manhattan": cls.MANHATTAN, |
| 80 | "l1": cls.MANHATTAN, |
| 81 | "inner_product": cls.INNER_PRODUCT, |
| 82 | "ip": cls.INNER_PRODUCT, |
| 83 | "l2sq": cls.L2_SQUARED, |
| 84 | "l2_squared": cls.L2_SQUARED, |
| 85 | "hamming": cls.HAMMING, |
| 86 | "tanimoto": cls.TANIMOTO, |
| 87 | } |
| 88 | metric_str = metric.lower() |
| 89 | if metric_str in mapping: |
| 90 | return mapping[metric_str] |
| 91 | raise ValueError(f"Unsupported metric: {metric}") |
no outgoing calls
no test coverage detected