Skip to content

Commit

Permalink
Batch request size to Azure OpenAI
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelAnckaert committed Dec 15, 2023
1 parent 3b3c4db commit 5507b80
Showing 1 changed file with 17 additions and 11 deletions.
28 changes: 17 additions & 11 deletions pinecone_text/dense/openai_encoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,17 +66,23 @@ def _encode(
f"texts must be a string or list of strings, got: {type(texts)}"
)

try:
response = self._client.embeddings.create(
input=texts_input, model=self._model_name
)
except OpenAIError as e:
# TODO: consider wrapping external provider errors
raise e

if isinstance(texts, str):
return response.data[0].embedding
return [result.embedding for result in response.data]
batch_size = 16 # Azure OpenAI limit as of 2023-11-27
result = []
for i in range(0, len(texts), batch_size):
batch = texts[i : i + batch_size]
try:
response = self._client.embeddings.create(
input=batch, model=self._model_name
)
except OpenAIError as e:
# TODO: consider wrapping external provider errors
raise e

if isinstance(batch, str):
result.extend(response.data[0].embedding)
result.extend([result.embedding for result in response.data])

return result


class AzureOpenAIEncoder(OpenAIEncoder):
Expand Down

0 comments on commit 5507b80

Please sign in to comment.