Skip to content

Commit

Permalink
[fix] jina json decode error
Browse files Browse the repository at this point in the history
  • Loading branch information
DninoAdnane committed Jan 3, 2025
1 parent 9b9c21d commit 0d644c1
Showing 1 changed file with 16 additions and 6 deletions.
22 changes: 16 additions & 6 deletions edenai_apis/apis/jina/jina_api.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from json import JSONDecodeError
from typing import List, Dict, Optional

import requests
Expand Down Expand Up @@ -32,16 +33,25 @@ def text__embeddings(
model = model or "jina-embeddings-v2-base-en"
resp = self.session.post( # type: ignore
self.api_url, json={"input": texts, "model": model}
).json()
if "data" not in resp:
raise ProviderException(resp["detail"])
embeddings = resp["data"]
)
try:
original_resp = resp.json()
except JSONDecodeError as exp:
raise ProviderException(
message="Internal server error", code=resp.status_code
) from exp
if "data" not in original_resp:
raise ProviderException(original_resp["detail"], resp.status_code)
embeddings = original_resp["data"]
# Sort resulting embeddings by index
sorted_embeddings = sorted(embeddings, key=lambda e: e["index"]) # type: ignore
# Return just the embeddings
items = [EmbeddingDataClass(embedding=result["embedding"]) for result in sorted_embeddings]
items = [
EmbeddingDataClass(embedding=result["embedding"])
for result in sorted_embeddings
]
standardized_response = EmbeddingsDataClass(items=items)
return ResponseType[EmbeddingsDataClass](
original_response=resp,
original_response=original_resp,
standardized_response=standardized_response,
)

0 comments on commit 0d644c1

Please sign in to comment.