Quincy Hsieh commited on
Commit
aeb8c1c
·
1 Parent(s): 50775af

Fix WARNING:llm:ecologits impact calc failed for model=gpt-5.1: float() argument must be a string or a real number, not 'RangeValue'

Browse files
Files changed (1) hide show
  1. llm.py +21 -2
llm.py CHANGED
@@ -31,6 +31,24 @@ except ImportError:
31
  logger.warning("ecologits not installed — CO2 emission will be reported as None.")
32
 
33
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
34
  def call_llm(
35
  prompt: str,
36
  *,
@@ -113,9 +131,10 @@ def call_llm(
113
  request_latency=latency_s,
114
  )
115
  if impacts is not None:
116
- energy_kwh = float(impacts.energy.value)
117
  # ecologits returns gwp in kgCO2eq; convert to grams
118
- co2_grams = float(impacts.gwp.value) * 1000.0
 
119
  except Exception as e:
120
  logger.warning(f"ecologits impact calc failed for model={model}: {e}")
121
 
 
31
  logger.warning("ecologits not installed — CO2 emission will be reported as None.")
32
 
33
 
34
+ def _to_scalar(value) -> Optional[float]:
35
+ """
36
+ Normalize an ecologits impact value to a single float.
37
+
38
+ ecologits returns either a plain float or a RangeValue(min, max) depending
39
+ on the model registry entry. For RangeValue we return the midpoint so a
40
+ single representative number flows through the API/UI.
41
+ """
42
+ if value is None:
43
+ return None
44
+ if hasattr(value, "min") and hasattr(value, "max"):
45
+ return (float(value.min) + float(value.max)) / 2.0
46
+ try:
47
+ return float(value)
48
+ except (TypeError, ValueError):
49
+ return None
50
+
51
+
52
  def call_llm(
53
  prompt: str,
54
  *,
 
131
  request_latency=latency_s,
132
  )
133
  if impacts is not None:
134
+ energy_kwh = _to_scalar(impacts.energy.value)
135
  # ecologits returns gwp in kgCO2eq; convert to grams
136
+ gwp_kg = _to_scalar(impacts.gwp.value)
137
+ co2_grams = gwp_kg * 1000.0 if gwp_kg is not None else None
138
  except Exception as e:
139
  logger.warning(f"ecologits impact calc failed for model={model}: {e}")
140