
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 ()インデックスのためにテキストドキュメントをRaptorに追加します。
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 カスタムモデルを実装した後、次のようにラプターと統合します。
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 )ラプターで使用するために、Llama/Mistral/Gemmaなどの独自の要約/QAモデルを指定する方法の例については、 demo.ipynbご覧ください。
注:ラプターを構成するためのその他の例と方法が近づいています。高度な使用法と追加機能は、ドキュメントとリポジトリの更新で提供されます。
ラプターはオープンソースプロジェクトであり、貢献を歓迎します。バグを修正したり、新機能を追加したり、ドキュメントを改善したりするかどうかにかかわらず、ヘルプが高く評価されています。
RaptorはMITライセンスの下でリリースされます。詳細については、リポジトリのライセンスファイルを参照してください。
ラプターがあなたの研究を支援している場合は、次のように引用してください。
@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 }
}その他の例、構成ガイド、更新をお楽しみに。