eveyuyi/ChatGPT_Prompts
0
1import os2import logging3 4from llama_index import GPTSimpleVectorIndex5from llama_index import download_loader6from llama_index import (7 Document,8 LLMPredictor,9 PromptHelper,10 QuestionAnswerPrompt,11 RefinePrompt,12)13from langchain.llms import OpenAI14import colorama15 16from modules.presets import *17from modules.utils import *18 19def get_index_name(file_src):20 index_name = []21 for file in file_src:22 index_name.append(os.path.basename(file.name))23 index_name = sorted(index_name)24 index_name = "".join(index_name)25 index_name = sha1sum(index_name)26 return index_name27 28def get_documents(file_src):29 documents = []30 logging.debug("Loading documents...")31 logging.debug(f"file_src: {file_src}")32 for file in file_src:33 logging.info(f"loading file: {file.name}")34 if os.path.splitext(file.name)[1] == ".pdf":35 logging.debug("Loading PDF...")36 CJKPDFReader = download_loader("CJKPDFReader")37 loader = CJKPDFReader()38 text_raw = loader.load_data(file=file.name)[0].text39 elif os.path.splitext(file.name)[1] == ".docx":40 logging.debug("Loading DOCX...")41 DocxReader = download_loader("DocxReader")42 loader = DocxReader()43 text_raw = loader.load_data(file=file.name)[0].text44 elif os.path.splitext(file.name)[1] == ".epub":45 logging.debug("Loading EPUB...")46 EpubReader = download_loader("EpubReader")47 loader = EpubReader()48 text_raw = loader.load_data(file=file.name)[0].text49 else:50 logging.debug("Loading text file...")51 with open(file.name, "r", encoding="utf-8") as f:52 text_raw = f.read()53 text = add_space(text_raw)54 documents += [Document(text)]55 return documents56 57 58def construct_index(59 api_key,60 file_src,61 max_input_size=4096,62 num_outputs=1,63 max_chunk_overlap=20,64 chunk_size_limit=600,65 embedding_limit=None,66 separator=" ",67 num_children=10,68 max_keywords_per_chunk=10,69):70 os.environ["OPENAI_API_KEY"] = api_key71 chunk_size_limit = None if chunk_size_limit == 0 else chunk_size_limit72 embedding_limit = None if embedding_limit == 0 else embedding_limit73 separator = " " if separator == "" else separator74 75 llm_predictor = LLMPredictor(76 llm=OpenAI(model_name="gpt-3.5-turbo-0301", openai_api_key=api_key)77 )78 prompt_helper = PromptHelper(79 max_input_size,80 num_outputs,81 max_chunk_overlap,82 embedding_limit,83 chunk_size_limit,84 separator=separator,85 )86 index_name = get_index_name(file_src)87 if os.path.exists(f"./index/{index_name}.json"):88 logging.info("找到了缓存的索引文件,加载中……")89 return GPTSimpleVectorIndex.load_from_disk(f"./index/{index_name}.json")90 else:91 try:92 documents = get_documents(file_src)93 logging.debug("构建索引中……")94 index = GPTSimpleVectorIndex(95 documents, llm_predictor=llm_predictor, prompt_helper=prompt_helper96 )97 os.makedirs("./index", exist_ok=True)98 index.save_to_disk(f"./index/{index_name}.json")99 return index100 except Exception as e:101 print(e)102 return None103 104 105def chat_ai(106 api_key,107 index,108 question,109 context,110 chatbot,111 reply_language,112):113 os.environ["OPENAI_API_KEY"] = api_key114 115 logging.info(f"Question: {question}")116 117 response, chatbot_display, status_text = ask_ai(118 api_key,119 index,120 question,121 replace_today(PROMPT_TEMPLATE),122 REFINE_TEMPLATE,123 SIM_K,124 INDEX_QUERY_TEMPRATURE,125 context,126 reply_language,127 )128 if response is None:129 status_text = "查询失败,请换个问法试试"130 return context, chatbot131 response = response132 133 context.append({"role": "user", "content": question})134 context.append({"role": "assistant", "content": response})135 chatbot.append((question, chatbot_display))136 137 os.environ["OPENAI_API_KEY"] = ""138 return context, chatbot, status_text139 140 141def ask_ai(142 api_key,143 index,144 question,145 prompt_tmpl,146 refine_tmpl,147 sim_k=1,148 temprature=0,149 prefix_messages=[],150 reply_language="中文",151):152 os.environ["OPENAI_API_KEY"] = api_key153 154 logging.debug("Index file found")155 logging.debug("Querying index...")156 llm_predictor = LLMPredictor(157 llm=OpenAI(158 temperature=temprature,159 model_name="gpt-3.5-turbo-0301",160 prefix_messages=prefix_messages,161 )162 )163 164 response = None # Initialize response variable to avoid UnboundLocalError165 qa_prompt = QuestionAnswerPrompt(prompt_tmpl.replace("{reply_language}", reply_language))166 rf_prompt = RefinePrompt(refine_tmpl.replace("{reply_language}", reply_language))167 response = index.query(168 question,169 llm_predictor=llm_predictor,170 similarity_top_k=sim_k,171 text_qa_template=qa_prompt,172 refine_template=rf_prompt,173 response_mode="compact",174 )175 176 if response is not None:177 logging.info(f"Response: {response}")178 ret_text = response.response179 nodes = []180 for index, node in enumerate(response.source_nodes):181 brief = node.source_text[:25].replace("\n", "")182 nodes.append(183 f"<details><summary>[{index + 1}]\t{brief}...</summary><p>{node.source_text}</p></details>"184 )185 new_response = ret_text + "\n----------\n" + "\n\n".join(nodes)186 logging.info(187 f"Response: {colorama.Fore.BLUE}{ret_text}{colorama.Style.RESET_ALL}"188 )189 os.environ["OPENAI_API_KEY"] = ""190 return ret_text, new_response, f"查询消耗了{llm_predictor.last_token_usage} tokens"191 else:192 logging.warning("No response found, returning None")193 os.environ["OPENAI_API_KEY"] = ""194 return None195 196 197def add_space(text):198 punctuations = {",": ", ", "。": "。 ", "?": "? ", "!": "! ", ":": ": ", ";": "; "}199 for cn_punc, en_punc in punctuations.items():200 text = text.replace(cn_punc, en_punc)201 return text202 