fix: auto-fix code issues (cron)
- 修复重复导入/字段 - 修复异常处理 - 修复PEP8格式问题 - 添加类型注解
This commit is contained in:
@@ -4,12 +4,12 @@ Entity Aligner - Phase 3
|
||||
使用 embedding 进行实体对齐
|
||||
"""
|
||||
|
||||
import os
|
||||
import json
|
||||
import os
|
||||
from dataclasses import dataclass
|
||||
|
||||
import httpx
|
||||
import numpy as np
|
||||
from typing import List, Optional, Dict
|
||||
from dataclasses import dataclass
|
||||
|
||||
# API Keys
|
||||
KIMI_API_KEY = os.getenv("KIMI_API_KEY", "")
|
||||
@@ -21,7 +21,7 @@ class EntityEmbedding:
|
||||
entity_id: str
|
||||
name: str
|
||||
definition: str
|
||||
embedding: List[float]
|
||||
embedding: list[float]
|
||||
|
||||
|
||||
class EntityAligner:
|
||||
@@ -29,9 +29,9 @@ class EntityAligner:
|
||||
|
||||
def __init__(self, similarity_threshold: float = 0.85):
|
||||
self.similarity_threshold = similarity_threshold
|
||||
self.embedding_cache: Dict[str, List[float]] = {}
|
||||
self.embedding_cache: dict[str, list[float]] = {}
|
||||
|
||||
def get_embedding(self, text: str) -> Optional[List[float]]:
|
||||
def get_embedding(self, text: str) -> list[float] | None:
|
||||
"""
|
||||
使用 Kimi API 获取文本的 embedding
|
||||
|
||||
@@ -67,7 +67,7 @@ class EntityAligner:
|
||||
print(f"Embedding API failed: {e}")
|
||||
return None
|
||||
|
||||
def compute_similarity(self, embedding1: List[float], embedding2: List[float]) -> float:
|
||||
def compute_similarity(self, embedding1: list[float], embedding2: list[float]) -> float:
|
||||
"""
|
||||
计算两个 embedding 的余弦相似度
|
||||
|
||||
@@ -111,9 +111,9 @@ class EntityAligner:
|
||||
project_id: str,
|
||||
name: str,
|
||||
definition: str = "",
|
||||
exclude_id: Optional[str] = None,
|
||||
threshold: Optional[float] = None,
|
||||
) -> Optional[object]:
|
||||
exclude_id: str | None = None,
|
||||
threshold: float | None = None,
|
||||
) -> object | None:
|
||||
"""
|
||||
查找相似的实体
|
||||
|
||||
@@ -175,8 +175,8 @@ class EntityAligner:
|
||||
return best_match
|
||||
|
||||
def _fallback_similarity_match(
|
||||
self, entities: List[object], name: str, exclude_id: Optional[str] = None
|
||||
) -> Optional[object]:
|
||||
self, entities: list[object], name: str, exclude_id: str | None = None
|
||||
) -> object | None:
|
||||
"""
|
||||
回退到简单的相似度匹配(不使用 embedding)
|
||||
|
||||
@@ -209,8 +209,8 @@ class EntityAligner:
|
||||
return None
|
||||
|
||||
def batch_align_entities(
|
||||
self, project_id: str, new_entities: List[Dict], threshold: Optional[float] = None
|
||||
) -> List[Dict]:
|
||||
self, project_id: str, new_entities: list[dict], threshold: float | None = None
|
||||
) -> list[dict]:
|
||||
"""
|
||||
批量对齐实体
|
||||
|
||||
@@ -257,7 +257,7 @@ class EntityAligner:
|
||||
|
||||
return results
|
||||
|
||||
def suggest_entity_aliases(self, entity_name: str, entity_definition: str = "") -> List[str]:
|
||||
def suggest_entity_aliases(self, entity_name: str, entity_definition: str = "") -> list[str]:
|
||||
"""
|
||||
使用 LLM 建议实体的别名
|
||||
|
||||
|
||||
Reference in New Issue
Block a user