Detect invalid response from api.siliconflow.cn (#3792)
Browse files### 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)
rag/llm/embedding_model.py
CHANGED
|
@@ -606,6 +606,8 @@ class SILICONFLOWEmbed(Base):
|
|
| 606 |
"encoding_format": "float",
|
| 607 |
}
|
| 608 |
res = requests.post(self.base_url, json=payload, headers=self.headers).json()
|
|
|
|
|
|
|
| 609 |
return (
|
| 610 |
np.array([d["embedding"] for d in res["data"]]),
|
| 611 |
res["usage"]["total_tokens"],
|
|
@@ -618,6 +620,8 @@ class SILICONFLOWEmbed(Base):
|
|
| 618 |
"encoding_format": "float",
|
| 619 |
}
|
| 620 |
res = requests.post(self.base_url, json=payload, headers=self.headers).json()
|
|
|
|
|
|
|
| 621 |
return np.array(res["data"][0]["embedding"]), res["usage"]["total_tokens"]
|
| 622 |
|
| 623 |
|
|
|
|
| 606 |
"encoding_format": "float",
|
| 607 |
}
|
| 608 |
res = requests.post(self.base_url, json=payload, headers=self.headers).json()
|
| 609 |
+
if "data" not in res or not isinstance(res["data"], list) or len(res["data"])!= len(texts):
|
| 610 |
+
raise ValueError(f"SILICONFLOWEmbed.encode got invalid response from {self.base_url}")
|
| 611 |
return (
|
| 612 |
np.array([d["embedding"] for d in res["data"]]),
|
| 613 |
res["usage"]["total_tokens"],
|
|
|
|
| 620 |
"encoding_format": "float",
|
| 621 |
}
|
| 622 |
res = requests.post(self.base_url, json=payload, headers=self.headers).json()
|
| 623 |
+
if "data" not in res or not isinstance(res["data"], list) or len(res["data"])!= 1:
|
| 624 |
+
raise ValueError(f"SILICONFLOWEmbed.encode_queries got invalid response from {self.base_url}")
|
| 625 |
return np.array(res["data"][0]["embedding"]), res["usage"]["total_tokens"]
|
| 626 |
|
| 627 |
|