123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869 |
- """
- @author: luojunhui
- """
- import requests
- from requests.exceptions import RequestException, JSONDecodeError
- from applications.feishuBotApi import bot
- def similarity_between_title_list(target_title_list: list[str], base_title_list: list[str]) -> list[list[float]]:
- """
- cal the similarity between two list of title
- :param target_title_list: target title_list
- :param base_title_list: base title_list
- :return: list of similarity
- """
- url = 'http://61.48.133.26:6060/nlp'
- url_backup = 'http://61.48.133.26:6061/nlp'
- body = {
- "data": {
- "text_list_a": target_title_list,
- "text_list_b": base_title_list
- },
- "function": "similarities_cross",
- "use_cache": False
- }
- try:
- response = requests.post(url, json=body, timeout=120)
- if response.status_code != 200:
- response = requests.post(url_backup, json=body, timeout=120)
- except RequestException as e:
- bot(
- title='NLP API 网络异常',
- detail={
- "error_type": type(e).__name__,
- "error_msg": str(e)
- },
- mention=False
- )
- return []
- if response.status_code != 200:
- bot(
- title='NLP API 业务异常',
- detail={
- "status_code": response.status_code,
- "response_text": response.text[:200] # 截取部分内容避免过大
- },
- mention=False
- )
- return []
- try:
- response_json = response.json()
- score_array = response_json['score_list_list']
- except (JSONDecodeError, KeyError) as e:
- bot(
- title='NLP响应数据异常',
- detail={
- "error_type": type(e).__name__,
- "raw_response": response.text[:200]
- },
- mention=False
- )
- return []
- return score_array
|