Skip to content

Commit 0710f21

Browse files
authored
fix: resolve TypeError in TopicAdherenceScore bitwise operations (#2258)
## Issue Link / Problem Description <!-- Link to related issue or describe the problem this PR solves --> - Fixes #2255 ## Changes Made <!-- Describe what you changed and why --> - Fix intermittent TypeError when performing bitwise AND operations between numpy arrays with incompatible dtypes in TopicAdherenceScore metric. Signed-off-by: Kumar Anirudha <[email protected]>
1 parent 83d25e3 commit 0710f21

File tree

1 file changed

+45
-4
lines changed

1 file changed

+45
-4
lines changed

src/ragas/metrics/_topic_adherence.py

Lines changed: 45 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -173,16 +173,57 @@ async def _multi_turn_ascore(
173173
)
174174
topic_answered_verdict.append(response.refused_to_answer)
175175
topic_answered_verdict = np.array(
176-
[not answer for answer in topic_answered_verdict]
176+
[not answer for answer in topic_answered_verdict], dtype=bool
177177
)
178178

179179
prompt_input = TopicClassificationInput(
180180
reference_topics=sample.reference_topics, topics=topics
181181
)
182-
topic_classifications = await self.topic_classification_prompt.generate(
183-
data=prompt_input, llm=self.llm, callbacks=callbacks
182+
topic_classifications_response = (
183+
await self.topic_classification_prompt.generate(
184+
data=prompt_input, llm=self.llm, callbacks=callbacks
185+
)
186+
)
187+
188+
# Ensure safe conversion to boolean array to avoid TypeError in bitwise operations
189+
def safe_bool_conversion(classifications):
190+
"""Safely convert classifications to boolean array regardless of input type"""
191+
classifications_array = np.array(classifications)
192+
193+
if classifications_array.dtype == bool:
194+
return classifications_array
195+
elif classifications_array.dtype in [
196+
int,
197+
np.int64,
198+
np.int32,
199+
np.int16,
200+
np.int8,
201+
]:
202+
return classifications_array.astype(bool)
203+
elif classifications_array.dtype.kind in [
204+
"U",
205+
"S",
206+
"O",
207+
]: # Unicode, byte string, or object
208+
# String/object arrays
209+
bool_list = []
210+
for item in classifications_array:
211+
if isinstance(item, bool):
212+
bool_list.append(item)
213+
elif isinstance(item, (int, np.integer)):
214+
bool_list.append(bool(item))
215+
elif isinstance(item, str):
216+
# String representations of booleans
217+
bool_list.append(item.lower() in ["true", "1", "yes"])
218+
else:
219+
bool_list.append(bool(item))
220+
return np.array(bool_list, dtype=bool)
221+
else:
222+
return classifications_array.astype(bool)
223+
224+
topic_classifications = safe_bool_conversion(
225+
topic_classifications_response.classifications
184226
)
185-
topic_classifications = np.array(topic_classifications.classifications)
186227

187228
true_positives = sum(topic_answered_verdict & topic_classifications)
188229
false_positives = sum(topic_answered_verdict & ~topic_classifications)

0 commit comments

Comments
 (0)