CoolFace
Apppublic

eveyuyi/ChatGPT_Prompts

sourceHugging Facegpl-3.0updated 3y agoView on Hugging Face
0likes
chat_func.py474 linesDownload Raw Back to modules
1# -*- coding:utf-8 -*-2from __future__ import annotations3from typing import TYPE_CHECKING, List4 5import logging6import json7import os8import requests9import urllib310 11from tqdm import tqdm12import colorama13from duckduckgo_search import ddg14import asyncio15import aiohttp16 17from modules.presets import *18from modules.llama_func import *19from modules.utils import *20import modules.shared as shared21 22# logging.basicConfig(level=logging.INFO, format="%(asctime)s [%(levelname)s] [%(filename)s:%(lineno)d] %(message)s")23 24if TYPE_CHECKING:25    from typing import TypedDict26 27    class DataframeData(TypedDict):28        headers: List[str]29        data: List[List[str | int | bool]]30 31 32initial_prompt = "You are a helpful assistant."33HISTORY_DIR = "history"34TEMPLATES_DIR = "templates"35 36def get_response(37    openai_api_key, system_prompt, history, temperature, top_p, stream, selected_model38):39    headers = {40        "Content-Type": "application/json",41        "Authorization": f"Bearer {openai_api_key}",42    }43 44    history = [construct_system(system_prompt), *history]45 46    payload = {47        "model": selected_model,48        "messages": history,  # [{"role": "user", "content": f"{inputs}"}],49        "temperature": temperature,  # 1.0,50        "top_p": top_p,  # 1.0,51        "n": 1,52        "stream": stream,53        "presence_penalty": 0,54        "frequency_penalty": 0,55    }56    if stream:57        timeout = timeout_streaming58    else:59        timeout = timeout_all60 61    # 获取环境变量中的代理设置62    http_proxy = os.environ.get("HTTP_PROXY") or os.environ.get("http_proxy")63    https_proxy = os.environ.get("HTTPS_PROXY") or os.environ.get("https_proxy")64 65    # 如果存在代理设置,使用它们66    proxies = {}67    if http_proxy:68        logging.info(f"使用 HTTP 代理: {http_proxy}")69        proxies["http"] = http_proxy70    if https_proxy:71        logging.info(f"使用 HTTPS 代理: {https_proxy}")72        proxies["https"] = https_proxy73 74    # 如果有自定义的api-url,使用自定义url发送请求,否则使用默认设置发送请求75    if shared.state.api_url != API_URL:76        logging.info(f"使用自定义API URL: {shared.state.api_url}")77    if proxies:78        response = requests.post(79            shared.state.api_url,80            headers=headers,81            json=payload,82            stream=True,83            timeout=timeout,84            proxies=proxies,85        )86    else:87        response = requests.post(88            shared.state.api_url,89            headers=headers,90            json=payload,91            stream=True,92            timeout=timeout,93        )94    return response95 96 97def stream_predict(98    openai_api_key,99    system_prompt,100    history,101    inputs,102    chatbot,103    all_token_counts,104    top_p,105    temperature,106    selected_model,107    fake_input=None,108    display_append=""109):110    def get_return_value():111        return chatbot, history, status_text, all_token_counts112 113    logging.info("实时回答模式")114    partial_words = ""115    counter = 0116    status_text = "开始实时传输回答……"117    history.append(construct_user(inputs))118    history.append(construct_assistant(""))119    if fake_input:120        chatbot.append((fake_input, ""))121    else:122        chatbot.append((inputs, ""))123    user_token_count = 0124    if len(all_token_counts) == 0:125        system_prompt_token_count = count_token(construct_system(system_prompt))126        user_token_count = (127            count_token(construct_user(inputs)) + system_prompt_token_count128        )129    else:130        user_token_count = count_token(construct_user(inputs))131    all_token_counts.append(user_token_count)132    logging.info(f"输入token计数: {user_token_count}")133    yield get_return_value()134    try:135        response = get_response(136            openai_api_key,137            system_prompt,138            history,139            temperature,140            top_p,141            True,142            selected_model,143        )144    except requests.exceptions.ConnectTimeout:145        status_text = (146            standard_error_msg + connection_timeout_prompt + error_retrieve_prompt147        )148        yield get_return_value()149        return150    except requests.exceptions.ReadTimeout:151        status_text = standard_error_msg + read_timeout_prompt + error_retrieve_prompt152        yield get_return_value()153        return154 155    yield get_return_value()156    error_json_str = ""157 158    for chunk in tqdm(response.iter_lines()):159        if counter == 0:160            counter += 1161            continue162        counter += 1163        # check whether each line is non-empty164        if chunk:165            chunk = chunk.decode()166            chunklength = len(chunk)167            try:168                chunk = json.loads(chunk[6:])169            except json.JSONDecodeError:170                logging.info(chunk)171                error_json_str += chunk172                status_text = f"JSON解析错误。请重置对话。收到的内容: {error_json_str}"173                yield get_return_value()174                continue175            # decode each line as response data is in bytes176            if chunklength > 6 and "delta" in chunk["choices"][0]:177                finish_reason = chunk["choices"][0]["finish_reason"]178                status_text = construct_token_message(179                    sum(all_token_counts), stream=True180                )181                if finish_reason == "stop":182                    yield get_return_value()183                    break184                try:185                    partial_words = (186                        partial_words + chunk["choices"][0]["delta"]["content"]187                    )188                except KeyError:189                    status_text = (190                        standard_error_msg191                        + "API回复中找不到内容。很可能是Token计数达到上限了。请重置对话。当前Token计数: "192                        + str(sum(all_token_counts))193                    )194                    yield get_return_value()195                    break196                history[-1] = construct_assistant(partial_words)197                chatbot[-1] = (chatbot[-1][0], partial_words+display_append)198                all_token_counts[-1] += 1199                yield get_return_value()200 201 202def predict_all(203    openai_api_key,204    system_prompt,205    history,206    inputs,207    chatbot,208    all_token_counts,209    top_p,210    temperature,211    selected_model,212    fake_input=None,213    display_append=""214):215    logging.info("一次性回答模式")216    history.append(construct_user(inputs))217    history.append(construct_assistant(""))218    if fake_input:219        chatbot.append((fake_input, ""))220    else:221        chatbot.append((inputs, ""))222    all_token_counts.append(count_token(construct_user(inputs)))223    try:224        response = get_response(225            openai_api_key,226            system_prompt,227            history,228            temperature,229            top_p,230            False,231            selected_model,232        )233    except requests.exceptions.ConnectTimeout:234        status_text = (235            standard_error_msg + connection_timeout_prompt + error_retrieve_prompt236        )237        return chatbot, history, status_text, all_token_counts238    except requests.exceptions.ProxyError:239        status_text = standard_error_msg + proxy_error_prompt + error_retrieve_prompt240        return chatbot, history, status_text, all_token_counts241    except requests.exceptions.SSLError:242        status_text = standard_error_msg + ssl_error_prompt + error_retrieve_prompt243        return chatbot, history, status_text, all_token_counts244    response = json.loads(response.text)245    content = response["choices"][0]["message"]["content"]246    history[-1] = construct_assistant(content)247    chatbot[-1] = (chatbot[-1][0], content+display_append)248    total_token_count = response["usage"]["total_tokens"]249    all_token_counts[-1] = total_token_count - sum(all_token_counts)250    status_text = construct_token_message(total_token_count)251    return chatbot, history, status_text, all_token_counts252 253 254def predict(255    openai_api_key,256    system_prompt,257    history,258    inputs,259    chatbot,260    all_token_counts,261    top_p,262    temperature,263    stream=False,264    selected_model=MODELS[0],265    use_websearch=False,266    files = None,267    reply_language="中文",268    should_check_token_count=True,269):  # repetition_penalty, top_k270    logging.info("输入为:" + colorama.Fore.BLUE + f"{inputs}" + colorama.Style.RESET_ALL)271    yield chatbot+[(inputs, "")], history, "开始生成回答……", all_token_counts272    if reply_language == "跟随问题语言(不稳定)":273        reply_language = "the same language as the question, such as English, 中文, 日本語, Español, Français, or Deutsch."274    if files:275        msg = "加载索引中……(这可能需要几分钟)"276        logging.info(msg)277        yield chatbot+[(inputs, "")], history, msg, all_token_counts278        index = construct_index(openai_api_key, file_src=files)279        msg = "索引构建完成,获取回答中……"280        yield chatbot+[(inputs, "")], history, msg, all_token_counts281        history, chatbot, status_text = chat_ai(openai_api_key, index, inputs, history, chatbot, reply_language)282        yield chatbot, history, status_text, all_token_counts283        return284 285    old_inputs = ""286    link_references = []287    if use_websearch:288        search_results = ddg(inputs, max_results=5)289        old_inputs = inputs290        web_results = []291        for idx, result in enumerate(search_results):292            logging.info(f"搜索结果{idx + 1}:{result}")293            domain_name = urllib3.util.parse_url(result["href"]).host294            web_results.append(f'[{idx+1}]"{result["body"]}"\nURL: {result["href"]}')295            link_references.append(f"{idx+1}. [{domain_name}]({result['href']})\n")296        link_references = "\n\n" + "".join(link_references)297        inputs = (298            replace_today(WEBSEARCH_PTOMPT_TEMPLATE)299            .replace("{query}", inputs)300            .replace("{web_results}", "\n\n".join(web_results))301            .replace("{reply_language}", reply_language )302        )303    else:304        link_references = ""305 306    if len(openai_api_key) != 51:307        status_text = standard_error_msg + no_apikey_msg308        logging.info(status_text)309        chatbot.append((inputs, ""))310        if len(history) == 0:311            history.append(construct_user(inputs))312            history.append("")313            all_token_counts.append(0)314        else:315            history[-2] = construct_user(inputs)316        yield chatbot+[(inputs, "")], history, status_text, all_token_counts317        return318    elif len(inputs.strip()) == 0:319        status_text = standard_error_msg + no_input_msg320        logging.info(status_text)321        yield chatbot+[(inputs, "")], history, status_text, all_token_counts322        return323 324    if stream:325        logging.info("使用流式传输")326        iter = stream_predict(327            openai_api_key,328            system_prompt,329            history,330            inputs,331            chatbot,332            all_token_counts,333            top_p,334            temperature,335            selected_model,336            fake_input=old_inputs,337            display_append=link_references338        )339        for chatbot, history, status_text, all_token_counts in iter:340            if shared.state.interrupted:341                shared.state.recover()342                return343            yield chatbot, history, status_text, all_token_counts344    else:345        logging.info("不使用流式传输")346        chatbot, history, status_text, all_token_counts = predict_all(347            openai_api_key,348            system_prompt,349            history,350            inputs,351            chatbot,352            all_token_counts,353            top_p,354            temperature,355            selected_model,356            fake_input=old_inputs,357            display_append=link_references358        )359        yield chatbot, history, status_text, all_token_counts360 361    logging.info(f"传输完毕。当前token计数为{all_token_counts}")362    if len(history) > 1 and history[-1]["content"] != inputs:363        logging.info(364            "回答为:"365            + colorama.Fore.BLUE366            + f"{history[-1]['content']}"367            + colorama.Style.RESET_ALL368        )369 370    if stream:371        max_token = max_token_streaming372    else:373        max_token = max_token_all374 375    if sum(all_token_counts) > max_token and should_check_token_count:376        status_text = f"精简token中{all_token_counts}/{max_token}"377        logging.info(status_text)378        yield chatbot, history, status_text, all_token_counts379        iter = reduce_token_size(380            openai_api_key,381            system_prompt,382            history,383            chatbot,384            all_token_counts,385            top_p,386            temperature,387            max_token//2,388            selected_model=selected_model,389        )390        for chatbot, history, status_text, all_token_counts in iter:391            status_text = f"Token 达到上限,已自动降低Token计数至 {status_text}"392            yield chatbot, history, status_text, all_token_counts393 394 395def retry(396    openai_api_key,397    system_prompt,398    history,399    chatbot,400    token_count,401    top_p,402    temperature,403    stream=False,404    selected_model=MODELS[0],405    reply_language="中文",406):407    logging.info("重试中……")408    if len(history) == 0:409        yield chatbot, history, f"{standard_error_msg}上下文是空的", token_count410        return411    history.pop()412    inputs = history.pop()["content"]413    token_count.pop()414    iter = predict(415        openai_api_key,416        system_prompt,417        history,418        inputs,419        chatbot,420        token_count,421        top_p,422        temperature,423        stream=stream,424        selected_model=selected_model,425        reply_language=reply_language,426    )427    logging.info("重试中……")428    for x in iter:429        yield x430    logging.info("重试完毕")431 432 433def reduce_token_size(434    openai_api_key,435    system_prompt,436    history,437    chatbot,438    token_count,439    top_p,440    temperature,441    max_token_count,442    selected_model=MODELS[0],443    reply_language="中文",444):445    logging.info("开始减少token数量……")446    iter = predict(447        openai_api_key,448        system_prompt,449        history,450        summarize_prompt,451        chatbot,452        token_count,453        top_p,454        temperature,455        selected_model=selected_model,456        should_check_token_count=False,457        reply_language=reply_language,458    )459    logging.info(f"chatbot: {chatbot}")460    flag = False461    for chatbot, history, status_text, previous_token_count in iter:462        num_chat = find_n(previous_token_count, max_token_count)463        if flag:464            chatbot = chatbot[:-1]465        flag = True466        history = history[-2*num_chat:] if num_chat > 0 else []467        token_count = previous_token_count[-num_chat:] if num_chat > 0 else []468        msg = f"保留了最近{num_chat}轮对话"469        yield chatbot, history, msg + "," + construct_token_message(470            sum(token_count) if len(token_count) > 0 else 0,471        ), token_count472    logging.info(msg)473    logging.info("减少token数量完毕")474