Skip to content

Commit 0a21649

Browse files
committed
add async query to improve latency
1 parent be4745c commit 0a21649

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

src/cleanlab_codex/validator.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from __future__ import annotations
66

77
from typing import TYPE_CHECKING, Any, Callable, Optional, cast
8+
import asyncio
89

910
from cleanlab_tlm import TrustworthyRAG
1011
from pydantic import BaseModel, Field, field_validator
@@ -138,10 +139,14 @@ def validate(
138139
- 'is_bad_response': True if the response is flagged as potentially bad (when True, a lookup in Codex is performed), False otherwise.
139140
- 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.
140141
"""
142+
expert_task = asyncio.create_task(self.remediate_async(query))
141143
scores, is_bad_response = self.detect(query, context, response, prompt, form_prompt)
142-
expert_answer = None
143144
if is_bad_response:
144-
expert_answer = self.remediate(query)
145+
expert_answer, maybe_entry = asyncio.run(expert_task)
146+
if expert_answer == None:
147+
self._project.add_entries([maybe_entry])
148+
else:
149+
expert_answer = None
145150

146151
return {
147152
"expert_answer": expert_answer,
@@ -198,3 +203,7 @@ def remediate(self, query: str) -> str | None:
198203
"""
199204
codex_answer, _ = self._project.query(question=query)
200205
return codex_answer
206+
207+
async def remediate_async(self, query: str):
208+
codex_answer, entry = self._project.query(question=query, read_only=True)
209+
return codex_answer, entry

0 commit comments

Comments
 (0)