|
|
|
|
|
|
|
import tiktoken |
|
tokenizer = tiktoken.get_encoding('cl100k_base') |
|
def tiktoken_len(text): |
|
tokens = tokenizer.encode(text) |
|
return len(tokens) |
|
|
|
from langchain.embeddings.sentence_transformer import SentenceTransformerEmbeddings |
|
from langchain.text_splitter import RecursiveCharacterTextSplitter |
|
from langchain.document_loaders import UnstructuredMarkdownLoader |
|
from langchain_huggingface import HuggingFaceEmbeddings |
|
from langchain_community.vectorstores.utils import filter_complex_metadata |
|
|
|
|
|
|
|
|
|
|
|
|
|
loader = UnstructuredMarkdownLoader('Document/Knowledge.md', mode="elements") |
|
pages = loader.load_and_split() |
|
|
|
|
|
text_splitter = RecursiveCharacterTextSplitter(chunk_size=500, chunk_overlap=80, length_function=tiktoken_len) |
|
sourceDocs = text_splitter.split_documents(pages) |
|
sourceDocs = filter_complex_metadata(sourceDocs) |
|
|
|
|
|
|
|
|
|
from langchain.vectorstores import Chroma |
|
|
|
model_huggingface = HuggingFaceEmbeddings(model_name = 'jhgan/ko-sroberta-multitask', |
|
model_kwargs = {'device':'cpu'}, |
|
encode_kwargs = {'normalize_embeddings' : True}) |
|
|
|
|
|
db = Chroma.from_documents(sourceDocs, model_huggingface) |
|
|
|
|
|
def SearchDocs(question, k=4): |
|
results = db.similarity_search_with_relevance_scores(question, k = k) |
|
merged = '' |
|
for result in results: |
|
merged += '\n\n' + result[0].page_content |
|
return merged |
|
|
|
|
|
|
|
|
|
|