Skip to content

Commit 94c626a

Browse files
committed
add try catch
1 parent 0ac8e5d commit 94c626a

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

src/cleanlab_codex/validator.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,11 +139,16 @@ def validate(
139139
- 'is_bad_response': True if the response is flagged as potentially bad (when True, a lookup in Codex is performed), False otherwise.
140140
- Additional keys: Various keys from a [`ThresholdedTrustworthyRAGScore`](/cleanlab_codex/types/validator/#class-thresholdedtrustworthyragscore) dictionary, with raw scores from [TrustworthyRAG](/tlm/api/python/utils.rag/#class-trustworthyrag) for each evaluation metric. `is_bad` indicating whether the score is below the threshold.
141141
"""
142-
loop = asyncio.get_event_loop()
142+
try:
143+
loop = asyncio.get_running_loop()
144+
except RuntimeError: # No running loop
145+
loop = asyncio.new_event_loop()
146+
asyncio.set_event_loop(loop)
143147
expert_task = loop.create_task(self.remediate_async(query))
144148
detect_task = loop.run_in_executor(None, self.detect, query, context, response, prompt, form_prompt)
145149
expert_answer, maybe_entry = loop.run_until_complete(expert_task)
146150
scores, is_bad_response = loop.run_until_complete(detect_task)
151+
loop.close()
147152
if is_bad_response:
148153
if expert_answer == None:
149154
self._project._sdk_client.projects.entries.add_question(

0 commit comments

Comments
 (0)