为了保持架构的一致性,三个相似度计算模块都实现了统一的笛卡尔积接口。
特点: GPU加速向量计算,一次API调用完成M×N计算
from lib.text_embedding_api import compare_phrases_cartesian
# 返回numpy矩阵(仅分数)
matrix = compare_phrases_cartesian(
["深度学习", "机器学习"],
["神经网络", "人工智能"],
return_matrix=True
)
# shape: (2, 2)
# 返回嵌套列表(完整结果)
results = compare_phrases_cartesian(
["深度学习", "机器学习"],
["神经网络", "人工智能"],
return_matrix=False
)
# results[i][j] = {"相似度": float, "说明": str}
性能:
特点: LLM并发调用,M×N个独立任务并发执行
from lib.semantic_similarity import compare_phrases_cartesian
# 返回numpy矩阵(仅分数)
matrix = await compare_phrases_cartesian(
["深度学习", "机器学习"],
["神经网络", "人工智能"],
return_matrix=True
)
# shape: (2, 2)
# 返回嵌套列表(完整结果)
results = await compare_phrases_cartesian(
["深度学习", "机器学习"],
["神经网络", "人工智能"],
return_matrix=False
)
# results[i][j] = {"相似度": float, "说明": str}
说明:
asyncio.gather() 实现并发特点: 结合向量API笛卡尔积(快)+ LLM并发(已优化)
from lib.hybrid_similarity import compare_phrases_cartesian
# 返回numpy矩阵(仅分数)
matrix = await compare_phrases_cartesian(
["深度学习", "机器学习"],
["神经网络", "人工智能"],
weight_embedding=0.7,
weight_semantic=0.3,
return_matrix=True
)
# shape: (2, 2)
# matrix[i][j] = embedding_score * 0.7 + semantic_score * 0.3
# 返回嵌套列表(完整结果)
results = await compare_phrases_cartesian(
["深度学习", "机器学习"],
["神经网络", "人工智能"],
weight_embedding=0.7,
weight_semantic=0.3,
return_matrix=False
)
# results[i][j] = {"相似度": float, "说明": str}
计算流程:
text_embedding_api.compare_phrases_cartesian() (一次API)semantic_similarity.compare_phrases_cartesian() (M×N并发)hybrid_score = embedding * w1 + semantic * w2返回嵌套列表 List[List[Dict]]:
results[i][j] = {
"相似度": float, # 0-1之间的相似度分数
"说明": str # 相似度说明
}
返回 numpy.ndarray,shape=(M, N):
matrix[i][j] = float # 仅包含相似度分数
| 参数 | text_embedding_api | semantic_similarity | hybrid_similarity |
|---|---|---|---|
| phrases_a | ✅ | ✅ | ✅ |
| phrases_b | ✅ | ✅ | ✅ |
| return_matrix | ✅ | ✅ | ✅ |
| model_name | ✅ | ✅ | semantic_model参数 |
| weight_embedding | ❌ | ❌ | ✅ |
| weight_semantic | ❌ | ❌ | ✅ |
| use_cache | ❌(API已快速) | ✅ | ✅ |
| cache_dir | ❌ | ✅ | ✅(分别配置) |
| **kwargs | ❌ | ✅(temperature等) | ✅(传给semantic) |
from lib.text_embedding_api import compare_phrases_cartesian
# 适用于对速度要求高,接受向量模型精度的场景
matrix = compare_phrases_cartesian(
feature_names, # M个特征
persona_names, # N个人设
return_matrix=True
)
# 耗时: ~500ms (M=10, N=100)
from lib.semantic_similarity import compare_phrases_cartesian
# 适用于对精度要求高,可接受较慢速度的场景
matrix = await compare_phrases_cartesian(
feature_names, # M个特征
persona_names, # N个人设
model_name='openai/gpt-4.1-mini',
return_matrix=True
)
# 耗时: ~30-60s (M=10, N=100,取决于并发)
from lib.hybrid_similarity import compare_phrases_cartesian
# 适用于需要平衡速度和精度的场景
matrix = await compare_phrases_cartesian(
feature_names, # M个特征
persona_names, # N个人设
weight_embedding=0.7, # 更倾向快速的向量结果
weight_semantic=0.3, # 辅以LLM精度
return_matrix=True
)
# 耗时: ~30-60s (瓶颈在LLM)
# 但结果融合了向量和LLM的优势
假设 M=10 个特征,N=100 个人设(共1000对计算):
| 模块 | 计算方式 | 耗时 | 说明 |
|---|---|---|---|
| 逐对调用 | M×N次单独API调用 | ~100s | 原方案(未优化) |
| text_embedding_api | 1次笛卡尔积API | ~0.5s | 200x加速 ⚡ |
| semantic_similarity | M×N并发LLM调用 | ~30-60s | 2-3x加速 |
| hybrid_similarity | 1次API + M×N并发 | ~30-60s | 瓶颈在LLM部分 |
{"相似度": float, "说明": str}text_embedding_api(快速迭代)semantic_similarity(高精度验证)hybrid_similarity(平衡性能和精度)运行测试脚本验证接口:
# 测试API笛卡尔积(快速)
python3 test_cartesian_simple.py
# 测试所有接口(需要完整环境)
python3 test_cartesian_interfaces.py
通过为三个模块统一实现笛卡尔积接口: