CoolFace
Apppublic

Rabbit482023/ChatImprovement

sourceHugging Faceupdated 3y agoView on Hugging Face
0likes
predict.py162 linesDownload Raw Back to root
1# 借鉴了 https://github.com/GaiZhenbiao/ChuanhuChatGPT 项目2 3import json4import gradio as gr5import logging6import traceback7import requests8import importlib9 10# config_private.py放自己的秘密如API和代理网址11# 读取时首先看是否存在私密的config_private配置文件(不受git管控),如果有,则覆盖原config文件12try: from config_private import proxies, API_URL, TIMEOUT_SECONDS, MAX_RETRY, LLM_MODEL13except: from config import proxies, API_URL, TIMEOUT_SECONDS, MAX_RETRY, LLM_MODEL14 15timeout_bot_msg = '[local] Request timeout, network error. please check proxy settings in config.py.'16 17def get_full_error(chunk, stream_response):18    while True:19        try:20            chunk += next(stream_response)21        except:22            break23    return chunk24 25def predict_no_ui(inputs, top_p, temperature, history=[]):26    headers, payload = generate_payload(inputs, top_p, temperature, history, system_prompt="", stream=False)27 28    retry = 029    while True:30        try:31            # make a POST request to the API endpoint, stream=False32            response = requests.post(API_URL, headers=headers, proxies=proxies,33                                    json=payload, stream=False, timeout=TIMEOUT_SECONDS*2); break34        except requests.exceptions.ReadTimeout as e:35            retry += 136            traceback.print_exc()37            if MAX_RETRY!=0: print(f'请求超时,正在重试 ({retry}/{MAX_RETRY}) ……')38            if retry > MAX_RETRY: raise TimeoutError39 40    try:41        result = json.loads(response.text)["choices"][0]["message"]["content"]42        return result43    except Exception as e:44        if "choices" not in response.text: print(response.text)45        raise ConnectionAbortedError("Json解析不合常规,可能是文本过长" + response.text)46 47 48def predict(api, inputs, top_p, temperature, chatbot=[], history=[], system_prompt='', 49            stream = True, additional_fn=None):50 51    if additional_fn is not None:52        import functional53        importlib.reload(functional)54        functional = functional.get_functionals()55        inputs = functional[additional_fn]["Prefix"] + inputs + functional[additional_fn]["Suffix"]56 57    if stream:58        raw_input = inputs59        logging.info(f'[raw_input] {raw_input}')60        chatbot.append((inputs, ""))61        yield chatbot, history, "等待响应"62 63    headers, payload = generate_payload(api, inputs, top_p, temperature, history, system_prompt, stream)64    history.append(inputs); history.append(" ")65 66    retry = 067    while True:68        try:69            # make a POST request to the API endpoint, stream=True70            response = requests.post(API_URL, headers=headers, proxies=proxies,71                                    json=payload, stream=True, timeout=TIMEOUT_SECONDS);break72        except:73            retry += 174            chatbot[-1] = ((chatbot[-1][0], timeout_bot_msg))75            retry_msg = f",正在重试 ({retry}/{MAX_RETRY}) ……" if MAX_RETRY > 0 else ""76            yield chatbot, history, "请求超时"+retry_msg77            if retry > MAX_RETRY: raise TimeoutError78 79    gpt_replying_buffer = ""80    81    is_head_of_the_stream = True82    if stream:83        stream_response =  response.iter_lines()84        while True:85            chunk = next(stream_response)86            # print(chunk.decode()[6:])87            if is_head_of_the_stream:88                # 数据流的第一帧不携带content89                is_head_of_the_stream = False; continue90            91            if chunk:92                try:93                    if len(json.loads(chunk.decode()[6:])['choices'][0]["delta"]) == 0:94                        # 判定为数据流的结束,gpt_replying_buffer也写完了95                        logging.info(f'[response] {gpt_replying_buffer}')96                        break97                    # 处理数据流的主体98                    chunkjson = json.loads(chunk.decode()[6:])99                    status_text = f"finish_reason: {chunkjson['choices'][0]['finish_reason']}"100                    # 如果这里抛出异常,一般是文本过长,详情见get_full_error的输出101                    gpt_replying_buffer = gpt_replying_buffer + json.loads(chunk.decode()[6:])['choices'][0]["delta"]["content"]102                    history[-1] = gpt_replying_buffer103                    chatbot[-1] = (history[-2], history[-1])104                    yield chatbot, history, status_text105 106                except Exception as e:107                    traceback.print_exc()108                    yield chatbot, history, "Json解析不合常规,很可能是文本过长"109                    chunk = get_full_error(chunk, stream_response)110                    error_msg = chunk.decode()111                    if "reduce the length" in error_msg:112                        chatbot[-1] = (history[-1], "[Local Message] Input (or history) is too long, please reduce input or clear history by refleshing this page.")113                        history = []114                    yield chatbot, history, "Json解析不合常规,很可能是文本过长" + error_msg115                    return116 117def generate_payload(api, inputs, top_p, temperature, history, system_prompt, stream):118    headers = {119        "Content-Type": "application/json",120        "Authorization": f"Bearer "+str(api)121    }122 123    conversation_cnt = len(history) // 2124 125    messages = [{"role": "system", "content": system_prompt}]126    if conversation_cnt:127        for index in range(0, 2*conversation_cnt, 2):128            what_i_have_asked = {}129            what_i_have_asked["role"] = "user"130            what_i_have_asked["content"] = history[index]131            what_gpt_answer = {}132            what_gpt_answer["role"] = "assistant"133            what_gpt_answer["content"] = history[index+1]134            if what_i_have_asked["content"] != "":135                if what_gpt_answer["content"] == "": continue136                if what_gpt_answer["content"] == timeout_bot_msg: continue137                messages.append(what_i_have_asked)138                messages.append(what_gpt_answer)139            else:140                messages[-1]['content'] = what_gpt_answer['content']141 142    what_i_ask_now = {}143    what_i_ask_now["role"] = "user"144    what_i_ask_now["content"] = inputs145    messages.append(what_i_ask_now)146 147    payload = {148        "model": LLM_MODEL,149        "messages": messages, 150        "temperature": temperature,  # 1.0,151        "top_p": top_p,  # 1.0,152        "n": 1,153        "stream": stream,154        "presence_penalty": 0,155        "frequency_penalty": 0,156    }157    158    print(f" {LLM_MODEL} : {conversation_cnt} : {inputs}")159    return headers,payload160 161 162