mirror of
https://github.com/csunny/DB-GPT.git
synced 2025-07-28 06:17:14 +00:00
refactor:refactor knowledge api
1.delete CFG in embedding_engine api 2.add a text_splitter param in embedding_engine api 3.fmt
This commit is contained in:
parent
e6aa46fc87
commit
929e7fe96b
@ -2,6 +2,7 @@ from typing import Dict, List, Optional
|
|||||||
|
|
||||||
from langchain.document_loaders import CSVLoader
|
from langchain.document_loaders import CSVLoader
|
||||||
from langchain.schema import Document
|
from langchain.schema import Document
|
||||||
|
from langchain.text_splitter import TextSplitter
|
||||||
|
|
||||||
from pilot.embedding_engine import SourceEmbedding, register
|
from pilot.embedding_engine import SourceEmbedding, register
|
||||||
|
|
||||||
@ -9,7 +10,12 @@ from pilot.embedding_engine import SourceEmbedding, register
|
|||||||
class CSVEmbedding(SourceEmbedding):
|
class CSVEmbedding(SourceEmbedding):
|
||||||
"""csv embedding for read csv document."""
|
"""csv embedding for read csv document."""
|
||||||
|
|
||||||
def __init__(self, file_path, vector_store_config, text_splitter=None):
|
def __init__(
|
||||||
|
self,
|
||||||
|
file_path,
|
||||||
|
vector_store_config,
|
||||||
|
text_splitter: Optional[TextSplitter] = None,
|
||||||
|
):
|
||||||
"""Initialize with csv path."""
|
"""Initialize with csv path."""
|
||||||
super().__init__(file_path, vector_store_config, text_splitter=None)
|
super().__init__(file_path, vector_store_config, text_splitter=None)
|
||||||
self.file_path = file_path
|
self.file_path = file_path
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
import os
|
import os
|
||||||
from typing import List
|
from typing import List, Optional
|
||||||
|
|
||||||
import markdown
|
import markdown
|
||||||
from bs4 import BeautifulSoup
|
from bs4 import BeautifulSoup
|
||||||
@ -10,6 +10,7 @@ from langchain.text_splitter import (
|
|||||||
SpacyTextSplitter,
|
SpacyTextSplitter,
|
||||||
CharacterTextSplitter,
|
CharacterTextSplitter,
|
||||||
RecursiveCharacterTextSplitter,
|
RecursiveCharacterTextSplitter,
|
||||||
|
TextSplitter,
|
||||||
)
|
)
|
||||||
|
|
||||||
from pilot.embedding_engine import SourceEmbedding, register
|
from pilot.embedding_engine import SourceEmbedding, register
|
||||||
@ -19,7 +20,12 @@ from pilot.embedding_engine.EncodeTextLoader import EncodeTextLoader
|
|||||||
class MarkdownEmbedding(SourceEmbedding):
|
class MarkdownEmbedding(SourceEmbedding):
|
||||||
"""markdown embedding for read markdown document."""
|
"""markdown embedding for read markdown document."""
|
||||||
|
|
||||||
def __init__(self, file_path, vector_store_config, text_splitter=None):
|
def __init__(
|
||||||
|
self,
|
||||||
|
file_path,
|
||||||
|
vector_store_config,
|
||||||
|
text_splitter: Optional[TextSplitter] = None,
|
||||||
|
):
|
||||||
"""Initialize raw text word path."""
|
"""Initialize raw text word path."""
|
||||||
super().__init__(file_path, vector_store_config, text_splitter=None)
|
super().__init__(file_path, vector_store_config, text_splitter=None)
|
||||||
self.file_path = file_path
|
self.file_path = file_path
|
||||||
|
@ -1,10 +1,14 @@
|
|||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
from typing import List
|
from typing import List, Optional
|
||||||
|
|
||||||
from langchain.document_loaders import PyPDFLoader
|
from langchain.document_loaders import PyPDFLoader
|
||||||
from langchain.schema import Document
|
from langchain.schema import Document
|
||||||
from langchain.text_splitter import SpacyTextSplitter, RecursiveCharacterTextSplitter
|
from langchain.text_splitter import (
|
||||||
|
SpacyTextSplitter,
|
||||||
|
RecursiveCharacterTextSplitter,
|
||||||
|
TextSplitter,
|
||||||
|
)
|
||||||
|
|
||||||
from pilot.embedding_engine import SourceEmbedding, register
|
from pilot.embedding_engine import SourceEmbedding, register
|
||||||
|
|
||||||
@ -12,7 +16,12 @@ from pilot.embedding_engine import SourceEmbedding, register
|
|||||||
class PDFEmbedding(SourceEmbedding):
|
class PDFEmbedding(SourceEmbedding):
|
||||||
"""pdf embedding for read pdf document."""
|
"""pdf embedding for read pdf document."""
|
||||||
|
|
||||||
def __init__(self, file_path, vector_store_config, text_splitter=None):
|
def __init__(
|
||||||
|
self,
|
||||||
|
file_path,
|
||||||
|
vector_store_config,
|
||||||
|
text_splitter: Optional[TextSplitter] = None,
|
||||||
|
):
|
||||||
"""Initialize pdf word path."""
|
"""Initialize pdf word path."""
|
||||||
super().__init__(file_path, vector_store_config, text_splitter=None)
|
super().__init__(file_path, vector_store_config, text_splitter=None)
|
||||||
self.file_path = file_path
|
self.file_path = file_path
|
||||||
|
@ -1,10 +1,14 @@
|
|||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
from typing import List
|
from typing import List, Optional
|
||||||
|
|
||||||
from langchain.document_loaders import UnstructuredPowerPointLoader
|
from langchain.document_loaders import UnstructuredPowerPointLoader
|
||||||
from langchain.schema import Document
|
from langchain.schema import Document
|
||||||
from langchain.text_splitter import SpacyTextSplitter, RecursiveCharacterTextSplitter
|
from langchain.text_splitter import (
|
||||||
|
SpacyTextSplitter,
|
||||||
|
RecursiveCharacterTextSplitter,
|
||||||
|
TextSplitter,
|
||||||
|
)
|
||||||
|
|
||||||
from pilot.embedding_engine import SourceEmbedding, register
|
from pilot.embedding_engine import SourceEmbedding, register
|
||||||
|
|
||||||
@ -12,7 +16,12 @@ from pilot.embedding_engine import SourceEmbedding, register
|
|||||||
class PPTEmbedding(SourceEmbedding):
|
class PPTEmbedding(SourceEmbedding):
|
||||||
"""ppt embedding for read ppt document."""
|
"""ppt embedding for read ppt document."""
|
||||||
|
|
||||||
def __init__(self, file_path, vector_store_config, text_splitter=None):
|
def __init__(
|
||||||
|
self,
|
||||||
|
file_path,
|
||||||
|
vector_store_config,
|
||||||
|
text_splitter: Optional[TextSplitter] = None,
|
||||||
|
):
|
||||||
"""Initialize ppt word path."""
|
"""Initialize ppt word path."""
|
||||||
super().__init__(file_path, vector_store_config, text_splitter=None)
|
super().__init__(file_path, vector_store_config, text_splitter=None)
|
||||||
self.file_path = file_path
|
self.file_path = file_path
|
||||||
|
@ -26,13 +26,13 @@ class SourceEmbedding(ABC):
|
|||||||
self,
|
self,
|
||||||
file_path,
|
file_path,
|
||||||
vector_store_config: {},
|
vector_store_config: {},
|
||||||
text_splitter: TextSplitter = None,
|
text_splitter: Optional[TextSplitter] = None,
|
||||||
embedding_args: Optional[Dict] = None,
|
embedding_args: Optional[Dict] = None,
|
||||||
):
|
):
|
||||||
"""Initialize with Loader url, model_name, vector_store_config"""
|
"""Initialize with Loader url, model_name, vector_store_config"""
|
||||||
self.file_path = file_path
|
self.file_path = file_path
|
||||||
self.vector_store_config = vector_store_config
|
self.vector_store_config = vector_store_config
|
||||||
self.text_splitter = text_splitter
|
self.text_splitter = text_splitter or None
|
||||||
self.embedding_args = embedding_args
|
self.embedding_args = embedding_args
|
||||||
self.embeddings = vector_store_config["embeddings"]
|
self.embeddings = vector_store_config["embeddings"]
|
||||||
|
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
from typing import List
|
from typing import List, Optional
|
||||||
|
|
||||||
from langchain.schema import Document
|
from langchain.schema import Document
|
||||||
|
from langchain.text_splitter import TextSplitter
|
||||||
|
|
||||||
from pilot.embedding_engine import SourceEmbedding, register
|
from pilot.embedding_engine import SourceEmbedding, register
|
||||||
|
|
||||||
@ -8,9 +9,14 @@ from pilot.embedding_engine import SourceEmbedding, register
|
|||||||
class StringEmbedding(SourceEmbedding):
|
class StringEmbedding(SourceEmbedding):
|
||||||
"""string embedding for read string document."""
|
"""string embedding for read string document."""
|
||||||
|
|
||||||
def __init__(self, file_path, vector_store_config, text_splitter=None):
|
def __init__(
|
||||||
|
self,
|
||||||
|
file_path,
|
||||||
|
vector_store_config,
|
||||||
|
text_splitter: Optional[TextSplitter] = None,
|
||||||
|
):
|
||||||
"""Initialize raw text word path."""
|
"""Initialize raw text word path."""
|
||||||
super().__init__(file_path, vector_store_config, text_splitter=None)
|
super().__init__(file_path=file_path, vector_store_config=vector_store_config)
|
||||||
self.file_path = file_path
|
self.file_path = file_path
|
||||||
self.vector_store_config = vector_store_config
|
self.vector_store_config = vector_store_config
|
||||||
self.text_splitter = text_splitter or None
|
self.text_splitter = text_splitter or None
|
||||||
|
@ -1,18 +1,26 @@
|
|||||||
from typing import List
|
from typing import List, Optional
|
||||||
|
|
||||||
from bs4 import BeautifulSoup
|
from bs4 import BeautifulSoup
|
||||||
from langchain.document_loaders import WebBaseLoader
|
from langchain.document_loaders import WebBaseLoader
|
||||||
from langchain.schema import Document
|
from langchain.schema import Document
|
||||||
from langchain.text_splitter import SpacyTextSplitter, RecursiveCharacterTextSplitter
|
from langchain.text_splitter import (
|
||||||
|
SpacyTextSplitter,
|
||||||
|
RecursiveCharacterTextSplitter,
|
||||||
|
TextSplitter,
|
||||||
|
)
|
||||||
|
|
||||||
from pilot.embedding_engine import SourceEmbedding, register
|
from pilot.embedding_engine import SourceEmbedding, register
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class URLEmbedding(SourceEmbedding):
|
class URLEmbedding(SourceEmbedding):
|
||||||
"""url embedding for read url document."""
|
"""url embedding for read url document."""
|
||||||
|
|
||||||
def __init__(self, file_path, vector_store_config, text_splitter=None):
|
def __init__(
|
||||||
|
self,
|
||||||
|
file_path,
|
||||||
|
vector_store_config,
|
||||||
|
text_splitter: Optional[TextSplitter] = None,
|
||||||
|
):
|
||||||
"""Initialize url word path."""
|
"""Initialize url word path."""
|
||||||
super().__init__(file_path, vector_store_config, text_splitter=None)
|
super().__init__(file_path, vector_store_config, text_splitter=None)
|
||||||
self.file_path = file_path
|
self.file_path = file_path
|
||||||
|
@ -1,10 +1,14 @@
|
|||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
from typing import List
|
from typing import List, Optional
|
||||||
|
|
||||||
from langchain.document_loaders import UnstructuredWordDocumentLoader
|
from langchain.document_loaders import UnstructuredWordDocumentLoader
|
||||||
from langchain.schema import Document
|
from langchain.schema import Document
|
||||||
from langchain.text_splitter import SpacyTextSplitter, RecursiveCharacterTextSplitter
|
from langchain.text_splitter import (
|
||||||
|
SpacyTextSplitter,
|
||||||
|
RecursiveCharacterTextSplitter,
|
||||||
|
TextSplitter,
|
||||||
|
)
|
||||||
|
|
||||||
from pilot.embedding_engine import SourceEmbedding, register
|
from pilot.embedding_engine import SourceEmbedding, register
|
||||||
|
|
||||||
@ -12,7 +16,12 @@ from pilot.embedding_engine import SourceEmbedding, register
|
|||||||
class WordEmbedding(SourceEmbedding):
|
class WordEmbedding(SourceEmbedding):
|
||||||
"""word embedding for read word document."""
|
"""word embedding for read word document."""
|
||||||
|
|
||||||
def __init__(self, file_path, vector_store_config, text_splitter=None):
|
def __init__(
|
||||||
|
self,
|
||||||
|
file_path,
|
||||||
|
vector_store_config,
|
||||||
|
text_splitter: Optional[TextSplitter] = None,
|
||||||
|
):
|
||||||
"""Initialize with word path."""
|
"""Initialize with word path."""
|
||||||
super().__init__(file_path, vector_store_config, text_splitter=None)
|
super().__init__(file_path, vector_store_config, text_splitter=None)
|
||||||
self.file_path = file_path
|
self.file_path = file_path
|
||||||
|
@ -6,8 +6,6 @@ from pymilvus import Collection, DataType, connections, utility
|
|||||||
from pilot.vector_store.vector_store_base import VectorStoreBase
|
from pilot.vector_store.vector_store_base import VectorStoreBase
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class MilvusStore(VectorStoreBase):
|
class MilvusStore(VectorStoreBase):
|
||||||
"""Milvus database"""
|
"""Milvus database"""
|
||||||
|
|
||||||
|
@ -7,10 +7,14 @@ chroma_persist_path = "your_persist_path"
|
|||||||
vector_store_config = {
|
vector_store_config = {
|
||||||
"vector_store_name": url.replace(":", ""),
|
"vector_store_name": url.replace(":", ""),
|
||||||
"vector_store_type": vector_store_type,
|
"vector_store_type": vector_store_type,
|
||||||
"chroma_persist_path": chroma_persist_path
|
"chroma_persist_path": chroma_persist_path,
|
||||||
}
|
}
|
||||||
embedding_engine = EmbeddingEngine(knowledge_source=url, knowledge_type=KnowledgeType.URL.value, model_name=embedding_model, vector_store_config=vector_store_config)
|
embedding_engine = EmbeddingEngine(
|
||||||
|
knowledge_source=url,
|
||||||
|
knowledge_type=KnowledgeType.URL.value,
|
||||||
|
model_name=embedding_model,
|
||||||
|
vector_store_config=vector_store_config,
|
||||||
|
)
|
||||||
|
|
||||||
# embedding url content to vector store
|
# embedding url content to vector store
|
||||||
embedding_engine.knowledge_embedding()
|
embedding_engine.knowledge_embedding()
|
||||||
|
|
||||||
|
@ -14,7 +14,8 @@ from pilot.server.knowledge.request.request import KnowledgeSpaceRequest
|
|||||||
from pilot.configs.config import Config
|
from pilot.configs.config import Config
|
||||||
from pilot.configs.model_config import (
|
from pilot.configs.model_config import (
|
||||||
DATASETS_DIR,
|
DATASETS_DIR,
|
||||||
LLM_MODEL_CONFIG, KNOWLEDGE_UPLOAD_ROOT_PATH,
|
LLM_MODEL_CONFIG,
|
||||||
|
KNOWLEDGE_UPLOAD_ROOT_PATH,
|
||||||
)
|
)
|
||||||
from pilot.embedding_engine.embedding_engine import EmbeddingEngine
|
from pilot.embedding_engine.embedding_engine import EmbeddingEngine
|
||||||
|
|
||||||
@ -68,7 +69,11 @@ if __name__ == "__main__":
|
|||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
vector_name = args.vector_name
|
vector_name = args.vector_name
|
||||||
store_type = CFG.VECTOR_STORE_TYPE
|
store_type = CFG.VECTOR_STORE_TYPE
|
||||||
vector_store_config = {"vector_store_name": vector_name, "vector_store_type": CFG.VECTOR_STORE_TYPE, "chroma_persist_path": KNOWLEDGE_UPLOAD_ROOT_PATH}
|
vector_store_config = {
|
||||||
|
"vector_store_name": vector_name,
|
||||||
|
"vector_store_type": CFG.VECTOR_STORE_TYPE,
|
||||||
|
"chroma_persist_path": KNOWLEDGE_UPLOAD_ROOT_PATH,
|
||||||
|
}
|
||||||
print(vector_store_config)
|
print(vector_store_config)
|
||||||
kv = LocalKnowledgeInit(vector_store_config=vector_store_config)
|
kv = LocalKnowledgeInit(vector_store_config=vector_store_config)
|
||||||
kv.knowledge_persist(file_path=DATASETS_DIR)
|
kv.knowledge_persist(file_path=DATASETS_DIR)
|
||||||
|
Loading…
Reference in New Issue
Block a user