init-db.py 1.2 KB

123456789101112131415161718192021222324252627282930313233
  1. import pymysql
  2. from vector_test.vector_util import MyVector
  3. class Database:
  4. def __init__(self, host, user, password, database):
  5. self.connection = pymysql.connect(
  6. host=host,
  7. user=user,
  8. password=password,
  9. database=database
  10. )
  11. def query(self, sql):
  12. with self.connection.cursor() as cursor:
  13. cursor.execute(sql)
  14. result = cursor.fetchall()
  15. return result
  16. def execute(self, sql):
  17. with self.connection.cursor() as cursor:
  18. cursor.execute(sql)
  19. self.connection.commit()
  20. db = Database('www.tianyunperfect.cn', 'memos', 's3ijTsaH5cciP8s8', 'memos')
  21. client = MyVector("101.43.195.48", port=6333, grpc_port=6334, api_key="tianyunperfect123456", embedding_path='/Users/alvin/IdeaProjects/python-base/vector_test/models--shibing624--text2vec-base-chinese')
  22. # 查询
  23. result = db.query("SELECT id,content,resource_name FROM memo where row_status = 'NORMAL' and creator_id=1")
  24. # 遍历result
  25. for row in result:
  26. client.create_document(collection_name="memo_collection", doc_id=row[0], vector=client.model.encode(row[1]), payload={"content": row[1], "resource_name": row[2]})