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

#coding:utf8
import os, sys
import io
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf8')
cur_dir = os.path.dirname(os.path.abspath(__file__)) or os.getcwd()
par_dir = os.path.abspath(os.path.join(cur_dir, os.path.pardir))
sys.path.append(cur_dir)
sys.path.append(par_dir)
import json
from django.http import HttpResponse
from text_analysis.tools import to_kafka
from django.views.decorators.csrf import csrf_exempt
from log_util.set_logger import set_logger
logging=set_logger('logs/results.log')
import traceback
import queue
import requests
from text_analysis.tools.tool import get_content
import time
global task_queue
task_queue = queue.Queue()
@csrf_exempt
def chatGptNew(request):
if request.method == 'POST':
try:
# txt=request.body.encode("utf-8")
raw_data = json.loads(request.body)
task_queue.put(raw_data)
return HttpResponse(json.dumps({"code": 1, "msg": "请求正常!"}, ensure_ascii=False))
except:
logging.error(traceback.format_exc())
return HttpResponse(json.dumps({"code": 0, "msg": "请求json格式不正确!"}, ensure_ascii=False))
else:
return HttpResponse(json.dumps({"code": 0, "msg": "请求方式错误,改为post请求"}, ensure_ascii=False))
def chatgpt():
while True:
try:
if task_queue.qsize() >0:
try:
logging.info("取任务队列长度{}".format(task_queue.qsize()))
raw_data = task_queue.get()
# logging.info(raw_data)
data=get_content(raw_data,logging)
logging.info("问题:{}".format(data))
url = "https://api.openai.com/v1/chat/completions"
headers = {
"Content-Type": "application/json;charset=UTF-8",
"Authorization": "Bearer "+data["authorization"]
}
payload = json.dumps({
"model": data["model"],
"messages": [{"role": "user","content": data["prompt"]}],
"temperature":float(data["temperature"]),
"top_p":float(data["top_p"]),
"n":int(data["n"])
})
# print(payload)
response = requests.request("POST", url, headers=headers, data=payload)
# print(response)
d = json.loads(response.text)
result = d['choices'][0]['message']['content']
raw_data["result"] = {"successCode": "1", "errorLog": "", "results": result}
# print(raw_data)
logging.info(raw_data)
to_kafka.send_kafka(raw_data,logging)
except:
raw_data["result"] = {"successCode": "0", "errorLog": "", "results": ""}
if response and response.text:
raw_data["result"]["errorLog"] = response.text
else:
raw_data["result"]["errorLog"] = traceback.format_exc()
logging.info("解析失败{}-{}".format(raw_data,traceback.format_exc()))
to_kafka.send_kafka(raw_data,logging)
else:
# logging.info("暂无任务,进入休眠--")
time.sleep(10)
except:
raw_data["result"] = {"successCode": "0", "errorLog": "", "results": ""}
raw_data["result"]["errorLog"] = traceback.format_exc()
logging.info(traceback.format_exc())
to_kafka.send_kafka(raw_data, logging)