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.

87 lines
3.6 KiB

  1. #coding:utf8
  2. import os, sys
  3. import io
  4. sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf8')
  5. cur_dir = os.path.dirname(os.path.abspath(__file__)) or os.getcwd()
  6. par_dir = os.path.abspath(os.path.join(cur_dir, os.path.pardir))
  7. sys.path.append(cur_dir)
  8. sys.path.append(par_dir)
  9. import json
  10. from django.http import HttpResponse
  11. from text_analysis.tools import to_kafka
  12. from django.views.decorators.csrf import csrf_exempt
  13. from log_util.set_logger import set_logger
  14. logging=set_logger('logs/results.log')
  15. import traceback
  16. import queue
  17. import requests
  18. from text_analysis.tools.tool import get_content
  19. import time
  20. global task_queue
  21. task_queue = queue.Queue()
  22. @csrf_exempt
  23. def chatGptNew(request):
  24. if request.method == 'POST':
  25. try:
  26. # txt=request.body.encode("utf-8")
  27. raw_data = json.loads(request.body)
  28. task_queue.put(raw_data)
  29. return HttpResponse(json.dumps({"code": 1, "msg": "请求正常!"}, ensure_ascii=False))
  30. except:
  31. logging.error(traceback.format_exc())
  32. return HttpResponse(json.dumps({"code": 0, "msg": "请求json格式不正确!"}, ensure_ascii=False))
  33. else:
  34. return HttpResponse(json.dumps({"code": 0, "msg": "请求方式错误,改为post请求"}, ensure_ascii=False))
  35. def chatgpt():
  36. while True:
  37. try:
  38. if task_queue.qsize() >0:
  39. try:
  40. logging.info("取任务队列长度{}".format(task_queue.qsize()))
  41. raw_data = task_queue.get()
  42. # logging.info(raw_data)
  43. data=get_content(raw_data,logging)
  44. logging.info("问题:{}".format(data))
  45. url = "https://api.openai.com/v1/chat/completions"
  46. headers = {
  47. "Content-Type": "application/json;charset=UTF-8",
  48. "Authorization": "Bearer "+data["authorization"]
  49. }
  50. payload = json.dumps({
  51. "model": data["model"],
  52. "messages": [{"role": "user","content": data["prompt"]}],
  53. "temperature":float(data["temperature"]),
  54. "top_p":float(data["top_p"]),
  55. "n":int(data["n"])
  56. })
  57. # print(payload)
  58. response = requests.request("POST", url, headers=headers, data=payload)
  59. # print(response)
  60. d = json.loads(response.text)
  61. result = d['choices'][0]['message']['content']
  62. raw_data["result"] = {"successCode": "1", "errorLog": "", "results": result}
  63. # print(raw_data)
  64. logging.info(raw_data)
  65. to_kafka.send_kafka(raw_data,logging)
  66. except:
  67. raw_data["result"] = {"successCode": "0", "errorLog": "", "results": ""}
  68. if response and response.text:
  69. raw_data["result"]["errorLog"] = response.text
  70. else:
  71. raw_data["result"]["errorLog"] = traceback.format_exc()
  72. logging.info("解析失败{}-{}".format(raw_data,traceback.format_exc()))
  73. to_kafka.send_kafka(raw_data,logging)
  74. else:
  75. # logging.info("暂无任务,进入休眠--")
  76. time.sleep(10)
  77. except:
  78. raw_data["result"] = {"successCode": "0", "errorLog": "", "results": ""}
  79. raw_data["result"]["errorLog"] = traceback.format_exc()
  80. logging.info(traceback.format_exc())
  81. to_kafka.send_kafka(raw_data, logging)