fix(avatar): normalize embedding API endpoint

This commit is contained in:
stefanfeng
2026-08-28 16:59:22 +08:00
parent 7884430b3d
commit 6794e88d53
3 changed files with 29 additions and 2 deletions
+9 -1
View File
@@ -18,6 +18,14 @@ EMBED_DIM = 256
MODEL = os.getenv("EMBEDDING_MODEL", "mock-hash-embed-v1")
def _embedding_endpoint(api_url):
"""Accept either an OpenAI-compatible base URL or its full endpoint."""
api_url = (api_url or "").strip().rstrip("/")
if not api_url or api_url.endswith("/embeddings"):
return api_url
return f"{api_url}/embeddings"
def _tokenize(text):
text = (text or "").lower()
# 英文/数字按词,CJK 逐字(中文无空格,需拆到字级才能命中子词)
@@ -47,7 +55,7 @@ def embed(texts):
"""返回 list[list[float]],与输入顺序一致。"""
if not texts:
return []
api_url = os.getenv("EMBEDDING_API_URL")
api_url = _embedding_endpoint(os.getenv("EMBEDDING_API_URL"))
if api_url:
api_key = os.getenv("EMBEDDING_API_KEY", "")
model = os.getenv("EMBEDDING_MODEL", "text-embedding-3-small")