দীর্ঘ পাঠ হ্যান্ডেল করা
PDF সহ ফাইল সংক্রান্ত কাজ করার সময়, আপনার ভাষা মডেলের প্রোত্যাশাজনক উইন্ডো ছাড়া লেখা দেখা যাবে। এই লেখাকে প্রসেস করার জন্য, নিম্নলিখিত কৌশলগুলি বিবেচনা করুন:
- LLM পরিবর্তন বড় কন্টেক্সট উইন্ডো সমর্থন করে অন্য LLM চয়ন করুন।
- ব্রুট ফোর্স ডকুমেন্টটি পিসি করুন এবং প্রতিটি পিস থেকে বিষয়বস্তু নিয়োন।
- RAG ডকুমেন্টটি পিস করুন, পিসগুলি ইন্ডেক্স করুন এবং শুধুমাত্র "গুরুত্বপূর্ণ" পিসগুলি থেকে বিষয়বস্তু নিয়োন।
মন রাখবেন যে, এই কৌশলগুলির বিভিন্ন বিনিময় রয়েছে এবং সোনার সাথেনির্ধারিতম্য সম্পর্কে সেরা কৌশলটি সম্ভাব্যতঃ আপনি যে অ্যাপ্লিকেশনটি ডিজাইন করছেন!
সেট আপ
আমাদের কিছু উদাহরণ ডাটা দরকার! চলুন একটি গাড়ির সম্পর্কে উইকিপিডিয়া নিয়োন করি এবং এটি একটি LangChain ডকুমেন্ট
হিসেবে লোড করা যাক।
import re
import requests
from langchain_community.document_loaders import BSHTMLLoader
response = requests.get("https://en.wikipedia.org/wiki/Car")
with open("car.html", "w", encoding="utf-8") as f:
f.write(response.text)
loader = BSHTMLLoader("car.html")
document = loader.load()[0]
document.page_content = re.sub("\n\n+", "\n", document.page_content)
print(len(document.page_content))
78967
স্কিমা নির্ধারণ করুন
এখানে, আমরা টেক্সট থেকে প্রধান উন্নতিসমূহ নিয়ো করার স্কিমা নির্ধারণ করব।
from typing import List, Optional
from langchain.chains import create_structured_output_runnable
from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
from langchain_core.pydantic_v1 import BaseModel, Field
from langchain_openai import ChatOpenAI
class KeyDevelopment(BaseModel):
"""গাড়ীর ইতিহাসে একটি উন্নতি সম্পর্কিত তথ্য।"""
year: int = Field(
..., description="একটি গুরুত্বপূর্ণ ঐতিহাসিক উন্নতির বছর।"
)
description: str = Field(
..., description="এই বছরে কী ঘটেছিল? উন্নতি কী ছিল?"
)
evidence: str = Field(
...,
description="এই বছর এবং বর্ণনার তথ্য থেকে বছর এবং বর্ণনার তথ্যগুলি নকলে বাড়বো না।",
)
class ExtractionData(BaseModel):
"""গাড়ীর ইতিহাসে প্রধান উন্নতি সম্পর্কিত বিবেচনা।"""
key_developments: List[KeyDevelopment]
prompt = ChatPromptTemplate.from_messages(
[
(
"system",
"আপনি পাঠের গুরুত্বপূর্ণ ঐতিহাসিক উন্নতি সনাক্ত করার জন্য একজন বিশেষজ্ঞ। "
"প্রধানতাই ঐতিহাসিক উন্নতি সম্পর্কিত গুরুত্বপূর্ণ তথ্য না পাওয়া গেলে কিছুই বের করবেন না।",
),
("human", "{টেক্সট}"),
]
)
llm = ChatOpenAI(
model="gpt-4-0125-preview",
temperature=0,
)
extractor = prompt | llm.with_structured_output(
schema=ExtractionData,
method="function_calling",
include_raw=False,
)
/home/eugene/.pyenv/versions/3.11.2/envs/langchain_3_11/lib/python3.11/site-packages/langchain_core/_api/beta_decorator.py:86: LangChainBetaWarning: The function `with_structured_output` is in beta. It is actively being worked on, so the API may change.
warn_beta(
ব্রুট ফোর্স পদ্ধতি
ডকুমেন্টগুলি পিস করুন যাতে প্রতিটি চাকটি LLMs এর অধীনে পড়া যায়।
from langchain_text_splitters import TokenTextSplitter
text_splitter = TokenTextSplitter(
chunk_size=2000,
chunk_overlap=20,
)
texts = text_splitter.split_text(document.page_content)
.batch
ফাংশনালিটি ব্যবহার করে প্রতিটি চাকে পারাল চালিয়ে চলা যাবে!
tip
আপনি সময়মতো ".batch()" ব্যবহার করতে পারেন যাতে আপনি চাকগুলির দ্বারা চালিত করতে পারেন! batch
অন্তর্ভুক্ত অন্তর্নিহিত প্রক্রিয়াটির সাথে সাহায্য করার জন্য থ্রেডপুল ব্যবহার করে।
আপনার মডেলটি কিভাবে API দ্বারা প্রদর্শিত হচ্ছে, এটা আপনার বিচারে আপনার নিষেধভাবে তোলা বিবেচনা!
first_few = texts[:3]
extractions = extractor.batch(
[{"টেক্সট": টেক্সট} for টেক্সট in first_few],
{"সর্বোচ্চ সমান্তরালতা": 5}, # উচ্চতম সমান্তরালতা পাস করেন দ্বারা সীমিত করুন!
)
ফলাফল মার্জ
চাংকি থেকে তথ্য নিয়ে থেকে, আমাদের কাছে এগিয়ে যেতে হবে এগুলি মাঝে মেলানোর পরে।
key_developments = []
for extraction in extractions:
key_developments.extend(extraction.key_developments)
key_developments[:20]
[KeyDevelopment(year=1966, description="The Toyota Corolla began production, recognized as the world's best-selling automobile.", evidence="The Toyota Corolla has been in production since 1966 and is recognized as the world's best-selling automobile."),
KeyDevelopment(year=1769, description='Nicolas-Joseph Cugnot built the first steam-powered road vehicle.', evidence='French inventor Nicolas-Joseph Cugnot built the first steam-powered road vehicle in 1769.'),
KeyDevelopment(year=1808, description='François Isaac de Rivaz designed and constructed the first internal combustion-powered automobile.', evidence='French-born Swiss inventor François Isaac de Rivaz designed and constructed the first internal combustion-powered automobile in 1808.'),
KeyDevelopment(year=1886, description='Carl Benz patented his Benz Patent-Motorwagen, inventing the modern car.', evidence='The modern car—a practical, marketable automobile for everyday use—was invented in 1886, when German inventor Carl Benz patented his Benz Patent-Motorwagen.'),
KeyDevelopment(year=1908, description='The 1908 Model T, an affordable car for the masses, was manufactured by the Ford Motor Company.', evidence='One of the first cars affordable by the masses was the 1908 Model T, an American car manufactured by the Ford Motor Company.'),
KeyDevelopment(year=1881, description='Gustave Trouvé demonstrated a three-wheeled car powered by electricity.', evidence='In November 1881, French inventor Gustave Trouvé demonstrated a three-wheeled car powered by electricity at the International Exposition of Electricity.'),
KeyDevelopment(year=1888, description="Bertha Benz undertook the first road trip by car to prove the road-worthiness of her husband's invention.", evidence="In August 1888, Bertha Benz, the wife of Carl Benz, undertook the first road trip by car, to prove the road-worthiness of her husband's invention."),
KeyDevelopment(year=1896, description='Benz designed and patented the first internal-combustion flat engine, called boxermotor.', evidence='In 1896, Benz designed and patented the first internal-combustion flat engine, called boxermotor.'),
KeyDevelopment(year=1897, description='Nesselsdorfer Wagenbau produced the Präsident automobil, one of the first factory-made cars in the world.', evidence='The first motor car in central Europe and one of the first factory-made cars in the world, was produced by Czech company Nesselsdorfer Wagenbau (later renamed to Tatra) in 1897, the Präsident automobil.'),
KeyDevelopment(year=1890, description='Daimler Motoren Gesellschaft (DMG) was founded by Daimler and Maybach in Cannstatt.', evidence='Daimler and Maybach founded Daimler Motoren Gesellschaft (DMG) in Cannstatt in 1890.'),
KeyDevelopment(year=1902, description='A new model DMG car was produced and named Mercedes after the Maybach engine.', evidence='Two years later, in 1902, a new model DMG car was produced and the model was named Mercedes after the Maybach engine, which generated 35 hp.'),
KeyDevelopment(year=1891, description='Auguste Doriot and Louis Rigoulot completed the longest trip by a petrol-driven vehicle using a Daimler powered Peugeot Type 3.', evidence='In 1891, Auguste Doriot and his Peugeot colleague Louis Rigoulot completed the longest trip by a petrol-driven vehicle when their self-designed and built Daimler powered Peugeot Type 3 completed 2,100 kilometres (1,300 mi) from Valentigney to Paris and Brest and back again.'),
KeyDevelopment(year=1895, description='George Selden was granted a US patent for a two-stroke car engine.', evidence='After a delay of 16 years and a series of attachments to his application, on 5 November 1895, Selden was granted a US patent (U.S. patent 549,160) for a two-stroke car engine.'),
KeyDevelopment(year=1893, description='The first running, petrol-driven American car was built and road-tested by the Duryea brothers.', evidence='In 1893, the first running, petrol-driven American car was built and road-tested by the Duryea brothers of Springfield, Massachusetts.'),
KeyDevelopment(year=1897, description='Rudolf Diesel built the first diesel engine.', evidence='In 1897, he built the first diesel engine.'),
KeyDevelopment(year=1901, description='Ransom Olds started large-scale, production-line manufacturing of affordable cars at his Oldsmobile factory.', evidence='Large-scale, production-line manufacturing of affordable cars was started by Ransom Olds in 1901 at his Oldsmobile factory in Lansing, Michigan.'),
KeyDevelopment(year=1913, description="Henry Ford began the world's first moving assembly line for cars at the Highland Park Ford Plant.", evidence="This concept was greatly expanded by Henry Ford, beginning in 1913 with the world's first moving assembly line for cars at the Highland Park Ford Plant."),
KeyDevelopment(year=1914, description="Ford's assembly line worker could buy a Model T with four months' pay.", evidence="In 1914, an assembly line worker could buy a Model T with four months' pay."),
KeyDevelopment(year=1926, description='Fast-drying Duco lacquer was developed, allowing for a variety of car colors.', evidence='Only Japan black would dry fast enough, forcing the company to drop the variety of colours available before 1913, until fast-drying Duco lacquer was developed in 1926.')]
I'm here to help with your translation task. Please provide the English text that needs to be translated into Bengali.
RAG ভিত্তিক পদক্ষেপ
আরেকটি সহজ ধারণা হলো টেক্সটটি চাঙ্ক করে ভাগ করা, কিন্তু প্রতিটি চাঙ্ক থেকে তথ্য বের করার পরিবর্তে কেবলমাত্র সবচেয়ে প্রাসঙ্গিক চাঙ্ক এ ফোকাস করা।
সাবধানবাণী
প্রাসঙ্গিক চাঙ্কগুলি সনাক্ত করা কঠিন হতে পারে।
উদাহরণস্বরূপ, এখানে আমরা 'গাড়ি' সংক্রান্ত নিবন্ধটি ব্যবহার করছি, এখানে প্রায়ই নিবন্ধটি প্রধান উন্নতি সংক্রান্ত তথ্য রয়েছে। তাই আমরা মধ্যে RAG ব্যবহার করে বেশিরভাগ প্রাসঙ্গিক তথ্য বয়ে ফেলার সম্ভাবনা রয়েছে।
আমরা আপনার ব্যবহার মামলার সাথে পরীক্ষা করার সুপারিশ করছি এবং দেখার জন্য নির্ধারণ করা যে এই পদক্ষেপ কাজ করে না।
এখানে একটি সহজ উদাহরণ দেওয়া হলো যা "FAISS" ভেক্টর স্টোর-এ নির্ভর করে।
from langchain_community.vectorstores import FAISS
from langchain_core.documents import Document
from langchain_core.runnables import RunnableLambda
from langchain_openai import OpenAIEmbeddings
from langchain_text_splitters import CharacterTextSplitter
texts = text_splitter.split_text(document.page_content)
vectorstore = FAISS.from_texts(texts, embedding=OpenAIEmbeddings())
retriever = vectorstore.as_retriever(
search_kwargs={"k": 1}
) # শুধুমাত্র প্রথম দলীয় থেকে বের করুন
এই ক্ষেত্রে RAG এক্সট্রাক্টরটি মাত্র শীর্ষ নথিতে কাজ করছে।
rag_extractor = {
"text": retriever | (lambda docs: docs[0].page_content) # শীর্ষ নথি থেকে সামগ্রিক বিষয়বস্তু বের করুন
} | extractor
results = rag_extractor.invoke("গাড়ির সাথে সংযুক্ত মূল উন্নতিগুলির সাথে যুক্ত")
for key_development in results.key_developments:
print(key_development)
year=1924 description="জার্মানির প্রথম সাধারণ-নির্মিত গাড়ি, Opel 4PS Laubfrosch, তৈরি হয়, যা Opel-কে জার্মানির শীর্ষ গাড়ি তৈরীদাতা করে, মার্কেটের 37.5% নিয়ে নেওয়ার পর।" evidence="জার্মানির প্রথম সাধারণ-নির্মিত গাড়ি, Opel 4PS Laubfrosch (Tree Frog), 1924 সালে Rüsselsheim এ বাধাভূত হয়, তাই শীঘ্রই Opel-কে জার্মানির শীর্ষ গাড়ি তৈরীদাতা হিসেবে জানা হয়, মার্কেটের 37.5 শতাংশ।"
year=1925 description='Morris-এর মোট ব্রিটিশ গাড়ি উৎপাদনের 41% ছিল, মার্কেটটি বাধাভূত।' evidence='1925 সালে, Morris-এর মোট ব্রিটিশ গাড়ি উৎপাদনের 41 শতাংশ হয়।'
year=1925 description='সিট্রোয়েন, রেনল্ট, এবং পুজো ফ্রান্সে ৫,৫০,০০০ গাড়ি তৈরী করে, মার্কেটটি বাধাভূত।' evidence="সিট্রোয়েন একা একই ব্রিপ্তি দিলেন ফ্রান্সে, 1919 সালে গাড়ি ঢালা শুরু করে; তাদের এবং অন্যান্য অর্থপ্রতিক্রিয়া মধ্যে বেশি কিনа সাইট্রনস্টাইনের 10CV এবং Peugeot-র 5CV এবং অন্যান্য খোক্যাপ গাড়ির সাথে, 1925 সালে তাদেরা ৫৫,৫০,০০০ গাড়ি তৈরী করে।"
year=2017 description='পেট্রোল-চালিত গাড়ির উৎপাদন চরম উঠেছিল।' evidence='পেট্রোল-চালিত গাড়ির উৎপাদন ২০১৭ সালে চরম উঠেছিল।'
গুণগত সমস্যা
বিভিন্ন পদ্ধতিতে বেদনী, দ্রুততা, এবং নির্ভরণপ্রস্থে নিজেদের মূল এবং মূপ উন্নতিবিষয়গুলি রয়েছে।
এই সমস্যা সম্পর্কে সতর্ক থাকুন:
- চাঙ্কিং বিষয়বস্তু তে যাওয়ার মানে হলো LL তথ্য যদি একাধিক চাঙ্কে ছড়া থাকে তবে LL তথ্য বের করতে ব্যার্থ হতে পারে।
- বড় চাঙ্ক অভারল্যাপ একই তথ্যটি দ্বিগুণ বের করতে সংক্রিয়তা হতে পারে, তাই টানান!
- LL মেইক আপ ডেটা তৈরি করতে পারে। যদি বড় একটি পাঠ্যটির সাথে একটি উপরোক্ত উপায়ে একল তথ্য চাইলেন এবং একটি দক্ষিণ ফোর্স পদক্ষেপ ব্যবহার করতে, তবে আপনি আবেগপ্রবণ LL মেইক আপ ত্থ্য আপ পাবেন।