fix: EXPECTED_API_KEY 改为惰性求值防加载顺序问题

This commit is contained in:
2026-07-10 15:27:38 +08:00
parent 29bdfcfb18
commit ebfae1f60c
3 changed files with 18 additions and 12 deletions
+3 -2
View File
@@ -154,6 +154,7 @@ class DashscopeEmbedder(_BaseAPIEmbedder):
def embed(self, texts: list[str]) -> list[list[float]]:
if not texts:
raise ValueError("文本列表不能为空")
import requests # 惰性导入(仅 DashScope 使用)
resp = requests.post(
self._api_base,
headers={
@@ -176,8 +177,8 @@ class DashscopeEmbedder(_BaseAPIEmbedder):
if not isinstance(embeddings_raw, list):
raise ValueError(f"DashScope embeddings 不是列表: {type(embeddings_raw)}")
# 按 text_index 排序确保顺序
embeddings_raw.sort(key=lambda x: x.get("text_index", 0))
return [e["embedding"] for e in embeddings_raw]
embeddings_raw_sorted = sorted(embeddings_raw, key=lambda x: x.get("text_index", 0))
return [e["embedding"] for e in embeddings_raw_sorted]
# -- 工厂函数 --