chatgpt大模型
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.

178 lines
14 KiB

  1. #coding:utf8
  2. import re
  3. from jsonpath_ng import parse
  4. import json
  5. import traceback
  6. import regex
  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 = regex.search(pattern, gptContent, flags=regex.DOTALL)
  30. # match = re.search(pattern, gptContent, re.DOTALL)
  31. if match:
  32. json_gpt = json.loads(match.group())
  33. for key in output.keys():
  34. if key in json_gpt.keys():
  35. json_result[key]=json_gpt[key]
  36. return json_result
  37. else:
  38. return None
  39. except:
  40. traceback.print_exc()
  41. return None
  42. def get_content(inputdata,logging):
  43. """
  44. :param inputdata:json数据
  45. :return: prompt及其他参数
  46. """
  47. res={}
  48. input=inputdata["input"]
  49. if "data" not in inputdata.keys():
  50. data=""
  51. else:
  52. data=inputdata["data"]
  53. prompts=input["prompt"]
  54. prompt_res=""
  55. if prompts:
  56. for prompt in prompts:
  57. if str(prompt["type"])=="1":
  58. prompt_res+=prompt["value"]
  59. elif str(prompt["type"])=="2":
  60. try:
  61. tmp=parse_data(data,prompt["value"])
  62. prompt_res +=tmp
  63. except:
  64. logging.info("动态字段获取数据失败。{}-{}".format(prompt, traceback.format_exc()))
  65. # logging.info("拼接后的问题:{}".format(prompt_res))
  66. res["prompt"]=prompt_res
  67. res["authorization"]=input["authorization"]
  68. res["model"]=input["model"]
  69. res["temperature"]=input["temperature"]
  70. res["authorization"]=input["authorization"]
  71. res["top_p"]=input["top_p"]
  72. res["n"]=input["n"]
  73. return res
  74. # def get_content(inputdata,logging):
  75. # """
  76. # 重新组装参数
  77. # :param inputdata:原json数据
  78. # :return: 组装的prompt及其他参数
  79. # """
  80. # res={}
  81. # admin=inputdata["metadata"]["admin"]
  82. # data=inputdata["data"]
  83. # prompt=admin["prompt"]
  84. # if_user=re.findall("{{(.*)}}",prompt)
  85. # if_data=re.findall("@@(.*)@@",prompt)
  86. # if if_user != []:
  87. # user_data=inputdata["metadata"]["user"]
  88. # if if_user[0] in user_data.keys():
  89. # tmp=user_data[if_user[0]]
  90. # prompt=re.sub("{{(.*)}}",tmp,prompt)
  91. # if if_data!=[] and if_data[0] in data.keys():
  92. # tmp1=data[if_data[0]]
  93. # prompt=re.sub("@@(.*)@@",tmp1,prompt)
  94. # res["prompt"]=prompt
  95. # res["authorization"]=admin["authorization"]
  96. # res["model"]=admin["model"]
  97. # res["temperature"]=admin["temperature"]
  98. # res["authorization"]=admin["authorization"]
  99. # res["top_p"]=admin["top_p"]
  100. # res["n"]=admin["n"]
  101. # return res
  102. if __name__=="__main__":
  103. inputdata={
  104. "output":{
  105. "id":"id",
  106. "content":"content"
  107. },
  108. "address":"http://172.18.1.181:9011/chatGpt/",
  109. "input":{
  110. "authorization":"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",
  111. "top_p":"1",
  112. "temperature":"1",
  113. "model":"gpt-3.5-turbo-16k",
  114. "prompt":[{"type":1,"value":"下面我给出一段文本,这段文本的内容是"},{"type":2,"value":"2_内容翻译:$['content']"},{"type":1,"value":"请总结上述内容"}],
  115. "n":"1"
  116. },
  117. "data":{
  118. "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\":\
  119. "2_内容翻译":"{\"content\":\"\\n\\n\\n\\no\\n\\n\\n\\n#中国#孟加拉国#登革热#援助#\\n……\\n(英文版)\\n中国向孟加拉国提供抗登革热援助\\n中国驻孟加拉国大使姚文周三在达卡郊区萨瓦尔的一家大医院捐赠中国援助的登革热工具包的仪式上宣布,中国将向孟加拉国提供抗登革热援助。\\n孟加拉卫生部的数据显示,今年到目前为止,在孟加拉国报告的228779例登革热病例中,共有1109人死于登革热。\\n新华社记者在达卡报道。(XHTV)\"}",
  120. "businessKey":"78278a5168e45304"
  121. },
  122. "created":1691004265000,
  123. "module":"ChatGPT",
  124. "start_tag":"false",
  125. "multi_branch":0,
  126. "last_edit":1698927821000,
  127. "next_app_id":[
  128. {
  129. "start_id":316,
  130. "edge_id":200,
  131. "end_id":317
  132. }
  133. ],
  134. "transfer_id":3,
  135. "version":1,
  136. "blueprint_id":12,
  137. "scenes_id":12,
  138. "scenario":{
  139. "dataloss":1,
  140. "autoCommitTriggerLast":1,
  141. "maxErrors":3,
  142. "autoCommit":1,
  143. "freshVariables":1
  144. },
  145. "wait_condition":[
  146. ],
  147. "scheduling":{
  148. "interval":-1,
  149. "type":"single"
  150. },
  151. "name":"正面引导",
  152. "businessKey":"78278a5168e45304",
  153. "id":316,
  154. "position":[
  155. 100,
  156. 200
  157. ],
  158. "describe":"正面引导"
  159. }
  160. a=get_content(inputdata,"")
  161. print(a)