test_embeddings.py 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. """
  2. 测试 Embeddings 模块(不依赖 Milvus)
  3. """
  4. import asyncio
  5. import sys
  6. from pathlib import Path
  7. sys.path.insert(0, str(Path(__file__).parent))
  8. # 加载环境变量
  9. from dotenv import load_dotenv
  10. load_dotenv(Path(__file__).parent / ".env")
  11. from knowhub.embeddings import get_embedding, get_embeddings_batch
  12. async def test_embeddings():
  13. print("=" * 60)
  14. print("测试 Embeddings 模块")
  15. print("=" * 60)
  16. # 测试单条
  17. print("\n1. 测试单条 embedding 生成...")
  18. text = "如何使用 Python 读取 PDF 文件"
  19. try:
  20. embedding = await get_embedding(text)
  21. print(f"✓ 成功生成 embedding")
  22. print(f" 文本: {text}")
  23. print(f" 向量维度: {len(embedding)}")
  24. print(f" 前 5 个值: {embedding[:5]}")
  25. except Exception as e:
  26. print(f"✗ 失败: {e}")
  27. return
  28. # 测试批量
  29. print("\n2. 测试批量 embedding 生成...")
  30. texts = [
  31. "使用 pymupdf 读取 PDF",
  32. "使用 selenium 进行网页自动化",
  33. "使用 pandas 处理数据"
  34. ]
  35. try:
  36. embeddings = await get_embeddings_batch(texts)
  37. print(f"✓ 成功生成批量 embeddings")
  38. print(f" 文本数量: {len(texts)}")
  39. print(f" 向量数量: {len(embeddings)}")
  40. print(f" 每个向量维度: {len(embeddings[0])}")
  41. except Exception as e:
  42. print(f"✗ 失败: {e}")
  43. return
  44. print("\n" + "=" * 60)
  45. print("Embeddings 模块测试通过!")
  46. print("=" * 60)
  47. if __name__ == "__main__":
  48. asyncio.run(test_embeddings())