diff --git a/.config/config.properties b/.config/config.properties new file mode 100644 index 0000000..3a0b67f --- /dev/null +++ b/.config/config.properties @@ -0,0 +1,2 @@ +API_KEY= +FILE_PATH= \ No newline at end of file diff --git a/.idea/LLM.iml b/.idea/LLM.iml new file mode 100644 index 0000000..d0876a7 --- /dev/null +++ b/.idea/LLM.iml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/inspectionProfiles/Project_Default.xml b/.idea/inspectionProfiles/Project_Default.xml new file mode 100644 index 0000000..02399c7 --- /dev/null +++ b/.idea/inspectionProfiles/Project_Default.xml @@ -0,0 +1,20 @@ + + + + \ No newline at end of file diff --git a/.idea/inspectionProfiles/profiles_settings.xml b/.idea/inspectionProfiles/profiles_settings.xml new file mode 100644 index 0000000..105ce2d --- /dev/null +++ b/.idea/inspectionProfiles/profiles_settings.xml @@ -0,0 +1,6 @@ + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..7427ae4 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,7 @@ + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..9f996f8 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/workspace.xml b/.idea/workspace.xml new file mode 100644 index 0000000..5da222a --- /dev/null +++ b/.idea/workspace.xml @@ -0,0 +1,212 @@ + + + + + + + + + + + + + + + + + + + + { + "lastFilter": { + "state": "OPEN", + "assignee": "YDzzz" + } +} + + + + + + { + "associatedIndex": 8 +} + + + + + + + + + + + + + + + + + + + + + + + + 1715274683455 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/LICENSE b/LICENSE deleted file mode 100644 index 2071b23..0000000 --- a/LICENSE +++ /dev/null @@ -1,9 +0,0 @@ -MIT License - -Copyright (c) - -Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/README.md b/README.md index 362455c..e69de29 100644 --- a/README.md +++ b/README.md @@ -1,2 +0,0 @@ -# LLM - diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..4ff54db --- /dev/null +++ b/requirements.txt @@ -0,0 +1,7 @@ +langchain>=0.1.20 +pypdf>=4.2.0 +zhipuai~=2.0.1.20240423.1 +langchainhub~=0.1.15 +httpx-sse~=0.4.0 +chardet~=5.2.0 +gradio \ No newline at end of file diff --git a/src/base/ZhuPuAiEmbeddings.py b/src/base/ZhuPuAiEmbeddings.py new file mode 100644 index 0000000..5fbb914 --- /dev/null +++ b/src/base/ZhuPuAiEmbeddings.py @@ -0,0 +1,47 @@ +import os +from abc import ABC +from typing import List + +from zhipuai import ZhipuAI + +from src.init.property import Property + +from langchain_core.embeddings import Embeddings + +os.environ["ZHIPUAI_API_KEY"] = Property.get_property("API_KEY") +client = ZhipuAI() + + +def _text_qualify(embedding_text): + """ + using ZhipuAI Embedding API to get embedding 1024 dimension support + :param embedding_text: + :return: + """ + if type(embedding_text) == str: + e_t = embedding_text + else: + e_t = embedding_text.page_content + # print usage of token number: + # response.usage.total_tokens + # embedding support: + # response.data[0].embedding + response = client.embeddings.create( + model="embedding-2", + input=e_t, + ) + return response.data[0].embedding + + +class ZhuPuAiEmbedding(Embeddings, ABC): + def embed_documents(self, texts: List[str]) -> List[List[float]]: + embeddings = [] + for i in texts: + embeddings.append(_text_qualify(i)) + return embeddings + + def embed_query(self, text: str) -> List[float]: + return _text_qualify(text) + + def __init__(self): + super().__init__() diff --git a/src/base/embedding.py b/src/base/embedding.py new file mode 100644 index 0000000..02fb86a --- /dev/null +++ b/src/base/embedding.py @@ -0,0 +1,26 @@ +from langchain_community.document_loaders import PyPDFLoader +from langchain_community.vectorstores import Chroma +from langchain_text_splitters import RecursiveCharacterTextSplitter + +from src.base.ZhuPuAiEmbeddings import ZhuPuAiEmbedding + + +class SentenceEmbedding: + def __init__(self, file_path: str): + self.file_path = file_path + docs = PyPDFLoader(file_path).load() + text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200, add_start_index=True) + sentences = text_splitter.split_documents(docs) + self.vectorstore = Chroma.from_documents(sentences, ZhuPuAiEmbedding()) + + def get_vectorstore(self): + return self.vectorstore + + def search(self, query: str) -> str: + docs = self.vectorstore.similarity_search(query) + return docs[0].page_content + + +if __name__ == '__main__': + a = SentenceEmbedding("C:\\Users\\16922\\Desktop\\文档1.pdf") + print(a.search("We will dedicate a segment of our project to discussing these ethical issues")) diff --git a/src/init/property.py b/src/init/property.py new file mode 100644 index 0000000..88b2dfa --- /dev/null +++ b/src/init/property.py @@ -0,0 +1,31 @@ +import os + + +class Property(object): + __props = {} + + filepath = os.path.join(os.path.dirname(__file__), os.path.pardir, os.path.pardir, ".config\\config.properties") + with open(filepath, "r") as f: + for line in f: + l = line.strip() + if l and not l.startswith('#'): + key_map = l.split('=') + key_name = key_map[0].strip() + key_value = '='.join(key_map[1:]).strip().strip() + __props[key_name] = key_value + + @staticmethod + def get_property(property_name): + try: + return Property.__props[property_name] + except KeyError: + print("there is no that property name") + return None + + +def main(): + print(Property.get_property("API_KEY")) + + +if __name__ == '__main__': + main() diff --git a/src/serve/HistoryRAG.py b/src/serve/HistoryRAG.py new file mode 100644 index 0000000..b2c114e --- /dev/null +++ b/src/serve/HistoryRAG.py @@ -0,0 +1,114 @@ +from langchain.chains.combine_documents import create_stuff_documents_chain +from langchain.chains.history_aware_retriever import create_history_aware_retriever +from langchain.chains.retrieval import create_retrieval_chain +from langchain_core.messages import HumanMessage +from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder + +from src.serve.RAG import RAG +from src.init.property import Property +import gradio as gr + +__contextualize_q_system_prompt = """Given a chat history and the latest user question \ +which might reference context in the chat history, formulate a standalone question \ +which can be understood without the chat history. Do NOT answer the question, \ +just reformulate it if needed and otherwise return it as is.""" +_contextualize_q_prompt = ChatPromptTemplate.from_messages( + [ + ("system", __contextualize_q_system_prompt), + MessagesPlaceholder("chat_history"), + ("human", "{input}"), + ] +) + +__qa_system_prompt = """You are an assistant for question-answering tasks. \ +Use the following pieces of retrieved context to answer the question. \ +If you don't know the answer, just say that you don't know. \ +Use three sentences maximum and keep the answer concise.\ + +{context}""" +_qa_prompt = ChatPromptTemplate.from_messages( + [ + ("system", __qa_system_prompt), + MessagesPlaceholder("chat_history"), + ("human", "{input}"), + ] +) + + +class HistoryRAG(RAG): + + def __init__(self, file_path): + global _contextualize_q_prompt + global _qa_prompt + super().__init__(file_path) + self.__chat_history = [] + + history_aware_retriever = create_history_aware_retriever( + HistoryRAG._llm, self._retriever, _contextualize_q_prompt + ) + question_answer_chain = create_stuff_documents_chain(HistoryRAG._llm, _qa_prompt) + self.__rag_chain = create_retrieval_chain(history_aware_retriever, question_answer_chain) + + def get_chat(self, question: str): + ai_msg = self.__rag_chain.invoke({"input": question, "chat_history": self.__chat_history}) + self.__chat_history.extend([HumanMessage(content=question), ai_msg["answer"]]) + return ai_msg["answer"] + + def clear_history(self): + self.__chat_history.clear() + + def select_prompt(self, prompt_index: int = 1): + pass + + +if __name__ == '__main__': + file_path = Property.get_property("FILE_PATH") + hr = HistoryRAG(file_path) + iface = gr.Interface(fn=hr.get_chat, inputs="text", outputs="text", title="Chatbot", + description="从自定义文档中提问并获取答案") + gr.HTML("""

