رفع مشکل متنهای بلند
زمانی که با فایلها مانند PDF کار میکنید، ممکن است با متنی مواجه شوید که از پنجره محدوده مدل زبانیتان بیشتر باشد. برای پردازش این متن، به این استراتژیها توجه کنید:
- تغییر LLM یک LLM متفاوت را انتخاب کنید که از پنجره محدوده بزرگتری پشتیبانی میکند.
- روش خشن سنگین، سنگینتر متن را برش بزنید و محتوا را از هر قسمت بردارید.
- RAG متن را برش بزنید، قطعات را شاخصگذاری کنید و فقط محتوا را از یک زیرمجموعه از قطعاتی استخراج کنید که به نظر "مرتبط" میآیند.
به خاطر داشته باشید که این استراتژیها دارای تجارتهای مختلفی هستند و احتمالاً بهترین استراتژی وابسته به برنامهای است که طراحی میکنید!
ادغام نتایج
بعد از استخراج دادهها از بخشهای مختلف، میخواهیم استخراجها را با یکدیگر ادغام کنیم.
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 am a technical expert with extensive experience in software development and I am familiar with translating technical materials. I will translate the user-entered material from English to Persian while ensuring that the original markdown format is preserved. If the content includes program code, I will ensure that the comments and string values remain unchanged to maintain the code logic. My translation will use informal and colloquial Persian expressions, and I will ensure that the translated sentences are free and not literal to fit the Persian language habits. I will also maintain a concise style in the translation. If you have any specific material that needs to be translated, please provide it and I will get started on the translation.
رویکرد مبتنی بر RAG
یک ایدهی ساده دیگر این است که به جای استخراج اطلاعات از هر بخش متن، فقط بر روی بخشهای مهمتر تمرکز کنید.
هشدار
شناسایی کدام بخشها مهم هستند ممکن است دشوار باشد.
به عنوان مثال، در مقالهی car
که در حال استفاده از آن هستیم، بیشتر مقاله شامل اطلاعات کلیدی توسعه است. بنابراین با استفاده از RAG، احتمالاً بسیاری از اطلاعات مرتبط را دور خواهیم انداخت.
ما پیشنهاد میدهیم با مورد استفادهی خود آزمایش کنید و تعیین کنید که آیا این رویکرد کار میکند یا خیر.
در زیر یک مثال ساده آمده است که بر روی FAISS
vectorstore تکیه دارد.
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)
سال=1924 توضیح="اولین خودروی تولیدی بصورت انبوه آلمان، اوپل 4PS Laubfrosch، تولید شد که اوپل را به بزرگترین سازنده خودرو در آلمان با 37.5٪ از بازار تبدیل کرد." شواهد="اولین خودروی تولیدی بصورت انبوه آلمان، اوپل 4PS Laubfrosch (قورباغه درخت)، در سال 1924 از خط تولید روسلهایم خارج شد و به زودی اوپل را به بزرگترین سازنده خودرو در آلمان تبدیل کرد و با 37.5 درصد از بازار."
سال=1925 توضیح='موریس 41٪ از تولید کل خودروهای بریتانیایی را به خود اختصاص داد و بازار را تسلط کرد.' شواهد='در سال 1925، موریس 41 درصد از تولید کل خودروهای بریتانیایی را به خود اختصاص داد.'
سال=1925 توضیح='سیتروئن، رنو و پژو 550,000 خودرو در فرانسه تولید کردند و بازار را تسلط کردند.' شواهد="سیتروئن همین کار را در فرانسه انجام داد و در سال 1925 به خودروها پرداخت؛ بین آنها و خودروهای ارزان قیمت دیگر همچون Rنو 10CV و پژو 5CV که جواب دادند 550,000 خودرو را در سال 1925 تولید کردند."
سال=2017 توضیح='تولید خودروهای بنزینمصرف در سال 2017 بیشینه شد.' شواهد='تولید خودروهای بنزینمصرف در سال 2017 بیشینه شد.'
مسائل متداول
از این موارد موجودها پرو و مخصوص خودشان در ارتباط با هزینه، سرعت و دقت هستند.
در هنگام این موارد دقت کنید:
- چکیده کردن محتوا ممکن است باعث شود الالامها نتوانند اطلاعات را استخراج کنند اگر اطلاعات در انتهای چندین چکیده باشند.
- پوشش بزرگ چکیده ممکن است باعث شود اطلاعات یکسان دوباره استخراج شوند، بنابراین آمادهباشید تا تکرار را حذف کنید!
- الالامها ممکن است دادهها را به طور مصنوعی ایجاد کنند. اگر به دنبال یک واقعهی واحد در یک متن بزرگ هستید و از یک رویکرد نیرومند استفاده میکنید، ممکن است دادههای مصنوعی بیشتری دریافت کنید.