#coding:utf8 import re from jsonpath_ng import parse import json import traceback def get_content(inputdata,logging): """ 重新组装参数 :param inputdata:原json数据 :return: 组装的prompt及其他参数 """ res={} admin=inputdata["metadata"]["admin"] data=inputdata["data"] prompt=admin["prompt"] if_user=re.findall("{{(.*)}}",prompt) if_data=re.findall("@@(.*?)@@",prompt) if if_user != []: user_data=inputdata["metadata"]["user"] if if_user[0] in user_data.keys(): tmp=user_data[if_user[0]] prompt=re.sub("{{(.*)}}",tmp,prompt) if if_data!=[] : for rule in if_data: try: if ':' in rule: s=rule.split(':') rule1=s[0] rule2=s[1] if rule1 in data.keys(): tmp1 = data[rule1] #按照给定的规则解析字段 json_obj = json.loads(tmp1) jsonpath_expr = parse(rule2) result = str([match.value for match in jsonpath_expr.find(json_obj)][0]) rep="@@{}@@".format(rule) #增加转义字符 rep_escaped = re.escape(rep) prompt=re.sub(rep_escaped,result,prompt) else: if rule in data.keys(): tmp1=data[rule] rep = "@@{}@@".format(rule) prompt=re.sub(rep,tmp1,prompt) except: rep = "@@{}@@".format(rule) prompt = prompt.replace(rep,'') logging.info("动态字段获取数据失败。{}-{}".format(rule, traceback.format_exc())) logging.info("拼接后的问题:{}".format(prompt)) res["prompt"]=prompt res["authorization"]=admin["authorization"] res["model"]=admin["model"] res["temperature"]=admin["temperature"] res["authorization"]=admin["authorization"] res["top_p"]=admin["top_p"] res["n"]=admin["n"] return res # def get_content(inputdata,logging): # """ # 重新组装参数 # :param inputdata:原json数据 # :return: 组装的prompt及其他参数 # """ # res={} # admin=inputdata["metadata"]["admin"] # data=inputdata["data"] # prompt=admin["prompt"] # if_user=re.findall("{{(.*)}}",prompt) # if_data=re.findall("@@(.*)@@",prompt) # if if_user != []: # user_data=inputdata["metadata"]["user"] # if if_user[0] in user_data.keys(): # tmp=user_data[if_user[0]] # prompt=re.sub("{{(.*)}}",tmp,prompt) # if if_data!=[] and if_data[0] in data.keys(): # tmp1=data[if_data[0]] # prompt=re.sub("@@(.*)@@",tmp1,prompt) # res["prompt"]=prompt # res["authorization"]=admin["authorization"] # res["model"]=admin["model"] # res["temperature"]=admin["temperature"] # res["authorization"]=admin["authorization"] # res["top_p"]=admin["top_p"] # res["n"]=admin["n"] # return res if __name__=="__main__": prompt = "用@@11_任务拆分:$.lang@@,生成@@11_任务拆分:$.quantity@@条@@11_任务拆分:$.age@@的@@11_任务拆分:$.sex@@发布@@11_任务拆分:$.emotion@@的@@11_任务拆分:$.subject@@的@@11_任务拆分:$.content_type@@。以JSON数组泛型是String类型的格式进行输出,结构外层需要用“resultList”进行接收,不用多余的文字。" if_data = re.findall("@@(.*?)@@", prompt) print(if_data) # inputdata={ # "metadata":{ # "output":{ # "output_type":"table", # "label_col":[ # "相似内容抽取" # ] # }, # "input":{ # "input_type":"text", # "label":[ # "3_相似内容过滤器" # ] # }, # "address":"http://172.18.1.181:9011/chatGpt/", # "admin":{ # "authorization":"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", # "top_p":"1", # "user_input":[ # { # "keyname":"tag", # "keydesc":"" # } # ], # "temperature":"1", # "model":"gpt-3.5-turbo-16k", # "prompt":"以JSON数组泛型是String类型的格式进行输出,不用多余的文字。参考”@@11_任务拆分:$.quantity@@不仅仅是一种工具,更是一种改变世界的力量“生成@@11_任务拆分:$.quantity@@条@@11_任务拆分:$.lang@@的@@11_任务拆分:$.content_type@@", # "n":"1" # }, # "index":3, # "user":{ # "tag":"" # } # }, # "data":{ # "10_任务提取":"[{\"age\":\"18~24岁\",\"collection_element\":\"0,1\",\"collection_quantity\":1438,\"collection_task\":\"https://twitter.com/MFA_China\",\"collection_type\":0,\"content_type\":\"发帖内容\",\"create_user_id\":\"652468062228768915\",\"del\":0,\"emotion\":\"积极/乐观\",\"id\":178,\"lang\":\"英语\",\"model_status\":1,\"model_type\":1,\"name\":\"TW用户-发言办公室01-04~12-31\",\"quantity\":10,\"sex\":\"男性\",\"site_id\":181,\"status\":0,\"subject\":\"社会问题和时事主题\",\"tenant_id\":237,\"type\":1},{\"age\":\"18~24岁\",\"collection_element\":\"0,1\",\"collection_quantity\":444378,\"collection_task\":\"hongkong\",\"collection_type\":1,\"content_type\":\"发帖内容\",\"create_user_id\":\"652468062228768915\",\"del\":0,\"emotion\":\"积极/乐观\",\"id\":179,\"lang\":\"英语\",\"model_status\":1,\"model_type\":1,\"name\":\"TW关键词-hongkong01-04~12-31\",\"quantity\":10,\"sex\":\"女性\",\"site_id\":181,\"status\":0,\"subject\":\"旅行和探险主题\",\"tenant_id\":237,\"type\":1},{\"age\":\"18~24岁\",\"collection_element\":\"0,1\",\"collection_quantity\":256,\"collection_task\":\"https://www.facebook.com/tsaiingwen\",\"collection_type\":0,\"content_type\":\"发帖内容\",\"create_user_id\":\"652468062228768915\",\"del\":0,\"emotion\":\"积极/乐观\",\"id\":180,\"lang\":\"英语\",\"model_status\":1,\"model_type\":2,\"name\":\"FB用户-蔡英文-01-04~12-31\",\"quantity\":10,\"sex\":\"男性\",\"site_id\":182,\"status\":0,\"subject\":\"科技和创新主题\",\"tenant_id\":237,\"type\":1},{\"age\":\"18~24岁\",\"collection_element\":\"0,1\",\"collection_quantity\":1253,\"collection_task\":\"台湾新闻\",\"collection_type\":1,\"content_type\":\"发帖内容\",\"create_user_id\":\"652468062228768915\",\"del\":0,\"emotion\":\"积极/乐观\",\"id\":183,\"lang\":\"英语\",\"model_status\":1,\"model_type\":2,\"name\":\"FB关键词-台湾新闻-0110~12-13\",\"quantity\":10,\"sex\":\"女性\",\"site_id\":182,\"status\":0,\"subject\":\"健康和生活方式主题\",\"tenant_id\":237,\"type\":1}]", # "3_相似内容过滤器":"{\"age\":\"18~24岁\",\"collection_element\":\"0,1\",\"collection_quantity\":1253,\"collection_task\":\"台湾新闻\",\"collection_type\":1,\"content_type\":\"发帖内容\",\"create_user_id\":\"652468062228768915\",\"del\":0,\"emotion\":\"积极/乐观\",\"id\":183,\"lang\":\"英语\",\"model_status\":1,\"model_type\":2,\"name\":\"FB关键词-台湾新闻-0110~12-13\",\"quantity\":10,\"sex\":\"女性\",\"site_id\":182,\"size\":21,\"status\":0,\"subject\":\"健康和生活方式主题\",\"tenant_id\":237,\"type\":1}", # "11_任务拆分":"{\"tenant_id\":237,\"create_user_id\":\"652468062228768915\",\"collection_quantity\":1253,\"quantity\":10,\"subject\":\"健康和生活方式主题\",\"sex\":\"女性\",\"model_type\":2,\"del\":0,\"type\":1,\"collection_element\":\"0,1\",\"collection_type\":1,\"model_status\":1,\"collection_task\":\"台湾新闻\",\"emotion\":\"积极/乐观\",\"content_type\":\"发帖内容\",\"size\":21,\"name\":\"FB关键词-台湾新闻-0110~12-13\",\"site_id\":182,\"id\":183,\"lang\":\"英语\",\"age\":\"18~24岁\",\"status\":0}", # "1_mysql数据查询":"{\"resultList\": [{\"id\": 178, \"tenant_id\": 237, \"name\": \"TW用户-发言办公室01-04~12-31\", \"site_id\": 181, \"collection_type\": 0, \"collection_element\": \"0,1\", \"collection_task\": \"https://twitter.com/MFA_China\", \"status\": 0, \"collection_quantity\": 1438, \"create_user\": null, \"create_user_id\": \"652468062228768915\", \"update_user\": null, \"update_user_id\": null, \"del\": 0, \"type\": 1, \"model_type\": 1, \"quantity\": 10, \"content_type\": \"发帖内容\", \"model_status\": 1, \"lang\": \"英语\", \"age\": \"18~24岁\", \"sex\": \"男性\", \"emotion\": \"积极/乐观\", \"subject\": \"社会问题和时事主题\", \"similar_content\": null}, {\"id\": 179, \"tenant_id\": 237, \"name\": \"TW关键词-hongkong01-04~12-31\", \"site_id\": 181, \"collection_type\": 1, \"collection_element\": \"0,1\", \"collection_task\": \"hongkong\", \"status\": 0, \"collection_quantity\": 444378, \"create_user\": null, \"create_user_id\": \"652468062228768915\", \"update_user\": null, \"update_user_id\": null, \"del\": 0, \"type\": 1, \"model_type\": 1, \"quantity\": 10, \"content_type\": \"发帖内容\", \"model_status\": 1, \"lang\": \"英语\", \"age\": \"18~24岁\", \"sex\": \"女性\", \"emotion\": \"积极/乐观\", \"subject\": \"旅行和探险主题\", \"similar_content\": null}, {\"id\": 180, \"tenant_id\": 237, \"name\": \"FB用户-蔡英文-01-04~12-31\", \"site_id\": 182, \"collection_type\": 0, \"collection_element\": \"0,1\", \"collection_task\": \"https://www.facebook.com/tsaiingwen\", \"status\": 0, \"collection_quantity\": 256, \"create_user\": null, \"create_user_id\": \"652468062228768915\", \"update_user\": null, \"update_user_id\": null, \"del\": 0, \"type\": 1, \"model_type\": 2, \"quantity\": 10, \"content_type\": \"发帖内容\", \"model_status\": 1, \"lang\": \"英语\", \"age\": \"18~24岁\", \"sex\": \"男性\", \"emotion\": \"积极/乐观\", \"subject\": \"科技和创新主题\", \"similar_content\": null}, {\"id\": 183, \"tenant_id\": 237, \"name\": \"FB关键词-台湾新闻-0110~12-13\", \"site_id\": 182, \"collection_type\": 1, \"collection_element\": \"0,1\", \"collection_task\": \"台湾新闻\", \"status\": 0, \"collection_quantity\": 1253, \"create_user\": null, \"create_user_id\": \"652468062228768915\", \"update_user\": null, \"update_user_id\": null, \"del\": 0, \"type\": 1, \"model_type\": 2, \"quantity\": 10, \"content_type\": \"发帖内容\", \"model_status\": 1, \"lang\": \"英语\", \"age\": \"18~24岁\", \"sex\": \"女性\", \"emotion\": \"积极/乐观\", \"subject\": \"健康和生活方式主题\", \"similar_content\": null}]}" # }, # "created":1691004265000, # "module":"ChatGPT", # "start_tag":"false", # "multi_branch":0, # "last_edit":1693932236000, # "next_app_id":[ # { # "start_id":188, # "edge_id":92, # "end_id":190 # } # ], # "transfer_id":5, # "version":1, # "blueprint_id":6, # "scenes_id":7, # "scenario":{ # "dataloss":1, # "autoCommitTriggerLast":1, # "maxErrors":3, # "autoCommit":1, # "freshVariables":1 # }, # "wait_condition":[ # # ], # "scheduling":{ # "interval":-1, # "type":"single" # }, # "name":"相似内容抽取", # "id":188, # "position":[ # 100, # 200 # ], # "describe":"相似内容抽取" # } # a=get_content(inputdata,"") # print(a)