# -*- coding: utf-8 -*- # @Time : 2023/7/10 # @Author : lkj # @description : 服务请求 import copy import json import grpc from proto.text2vector_pb2 import Text2VectorReq, Text2VectorResp import requests from pymongo import MongoClient from a2s.a2s_client import a2s_execute from a2s.tools import json_serialize, json_deserialize from config import daili def top_t(text): data = { "text": text, } result = a2s_execute(a2s_ip=daili, topic="topic_tract", timeout=6, bytes_data=json_serialize(data)) result=json_deserialize(result) if result.get('status',400) == 200: return result.get('result','') else: return '' def text_to_vector(data): """ 文本转向量 :param text: :return: """ result = [] try: # 调用服务端方法 resp = Text2VectorReq(text=data) response = a2s_execute(daili, "t2v2", timeout=30, bytes_data=resp.SerializeToString()) # 获取结果 req = Text2VectorResp() req.ParseFromString(response) result = list(req.vector) except Exception as e: print(e) return result def chat_glm(text): "百川" response = requests.post('http://123.207.51.36:7885/', json={"prompt":text,"identity":"剑鱼chat","top_p":0.8,"temperature":0.8,},timeout=150).json() result = response.get('response', '') glm_flg = 1 # print('glm',result) for i in ['属于不同类', '不属于','可能属于', '不属于同一类', '不是同一类', '是不同类', '属于不同', '不是', '不同的', '不一定', '一定不是']: if i in result: glm_flg = 0 break return glm_flg,result def ali2(text): """ 阿里大模型 :param text: :param classify: :return: """ 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() result = response.get('response', '') glm_flg = 1 # print('al',result) for i in ['属于不同类', '不属于','可能属于', '不属于同一类', '不是同一类', '是不同类', '属于不同', '不是', '不同的', '不一定', '一定不是']: if i in result: glm_flg = 0 break return glm_flg,result def chat_jzy(text): '书生' 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() result = response.get('response', '') glm_flg = 1 # print('zy',result) for i in ['属于不同类', '不属于','可能属于', '不属于同一类', '不是同一类', '是不同类', '属于不同', '不是', '不同的', '不一定', '一定不是']: if i in result: glm_flg = 0 break return glm_flg,result def three_classify(title_,detail): if '维修' in title_: title = title_.replace('维修','') response = requests.post('http://192.168.3.109:20623/search/', data={'title': title, 'detail': detail}).json() result = response.get('result', []) if result[0] == '货物': result = ['服务', '60%'] return result else: response = requests.post('http://192.168.3.109:20623/search/', data={'title': title_, 'detail': detail}).json() result = response.get('result', []) return result else: response = requests.post('http://192.168.3.109:20623/search/', data={'title': title_, 'detail': detail}).json() result = response.get('result', []) return result def seq_gpt(text,labels,task='分类'): """ #### 三大分类测试 text:'{text}\n属于下面哪一个分类' labels:'服务类,工程类,货物类' :param text: :param labels: :param task: 可选'分类'or '抽取' 默认分类 :return: """ response = requests.post('http://192.168.3.109:20016/cls',data={ 'text':f'{text}','task':f'{task}','labels':f'{labels}'}).json() print(response) result = response.get('result',{}).get('text','') return result def is_goods(title): response = requests.post('http://192.168.3.109:20622/',data={'title':title}).json() result = response.get('result', []) return result def ts_ent(title): response = requests.post('http://192.168.3.109:20631/',data={'text':title}).json() print(response) result = response.get('result', []) entity = result.get('entity',[]) entity_list = [] for e in entity: if e: for flag in ['basic','food','product', 'matter','medicine','activity']: if flag in e[1] and e[0] not in entity_list: entity_list.append(e[0]) res = ''.join(entity_list) if not res: res = title return result,res def process_model(text): # response = requests.get(f'http://192.168.3.109:8998/product_detail?prompt={text}').text old_text = copy.deepcopy(text) # response = eval(response) # print(response.text) text_list = [] # result = response.get('output', []) result = a2s_execute("192.168.3.240:9090","recognition_goods",timeout=30,bytes_data=json_serialize({"text":text})) result = eval(result).get('output',[]) for res in result: type_ = res.get('type','') span = res.get('span','') if '材质' in type_ or '款式' in type_ or '产品' in type_ or '对象' in type_ or '适用场景' in type_: if span not in text_list: text_list.append(span) # for flag in ['品牌','系列','型号','规格','颜色','其他','修饰','组织机构']: # if flag in type_: # if '材质' in type_ or '款式' in type_ or '产品' in type_: # continue # # text = text.replace(span, '') # break text = ''.join(text_list) if len(text)<=1: text = old_text return text def con(): mongo_client = MongoClient('192.168.3.71:29099') col_ = mongo_client['re4art']['better_goods'] return col_ if __name__ == '__main__': # text = '晨光 M&G Eplus盒 装黑色长尾夹 ABS92728 32mm 12个/盒 12盒/包 120盒/箱' # c = '长尾票夹' # print(1111,three_classify('恒源祥 H4YH10全棉长绒棉60支贡缎刺四件套1.5/1.8米床单被套200*230','')) # print(222,chat_glm('新华书店/百年初心成大道——党史学习教育案例选编/政治类书籍属于装订图书吗')) # print(333,chat_jzy(text,c)) # exit() tes = '润农生物饲料研发生产线技改项目' # print(three_classify(tes,'')) print(text_to_vector(tes)) exit() col = con() count = 0 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): title = row.get('title', '') ids = row['_id'] detail = row.get('detail', '') c = three_classify(title,detail) basicClass = '' rate = '0%' if c: basicClass = c[0] rate = c[1] # col.update_one({'_id': ids}, {'$set': {'basicClass': basicClass,'rate':rate}}) print(ids)