File size: 5,623 Bytes
ef19859
 
 
 
 
 
42279cd
ef19859
 
42279cd
 
6036494
 
ef19859
 
 
54c3758
 
ef19859
 
 
9f72f91
ef19859
 
 
6036494
ef19859
6036494
 
ef19859
 
 
 
 
6036494
ef19859
 
6036494
ef19859
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9f72f91
ef19859
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5935bc1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ef19859
 
 
 
 
7323674
 
 
 
 
6036494
 
5935bc1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
from langchain_community.document_loaders import TextLoader
from langchain_community.docstore.document import Document
from langchain.text_splitter import CharacterTextSplitter, RecursiveCharacterTextSplitter
from langchain_community.vectorstores import Chroma
from langchain_community.embeddings import HuggingFaceEmbeddings
from langchain_community.retrievers import BM25Retriever
from langchain_community.llms import OpenAI
from langchain_openai import ChatOpenAI
from langchain.chains import RetrievalQA
from langchain.schema import AIMessage, HumanMessage
from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
import os

def split_with_source(text, source):
    splitter = CharacterTextSplitter(
        separator = "\n",
        chunk_size = 512,
        chunk_overlap  = 128,
        add_start_index = True,
    )
    documents = splitter.create_documents([text])
    # print(documents)
    for doc in documents:
        doc.metadata["source"] = source
        # print(doc.metadata)

    return documents


def count_files_in_folder(folder_path):
    # Kiểm tra xem đường dẫn thư mục có tồn tại không
    if not os.path.isdir(folder_path):
        print("Đường dẫn không hợp lệ.")
        return None

    # Sử dụng os.listdir() để lấy danh sách các tập tin và thư mục trong thư mục
    files = os.listdir(folder_path)

    # Đếm số lượng tập tin trong danh sách
    file_count = len(files)

    return file_count

def get_document_from_raw_text():
    documents = [Document(page_content="", metadata={'source': 0})]
    files = os.listdir(os.path.join(os.getcwd(), "raw_data"))
    # print(files)
    for i in files:
        file_path = i
        with open(os.path.join(os.path.join(os.getcwd(), "raw_data"),file_path), 'r', encoding="utf-8") as file:
            # Xử lý bằng text_spliter
            # Tiền xử lý văn bản
            content = file.read().replace('\n\n', "\n")
            # content = ''.join(content.split('.'))
            new_doc = content
            texts = split_with_source(new_doc, i)
            documents = documents + texts

            ##Xử lý mỗi khi xuống dòng
            # for line in file:
            #     # Loại bỏ khoảng trắng thừa và ký tự xuống dòng ở đầu và cuối mỗi dòng
            #     line = line.strip()
            #     documents.append(Document(page_content=line, metadata={"source": i}))
    # print(documents)
    return documents

def load_the_embedding_retrieve(is_ready = False, k = 3, model= 'sentence-transformers/paraphrase-multilingual-MiniLM-L12-v2'):
    embeddings = HuggingFaceEmbeddings(model_name=model)
    if is_ready:
        retriever = Chroma(persist_directory=os.path.join(os.getcwd(), "Data"), embedding_function=embeddings).as_retriever(
            search_kwargs={"k": k}
        )
    else:
        documents = get_document_from_raw_text()
        print(type(documents))
        retriever = Chroma.from_documents(documents, embeddings).as_retriever(
            search_kwargs={"k": k}
        )


    return retriever

def load_the_bm25_retrieve(k = 3):
    documents = get_document_from_raw_text()
    bm25_retriever = BM25Retriever.from_documents(documents)
    bm25_retriever.k = k

    return bm25_retriever

def get_qachain(llm_name = "gpt-3.5-turbo-0125", chain_type = "stuff", retriever = None, return_source_documents = True):
    llm = ChatOpenAI(temperature=0,
                     model_name=llm_name)
    return RetrievalQA.from_chain_type(llm=llm,
                                  chain_type=chain_type,
                                  retriever=retriever,
                                  return_source_documents=return_source_documents)


def summarize_messages(demo_ephemeral_chat_history, llm):
    stored_messages = demo_ephemeral_chat_history.messages
    if len(stored_messages) == 0:
        return False
    summarization_prompt = ChatPromptTemplate.from_messages(
        [
            MessagesPlaceholder(variable_name="chat_history"),
            (
                "user", os.environ['SUMARY_MESSAGE_PROMPT'],
            ),
        ]
    )
    summarization_chain = summarization_prompt | llm

    summary_message = summarization_chain.invoke({"chat_history": stored_messages})

    demo_ephemeral_chat_history.clear()

    demo_ephemeral_chat_history.add_message(summary_message)

    return demo_ephemeral_chat_history

def get_question_from_summarize(summary, question, llm):
    new_qa_prompt = ChatPromptTemplate.from_messages([
        ("system", os.environ['NEW_QUESTION_PROMPT']),
        ("human",
         '''
         Sumary: {summary}
         Question: {question}
         Output:
         '''
         )
    ]
    )

    new_qa_chain = new_qa_prompt | llm
    return new_qa_chain.invoke({'summary': summary, 'question': question}).content

def get_final_answer(question, context, chat_history, prompt, llm):
    qa_prompt = ChatPromptTemplate.from_messages(
        [
            MessagesPlaceholder("chat_history"),
            ("system", prompt),
            ("human", '''
            Context: {context}
            Question: {question}
            Output:
            '''),
        ]
    )

    answer_chain = qa_prompt | llm

    answer = answer_chain.invoke({'question': question, 'context': context, 'chat_history': chat_history})

    return answer.content

def process_llm_response(llm_response):
    print(llm_response['result'])
    print('\n\nSources:')
    for source in llm_response["source_documents"]:
        print(source.metadata['source'])