request_fun.py 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. # -*- coding: utf-8 -*-
  2. # @Time : 2023/7/10
  3. # @Author : lkj
  4. # @description : 服务请求
  5. import copy
  6. import json
  7. import grpc
  8. from proto.text2vector_pb2 import Text2VectorReq, Text2VectorResp
  9. import requests
  10. from pymongo import MongoClient
  11. from a2s.a2s_client import a2s_execute
  12. from a2s.tools import json_serialize, json_deserialize
  13. from config import daili
  14. def top_t(text):
  15. data = {
  16. "text": text,
  17. }
  18. result = a2s_execute(a2s_ip=daili, topic="topic_tract", timeout=6, bytes_data=json_serialize(data))
  19. result=json_deserialize(result)
  20. if result.get('status',400) == 200:
  21. return result.get('result','')
  22. else:
  23. return ''
  24. def text_to_vector(data):
  25. """
  26. 文本转向量
  27. :param text:
  28. :return:
  29. """
  30. result = []
  31. try:
  32. # 调用服务端方法
  33. resp = Text2VectorReq(text=data)
  34. response = a2s_execute(daili, "t2v2", timeout=30, bytes_data=resp.SerializeToString())
  35. # 获取结果
  36. req = Text2VectorResp()
  37. req.ParseFromString(response)
  38. result = list(req.vector)
  39. except Exception as e:
  40. print(e)
  41. return result
  42. def chat_glm(text):
  43. "百川"
  44. response = requests.post('http://123.207.51.36:7885/', json={"prompt":text,"identity":"剑鱼chat","top_p":0.8,"temperature":0.8,},timeout=150).json()
  45. result = response.get('response', '')
  46. glm_flg = 1
  47. # print('glm',result)
  48. for i in ['属于不同类', '不属于','可能属于', '不属于同一类', '不是同一类', '是不同类', '属于不同', '不是', '不同的', '不一定', '一定不是']:
  49. if i in result:
  50. glm_flg = 0
  51. break
  52. return glm_flg,result
  53. def ali2(text):
  54. """
  55. 阿里大模型
  56. :param text:
  57. :param classify:
  58. :return:
  59. """
  60. response = requests.post('http://123.207.51.36:7880', json={"prompt":text,"identity":"剑鱼chat","top_p":0.8,"temperature":0.8,'max_length':100},timeout=150 ).json()
  61. result = response.get('response', '')
  62. glm_flg = 1
  63. # print('al',result)
  64. for i in ['属于不同类', '不属于','可能属于', '不属于同一类', '不是同一类', '是不同类', '属于不同', '不是', '不同的', '不一定', '一定不是']:
  65. if i in result:
  66. glm_flg = 0
  67. break
  68. return glm_flg,result
  69. def chat_jzy(text):
  70. '书生'
  71. response = requests.post(f'http://119.91.64.110:7885/', json={"prompt":text,"identity":"剑鱼chat","top_p":0.8,"temperature":0.8,"max_length":100},timeout=300).json()
  72. result = response.get('response', '')
  73. glm_flg = 1
  74. # print('zy',result)
  75. for i in ['属于不同类', '不属于','可能属于', '不属于同一类', '不是同一类', '是不同类', '属于不同', '不是', '不同的', '不一定', '一定不是']:
  76. if i in result:
  77. glm_flg = 0
  78. break
  79. return glm_flg,result
  80. def three_classify(title_,detail):
  81. if '维修' in title_:
  82. title = title_.replace('维修','')
  83. response = requests.post('http://192.168.3.109:20623/search/', data={'title': title, 'detail': detail}).json()
  84. result = response.get('result', [])
  85. if result[0] == '货物':
  86. result = ['服务', '60%']
  87. return result
  88. else:
  89. response = requests.post('http://192.168.3.109:20623/search/',
  90. data={'title': title_, 'detail': detail}).json()
  91. result = response.get('result', [])
  92. return result
  93. else:
  94. response = requests.post('http://192.168.3.109:20623/search/', data={'title': title_, 'detail': detail}).json()
  95. result = response.get('result', [])
  96. return result
  97. def seq_gpt(text,labels,task='分类'):
  98. """
  99. #### 三大分类测试 text:'{text}\n属于下面哪一个分类' labels:'服务类,工程类,货物类'
  100. :param text:
  101. :param labels:
  102. :param task: 可选'分类'or '抽取' 默认分类
  103. :return:
  104. """
  105. response = requests.post('http://192.168.3.109:20016/cls',data={
  106. 'text':f'{text}','task':f'{task}','labels':f'{labels}'}).json()
  107. print(response)
  108. result = response.get('result',{}).get('text','')
  109. return result
  110. def is_goods(title):
  111. response = requests.post('http://192.168.3.109:20622/',data={'title':title}).json()
  112. result = response.get('result', [])
  113. return result
  114. def ts_ent(title):
  115. response = requests.post('http://192.168.3.109:20631/',data={'text':title}).json()
  116. print(response)
  117. result = response.get('result', [])
  118. entity = result.get('entity',[])
  119. entity_list = []
  120. for e in entity:
  121. if e:
  122. for flag in ['basic','food','product', 'matter','medicine','activity']:
  123. if flag in e[1] and e[0] not in entity_list:
  124. entity_list.append(e[0])
  125. res = ''.join(entity_list)
  126. if not res:
  127. res = title
  128. return result,res
  129. def process_model(text):
  130. # response = requests.get(f'http://192.168.3.109:8998/product_detail?prompt={text}').text
  131. old_text = copy.deepcopy(text)
  132. # response = eval(response)
  133. # print(response.text)
  134. text_list = []
  135. # result = response.get('output', [])
  136. result = a2s_execute("192.168.3.240:9090","recognition_goods",timeout=30,bytes_data=json_serialize({"text":text}))
  137. result = eval(result).get('output',[])
  138. for res in result:
  139. type_ = res.get('type','')
  140. span = res.get('span','')
  141. if '材质' in type_ or '款式' in type_ or '产品' in type_ or '对象' in type_ or '适用场景' in type_:
  142. if span not in text_list:
  143. text_list.append(span)
  144. # for flag in ['品牌','系列','型号','规格','颜色','其他','修饰','组织机构']:
  145. # if flag in type_:
  146. # if '材质' in type_ or '款式' in type_ or '产品' in type_:
  147. # continue
  148. #
  149. # text = text.replace(span, '')
  150. # break
  151. text = ''.join(text_list)
  152. if len(text)<=1:
  153. text = old_text
  154. return text
  155. def con():
  156. mongo_client = MongoClient('192.168.3.71:29099')
  157. col_ = mongo_client['re4art']['better_goods']
  158. return col_
  159. if __name__ == '__main__':
  160. # text = '晨光 M&G Eplus盒 装黑色长尾夹 ABS92728 32mm 12个/盒 12盒/包 120盒/箱'
  161. # c = '长尾票夹'
  162. # print(1111,three_classify('恒源祥 H4YH10全棉长绒棉60支贡缎刺四件套1.5/1.8米床单被套200*230',''))
  163. # print(222,chat_glm('新华书店/百年初心成大道——党史学习教育案例选编/政治类书籍属于装订图书吗'))
  164. # print(333,chat_jzy(text,c))
  165. # exit()
  166. tes = '润农生物饲料研发生产线技改项目'
  167. # print(three_classify(tes,''))
  168. print(text_to_vector(tes))
  169. exit()
  170. col = con()
  171. count = 0
  172. for row in col.find({"purchasing_score": {'$gte':0.7}},{'title':1,'detail':1,'s_subscopeclass':1,'projectname':1,'purchasinglist':1,'purchasing_score':1}).sort('_id', 1):
  173. title = row.get('title', '')
  174. ids = row['_id']
  175. detail = row.get('detail', '')
  176. c = three_classify(title,detail)
  177. basicClass = ''
  178. rate = '0%'
  179. if c:
  180. basicClass = c[0]
  181. rate = c[1]
  182. # col.update_one({'_id': ids}, {'$set': {'basicClass': basicClass,'rate':rate}})
  183. print(ids)