検索された生成(RAG)は、外部知識を統合することによりAIモデルを強化します。ただし、従来のぼろきれはドキュメントを断片化し、重要なコンテキストを失い、精度に影響を与えることがよくあります。
Anthropicのコンテキスト検索は、埋め込む前に各ドキュメントチャンクに簡潔でコンテキストが豊富な説明を追加することにより、これに対処します。 これにより、検索エラーが大幅に減少し、ダウンストリームタスクのパフォーマンスが向上します。 この記事では、コンテキストの検索とその実装について詳しく説明していますlangchain
でrag従来のRAGメソッドは、ドキュメントをより小さなチャンクに分割して、簡単に検索しますが、これにより本質的なコンテキストが排除されます。たとえば、チャンクは、「385万人以上の住民が欧州連合で最も人口の多い都市になっている」と述べているかもしれません。 このコンテキストの欠如は、正確性を妨げます。
多様なデータセット(コードベース、科学論文、フィクション)にわたる人類の内部テストは、コンテキストの検索が、コンテキスト埋め込みモデルとコンテキストBM25とペアになった場合、検索エラーを最大49%削減することを示しています。
<code>contextualized_chunk = """Berlin is the capital and largest city of Germany, known for being the EU's most populous city within its limits. Its more than 3.85 million inhabitants make it the European Union's most populous city, as measured by population within city limits. """</code>
コンテキスト検索の実装
このセクションでは、サンプルドキュメントを使用してステップバイステップの実装の概要を説明します。
ステップ1:Chunk Creation ドキュメントをより小さく独立したチャンク(ここ、文)に分割します:
<code># Input text for the knowledge base input_text = """Berlin is the capital and largest city of Germany, both by area and by population. Its more than 3.85 million inhabitants make it the European Union's most populous city, as measured by population within city limits. The city is also one of the states of Germany and is the third smallest state in the country in terms of area. Paris is the capital and most populous city of France. It is situated along the Seine River in the north-central part of the country. The city has a population of over 2.1 million residents within its administrative limits, making it one of Europe's major population centers."""</code>
コンテキスト生成のプロンプトを定義します(Anthropicのテンプレートが使用されています):
ステップ3:LLM初期化# Splitting the input text into smaller chunks
test_chunks = [
'Berlin is the capital and largest city of Germany, both by area and by population.',
"\n\nIts more than 3.85 million inhabitants make it the European Union's most populous city, as measured by population within city limits.",
'\n\nThe city is also one of the states of Germany and is the third smallest state in the country in terms of area.',
'\n\n# Paris is the capital and most populous city of France.',
'\n\n# It is situated along the Seine River in the north-central part of the country.',
"\n\n# The city has a population of over 2.1 million residents within its administrative limits, making it one of Europe's major population centers."
]
LLMを選択します(OpenaiのGPT-4Oはここで使用されています):ステップ4:チェーン作成
<code>from langchain.prompts import ChatPromptTemplate, PromptTemplate, HumanMessagePromptTemplate # Define the prompt for generating contextual information anthropic_contextual_retrieval_system_prompt = """<document> {WHOLE_DOCUMENT} </document> Here is the chunk we want to situate within the whole document <chunk> {CHUNK_CONTENT} </chunk> Please give a short succinct context to situate this chunk within the overall document for the purposes of improving search retrieval of the chunk. Answer only with the succinct context and nothing else.""" # ... (rest of the prompt template code remains the same)</code>
ステップ5:チャンク処理 各チャンクのコンテキストを生成します:
<code>import os from langchain_openai import ChatOpenAI # Load environment variables os.environ["OPENAI_API_KEY"] = "YOUR_OPENAI_API_KEY" # Initialize the model instance llm_model_instance = ChatOpenAI( model="gpt-4o", )</code>
(出力は元の例に示されています) 精度を向上させるための
再ランキング再ランキングは、最も関連性の高いチャンクに優先順位を付けることにより、検索をさらに改善します。 これにより、精度が向上し、コストが削減されます。人類のテストでは、再ランキングが5.7%から1.9%に減少し、67%の改善。
<code>from langchain.output_parsers import StrOutputParser # Chain the prompt with the model instance contextual_chunk_creation = anthropic_contextual_retrieval_final_prompt | llm_model_instance | StrOutputParser()</code>
より小さな知識ベース(&lt; 200,000トークン)の場合、プロンプト内の知識ベース全体を直接含めることは、検索システムを使用するよりも効率的かもしれません。 また、迅速なキャッシュ(クロードで利用可能)を使用すると、コストを大幅に削減し、応答時間を改善できます。
追加の考慮事項
Anthropicのコンテキスト検索は、RAGシステムを改善するための簡単で強力な方法を提供します。 コンテキスト埋め込み、BM25、および再ランキングの組み合わせにより、精度が大幅に向上します。 他の検索技術のさらなる調査が推奨されます
以上が人類のコンテキスト検索:実装のガイドの詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。