nlp_api.py 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. """
  2. @author: luojunhui
  3. """
  4. import requests
  5. import traceback
  6. from requests.exceptions import RequestException, JSONDecodeError
  7. from applications.feishuBotApi import bot
  8. from applications.aliyunLogApi import log
  9. def similarity_between_title_list(target_title_list: list[str], base_title_list: list[str]) -> list[list[float]]:
  10. """
  11. cal the similarity between two list of title
  12. :param target_title_list: target title_list
  13. :param base_title_list: base title_list
  14. :return: list of similarity
  15. """
  16. url = 'http://61.48.133.26:6060/nlp'
  17. url_backup = 'http://192.168.203.4:6060/nlp'
  18. body = {
  19. "data": {
  20. "text_list_a": target_title_list,
  21. "text_list_b": base_title_list
  22. },
  23. "function": "similarities_cross",
  24. "use_cache": False
  25. }
  26. try:
  27. response = requests.post(url, json=body, timeout=120)
  28. if response.status_code != 200:
  29. response = requests.post(url_backup, json=body, timeout=120)
  30. except RequestException as e:
  31. log(
  32. task="article_exit_with_title",
  33. function="similarity_between_title_list",
  34. status="fail",
  35. data={
  36. "e": str(e),
  37. "error_msg": traceback.format_exc()
  38. }
  39. )
  40. # use back up
  41. response = requests.post(url_backup, json=body, timeout=120)
  42. if response.status_code != 200:
  43. bot(
  44. title='NLP API 接口异常',
  45. detail={
  46. "status_code": response.status_code,
  47. "response_text": response.text[:200] # 截取部分内容避免过大
  48. },
  49. mention=False
  50. )
  51. return []
  52. try:
  53. response_json = response.json()
  54. score_array = response_json['score_list_list']
  55. except (JSONDecodeError, KeyError) as e:
  56. print("NLP响应数据异常")
  57. # bot(
  58. # title='NLP响应数据异常',
  59. # detail={
  60. # "error_type": type(e).__name__,
  61. # "raw_response": response.text[:200]
  62. # },
  63. # mention=False
  64. # )
  65. return []
  66. return score_array