From 91444e753231d71f330efc8c712ddb520d4d2349 Mon Sep 17 00:00:00 2001
From: Frieren <1692219062wang@gmail.com>
Date: Mon, 13 May 2024 00:11:07 +0800
Subject: [PATCH 1/3] second commit
---
.idea/misc.xml | 3 +
.idea/workspace.xml | 106 ++++++++++++++++++++++++++----
requirements.txt | 9 ++-
src/base/PdfLoader.py | 15 +++++
src/base/chinese_text_splitter.py | 48 ++++++++++++--
src/serve/embedding.py | 28 ++++++++
6 files changed, 185 insertions(+), 24 deletions(-)
create mode 100644 src/base/PdfLoader.py
create mode 100644 src/serve/embedding.py
diff --git a/.idea/misc.xml b/.idea/misc.xml
index 7830771..7427ae4 100644
--- a/.idea/misc.xml
+++ b/.idea/misc.xml
@@ -1,4 +1,7 @@
+
+
+
\ No newline at end of file
diff --git a/.idea/workspace.xml b/.idea/workspace.xml
index d70da44..205572e 100644
--- a/.idea/workspace.xml
+++ b/.idea/workspace.xml
@@ -1,33 +1,75 @@
+
+
+
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
+ {
+ "associatedIndex": 8
+}
-
+
+
+
- {
+ "keyToString": {
+ "Python.RAG.executor": "Run",
+ "Python.embedding.executor": "Run",
+ "RunOnceActivity.OpenProjectViewOnStart": "true",
+ "RunOnceActivity.ShowReadmeOnStart": "true",
+ "git-widget-placeholder": "f7939674",
+ "last_opened_file_path": "G:/code/py/LLM",
+ "node.js.detected.package.eslint": "true",
+ "node.js.detected.package.tslint": "true",
+ "node.js.selected.package.eslint": "(autodetect)",
+ "node.js.selected.package.tslint": "(autodetect)",
+ "nodejs_package_manager_path": "npm",
+ "settings.editor.selected.configurable": "com.jetbrains.python.configuration.PyActiveSdkModuleConfigurable",
+ "vue.rearranger.settings.migration": "true"
}
-}]]>
+}
+
+
+
+
+
+
@@ -43,8 +85,44 @@
1715274683455
-
+
+
+
+
+
+
+
+
+ 1715279142669
+
+
+
+ 1715279142669
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/requirements.txt b/requirements.txt
index ecdfcaa..f5aee2c 100644
--- a/requirements.txt
+++ b/requirements.txt
@@ -1,4 +1,7 @@
langchain~=0.1.19
-httpx-sse
-langchainhub
-pyjwt
\ No newline at end of file
+httpx
+pyjwt
+transformers
+text2vec~=1.2.9
+chardet
+pypdf~=4.2.0
\ No newline at end of file
diff --git a/src/base/PdfLoader.py b/src/base/PdfLoader.py
new file mode 100644
index 0000000..25bdd12
--- /dev/null
+++ b/src/base/PdfLoader.py
@@ -0,0 +1,15 @@
+from langchain_community.document_loaders import PyPDFLoader
+
+
+class PDFLoader:
+ @staticmethod
+ def loader(file_path: str):
+ content = ""
+ loader = PyPDFLoader(file_path)
+ for page in loader.load():
+ content += page.page_content
+ return content
+
+
+if __name__ == '__main__':
+ print(PDFLoader.loader("C:\\Users\\16922\\Desktop\\文档1.pdf"))
diff --git a/src/base/chinese_text_splitter.py b/src/base/chinese_text_splitter.py
index 14914ec..414fd41 100644
--- a/src/base/chinese_text_splitter.py
+++ b/src/base/chinese_text_splitter.py
@@ -1,25 +1,59 @@
+from langchain.text_splitter import CharacterTextSplitter
import re
from typing import List
-from langchain.text_splitter import CharacterTextSplitter
-
class ChineseTextSplitter(CharacterTextSplitter):
- def __init__(self, pdf: bool = False, **kwargs):
+ def __init__(self, pdf: bool = False, sentence_size: int = 250, **kwargs):
super().__init__(**kwargs)
self.pdf = pdf
+ self.sentence_size = sentence_size
- def split_text(self, text: str) -> List[str]:
+ def split_text1(self, text: str) -> List[str]:
if self.pdf:
text = re.sub(r"\n{3,}", "\n", text)
text = re.sub('\s', ' ', text)
text = text.replace("\n\n", "")
- sent_sep_pattern = re.compile(
- '([﹒﹔﹖﹗.。!?]["’”」』]{0,2}|(?=["‘“「『]{1,2}|$))')
+ sent_sep_pattern = re.compile('([﹒﹔﹖﹗.。!?]["’”」』]{0,2}|(?=["‘“「『]{1,2}|$))') # del :;
sent_list = []
for ele in sent_sep_pattern.split(text):
if sent_sep_pattern.match(ele) and sent_list:
sent_list[-1] += ele
elif ele:
sent_list.append(ele)
- return sent_list
\ No newline at end of file
+ return sent_list
+
+ def split_text(self, text: str) -> List[str]: ##此处需要进一步优化逻辑
+ if self.pdf:
+ text = re.sub(r"\n{3,}", r"\n", text)
+ text = re.sub('\s', " ", text)
+ text = re.sub("\n\n", "", text)
+
+ text = re.sub(r'([;;.!?。!?\?])([^”’])', r"\1\n\2", text) # 单字符断句符
+ text = re.sub(r'(\.{6})([^"’”」』])', r"\1\n\2", text) # 英文省略号
+ text = re.sub(r'(\…{2})([^"’”」』])', r"\1\n\2", text) # 中文省略号
+ text = re.sub(r'([;;!?。!?\?]["’”」』]{0,2})([^;;!?,。!?\?])', r'\1\n\2', text)
+ # 如果双引号前有终止符,那么双引号才是句子的终点,把分句符\n放到双引号后,注意前面的几句都小心保留了双引号
+ text = text.rstrip() # 段尾如果有多余的\n就去掉它
+ # 很多规则中会考虑分号;,但是这里我把它忽略不计,破折号、英文双引号等同样忽略,需要的再做些简单调整即可。
+ ls = [i for i in text.split("\n") if i]
+ for ele in ls:
+ if len(ele) > self.sentence_size:
+ ele1 = re.sub(r'([,,.]["’”」』]{0,2})([^,,.])', r'\1\n\2', ele)
+ ele1_ls = ele1.split("\n")
+ for ele_ele1 in ele1_ls:
+ if len(ele_ele1) > self.sentence_size:
+ ele_ele2 = re.sub(r'([\n]{1,}| {2,}["’”」』]{0,2})([^\s])', r'\1\n\2', ele_ele1)
+ ele2_ls = ele_ele2.split("\n")
+ for ele_ele2 in ele2_ls:
+ if len(ele_ele2) > self.sentence_size:
+ ele_ele3 = re.sub('( ["’”」』]{0,2})([^ ])', r'\1\n\2', ele_ele2)
+ ele2_id = ele2_ls.index(ele_ele2)
+ ele2_ls = ele2_ls[:ele2_id] + [i for i in ele_ele3.split("\n") if i] + ele2_ls[
+ ele2_id + 1:]
+ ele_id = ele1_ls.index(ele_ele1)
+ ele1_ls = ele1_ls[:ele_id] + [i for i in ele2_ls if i] + ele1_ls[ele_id + 1:]
+
+ id = ls.index(ele)
+ ls = ls[:id] + [i for i in ele1_ls if i] + ls[id + 1:]
+ return ls
\ No newline at end of file
diff --git a/src/serve/embedding.py b/src/serve/embedding.py
new file mode 100644
index 0000000..c7b71f4
--- /dev/null
+++ b/src/serve/embedding.py
@@ -0,0 +1,28 @@
+from langchain_community.document_loaders import PyPDFLoader
+from langchain_community.vectorstores import Chroma
+from text2vec import SentenceModel
+from src.base.chinese_text_splitter import ChineseTextSplitter
+
+
+class SentenceEmbedding:
+ __model = SentenceModel('shibing624/text2vec-base-chinese')
+ def __init__(self, file_path: str):
+ self.file_path = file_path
+ content = ""
+ loader = PyPDFLoader(file_path)
+ for page in loader.load():
+ content += page.page_content
+ sentences = ChineseTextSplitter(True).split_text(content)
+ embeddings = SentenceEmbedding.__model.encode(sentences)
+ self.vectorstore = Chroma.add_texts(iter(sentences), embeddings)
+
+ def get_vectorstore(self):
+ return self.vectorstore
+
+ def search(self, query:str):
+ embeddings = SentenceEmbedding.__model.encode(query)
+ self.vectorstore
+
+
+if __name__ == '__main__':
+ SentenceEmbedding("C:\\Users\\16922\\Desktop\\文档1.pdf").
From 2f9b99b50ace11603b5741a28fe683f24534f104 Mon Sep 17 00:00:00 2001
From: Frieren <1692219062wang@gmail.com>
Date: Mon, 13 May 2024 00:12:43 +0800
Subject: [PATCH 2/3] second commit
---
.idea/workspace.xml | 78 +++++++++++++++++++++++++++++----------------
1 file changed, 50 insertions(+), 28 deletions(-)
diff --git a/.idea/workspace.xml b/.idea/workspace.xml
index 205572e..47574ce 100644
--- a/.idea/workspace.xml
+++ b/.idea/workspace.xml
@@ -4,14 +4,7 @@
-
-
-
-
-
-
-
-
+
@@ -47,23 +40,23 @@
- {
- "keyToString": {
- "Python.RAG.executor": "Run",
- "Python.embedding.executor": "Run",
- "RunOnceActivity.OpenProjectViewOnStart": "true",
- "RunOnceActivity.ShowReadmeOnStart": "true",
- "git-widget-placeholder": "f7939674",
- "last_opened_file_path": "G:/code/py/LLM",
- "node.js.detected.package.eslint": "true",
- "node.js.detected.package.tslint": "true",
- "node.js.selected.package.eslint": "(autodetect)",
- "node.js.selected.package.tslint": "(autodetect)",
- "nodejs_package_manager_path": "npm",
- "settings.editor.selected.configurable": "com.jetbrains.python.configuration.PyActiveSdkModuleConfigurable",
- "vue.rearranger.settings.migration": "true"
+
+}]]>
@@ -90,7 +83,7 @@
-
+
@@ -100,7 +93,23 @@
1715279142669
-
+
+
+ 1715530268064
+
+
+
+ 1715530268064
+
+
+
+ 1715530309702
+
+
+
+ 1715530309702
+
+
@@ -111,7 +120,19 @@
@@ -119,7 +140,8 @@
-
+
+
From 3e1403fd949a164e6dedc93109b1302576fe8d07 Mon Sep 17 00:00:00 2001
From: Frieren <1692219062wang@gmail.com>
Date: Mon, 13 May 2024 00:17:18 +0800
Subject: [PATCH 3/3] second commit
---
.config/config.properties | 1 +
src/base/README.md | 10 +++++++
src/init/property.py | 31 ++++++++++++++++++++++
src/serve/RAG.py | 56 +++++++++++++++++++++++++++++++++++++++
4 files changed, 98 insertions(+)
create mode 100644 .config/config.properties
create mode 100644 src/base/README.md
create mode 100644 src/init/property.py
create mode 100644 src/serve/RAG.py
diff --git a/.config/config.properties b/.config/config.properties
new file mode 100644
index 0000000..408d0f0
--- /dev/null
+++ b/.config/config.properties
@@ -0,0 +1 @@
+API_KEY=9d2d36b2c8b20bb329fd44fe058b7ac2.UF14LzIdcAH2Ob21
\ No newline at end of file
diff --git a/src/base/README.md b/src/base/README.md
new file mode 100644
index 0000000..81bde49
--- /dev/null
+++ b/src/base/README.md
@@ -0,0 +1,10 @@
+## Document Loader Interface
+| Method Name | Explanation |
+|-------------|-----------------------------------------------------------------------------------------------------------------------------|
+| lazy_load | Used to load documents one by one lazily. Use for production code. |
+| alazy_load | Async variant of lazy_load |
+| load | Used to load all the documents into memory eagerly. Use for prototyping or interactive work. |
+| aload | Used to load all the documents into memory eagerly. Use for prototyping or interactive work. Added in 2024-04 to LangChain. |
+- The load methods is a convenience method meant solely for prototyping work – it just invokes list(self.lazy_load()).
+- The alazy_load has a default implementation that will delegate to lazy_load. If you’re using async, we recommend overriding the default implementation and providing a native async implementation.
+
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/RAG.py b/src/serve/RAG.py
new file mode 100644
index 0000000..d5cde14
--- /dev/null
+++ b/src/serve/RAG.py
@@ -0,0 +1,56 @@
+import os
+
+from langchain_community.document_loaders import PyPDFLoader
+from langchain_core.callbacks import StreamingStdOutCallbackHandler, CallbackManager
+
+from src.init.property import Property
+from langchain_community.chat_models.zhipuai import ChatZhipuAI
+from langchain_core.prompts import ChatPromptTemplate
+
+
+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")
+
+ # 初始化prompt工程
+ __prompt = ChatPromptTemplate.from_messages([
+ ("system", "You are a world class technical documentation writer."),
+ ("user", "{input}")
+ ])
+
+ # 初始化模型
+ __llm = ChatZhipuAI(
+ temperature=0.95,
+ model="glm-4"
+ )
+
+ __streaming_chat = ChatZhipuAI(
+ model="glm-4",
+ temperature=0.5,
+ streaming=True,
+ callback_manager=CallbackManager([StreamingStdOutCallbackHandler()]),
+ )
+
+ # 构建langchain
+ chain = __prompt | __llm
+
+ def __init__(self, file_path: str):
+ self.__file_path = file_path
+ loader = PyPDFLoader(self.__file_path)
+ file = loader.load()
+
+ def get_answer(self, message) -> str:
+ return RAG.__llm.invoke(message).content
+
+ def get_streaming_chat(self, message) -> str:
+ return RAG.__streaming_chat.invoke(message).content
+
+
+if __name__ == '__main__':
+ r = RAG("C:\\Users\\16922\\Desktop\\文档1.pdf")
+ print(r.get_streaming_chat("hello"))
+ print(r.get_streaming_chat("what can you do"))