ChatGLM4

""") + iface.launch() + # print("welcome to use RAG question, input exit() to end") + # try: + # file_path = input("please input file path:").strip('"') + # if not len(file_path): + # raise ValueError("path not be empty") + # except ValueError: + # print("arise error:" + repr(ValueError)) + # finally: + # hr = HistoryRAG(file_path) + # while True: + # chat = input("user:") + # if chat == "exit()": + # break + # print("system:" + hr.get_chat(chat)) + # + # + # with gr.Blocks() as demo: + # gr.HTML("""

ChatGLM4

""") + # chatbot = gr.Chatbot() + # + # with gr.Row(): + # with gr.Column(scale=4): + # with gr.Column(scale=12): + # user_input = gr.Textbox(show_label=False, placeholder="Input...", lines=10, container=False) + # with gr.Column(min_width=32, scale=1): + # submitBtn = gr.Button("Submit") + # with gr.Column(scale=1): + # emptyBtn = gr.Button("Clear History") + # max_length = gr.Slider(0, 32768, value=8192, step=1.0, label="Maximum length", interactive=True) + # top_p = gr.Slider(0, 1, value=0.8, step=0.01, label="Top P", interactive=True) + # temperature = gr.Slider(0.01, 1, value=0.6, step=0.01, label="Temperature", interactive=True) + # + # + # def user(query, history): + # return "", history + [[parse_text(query), ""]] + # + # + # submitBtn.click(user, [user_input, chatbot], [user_input, chatbot], queue=False).then( + # [chatbot, max_length, top_p, temperature], chatbot + # ) + # emptyBtn.click(lambda: None, None, chatbot, queue=False) + # + # demo.queue() + # demo.launch(server_name="127.0.0.1", server_port=7870, inbrowser=True, share=False) diff --git a/src/serve/RAG.py b/src/serve/RAG.py new file mode 100644 index 0000000..bde2b0b --- /dev/null +++ b/src/serve/RAG.py @@ -0,0 +1,84 @@ +import os + +from langchain_core.callbacks import StreamingStdOutCallbackHandler, CallbackManager +from langchain_community.chat_models.zhipuai import ChatZhipuAI +from langchain_core.output_parsers import StrOutputParser +from langchain_core.prompts import ChatPromptTemplate +from langchain import hub +from langchain_core.runnables import RunnablePassthrough + +from src.init.property import Property +from src.base.embedding import SentenceEmbedding + +# 初始化prompt工程 +_prompt = { + 'prompt_1': ChatPromptTemplate.from_template( + """你是问答任务的助手。使用以下检索到的上下文来回答问题。如果你不知道答案,就说你不知道。最多使用三个句子并保持答案简洁。 + + {context} + + Question: {question} + + Helpful Answer:""" + ), + 'prompt_2': hub.pull("rlm/rag-prompt") +} + + +def format_docs(docs): + return "\n\n".join(doc.page_content for doc in docs) + + +class RAG: + # 初始化ZHIPU API KEY + os.environ["ZHIPUAI_API_KEY"] = Property.get_property("API_KEY") + + # 初始化模型 + _llm = ChatZhipuAI( + temperature=0.95, + model="glm-4" + ) + + __streaming_chat = ChatZhipuAI( + model="glm-4", + temperature=0.5, + streaming=True, + callback_manager=CallbackManager([StreamingStdOutCallbackHandler()]), + ) + + def __init__(self, file_path: str): + try: + global _prompt + self._example_prompt = _prompt["prompt_1"] + except NameError: + pass + self.__file_path = file_path + self.__sentenceEmbedding = SentenceEmbedding(file_path) + self._retriever = self.__sentenceEmbedding.get_vectorstore().as_retriever(search_type="similarity", + search_kwargs={"k": 5}) + + try: + self.__rag_chain = ( + {"context": self._retriever | format_docs, "question": RunnablePassthrough()} + | self._example_prompt + | RAG._llm + | StrOutputParser() + ) + except AttributeError: + pass + + def get_chat(self, message: str): + for chunk in self.__rag_chain.stream(message): + print(chunk, end="", flush=True) + + def select_prompt(self, prompt_index: int = 1): + global _prompt + prompt_name = "prompt_" + str(prompt_index) + self._example_prompt = _prompt[prompt_name] + + +if __name__ == '__main__': + r = RAG("C:\\Users\\16922\\Desktop\\文档1.pdf") + r.select_prompt(2) + r.get_chat("what can Multimodal Agent AI systems do?") +