
يقدم Raptor نهجًا جديدًا في نماذج اللغة المتمثلة في الاسترجاع من خلال بناء بنية شجرة متكررة من المستندات. يسمح ذلك باسترداد معلومات أكثر كفاءة ودراسة في السياق عبر نصوص كبيرة ، معالجة القيود المشتركة في نماذج اللغة التقليدية.
للحصول على منهجيات وتطبيقات مفصلة ، راجع الورقة الأصلية:
قبل استخدام Raptor ، تأكد من تثبيت Python 3.8+. استنساخ مستودع Raptor وتثبيت التبعيات اللازمة:
git clone https://github.com/parthsarthi03/raptor.git
cd raptor
pip install -r requirements.txtللبدء مع رابتور ، اتبع هذه الخطوات:
أولاً ، قم بتعيين مفتاح Openai API وتهيئة تكوين Raptor:
import os
os . environ [ "OPENAI_API_KEY" ] = "your-openai-api-key"
from raptor import RetrievalAugmentation
# Initialize with default configuration. For advanced configurations, check the documentation. [WIP]
RA = RetrievalAugmentation ()أضف مستنداتك النصية إلى رابتور للفهرسة:
with open ( 'sample.txt' , 'r' ) as file :
text = file . read ()
RA . add_documents ( text )يمكنك الآن استخدام Raptor للإجابة على الأسئلة بناءً على المستندات المفهرسة:
question = "How did Cinderella reach her happy ending?"
answer = RA . answer_question ( question = question )
print ( "Answer: " , answer )احفظ الشجرة التي تم بناؤها إلى مسار محدد:
SAVE_PATH = "demo/cinderella"
RA . save ( SAVE_PATH )قم بتحميل الشجرة المحفوظة إلى رابتور:
RA = RetrievalAugmentation ( tree = SAVE_PATH )
answer = RA . answer_question ( question = question )تم تصميم Raptor لتكون مرنًا ويسمح لك بدمج أي نماذج للتلخيص ، وإلغاء الأسئلة (QA) ، وتوليد التضمين. فيما يلي كيفية تمديد رابتور مع نماذجك الخاصة:
إذا كنت ترغب في استخدام نموذج لغة مختلف للتلخيص ، فيمكنك القيام بذلك عن طريق توسيع فئة BaseSummarizationModel . قم بتنفيذ طريقة summarize لدمج منطق تلخيصك المخصص:
from raptor import BaseSummarizationModel
class CustomSummarizationModel ( BaseSummarizationModel ):
def __init__ ( self ):
# Initialize your model here
pass
def summarize ( self , context , max_tokens = 150 ):
# Implement your summarization logic here
# Return the summary as a string
summary = "Your summary here"
return summary بالنسبة لنماذج QA المخصصة ، قم بتوسيع فئة BaseQAModel وتنفيذ طريقة answer_question . يجب أن تُرجع هذه الطريقة أفضل إجابة موجودة من خلال النموذج الخاص بك بالنظر إلى سياق وسؤال:
from raptor import BaseQAModel
class CustomQAModel ( BaseQAModel ):
def __init__ ( self ):
# Initialize your model here
pass
def answer_question ( self , context , question ):
# Implement your QA logic here
# Return the answer as a string
answer = "Your answer here"
return answer لاستخدام نموذج التضمين مختلف ، تمديد فئة BaseEmbeddingModel . قم بتنفيذ طريقة create_embedding ، والتي يجب أن تُرجع تمثيل متجه لنص الإدخال:
from raptor import BaseEmbeddingModel
class CustomEmbeddingModel ( BaseEmbeddingModel ):
def __init__ ( self ):
# Initialize your model here
pass
def create_embedding ( self , text ):
# Implement your embedding logic here
# Return the embedding as a numpy array or a list of floats
embedding = [ 0.0 ] * embedding_dim # Replace with actual embedding logic
return embedding بعد تنفيذ النماذج المخصصة الخاصة بك ، قم بدمجها مع Raptor على النحو التالي:
from raptor import RetrievalAugmentation , RetrievalAugmentationConfig
# Initialize your custom models
custom_summarizer = CustomSummarizationModel ()
custom_qa = CustomQAModel ()
custom_embedding = CustomEmbeddingModel ()
# Create a config with your custom models
custom_config = RetrievalAugmentationConfig (
summarization_model = custom_summarizer ,
qa_model = custom_qa ,
embedding_model = custom_embedding
)
# Initialize RAPTOR with your custom config
RA = RetrievalAugmentation ( config = custom_config ) تحقق من demo.ipynb للحصول على أمثلة حول كيفية تحديد نماذج التلخيص/QA الخاصة بك ، مثل Llama/Mistral/Gemma ، ونماذج تضمين مثل Sbert ، لاستخدامها مع Raptor.
ملاحظة: هناك المزيد من الأمثلة وطرق تكوين رابتور. سيتم توفير الاستخدام المتقدم والميزات الإضافية في تحديثات الوثائق والمستودعات.
Raptor هو مشروع مفتوح المصدر ، والمساهمات مرحب بها. سواء كنت تقوم بتثبيت الأخطاء أو إضافة ميزات جديدة أو تحسين الوثائق ، فسيتم تقدير مساعدتك.
يتم إصدار Raptor تحت رخصة معهد ماساتشوستس للتكنولوجيا. راجع ملف الترخيص في المستودع للحصول على التفاصيل الكاملة.
إذا ساعد Raptor في بحثك ، فيرجى الاستشهاد به على النحو التالي:
@inproceedings { sarthi2024raptor ,
title = { RAPTOR: Recursive Abstractive Processing for Tree-Organized Retrieval } ,
author = { Sarthi, Parth and Abdullah, Salman and Tuli, Aditi and Khanna, Shubh and Goldie, Anna and Manning, Christopher D. } ,
booktitle = { International Conference on Learning Representations (ICLR) } ,
year = { 2024 }
}ترقبوا المزيد من الأمثلة ، وأدلة التكوين ، والتحديثات.