insert_knowledge.py 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. #!/usr/bin/env python3
  2. """
  3. 插入知识条目到 PostgreSQL 数据库
  4. """
  5. import asyncio
  6. import uuid
  7. import time
  8. import sys
  9. import os
  10. # 添加项目路径
  11. sys.path.insert(0, '/root/Agent')
  12. from knowhub.knowhub_db.pg_store import PostgreSQLStore
  13. from knowhub.embeddings import get_embedding
  14. async def main():
  15. # 知识数据
  16. task = "人物近景肖像"
  17. content = "生成人物近景半身或胸部以上画面、突出面部表情和情绪、背景虚化的核心工序要素包括:1.近景/半身构图 2.面部表情细节 3.背景虚化效果 4.皮肤质感真实 5.眼神互动感"
  18. types = ["strategy"]
  19. tags = {"domain": "AI 生图", "source": "多渠调研汇总", "task": "人物近景肖像"}
  20. score = 5
  21. source = {"category": "research"}
  22. # 生成唯一 ID
  23. knowledge_id = str(uuid.uuid4())
  24. message_id = str(uuid.uuid4())
  25. current_time = int(time.time())
  26. # 生成 embeddings
  27. print(f"正在生成 task embedding...")
  28. task_embedding = await get_embedding(task)
  29. print(f"正在生成 content embedding...")
  30. content_embedding = await get_embedding(content)
  31. # 构建知识对象
  32. knowledge = {
  33. 'id': knowledge_id,
  34. 'message_id': message_id,
  35. 'task': task,
  36. 'content': content,
  37. 'types': types,
  38. 'tags': tags,
  39. 'tag_keys': list(tags.keys()),
  40. 'scopes': ['org:cybertogether'], # 默认可见范围
  41. 'owner': 'system', # 默认所有者
  42. 'source': source,
  43. 'eval': {'score': score},
  44. 'task_embedding': task_embedding,
  45. 'content_embedding': content_embedding,
  46. 'created_at': current_time,
  47. 'updated_at': current_time,
  48. 'status': 'approved',
  49. }
  50. # 插入数据库
  51. print(f"正在插入知识条目到 PostgreSQL...")
  52. store = PostgreSQLStore()
  53. store.insert(knowledge)
  54. store.close()
  55. print(f"成功插入知识条目!")
  56. print(f" ID: {knowledge_id}")
  57. print(f" Task: {task}")
  58. print(f" Types: {types}")
  59. print(f" Score: {score}")
  60. if __name__ == '__main__':
  61. asyncio.run(main())