Detect invalid response from api.siliconflow.cn (#3792)

### What problem does this PR solve?

Detect invalid response from api.siliconflow.cn. Close #2643

### Type of change

- [x] Bug Fix (non-breaking change which fixes an issue)
This commit is contained in:
Zhichang Yu 2024-12-02 12:55:05 +08:00 committed by GitHub
parent deca6c1b72
commit d19f059f34
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -606,6 +606,8 @@ class SILICONFLOWEmbed(Base):
"encoding_format": "float",
}
res = requests.post(self.base_url, json=payload, headers=self.headers).json()
if "data" not in res or not isinstance(res["data"], list) or len(res["data"])!= len(texts):
raise ValueError(f"SILICONFLOWEmbed.encode got invalid response from {self.base_url}")
return (
np.array([d["embedding"] for d in res["data"]]),
res["usage"]["total_tokens"],
@ -618,6 +620,8 @@ class SILICONFLOWEmbed(Base):
"encoding_format": "float",
}
res = requests.post(self.base_url, json=payload, headers=self.headers).json()
if "data" not in res or not isinstance(res["data"], list) or len(res["data"])!= 1:
raise ValueError(f"SILICONFLOWEmbed.encode_queries got invalid response from {self.base_url}")
return np.array(res["data"][0]["embedding"]), res["usage"]["total_tokens"]