embedding_utils.py 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. import json
  2. import requests
  3. from core.config import logger
  4. from core.database import DBHelper
  5. from data_models.content_chunks import ContentChunks
  6. def get_embedding_data(query):
  7. try:
  8. response = requests.post(
  9. url='http://61.48.133.26:8001/api/search',
  10. json={
  11. "query": query,
  12. "search_type": "by_vector",
  13. "limit": 5},
  14. headers={"Content-Type": "application/json"},
  15. )
  16. return response.json()['results']
  17. except Exception as e:
  18. logger.error(e)
  19. return []
  20. def get_embedding_content_data(query):
  21. res = []
  22. db_helper = DBHelper()
  23. results = get_embedding_data(query)
  24. if results:
  25. for result in results:
  26. content_chunk = db_helper.get(ContentChunks, doc_id=result['doc_id'], chunk_id=result['chunk_id'])
  27. res.append(
  28. {'content': content_chunk.text, 'content_summary': content_chunk.summary, 'score': result['score']})
  29. return res
  30. if __name__ == '__main__':
  31. results = get_embedding_content_data("帮我查询一些篮球相关的知识")
  32. print(results)