Skip to content

Commit

Permalink
Async call of OpenAISimilarityScorer is slower than sync version
Browse files Browse the repository at this point in the history
Fixes #160
  • Loading branch information
Vela-zz committed Nov 13, 2024
1 parent d9ff16f commit 8a9eaa6
Showing 1 changed file with 13 additions and 14 deletions.
27 changes: 13 additions & 14 deletions src/langcheck/metrics/eval_clients/_openai.py
Original file line number Diff line number Diff line change
Expand Up @@ -409,26 +409,25 @@ def __init__(
self.openai_args = openai_args
self._use_async = use_async

async def _async_embed(self, inputs: list[str]) -> torch.Tensor:
if self.openai_args:
responses = await self.openai_client.embeddings.create(
input=inputs, **self.openai_args
)
else:
responses = await self.openai_client.embeddings.create(
input=inputs, model="text-embedding-3-small"
)
return responses

def _embed(self, inputs: list[str]) -> torch.Tensor:
"""Embed the inputs using the OpenAI API."""

# TODO: Fix that this async call could be much slower than the sync
# version. https://github.com/citadel-ai/langcheck/issues/160
if self._use_async:

async def _call_async_api() -> Any:
assert isinstance(self.openai_client, AsyncOpenAI)
if self.openai_args:
responses = await self.openai_client.embeddings.create(
input=inputs, **self.openai_args
)
else:
responses = await self.openai_client.embeddings.create(
input=inputs, model="text-embedding-3-small"
)
return responses

embed_response = asyncio.run(_call_async_api())
assert isinstance(self.openai_client, AsyncOpenAI)
embed_response = asyncio.get_event_loop().run_until_complete(self._async_embed(inputs))
embeddings = [item.embedding for item in embed_response.data]

else:
Expand Down

0 comments on commit 8a9eaa6

Please sign in to comment.