Các dạng móc xích LangChain (phần 9)

MapReduceChain: Xử lí song song cho các tập dữ liệu lớn

MapReduceChain là một dạng xích tiên tiến trong LangChain cái làm có thể xử lí hiệu quả các tập dữ liệu lớn qua
tính toán song song. Xích này thực thi mô hình lập trình MapReduce, cho phép các nhà phát triển chia ra các nhiệm
vụ phức tạp thành các đoạn quản lí được, nhỏ hơn cái có thể được xử lí đồng thời.
Các đặc tính then chốt của MapReduceChain
+ Tăng quy mô: Xử lí khối lượng lớn dữ liệu bằng cách phân bằng cách phân phát workload
+ Xử lí song song: Cải thiện hiệu suất qua thực thi đồng thời
+ Giảm và chập linh động: Các hàm tùy chỉnh được cho xử lí dữ liệu
+ Tích lũy: Kết hợp các kết quả từ nhiều hoạt động song song.
Xử lí MapReduce trong LangChain điển hình đòi hỏi 3 bước chính:
1. Chập: Áp dụng một hàm vào mỗi vật trong tập dữ liệu đầu vào.
2. Xử lí: Thực hiện một hoạt động cụ thể trên dữ liệu chập (thường sử dụng một LLM)
3. Giảm: Tích lũy các kết quả từ dữ liệu được xử lí.
Trường hợp sử dụng ví dụ
Tóm tắt một tài liệu lớn
Cho ví dụ này, hãy sử dụng file này từ repo: large_document.txt

from langchain.chains import LLMChain
from langchain.text_splitter import CharacterTextSplitter
from langchain.prompts import PromptTemplate
from langchain.llms import OpenAI
from langchain.chains.combine_documents.map_reduce import MapReduceDocumentsChain
from langchain.chains.combine_documents.stuff import StuffDocumentsChain
from langchain.docstore.document import Document
# Initialize the language model
llm = AzureOpenAI(deployment_name=”<Azure deployment name>”, model_name=”<Model name>”)
# Initialize text splitter
text_splitter = CharacterTextSplitter(

separator=”\n”,
chunk_size=1000,
chunk_overlap=200

)
# Define map and reduce prompts
map_prompt = PromptTemplate(

input_variables=[“text”],
template=”Summarize this text in one sentence: {text}”

)
reduce_prompt = PromptTemplate(

input_variables=[“text”],
template=”Combine these summaries into a coherent paragraph: {text}”

)
# Create the map and reduce chains
map_chain = LLMChain(llm=llm, prompt=map_prompt)
reduce_chain = LLMChain(llm=llm, prompt=reduce_prompt)
# Create combine documents chain
combine_documents_chain = StuffDocumentsChain(

llm_chain=reduce_chain,
document_variable_name=”text”

)
# Create the map reduce chain
map_reduce_chain = MapReduceDocumentsChain(

llm_chain=map_chain,
# Use reduce_documents_chain instead of combine_documents_chain
reduce_documents_chain=combine_documents_chain,
document_variable_name=”text”,
return_intermediate_steps=True

)
# Process large document
with open(“large_document.txt”, “r”) as file:

large_text = file.read()

# Split text into documents
texts = text_splitter.split_text(large_text)
docs = [Document(page_content=t) for t in texts]
# Process the documents
try:

result = map_reduce_chain.invoke(docs)
print(“\nFinal Summary:”)
print(result[‘output_text’])

except Exception as e:

print(f”An error occurred: {str(e)}”)

output:
Final Summary:
Each paragraph should also flow logically from one to the next, building upon the previous one and contributing to the overall argument and thesis statement. Just as a seed needs a strong foundation and support to grow, a paper requires a strong argument and well-structured paragraphs to effectively convey its message. Brainstorming and organizing ideas are crucial steps in this process, as they help to develop a coherent and well-supported central idea. By utilizing different structures such as narration, description, process, classification, and illustration, a writer can effectively present and support their argument, just as each part of a seed supports its growth into a strong and thriving plant

MapReduceChain thực thi một cách tiếp cận chia để trị cho xử lí các texts lớn. Nó trước tiên chia tách text thành
các đoạn quản lí được, áp dụng một prompt tóm tắt vào mỗi đoạn (pha chập), và sau đó kết hợp các tóm tắt này thành
một kết quả gắn kết cuối cùng (pha giảm). Mô hình này đặc biệt hiệu quả cho xử lí các tài liệu dài trong khi duy trì
bối cảnh và tính gắn kết.
MapReduceChain đặc biệt hữu dụng cho các nhiệm vụ như tóm tắt tài liệu, phân tích sắc thái của  các tập dữ liệu
lớn, hay rút thông tin then chốt từ nhiều nguồn. Bằng cách sử dụng xử lí song song, nó cho phép các ứng dụng
LangChain xử lí nhiều khối lượng lớn dữ liệu hơn có thể với xử lí tuần tự.

Chia sẻ