添加search方法
This commit is contained in:
@@ -57,7 +57,7 @@ def chunks_embedding(texts: list[str], api_key: str) -> list[list[float]]:
|
||||
|
||||
if "output" in result and "embeddings" in result["output"]:
|
||||
embeddings_list = result["output"]["embeddings"]
|
||||
embeddings_list.sort(key=lambda x: x["text_index"])
|
||||
embeddings_list.sort(key=lambda x: x["text_index"]) # 按文本索引排序,确保顺序一致
|
||||
|
||||
# --- 核心修复:对每个浮点数保留 8 位小数,解决精度过高报错 ---
|
||||
final_vectors = []
|
||||
|
||||
46
nodes/embedding.py
Normal file
46
nodes/embedding.py
Normal file
@@ -0,0 +1,46 @@
|
||||
import requests
|
||||
|
||||
def chunks_embedding(texts: list[str], api_key: str) -> list[list[float]]:
|
||||
if not texts:
|
||||
return []
|
||||
|
||||
MODEL_NAME = "text-embedding-v4"
|
||||
url = "https://dashscope.aliyuncs.com/api/v1/services/embeddings/text-embedding/text-embedding"
|
||||
headers = {
|
||||
"Authorization": f"Bearer {api_key}",
|
||||
"Content-Type": "application/json"
|
||||
}
|
||||
payload = {
|
||||
"model": MODEL_NAME,
|
||||
"input": {"texts": texts},
|
||||
"parameters": {"text_type": "document", "dimension": 1536}
|
||||
}
|
||||
|
||||
try:
|
||||
response = requests.post(url, headers=headers, json=payload, timeout=60)
|
||||
response.raise_for_status()
|
||||
result = response.json()
|
||||
|
||||
if "output" in result and "embeddings" in result["output"]:
|
||||
embeddings_list = result["output"]["embeddings"]
|
||||
embeddings_list.sort(key=lambda x: x["text_index"])
|
||||
|
||||
# --- 核心修复:对每个浮点数保留 8 位小数,解决精度过高报错 ---
|
||||
final_vectors = []
|
||||
for item in embeddings_list:
|
||||
# 将每个 float 限制在 8 位精度以内
|
||||
rounded_vector = [round(float(val), 8) for val in item["embedding"]]
|
||||
final_vectors.append(rounded_vector)
|
||||
return final_vectors
|
||||
else:
|
||||
return [None] * len(texts)
|
||||
except Exception as e:
|
||||
print(f"Alibaba Embedding Error: {e}")
|
||||
return [None] * len(texts)
|
||||
|
||||
def main(text: str, api_key: str):
|
||||
|
||||
vector = chunks_embedding([text], api_key)[0]
|
||||
return {
|
||||
'vector': vector
|
||||
}
|
||||
Reference in New Issue
Block a user