Skip to content

Commit

Permalink
Detect invalid response from api.siliconflow.cn. Close #2463
Browse files Browse the repository at this point in the history
  • Loading branch information
yuzhichang committed Dec 2, 2024
1 parent deca6c1 commit 7432734
Showing 1 changed file with 4 additions and 0 deletions.
4 changes: 4 additions & 0 deletions rag/llm/embedding_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -606,6 +606,8 @@ def encode(self, texts: list, batch_size=16):
"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"],
Expand All @@ -618,6 +620,8 @@ def encode_queries(self, text):
"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"]


Expand Down

0 comments on commit 7432734

Please sign in to comment.