千问开源大模型
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

172 lines
14 KiB

  1. #coding:utf8
  2. import re
  3. from jsonpath_ng import parse
  4. import json
  5. import traceback
  6. from log_util.set_logger import set_logger
  7. def parse_data(data,para):
  8. param_split = str(para).split(":")
  9. datasourcestr = data[param_split[0]]
  10. datasource = json.loads(datasourcestr)
  11. # 创建 JsonPath 表达式对象
  12. expr = parse(param_split[1])
  13. # 使用表达式来选择 JSON 元素
  14. match = [match.value for match in expr.find(datasource)]
  15. val = match[0]
  16. return val
  17. def parse_gptResult(output,gptContent):
  18. json_result = {}
  19. try:
  20. json_gpt=json.loads(str(gptContent).replace("```json","").replace("```",""))
  21. for key in output.keys():
  22. if key in json_gpt.keys():
  23. json_result[key]=json_gpt[key]
  24. return json_result
  25. except Exception as e:
  26. try:
  27. # 直接解析失败,使用正则表达式匹配外层的 {}
  28. pattern = r'\{(?:[^{}]*|(?R))*\}'
  29. match = re.search(pattern, gptContent, re.DOTALL)
  30. if match:
  31. json_gpt = json.loads(match.group())
  32. for key in output.keys():
  33. if key in json_gpt.keys():
  34. json_result[key]=json_gpt[key]
  35. return json_result
  36. else:
  37. return None
  38. except:
  39. traceback.print_exc()
  40. return None
  41. def get_content(inputdata,logging):
  42. """
  43. 重新组装参数
  44. :param inputdata:原json数据
  45. :return: 组装的prompt及其他参数
  46. """
  47. res={}
  48. input=inputdata["input"]
  49. data=inputdata["data"]
  50. prompts=input["prompt"]
  51. prompt_res=""
  52. if prompts:
  53. for prompt in prompts:
  54. if str(prompt["type"])=="1":
  55. prompt_res+=prompt["value"]
  56. elif str(prompt["type"])=="2":
  57. try:
  58. tmp=parse_data(data,prompt["value"])
  59. prompt_res +=tmp
  60. except:
  61. logging.info("动态字段获取数据失败。{}-{}".format(prompt, traceback.format_exc()))
  62. # logging.info("拼接后的问题:{}".format(prompt_res))
  63. res["prompt"]=prompt_res
  64. # res["authorization"]=input["authorization"]
  65. # res["model"]=input["model"]
  66. res["temperature"]=input["temperature"]
  67. res["top_p"]=input["top_p"]
  68. res["n"]=input["n"]
  69. return res
  70. # def get_content(inputdata,logging):
  71. # """
  72. # 重新组装参数
  73. # :param inputdata:原json数据
  74. # :return: 组装的prompt及其他参数
  75. # """
  76. # res={}
  77. # admin=inputdata["metadata"]["admin"]
  78. # data=inputdata["data"]
  79. # prompt=admin["prompt"]
  80. # if_user=re.findall("{{(.*)}}",prompt)
  81. # if_data=re.findall("@@(.*)@@",prompt)
  82. # if if_user != []:
  83. # user_data=inputdata["metadata"]["user"]
  84. # if if_user[0] in user_data.keys():
  85. # tmp=user_data[if_user[0]]
  86. # prompt=re.sub("{{(.*)}}",tmp,prompt)
  87. # if if_data!=[] and if_data[0] in data.keys():
  88. # tmp1=data[if_data[0]]
  89. # prompt=re.sub("@@(.*)@@",tmp1,prompt)
  90. # res["prompt"]=prompt
  91. # res["authorization"]=admin["authorization"]
  92. # res["model"]=admin["model"]
  93. # res["temperature"]=admin["temperature"]
  94. # res["authorization"]=admin["authorization"]
  95. # res["top_p"]=admin["top_p"]
  96. # res["n"]=admin["n"]
  97. # return res
  98. if __name__=="__main__":
  99. inputdata={
  100. "output":{
  101. "id":"id",
  102. "content":"content"
  103. },
  104. "address":"http://172.18.1.181:9011/chatGpt/",
  105. "input":{
  106. "authorization":"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",
  107. "top_p":"1",
  108. "temperature":"1",
  109. "model":"gpt-3.5-turbo-16k",
  110. "prompt":[{"type":1,"value":"下面我给出一段文本,这段文本的内容是"},{"type":2,"value":"2_内容翻译:$['content']"},{"type":1,"value":"请总结上述内容"}],
  111. "n":"1"
  112. },
  113. "data":{
  114. "1_Youtube采集":"{\"isDownload\":\"true\",\"imageCount\":0,\"groupRules\":[],\"commentUrl\":\"\",\"channel\":\"网络视频\",\"readCount\":55,\"resolution\":\"best\",\"srcimagePath\":[],\"fileCount\":0,\"subjectId\":304864,\"forwardQuoteCount\":-1,\"province\":\"\",\"crawlDataFlagType\":\"0\",\"price\":0,\"quoteCount\":-1,\"translateContentLength\":\"\",\"forwardUserType\":0,\"brand\":\"\",\"createTimeStr\":\"2023-11-02 14:30:24\",\"ocrLength\":0,\"hasOCR\":0,\"authornickname\":\"\",\"contentSimHash\":\"a870a5b58416c163e2618400adf2ea0f\",\"dns\":\"https://www.youtube.com/\",\"crawlDay\":1698854400000,\"asrText\":\"\",\"nomorprice\":0,\"mentionAccountUrl\":[],\"titleSimHash\":\"fc47b579bbee866173216c05bf3a8dcd\",\"videoPath\":[\"/group16/default/20231102/14/25/6/sbbJSkJdgF4.mp4\"],\"brandId\":\"\",\"commentId\":\"\",\"impression\":\"\",\"originalPhrase\":\"[\\\"8473704592933378\\\",\\\"video\\\"]\",\"userTypeContent\":\"\",\"forwardContent\":\"\",\"hashTag\":[],\"primary\":1,\"poorrate\":-1,\"extension\":\"mp4\",\"translateContent\":\"\",\"imagePathSize\":[],\"mentionTopic\":[\"china\",\"bangladesh\",\"denguefever\",\"aid\",\"တရုတ်နိုင်ငံ\",\"ဘင်္ဂလားဒေ့ရှ်နိုင်ငံ\",\"သွေးလွန်တုပ်ကွေးရောဂါ\",\"အကူအညီ\"],\"finalPhrase\":\"[\\\"8473704592933378\\\",\\\"video\\\"]\",\"city\":\"\",\"availability\":1,\"mentionAccount\":[],\"dataCount\":0,\"forwardUserUrl\":\"\",\"videoCount\":1,\"pageType\":\"newscontent\",\"dataId\":\"ba42f41f2dabcf026fef45e14100452c\",\"fourListBrand\":\"\",\"titleLength\":96,\"videoUrl\":\"\",\"ocrText\":[],\"mentionTopicUrl\":[\"https://www.youtube.com/hashtag/china\",\"https://www.youtube.com/hashtag/bangladesh\",\"https://www.youtube.com/hashtag/denguefever\",\"https://www.youtube.com/hashtag/aid\",\"https://www.youtube.com/hashtag/%E1%80%90%E1%80%9B%E1%80%AF%E1%80%90%E1%80%BA%E1%80%94%E1%80%AD%E1%80%AF%E1%80%84%E1%80%BA%E1%80%84%E1%80%B6\",\"https://www.youtube.com/hashtag/%E1%80%98%E1%80%84%E1%80%BA%E1%80%B9%E1%80%82%E1%80%9C%E1%80%AC%E1%80%B8%E1%80%92%E1%80%B1%E1%80%B7%E1%80%9B%E1%80%BE%E1%80%BA%E1%80%94%E1%80%AD%E1%80%AF%E1%80%84%E1%80%BA%E1%80%84%E1%80%B6\",\"https://www.youtube.com/hashtag/%E1%80%9E%E1%80%BD%E1%80%B1%E1%80%B8%E1%80%9C%E1%80%BD%E1%80%94%E1%80%BA%E1%80%90%E1%80%AF%E1%80%95%E1%80%BA%E1%80%80%E1%80%BD%E1%80%B1%E1%80%B8%E1%80%9B%E1%80%B1%E1%80%AC%E1%80%82%E1%80%AB\",\"https://www.youtube.com/hashtag/%E1%80%A1%E1%80%80%E1%80%B0%E1%80%A1%E1%80%8A%E1%80%AE\"],\"forwardAuthor\":\"\",\"sysAbstract\":\"\",\"forwardUrl\":\"\",\"createDate\":\"2023-11-02T14:22:23.895+08:00\",\"cate\":\"\",\"forwardPostSource\":\"\",\"expression\":[],\"docType\":\"video\",\"sex\":\"\",\"threeListBrand\":\"\",\"collectCount\":-1,\"crawlDate\":\"2023-11-02T14:22:10.000+08:00\",\"channelNum\":\"4\",\"avatar\":\"https://yt3.ggpht.com/ytc/APkrFKYE3XbulRMrXptLUgTWBKoe5P3SScIwGiny-yVMdA=s48-c-k-c0x00ffffff-no-rj\",\"promotionInfo\":\"\",\"authorId\":\"UCY0w4lNDjC-tQtk8CtSB6rQ\",\"isVip\":0,\"url\":\"https://www.youtube.com/watch?v=sbbJSkJdgF4\",\"skuProperties\":\"\",\"places\":[],\"hlKeywords\":[],\"createTime\":1698906624167,\"contentLength\":1393,\"thumbnails\":\"https://i.ytimg.com/vi/sbbJSkJdgF4/hq720_2.jpg?sqp=-oaymwE2COgCEMoBSFXyq4qpAygIARUAAIhCGABwAcABBvABAfgBtgiAAoAPigIMCAAQARhlIFsoXDAP&rs=AOn4CLC6ks5eK_6RGVwiNG1XNGmE5i28vQ\",\"downCnt\":0,\"country\":\"\",\"forwardUserId\":\"\",\"hasASR\":0,\"imagePath\":[],\"pubTime\":1697155200000,\"sign\":\"\",\"asrLength\":0,\"fansCount\":\"42300\",\"language\":\"\",\"otherSourceJson\":\"\",\"source\":\"YouTube\",\"smallImgs\":\"\",\"forwardAvatar\":\"\",\"goodrate\":-1,\"pageCommentCount\":0,\"crawlDataFlag\":\"keyword:Bangladesh is anti-China\",\"viewCnt\":-1,\"members\":[],\"postCount\":\"\",\"videoTime\":\"0:11\",\"tag\":\"\",\"filePathSize\":[],\"enSource\":\"youtube\",\"pictureList\":\"\",\"userUrl\":\"https://www.youtube.com/channel/UCY0w4lNDjC-tQtk8CtSB6rQ\",\"area\":\"\",\"fiveListBrand\":\"\",\"srcvideoPath\":[],\"firstListBrand\":\
  115. "2_内容翻译":"{\"content\":\"\\n\\n\\n\\no\\n\\n\\n\\n#中国#孟加拉国#登革热#援助#\\n……\\n(英文版)\\n中国向孟加拉国提供抗登革热援助\\n中国驻孟加拉国大使姚文周三在达卡郊区萨瓦尔的一家大医院捐赠中国援助的登革热工具包的仪式上宣布,中国将向孟加拉国提供抗登革热援助。\\n孟加拉卫生部的数据显示,今年到目前为止,在孟加拉国报告的228779例登革热病例中,共有1109人死于登革热。\\n新华社记者在达卡报道。(XHTV)\"}",
  116. "businessKey":"78278a5168e45304"
  117. },
  118. "created":1691004265000,
  119. "module":"ChatGPT",
  120. "start_tag":"false",
  121. "multi_branch":0,
  122. "last_edit":1698927821000,
  123. "next_app_id":[
  124. {
  125. "start_id":316,
  126. "edge_id":200,
  127. "end_id":317
  128. }
  129. ],
  130. "transfer_id":3,
  131. "version":1,
  132. "blueprint_id":12,
  133. "scenes_id":12,
  134. "scenario":{
  135. "dataloss":1,
  136. "autoCommitTriggerLast":1,
  137. "maxErrors":3,
  138. "autoCommit":1,
  139. "freshVariables":1
  140. },
  141. "wait_condition":[
  142. ],
  143. "scheduling":{
  144. "interval":-1,
  145. "type":"single"
  146. },
  147. "name":"正面引导",
  148. "businessKey":"78278a5168e45304",
  149. "id":316,
  150. "position":[
  151. 100,
  152. 200
  153. ],
  154. "describe":"正面引导"
  155. }
  156. a=get_content(inputdata,"")
  157. print(a)